Skip to content

Commit 3ac172d

Browse files
committed
Added unit test for CheckGuestCheckoutObserver
1 parent a160f2b commit 3ac172d

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Captcha\Test\Unit\Observer;
8+
9+
use Magento\Captcha\Model\DefaultModel as CaptchaModel;
10+
use Magento\Captcha\Observer\CheckGuestCheckoutObserver;
11+
use Magento\Captcha\Helper\Data as CaptchaDataHelper;
12+
use Magento\Framework\App\Action\Action;
13+
use Magento\Framework\App\ActionFlag;
14+
use Magento\Captcha\Observer\CaptchaStringResolver;
15+
use Magento\Checkout\Model\Type\Onepage;
16+
use Magento\Framework\App\Request\Http;
17+
use Magento\Framework\App\Response\Http as HttpResponse;
18+
use Magento\Framework\Event\Observer;
19+
use Magento\Framework\Json\Helper\Data as JsonHelper;
20+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
21+
use Magento\Quote\Model\Quote;
22+
23+
/**
24+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25+
*/
26+
class CheckGuestCheckoutObserverTest extends \PHPUnit_Framework_TestCase
27+
{
28+
const FORM_ID = 'guest_checkout';
29+
30+
/**
31+
* @var CheckGuestCheckoutObserver
32+
*/
33+
private $checkGuestCheckoutObserver;
34+
35+
/**
36+
* @var ObjectManager
37+
*/
38+
private $objectManager;
39+
40+
/**
41+
* @var Observer
42+
*/
43+
private $observer;
44+
45+
/**
46+
* @var HttpResponse|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
private $responseMock;
49+
50+
/**
51+
* @var HttpResponse|\PHPUnit_Framework_MockObject_MockObject
52+
*/
53+
private $requestMock;
54+
55+
/**
56+
* @var ActionFlag|\PHPUnit_Framework_MockObject_MockObject
57+
*/
58+
private $actionFlagMock;
59+
60+
/**
61+
* @var CaptchaStringResolver|\PHPUnit_Framework_MockObject_MockObject
62+
*/
63+
private $captchaStringResolverMock;
64+
65+
/**
66+
* @var JsonHelper|\PHPUnit_Framework_MockObject_MockObject
67+
*/
68+
private $jsonHelperMock;
69+
70+
/**
71+
* @var CaptchaModel|\PHPUnit_Framework_MockObject_MockObject
72+
*/
73+
private $captchaModelMock;
74+
75+
/**
76+
* @var Quote|\PHPUnit_Framework_MockObject_MockObject
77+
*/
78+
private $quoteModelMock;
79+
80+
/**
81+
* @var Action|\PHPUnit_Framework_MockObject_MockObject
82+
*/
83+
private $controllerMock;
84+
85+
protected function setUp()
86+
{
87+
$onepageModelTypeMock = $this->getMock(Onepage::class, [], [], '', false);
88+
$captchaHelperMock = $this->getMock(CaptchaDataHelper::class, [], [], '', false);
89+
$this->objectManager = new ObjectManager($this);
90+
$this->actionFlagMock = $this->getMock(ActionFlag::class, [], [], '', false);
91+
$this->captchaStringResolverMock = $this->getMock(CaptchaStringResolver::class, [], [], '', false);
92+
$this->captchaModelMock = $this->getMock(CaptchaModel::class, [], [], '', false);
93+
$this->quoteModelMock = $this->getMock(Quote::class, [], [], '', false);
94+
$this->controllerMock = $this->getMock(Action::class, [], [], '', false);
95+
$this->requestMock = $this->getMock(Http::class, [], [], '', false);
96+
$this->responseMock = $this->getMock(HttpResponse::class, [], [], '', false);
97+
$this->observer = new Observer(['controller_action' => $this->controllerMock]);
98+
$this->jsonHelperMock = $this->getMock(JsonHelper::class, [], [], '', false);
99+
100+
$this->checkGuestCheckoutObserver = $this->objectManager->getObject(
101+
CheckGuestCheckoutObserver::class,
102+
[
103+
'helper' => $captchaHelperMock,
104+
'actionFlag' => $this->actionFlagMock,
105+
'captchaStringResolver' => $this->captchaStringResolverMock,
106+
'typeOnepage' => $onepageModelTypeMock,
107+
'jsonHelper' => $this->jsonHelperMock
108+
]
109+
);
110+
111+
$captchaHelperMock->expects($this->once())
112+
->method('getCaptcha')
113+
->with(self::FORM_ID)
114+
->willReturn($this->captchaModelMock);
115+
$onepageModelTypeMock->expects($this->once())
116+
->method('getQuote')
117+
->willReturn($this->quoteModelMock);
118+
}
119+
120+
public function testCheckGuestCheckoutForRegister()
121+
{
122+
$this->quoteModelMock->expects($this->once())
123+
->method('getCheckoutMethod')
124+
->willReturn(Onepage::METHOD_REGISTER);
125+
$this->captchaModelMock->expects($this->never())
126+
->method('isRequired');
127+
128+
$this->checkGuestCheckoutObserver->execute($this->observer);
129+
}
130+
131+
public function testCheckGuestCheckoutWithNoCaptchaRequired()
132+
{
133+
$this->quoteModelMock->expects($this->once())
134+
->method('getCheckoutMethod')
135+
->willReturn(Onepage::METHOD_GUEST);
136+
$this->captchaModelMock->expects($this->once())
137+
->method('isRequired')
138+
->willReturn(false);
139+
$this->captchaModelMock->expects($this->never())
140+
->method('isCorrect');
141+
142+
$this->checkGuestCheckoutObserver->execute($this->observer);
143+
}
144+
145+
public function testCheckGuestCheckoutWithIncorrectCaptcha()
146+
{
147+
$captchaValue = 'some_word';
148+
$encodedJsonValue = '{}';
149+
150+
$this->quoteModelMock->expects($this->once())
151+
->method('getCheckoutMethod')
152+
->willReturn(Onepage::METHOD_GUEST);
153+
$this->captchaModelMock->expects($this->once())
154+
->method('isRequired')
155+
->willReturn(true);
156+
$this->controllerMock->expects($this->once())
157+
->method('getRequest')
158+
->willReturn($this->requestMock);
159+
$this->controllerMock->expects($this->once())
160+
->method('getResponse')
161+
->willReturn($this->responseMock);
162+
$this->controllerMock->expects($this->once())
163+
->method('getResponse')
164+
->willReturn($this->responseMock);
165+
$this->captchaStringResolverMock->expects($this->once())
166+
->method('resolve')
167+
->with($this->requestMock, self::FORM_ID)
168+
->willReturn($captchaValue);
169+
$this->captchaModelMock->expects($this->once())
170+
->method('isCorrect')
171+
->with($captchaValue)
172+
->willReturn(false);
173+
$this->actionFlagMock->expects($this->once())
174+
->method('set')
175+
->with('', Action::FLAG_NO_DISPATCH, true);
176+
$this->jsonHelperMock->expects($this->once())
177+
->method('jsonEncode')
178+
->willReturn($encodedJsonValue);
179+
$this->responseMock->expects($this->once())
180+
->method('representJson')
181+
->with($encodedJsonValue);
182+
183+
$this->checkGuestCheckoutObserver->execute($this->observer);
184+
}
185+
186+
public function testCheckGuestCheckoutWithCorrectCaptcha()
187+
{
188+
$this->quoteModelMock->expects($this->once())
189+
->method('getCheckoutMethod')
190+
->willReturn(Onepage::METHOD_GUEST);
191+
$this->captchaModelMock->expects($this->once())
192+
->method('isRequired')
193+
->willReturn(true);
194+
$this->controllerMock->expects($this->once())
195+
->method('getRequest')
196+
->willReturn($this->requestMock);
197+
$this->captchaStringResolverMock->expects($this->once())
198+
->method('resolve')
199+
->with($this->requestMock, self::FORM_ID)
200+
->willReturn('some_word');
201+
$this->captchaModelMock->expects($this->once())
202+
->method('isCorrect')
203+
->with('some_word')
204+
->willReturn(true);
205+
$this->actionFlagMock->expects($this->never())
206+
->method('set');
207+
208+
$this->checkGuestCheckoutObserver->execute($this->observer);
209+
}
210+
}

0 commit comments

Comments
 (0)