|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\ReCaptchaPaypal\Test\Unit\Observer; |
| 9 | + |
| 10 | +use Magento\Framework\App\Action\AbstractAction; |
| 11 | +use Magento\Framework\App\ActionFlag; |
| 12 | +use Magento\Framework\App\RequestInterface; |
| 13 | +use Magento\Framework\App\ResponseInterface; |
| 14 | +use Magento\Framework\Event\Observer; |
| 15 | +use Magento\Framework\Serialize\SerializerInterface; |
| 16 | +use Magento\Framework\Validation\ValidationResult; |
| 17 | +use Magento\ReCaptchaPaypal\Model\ReCaptchaSession; |
| 18 | +use Magento\ReCaptchaPaypal\Observer\PayPalObserver; |
| 19 | +use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface; |
| 20 | +use Magento\ReCaptchaUi\Model\ErrorMessageConfigInterface; |
| 21 | +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; |
| 22 | +use Magento\ReCaptchaUi\Model\ValidationConfigResolverInterface; |
| 23 | +use Magento\ReCaptchaValidationApi\Api\ValidatorInterface; |
| 24 | +use Magento\ReCaptchaValidationApi\Model\ValidationErrorMessagesProvider; |
| 25 | +use PHPUnit\Framework\MockObject\MockObject; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | +use Psr\Log\LoggerInterface; |
| 28 | + |
| 29 | +class PayPalObserverTest extends TestCase |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @var ValidatorInterface|MockObject |
| 33 | + */ |
| 34 | + private $captchaValidator; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var IsCaptchaEnabledInterface|MockObject |
| 38 | + */ |
| 39 | + private $isCaptchaEnabled; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var ReCaptchaSession|MockObject |
| 43 | + */ |
| 44 | + private $reCaptchaSession; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var PayPalObserver |
| 48 | + */ |
| 49 | + private $model; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Observer |
| 53 | + */ |
| 54 | + private $observer; |
| 55 | + |
| 56 | + /** |
| 57 | + * @inheritdoc |
| 58 | + */ |
| 59 | + protected function setUp(): void |
| 60 | + { |
| 61 | + parent::setUp(); |
| 62 | + $captchaResponseResolver = $this->getMockForAbstractClass(CaptchaResponseResolverInterface::class); |
| 63 | + $validationConfigResolver = $this->getMockForAbstractClass(ValidationConfigResolverInterface::class); |
| 64 | + $this->captchaValidator = $this->getMockForAbstractClass(ValidatorInterface::class); |
| 65 | + $actionFlag = $this->createMock(ActionFlag::class); |
| 66 | + $serializer = $this->getMockForAbstractClass(SerializerInterface::class); |
| 67 | + $this->isCaptchaEnabled = $this->getMockForAbstractClass(IsCaptchaEnabledInterface::class); |
| 68 | + $logger = $this->getMockForAbstractClass(LoggerInterface::class); |
| 69 | + $errorMessageConfig = $this->getMockForAbstractClass(ErrorMessageConfigInterface::class); |
| 70 | + $validationErrorMessagesProvider = $this->createMock(ValidationErrorMessagesProvider::class); |
| 71 | + $this->reCaptchaSession = $this->createMock(ReCaptchaSession::class); |
| 72 | + $this->model = new PayPalObserver( |
| 73 | + $captchaResponseResolver, |
| 74 | + $validationConfigResolver, |
| 75 | + $this->captchaValidator, |
| 76 | + $actionFlag, |
| 77 | + $serializer, |
| 78 | + $this->isCaptchaEnabled, |
| 79 | + $logger, |
| 80 | + $errorMessageConfig, |
| 81 | + $validationErrorMessagesProvider, |
| 82 | + $this->reCaptchaSession |
| 83 | + ); |
| 84 | + $controller = $this->getMockBuilder(AbstractAction::class) |
| 85 | + ->disableOriginalConstructor() |
| 86 | + ->onlyMethods(['getRequest', 'getResponse']) |
| 87 | + ->getMockForAbstractClass(); |
| 88 | + $request = $this->getMockForAbstractClass(RequestInterface::class); |
| 89 | + $response = $this->getMockBuilder(ResponseInterface::class) |
| 90 | + ->disableOriginalConstructor() |
| 91 | + ->addMethods(['representJson']) |
| 92 | + ->getMockForAbstractClass(); |
| 93 | + $controller->method('getRequest')->willReturn($request); |
| 94 | + $controller->method('getResponse')->willReturn($response); |
| 95 | + $this->observer = new Observer(['controller_action' => $controller]); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param array $mocks |
| 100 | + * @dataProvider executeDataProvider |
| 101 | + */ |
| 102 | + public function testExecute(array $mocks): void |
| 103 | + { |
| 104 | + $validationResult = $this->createMock(ValidationResult::class); |
| 105 | + $validationResult->expects($mocks['validationResult'][0]['expects'] ?? $this->never()) |
| 106 | + ->method('isValid')->willReturn($mocks['validationResult'][0]['willReturn'] ?? false); |
| 107 | + $this->captchaValidator->expects($mocks['captchaValidator'][0]['expects'] ?? $this->never()) |
| 108 | + ->method('isValid') |
| 109 | + ->willReturn($validationResult); |
| 110 | + unset($mocks['validationResult'], $mocks['captchaValidator']); |
| 111 | + $this->configureMock($mocks); |
| 112 | + $this->model->execute($this->observer); |
| 113 | + } |
| 114 | + |
| 115 | + public function executeDataProvider(): array |
| 116 | + { |
| 117 | + return [ |
| 118 | + [ |
| 119 | + [ |
| 120 | + 'isCaptchaEnabled' => [ |
| 121 | + [ |
| 122 | + 'method' => 'isCaptchaEnabledFor', |
| 123 | + 'willReturnMap' => [ |
| 124 | + ['paypal_payflowpro', false], |
| 125 | + ['place_order', false], |
| 126 | + ] |
| 127 | + ] |
| 128 | + ], |
| 129 | + 'reCaptchaSession' => [ |
| 130 | + [ |
| 131 | + 'method' => 'save', |
| 132 | + 'expects' => $this->never(), |
| 133 | + ] |
| 134 | + ] |
| 135 | + ] |
| 136 | + ], |
| 137 | + [ |
| 138 | + [ |
| 139 | + 'isCaptchaEnabled' => [ |
| 140 | + [ |
| 141 | + 'method' => 'isCaptchaEnabledFor', |
| 142 | + 'willReturnMap' => [ |
| 143 | + ['paypal_payflowpro', true], |
| 144 | + ['place_order', false], |
| 145 | + ] |
| 146 | + ] |
| 147 | + ], |
| 148 | + 'reCaptchaSession' => [ |
| 149 | + [ |
| 150 | + 'method' => 'save', |
| 151 | + 'expects' => $this->never(), |
| 152 | + ] |
| 153 | + ], |
| 154 | + 'captchaValidator' => [ |
| 155 | + [ |
| 156 | + 'method' => 'isValid', |
| 157 | + 'expects' => $this->once(), |
| 158 | + ] |
| 159 | + ], |
| 160 | + 'validationResult' => [ |
| 161 | + [ |
| 162 | + 'method' => 'isValid', |
| 163 | + 'expects' => $this->once(), |
| 164 | + 'willReturn' => true, |
| 165 | + ] |
| 166 | + ] |
| 167 | + ] |
| 168 | + ], |
| 169 | + [ |
| 170 | + [ |
| 171 | + 'isCaptchaEnabled' => [ |
| 172 | + [ |
| 173 | + 'method' => 'isCaptchaEnabledFor', |
| 174 | + 'willReturnMap' => [ |
| 175 | + ['paypal_payflowpro', true], |
| 176 | + ['place_order', true], |
| 177 | + ] |
| 178 | + ] |
| 179 | + ], |
| 180 | + 'reCaptchaSession' => [ |
| 181 | + [ |
| 182 | + 'method' => 'save', |
| 183 | + 'expects' => $this->once(), |
| 184 | + ] |
| 185 | + ], |
| 186 | + 'captchaValidator' => [ |
| 187 | + [ |
| 188 | + 'method' => 'isValid', |
| 189 | + 'expects' => $this->once(), |
| 190 | + ] |
| 191 | + ], |
| 192 | + 'validationResult' => [ |
| 193 | + [ |
| 194 | + 'method' => 'isValid', |
| 195 | + 'expects' => $this->once(), |
| 196 | + 'willReturn' => true, |
| 197 | + ] |
| 198 | + ] |
| 199 | + ] |
| 200 | + ] |
| 201 | + ]; |
| 202 | + } |
| 203 | + |
| 204 | + private function configureMock(array $mocks): void |
| 205 | + { |
| 206 | + foreach ($mocks as $prop => $propMocks) { |
| 207 | + foreach ($propMocks as $mock) { |
| 208 | + $builder = $this->$prop->expects($mock['expects'] ?? $this->any()); |
| 209 | + unset($mock['expects']); |
| 210 | + foreach ($mock as $method => $args) { |
| 211 | + $builder->$method(...[$args]); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments