|
5 | 5 | */
|
6 | 6 | namespace Magento\Captcha\Test\Unit\Controller\Refresh;
|
7 | 7 |
|
8 |
| -class IndexTest extends \PHPUnit\Framework\TestCase |
| 8 | +use Magento\Captcha\Controller\Refresh\Index; |
| 9 | +use Magento\Captcha\Helper\Data as CaptchaHelper; |
| 10 | +use Magento\Captcha\Model\CaptchaInterface; |
| 11 | +use Magento\Framework\App\RequestInterface; |
| 12 | +use Magento\Framework\Controller\Result\Json as ResultJson; |
| 13 | +use Magento\Framework\Controller\Result\JsonFactory as ResultJsonFactory; |
| 14 | +use Magento\Framework\Serialize\Serializer\Json as JsonSerializer; |
| 15 | +use Magento\Framework\View\Element\BlockInterface; |
| 16 | +use Magento\Framework\View\LayoutInterface; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +class IndexTest extends TestCase |
9 | 21 | {
|
10 |
| - /** |
11 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
12 |
| - */ |
13 |
| - protected $captchaHelperMock; |
| 22 | + private const STUB_FORM_ID = 'StubFormId'; |
| 23 | + private const STUB_CAPTCHA_SOURCE = '/stub-captcha-source.jpg'; |
14 | 24 |
|
15 |
| - /** |
16 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
17 |
| - */ |
18 |
| - protected $captchaMock; |
19 |
| - |
20 |
| - /** |
21 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
22 |
| - */ |
23 |
| - protected $requestMock; |
| 25 | + /** @var MockObject|RequestInterface */ |
| 26 | + private $requestMock; |
24 | 27 |
|
25 |
| - /** |
26 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
27 |
| - */ |
28 |
| - protected $responseMock; |
| 28 | + /** @var MockObject|ResultJsonFactory */ |
| 29 | + private $jsonResultFactoryMock; |
29 | 30 |
|
30 |
| - /** |
31 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
32 |
| - */ |
33 |
| - protected $contextMock; |
| 31 | + /** @var MockObject|ResultJson */ |
| 32 | + private $jsonResultMock; |
34 | 33 |
|
35 |
| - /** |
36 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
37 |
| - */ |
38 |
| - protected $viewMock; |
| 34 | + /** @var MockObject|CaptchaHelper */ |
| 35 | + private $captchaHelperMock; |
39 | 36 |
|
40 |
| - /** |
41 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
42 |
| - */ |
43 |
| - protected $layoutMock; |
| 37 | + /** @var MockObject|LayoutInterface */ |
| 38 | + private $layoutMock; |
44 | 39 |
|
45 |
| - /** |
46 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
47 |
| - */ |
48 |
| - protected $flagMock; |
| 40 | + /** @var MockObject|BlockInterface */ |
| 41 | + private $blockMock; |
49 | 42 |
|
50 |
| - /** |
51 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
52 |
| - */ |
53 |
| - protected $serializerMock; |
| 43 | + /** @var MockObject|JsonSerializer */ |
| 44 | + private $jsonSerializerMock; |
54 | 45 |
|
55 |
| - /** |
56 |
| - * @var \Magento\Captcha\Controller\Refresh\Index |
57 |
| - */ |
58 |
| - protected $model; |
| 46 | + /** @var Index */ |
| 47 | + private $refreshAction; |
59 | 48 |
|
60 | 49 | protected function setUp()
|
61 | 50 | {
|
62 |
| - $this->captchaHelperMock = $this->createMock(\Magento\Captcha\Helper\Data::class); |
63 |
| - $this->captchaMock = $this->createMock(\Magento\Captcha\Model\DefaultModel::class); |
64 |
| - $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class); |
65 |
| - $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class); |
66 |
| - $this->contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class); |
67 |
| - $this->viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class); |
68 |
| - $this->layoutMock = $this->createMock(\Magento\Framework\View\LayoutInterface::class); |
69 |
| - $this->flagMock = $this->createMock(\Magento\Framework\App\ActionFlag::class); |
70 |
| - $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class); |
71 |
| - |
72 |
| - $this->contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock)); |
73 |
| - $this->contextMock->expects($this->any())->method('getView')->will($this->returnValue($this->viewMock)); |
74 |
| - $this->contextMock->expects($this->any())->method('getResponse')->will($this->returnValue($this->responseMock)); |
75 |
| - $this->contextMock->expects($this->any())->method('getActionFlag')->will($this->returnValue($this->flagMock)); |
76 |
| - $this->viewMock->expects($this->any())->method('getLayout')->will($this->returnValue($this->layoutMock)); |
77 |
| - |
78 |
| - $this->model = new \Magento\Captcha\Controller\Refresh\Index( |
79 |
| - $this->contextMock, |
| 51 | + $this->requestMock = $this->getMockBuilder(RequestInterface::class) |
| 52 | + ->setMethods(['getPost', 'getContent']) |
| 53 | + ->getMockForAbstractClass(); |
| 54 | + $this->layoutMock = $this->getMockBuilder(LayoutInterface::class) |
| 55 | + ->setMethods(['createBlock']) |
| 56 | + ->getMockForAbstractClass(); |
| 57 | + $this->blockMock = $this->getMockBuilder(BlockInterface::class) |
| 58 | + ->setMethods(['setFormId', 'setIsAjax', 'toHtml']) |
| 59 | + ->getMockForAbstractClass(); |
| 60 | + $this->jsonResultFactoryMock = $this->createMock(ResultJsonFactory::class); |
| 61 | + $this->jsonResultMock = $this->createMock(ResultJson::class); |
| 62 | + $this->jsonResultFactoryMock->method('create') |
| 63 | + ->willReturn($this->jsonResultMock); |
| 64 | + $this->jsonSerializerMock = $this->createMock(JsonSerializer::class); |
| 65 | + $this->captchaHelperMock = $this->createMock(CaptchaHelper::class); |
| 66 | + |
| 67 | + $this->blockMock->method('setIsAjax') |
| 68 | + ->willReturnSelf(); |
| 69 | + |
| 70 | + $this->layoutMock->method('createBlock') |
| 71 | + ->willReturn($this->blockMock); |
| 72 | + |
| 73 | + $this->refreshAction = new Index( |
| 74 | + $this->requestMock, |
| 75 | + $this->jsonResultFactoryMock, |
80 | 76 | $this->captchaHelperMock,
|
81 |
| - $this->serializerMock |
| 77 | + $this->layoutMock, |
| 78 | + $this->jsonSerializerMock |
82 | 79 | );
|
83 | 80 | }
|
84 | 81 |
|
85 |
| - /** |
86 |
| - * @dataProvider executeDataProvider |
87 |
| - * @param int $formId |
88 |
| - * @param int $callsNumber |
89 |
| - */ |
90 |
| - public function testExecute($formId, $callsNumber) |
| 82 | + public function testCaptchaGeneratedWhenPostDataContainsFormId() |
91 | 83 | {
|
92 |
| - $content = ['formId' => $formId]; |
93 |
| - $imgSource = ['imgSrc' => 'source']; |
94 |
| - |
95 |
| - $blockMethods = ['setFormId', 'setIsAjax', 'toHtml']; |
96 |
| - $blockMock = $this->createPartialMock(\Magento\Captcha\Block\Captcha::class, $blockMethods); |
97 |
| - |
98 |
| - $this->requestMock->expects($this->any())->method('getPost')->with('formId')->will($this->returnValue($formId)); |
99 |
| - $this->requestMock->expects($this->exactly($callsNumber))->method('getContent') |
100 |
| - ->will($this->returnValue(json_encode($content))); |
101 |
| - $this->captchaHelperMock->expects($this->any())->method('getCaptcha')->with($formId) |
102 |
| - ->will($this->returnValue($this->captchaMock)); |
103 |
| - $this->captchaMock->expects($this->once())->method('generate'); |
104 |
| - $this->captchaMock->expects($this->once())->method('getBlockName')->will($this->returnValue('block')); |
105 |
| - $this->captchaMock->expects($this->once())->method('getImgSrc')->will($this->returnValue('source')); |
106 |
| - $this->layoutMock->expects($this->once())->method('createBlock')->with('block') |
107 |
| - ->will($this->returnValue($blockMock)); |
108 |
| - $blockMock->expects($this->any())->method('setFormId')->with($formId)->will($this->returnValue($blockMock)); |
109 |
| - $blockMock->expects($this->any())->method('setIsAjax')->with(true)->will($this->returnValue($blockMock)); |
110 |
| - $blockMock->expects($this->once())->method('toHtml'); |
111 |
| - $this->responseMock->expects($this->once())->method('representJson')->with(json_encode($imgSource)); |
112 |
| - $this->flagMock->expects($this->once())->method('set')->with('', 'no-postDispatch', true); |
113 |
| - $this->serializerMock->expects($this->exactly($callsNumber)) |
114 |
| - ->method('unserialize')->will($this->returnValue($content)); |
115 |
| - $this->serializerMock->expects($this->once()) |
116 |
| - ->method('serialize')->will($this->returnValue(json_encode($imgSource))); |
117 |
| - |
118 |
| - $this->model->execute(); |
| 84 | + // Given |
| 85 | + $this->requestMock->method('getPost') |
| 86 | + ->with('formId') |
| 87 | + ->willReturn(self::STUB_FORM_ID); |
| 88 | + $this->blockMock->method('setFormId') |
| 89 | + ->willReturnSelf(); |
| 90 | + |
| 91 | + // Expect |
| 92 | + $this->requestMock->expects($this->never()) |
| 93 | + ->method('getContent'); |
| 94 | + $this->captchaHelperMock->expects($this->once()) |
| 95 | + ->method('getCaptcha') |
| 96 | + ->with(self::STUB_FORM_ID) |
| 97 | + ->willReturn( |
| 98 | + $this->getCaptchaModelMock(self::STUB_CAPTCHA_SOURCE) |
| 99 | + ); |
| 100 | + |
| 101 | + // When |
| 102 | + $this->refreshAction->execute(); |
| 103 | + } |
| 104 | + |
| 105 | + public function testCaptchaFallsBackToRequestContentIfPostMissing() |
| 106 | + { |
| 107 | + // Given |
| 108 | + $this->requestMock->method('getPost') |
| 109 | + ->with('formId') |
| 110 | + ->willReturn(null); |
| 111 | + $this->blockMock->method('setFormId') |
| 112 | + ->willReturnSelf(); |
| 113 | + |
| 114 | + // Expect |
| 115 | + $this->requestMock->expects(self::once()) |
| 116 | + ->method('getContent') |
| 117 | + ->willReturn(null); |
| 118 | + $this->captchaHelperMock->expects($this->once()) |
| 119 | + ->method('getCaptcha') |
| 120 | + ->with(null) |
| 121 | + ->willReturn( |
| 122 | + $this->getCaptchaModelMock(self::STUB_CAPTCHA_SOURCE) |
| 123 | + ); |
| 124 | + |
| 125 | + // When |
| 126 | + $this->refreshAction->execute(); |
119 | 127 | }
|
120 | 128 |
|
121 | 129 | /**
|
122 |
| - * @return array |
| 130 | + * @param string $imageSource |
| 131 | + * @return MockObject|CaptchaInterface |
123 | 132 | */
|
124 |
| - public function executeDataProvider() |
| 133 | + private function getCaptchaModelMock(string $imageSource): CaptchaInterface |
125 | 134 | {
|
126 |
| - return [ |
127 |
| - [ |
128 |
| - 'formId' => null, |
129 |
| - 'callsNumber' => 1, |
130 |
| - ], |
131 |
| - [ |
132 |
| - 'formId' => 1, |
133 |
| - 'callsNumber' => 0, |
134 |
| - ] |
135 |
| - ]; |
| 135 | + $modelMock = $this->getMockBuilder(CaptchaInterface::class) |
| 136 | + ->setMethods(['generate', 'getBlockName', 'getImgSrc']) |
| 137 | + ->getMockForAbstractClass(); |
| 138 | + |
| 139 | + $modelMock->method('getImgSrc') |
| 140 | + ->willReturn($imageSource); |
| 141 | + |
| 142 | + return $modelMock; |
136 | 143 | }
|
137 | 144 | }
|
0 commit comments