|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Adyen Payment module (https://www.adyen.com/) |
| 5 | + * |
| 6 | + * Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/) |
| 7 | + * See LICENSE.txt for license details. |
| 8 | + * |
| 9 | + * Author: Adyen <magento@adyen.com> |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Adyen\Payment\Test\Helper\Unit\Model; |
| 13 | + |
| 14 | +use Adyen\AdyenException; |
| 15 | +use Adyen\Payment\Api\Data\OrderPaymentInterface; |
| 16 | +use Adyen\Payment\Logger\AdyenLogger; |
| 17 | +use Adyen\Payment\Model\AdyenOrderPaymentRepository; |
| 18 | +use Adyen\Payment\Model\Order\Payment; |
| 19 | +use Adyen\Payment\Model\Order\PaymentFactory as AdyenOrderPaymentFactory; |
| 20 | +use Adyen\Payment\Model\ResourceModel\Order\Payment as AdyenOrderPaymentResourceModel; |
| 21 | +use Adyen\Payment\Model\ResourceModel\Order\Payment\Collection; |
| 22 | +use Adyen\Payment\Model\ResourceModel\Order\Payment\CollectionFactory; |
| 23 | +use Adyen\Payment\Test\Unit\AbstractAdyenTestCase; |
| 24 | +use Magento\Framework\Api\AbstractSimpleObject; |
| 25 | +use Magento\Framework\Api\Filter; |
| 26 | +use Magento\Framework\Api\FilterBuilder; |
| 27 | +use Magento\Framework\Api\Search\FilterGroupBuilder; |
| 28 | +use Magento\Framework\Api\Search\SearchResultFactory; |
| 29 | +use Magento\Framework\Api\Search\SearchResultInterface; |
| 30 | +use Magento\Framework\Api\SearchCriteria; |
| 31 | +use Magento\Framework\Api\SearchCriteria\CollectionProcessor; |
| 32 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 33 | +use Magento\Framework\Api\SearchCriteriaInterface; |
| 34 | +use Magento\Framework\Api\SearchResultsInterface; |
| 35 | +use PHPUnit\Framework\MockObject\MockObject; |
| 36 | + |
| 37 | +class AdyenOrderPaymentRepositoryTest extends AbstractAdyenTestCase |
| 38 | +{ |
| 39 | + private ?AdyenOrderPaymentRepository $adyenOrderPaymentRepository; |
| 40 | + private AdyenOrderPaymentResourceModel|MockObject $resourceModelMock; |
| 41 | + private AdyenOrderPaymentFactory|MockObject $adyenOrderPaymentFactoryMock; |
| 42 | + private SearchResultFactory|MockObject $searchResultsFactoryMock; |
| 43 | + private CollectionFactory|MockObject $collectionFactoryMock; |
| 44 | + private CollectionProcessor|MockObject $collectionProcessorMock; |
| 45 | + private SearchCriteriaBuilder|MockObject $searchCriteriaBuilderMock; |
| 46 | + private FilterBuilder|MockObject $filterBuilderMock; |
| 47 | + private FilterGroupBuilder|MockObject $filterGroupBuilderMock; |
| 48 | + private AdyenLogger|MockObject $adyenLoggerMock; |
| 49 | + |
| 50 | + protected function setUp(): void |
| 51 | + { |
| 52 | + $this->resourceModelMock = $this->createMock(AdyenOrderPaymentResourceModel::class); |
| 53 | + $this->adyenOrderPaymentFactoryMock = $this->createGeneratedMock(AdyenOrderPaymentFactory::class, [ |
| 54 | + 'create' |
| 55 | + ]); |
| 56 | + $this->searchResultsFactoryMock = $this->createMock(SearchResultFactory::class); |
| 57 | + $this->collectionFactoryMock = $this->createGeneratedMock(CollectionFactory::class, [ |
| 58 | + 'create' |
| 59 | + ]); |
| 60 | + $this->collectionProcessorMock = $this->createMock(CollectionProcessor::class); |
| 61 | + $this->searchCriteriaBuilderMock = $this->createMock(SearchCriteriaBuilder::class); |
| 62 | + $this->filterBuilderMock = $this->createMock(FilterBuilder::class); |
| 63 | + $this->filterGroupBuilderMock = $this->createMock(FilterGroupBuilder::class); |
| 64 | + $this->adyenLoggerMock = $this->createMock(AdyenLogger::class); |
| 65 | + |
| 66 | + $this->adyenOrderPaymentRepository = new AdyenOrderPaymentRepository( |
| 67 | + $this->resourceModelMock, |
| 68 | + $this->adyenOrderPaymentFactoryMock, |
| 69 | + $this->searchResultsFactoryMock, |
| 70 | + $this->collectionFactoryMock, |
| 71 | + $this->collectionProcessorMock, |
| 72 | + $this->searchCriteriaBuilderMock, |
| 73 | + $this->filterBuilderMock, |
| 74 | + $this->filterGroupBuilderMock, |
| 75 | + $this->adyenLoggerMock |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + public function testGetList() |
| 83 | + { |
| 84 | + $searchResultMock = $this->createPartialMock(SearchResultInterface::class, []); |
| 85 | + $this->searchResultsFactoryMock->expects($this->once()) |
| 86 | + ->method('create') |
| 87 | + ->willReturn($searchResultMock); |
| 88 | + |
| 89 | + $collectionMock = $this->createMock(Collection::class); |
| 90 | + $collectionResult[] = $this->createMock(Payment::class); |
| 91 | + $collectionMock->method('getItems')->willReturn($collectionResult); |
| 92 | + $collectionMock->method('getSize')->willReturn(count($collectionResult)); |
| 93 | + $this->collectionFactoryMock->expects($this->once()) |
| 94 | + ->method('create') |
| 95 | + ->willReturn($collectionMock); |
| 96 | + |
| 97 | + $searchCriteriaMock = $this->createMock(SearchCriteriaInterface::class); |
| 98 | + |
| 99 | + $this->collectionProcessorMock->expects($this->once()) |
| 100 | + ->method('process') |
| 101 | + ->with($searchCriteriaMock, $collectionMock); |
| 102 | + |
| 103 | + $searchResultMock->expects($this->once()) |
| 104 | + ->method('setItems') |
| 105 | + ->with($collectionResult); |
| 106 | + $searchResultMock->expects($this->once()) |
| 107 | + ->method('setTotalCount') |
| 108 | + ->with(count($collectionResult)); |
| 109 | + |
| 110 | + $result = $this->adyenOrderPaymentRepository->getList($searchCriteriaMock); |
| 111 | + |
| 112 | + $this->assertInstanceOf(SearchResultsInterface::class, $result); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @return void |
| 117 | + */ |
| 118 | + public function testGet() |
| 119 | + { |
| 120 | + $entityId = 1; |
| 121 | + $adyenOrderPaymentMock = $this->createMock(Payment::class); |
| 122 | + |
| 123 | + $this->adyenOrderPaymentFactoryMock->expects($this->once()) |
| 124 | + ->method('create') |
| 125 | + ->willReturn($adyenOrderPaymentMock); |
| 126 | + |
| 127 | + $this->resourceModelMock->expects($this->once()) |
| 128 | + ->method('load') |
| 129 | + ->with($adyenOrderPaymentMock, $entityId, OrderPaymentInterface::ENTITY_ID) |
| 130 | + ->willReturnSelf(); |
| 131 | + |
| 132 | + $result = $this->adyenOrderPaymentRepository->get($entityId); |
| 133 | + $this->assertInstanceOf(OrderPaymentInterface::class, $result); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @return void |
| 138 | + * @throws AdyenException |
| 139 | + */ |
| 140 | + public function testGetByPaymentId() |
| 141 | + { |
| 142 | + $paymentId = 1; |
| 143 | + $captureStatuses = [ |
| 144 | + OrderPaymentInterface::CAPTURE_STATUS_AUTO_CAPTURE, |
| 145 | + OrderPaymentInterface::CAPTURE_STATUS_MANUAL_CAPTURE, |
| 146 | + ]; |
| 147 | + |
| 148 | + $paymentIdFilterMock = $this->createMock(Filter::class); |
| 149 | + |
| 150 | + $this->filterBuilderMock->expects($this->exactly(3)) |
| 151 | + ->method('setField') |
| 152 | + ->willReturnMap([ |
| 153 | + [OrderPaymentInterface::PAYMENT_ID, $this->filterBuilderMock], |
| 154 | + [OrderPaymentInterface::CAPTURE_STATUS, $this->filterBuilderMock] |
| 155 | + ]); |
| 156 | + |
| 157 | + $this->filterBuilderMock->expects($this->exactly(3)) |
| 158 | + ->method('setConditionType') |
| 159 | + ->with('eq') |
| 160 | + ->willReturnSelf(); |
| 161 | + |
| 162 | + $this->filterBuilderMock->expects($this->exactly(3)) |
| 163 | + ->method('setValue') |
| 164 | + ->willReturnSelf(); |
| 165 | + |
| 166 | + $this->filterBuilderMock->expects($this->exactly(3)) |
| 167 | + ->method('create') |
| 168 | + ->willReturn($paymentIdFilterMock); |
| 169 | + |
| 170 | + $this->filterGroupBuilderMock->expects($this->exactly(2)) |
| 171 | + ->method('setFilters') |
| 172 | + ->willReturnSelf(); |
| 173 | + |
| 174 | + $filterGroupMock = $this->createMock(AbstractSimpleObject::class); |
| 175 | + |
| 176 | + $this->filterGroupBuilderMock->expects($this->exactly(2)) |
| 177 | + ->method('create') |
| 178 | + ->willReturn($filterGroupMock); |
| 179 | + |
| 180 | + $searchCriteriaMock = $this->createMock(SearchCriteria::class); |
| 181 | + |
| 182 | + $this->searchCriteriaBuilderMock->expects($this->once()) |
| 183 | + ->method('setFilterGroups') |
| 184 | + ->with([$filterGroupMock, $filterGroupMock]) |
| 185 | + ->willReturnSelf(); |
| 186 | + |
| 187 | + $this->searchCriteriaBuilderMock->expects($this->once()) |
| 188 | + ->method('create') |
| 189 | + ->willReturn($searchCriteriaMock); |
| 190 | + |
| 191 | + $collectionMock = $this->createMock(Collection::class); |
| 192 | + $this->collectionFactoryMock->expects($this->once()) |
| 193 | + ->method('create') |
| 194 | + ->willReturn($collectionMock); |
| 195 | + |
| 196 | + $collectionResult[] = $this->createMock(Payment::class); |
| 197 | + $collectionMock->method('getItems')->willReturn($collectionResult); |
| 198 | + $collectionMock->method('getSize')->willReturn(count($collectionResult)); |
| 199 | + |
| 200 | + $searchResultMock = $this->createPartialMock(SearchResultInterface::class, []); |
| 201 | + $searchResultMock->expects($this->once()) |
| 202 | + ->method('setItems') |
| 203 | + ->with($collectionResult); |
| 204 | + $searchResultMock->expects($this->once()) |
| 205 | + ->method('setTotalCount') |
| 206 | + ->with(count($collectionResult)); |
| 207 | + $searchResultMock->expects($this->once()) |
| 208 | + ->method('getItems') |
| 209 | + ->willReturn($collectionResult); |
| 210 | + |
| 211 | + $this->searchResultsFactoryMock->expects($this->once()) |
| 212 | + ->method('create') |
| 213 | + ->willReturn($searchResultMock); |
| 214 | + |
| 215 | + $result = $this->adyenOrderPaymentRepository->getByPaymentId($paymentId, $captureStatuses); |
| 216 | + |
| 217 | + $this->assertIsArray($result); |
| 218 | + $this->assertInstanceOf(OrderPaymentInterface::class, $result[0]); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * @return void |
| 223 | + * @throws AdyenException |
| 224 | + */ |
| 225 | + public function testGetByPaymentIdInvalidCaptureStatusException() |
| 226 | + { |
| 227 | + $this->expectException(AdyenException::class); |
| 228 | + |
| 229 | + $paymentId = 1; |
| 230 | + $captureStatuses = ['invalid_capture_status_mock']; |
| 231 | + |
| 232 | + $this->filterBuilderMock->method('setField')->willReturnSelf(); |
| 233 | + $this->filterBuilderMock->method('setConditionType')->willReturnSelf(); |
| 234 | + $this->filterBuilderMock->method('setValue')->willReturnSelf(); |
| 235 | + |
| 236 | + $this->adyenLoggerMock->expects($this->once())->method('error'); |
| 237 | + |
| 238 | + // Assert expect exception |
| 239 | + $this->adyenOrderPaymentRepository->getByPaymentId($paymentId, $captureStatuses); |
| 240 | + } |
| 241 | +} |
0 commit comments