@@ -49,24 +49,39 @@ class CheckExpirePersistentQuoteObserverTest extends \PHPUnit\Framework\TestCase
49
49
*/
50
50
protected $ eventManagerMock ;
51
51
52
+ /**
53
+ * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\RequestInterface
54
+ */
55
+ private $ requestMock ;
56
+
57
+ /**
58
+ * @inheritdoc
59
+ */
52
60
protected function setUp ()
53
61
{
54
62
$ this ->sessionMock = $ this ->createMock (\Magento \Persistent \Helper \Session::class);
55
63
$ this ->customerSessionMock = $ this ->createMock (\Magento \Customer \Model \Session::class);
56
64
$ this ->persistentHelperMock = $ this ->createMock (\Magento \Persistent \Helper \Data::class);
57
- $ this ->observerMock
58
- = $ this ->createPartialMock (\Magento \Framework \Event \Observer::class, ['getControllerAction ' ,
59
- '__wakeUp ' ]);
65
+ $ this ->observerMock = $ this ->createPartialMock (
66
+ \Magento \Framework \Event \Observer::class,
67
+ ['getControllerAction ' ,'__wakeUp ' ]
68
+ );
60
69
$ this ->quoteManagerMock = $ this ->createMock (\Magento \Persistent \Model \QuoteManager::class);
61
70
$ this ->eventManagerMock = $ this ->createMock (\Magento \Framework \Event \ManagerInterface::class);
62
71
$ this ->checkoutSessionMock = $ this ->createMock (\Magento \Checkout \Model \Session::class);
72
+ $ this ->requestMock = $ this ->getMockBuilder (\Magento \Framework \App \RequestInterface::class)
73
+ ->disableOriginalConstructor ()
74
+ ->setMethods (['getRequestUri ' , 'getServer ' ])
75
+ ->getMockForAbstractClass ();
76
+
63
77
$ this ->model = new \Magento \Persistent \Observer \CheckExpirePersistentQuoteObserver (
64
78
$ this ->sessionMock ,
65
79
$ this ->persistentHelperMock ,
66
80
$ this ->quoteManagerMock ,
67
81
$ this ->eventManagerMock ,
68
82
$ this ->customerSessionMock ,
69
- $ this ->checkoutSessionMock
83
+ $ this ->checkoutSessionMock ,
84
+ $ this ->requestMock
70
85
);
71
86
}
72
87
@@ -76,7 +91,7 @@ public function testExecuteWhenCanNotApplyPersistentData()
76
91
->expects ($ this ->once ())
77
92
->method ('canProcess ' )
78
93
->with ($ this ->observerMock )
79
- ->will ( $ this -> returnValue ( false ) );
94
+ ->willReturn ( false );
80
95
$ this ->persistentHelperMock ->expects ($ this ->never ())->method ('isEnabled ' );
81
96
$ this ->model ->execute ($ this ->observerMock );
82
97
}
@@ -87,31 +102,97 @@ public function testExecuteWhenPersistentIsNotEnabled()
87
102
->expects ($ this ->once ())
88
103
->method ('canProcess ' )
89
104
->with ($ this ->observerMock )
90
- ->will ( $ this -> returnValue ( true ) );
91
- $ this ->persistentHelperMock ->expects ($ this ->once ())->method ('isEnabled ' )->will ( $ this -> returnValue ( false ) );
105
+ ->willReturn ( true );
106
+ $ this ->persistentHelperMock ->expects ($ this ->once ())->method ('isEnabled ' )->willReturn ( false );
92
107
$ this ->eventManagerMock ->expects ($ this ->never ())->method ('dispatch ' );
93
108
$ this ->model ->execute ($ this ->observerMock );
94
109
}
95
110
96
- public function testExecuteWhenPersistentIsEnabled ()
97
- {
111
+ /**
112
+ * Test method \Magento\Persistent\Observer\CheckExpirePersistentQuoteObserver::execute when persistent is enabled.
113
+ *
114
+ * @param string $refererUri
115
+ * @param string $requestUri
116
+ * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $expireCounter
117
+ * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $dispatchCounter
118
+ * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $setCustomerIdCounter
119
+ * @return void
120
+ * @dataProvider requestDataProvider
121
+ */
122
+ public function testExecuteWhenPersistentIsEnabled (
123
+ string $ refererUri ,
124
+ string $ requestUri ,
125
+ \PHPUnit_Framework_MockObject_Matcher_InvokedCount $ expireCounter ,
126
+ \PHPUnit_Framework_MockObject_Matcher_InvokedCount $ dispatchCounter ,
127
+ \PHPUnit_Framework_MockObject_Matcher_InvokedCount $ setCustomerIdCounter
128
+ ): void {
98
129
$ this ->persistentHelperMock
99
130
->expects ($ this ->once ())
100
131
->method ('canProcess ' )
101
132
->with ($ this ->observerMock )
102
- ->will ($ this ->returnValue (true ));
103
- $ this ->persistentHelperMock ->expects ($ this ->once ())->method ('isEnabled ' )->will ($ this ->returnValue (true ));
104
- $ this ->sessionMock ->expects ($ this ->once ())->method ('isPersistent ' )->will ($ this ->returnValue (false ));
105
- $ this ->customerSessionMock ->expects ($ this ->once ())->method ('isLoggedIn ' )->will ($ this ->returnValue (false ));
106
- $ this ->checkoutSessionMock ->expects ($ this ->once ())->method ('getQuoteId ' )->will ($ this ->returnValue (10 ));
107
- $ this ->observerMock ->expects ($ this ->once ())->method ('getControllerAction ' );
108
- $ this ->eventManagerMock ->expects ($ this ->once ())->method ('dispatch ' );
109
- $ this ->quoteManagerMock ->expects ($ this ->once ())->method ('expire ' );
133
+ ->willReturn (true );
134
+ $ this ->persistentHelperMock ->expects ($ this ->once ())->method ('isEnabled ' )->willReturn (true );
135
+ $ this ->sessionMock ->expects ($ this ->once ())->method ('isPersistent ' )->willReturn (false );
110
136
$ this ->customerSessionMock
111
- ->expects ($ this ->once ())
137
+ ->expects ($ this ->atLeastOnce ())
138
+ ->method ('isLoggedIn ' )
139
+ ->willReturn (false );
140
+ $ this ->checkoutSessionMock
141
+ ->expects ($ this ->atLeastOnce ())
142
+ ->method ('getQuoteId ' )
143
+ ->willReturn (10 );
144
+ $ this ->eventManagerMock ->expects ($ dispatchCounter )->method ('dispatch ' );
145
+ $ this ->quoteManagerMock ->expects ($ expireCounter )->method ('expire ' );
146
+ $ this ->customerSessionMock
147
+ ->expects ($ setCustomerIdCounter )
112
148
->method ('setCustomerId ' )
113
149
->with (null )
114
- ->will ($ this ->returnSelf ());
150
+ ->willReturnSelf ();
151
+ $ this ->requestMock ->expects ($ this ->atLeastOnce ())->method ('getRequestUri ' )->willReturn ($ refererUri );
152
+ $ this ->requestMock
153
+ ->expects ($ this ->atLeastOnce ())
154
+ ->method ('getServer ' )
155
+ ->with ('HTTP_REFERER ' )
156
+ ->willReturn ($ requestUri );
115
157
$ this ->model ->execute ($ this ->observerMock );
116
158
}
159
+
160
+ /**
161
+ * Request Data Provider
162
+ *
163
+ * @return array
164
+ */
165
+ public function requestDataProvider ()
166
+ {
167
+ return [
168
+ [
169
+ 'refererUri ' => 'checkout ' ,
170
+ 'requestUri ' => 'index ' ,
171
+ 'expireCounter ' => $ this ->never (),
172
+ 'dispatchCounter ' => $ this ->never (),
173
+ 'setCustomerIdCounter ' => $ this ->never (),
174
+ ],
175
+ [
176
+ 'refererUri ' => 'checkout ' ,
177
+ 'requestUri ' => 'checkout ' ,
178
+ 'expireCounter ' => $ this ->never (),
179
+ 'dispatchCounter ' => $ this ->never (),
180
+ 'setCustomerIdCounter ' => $ this ->never (),
181
+ ],
182
+ [
183
+ 'refererUri ' => 'index ' ,
184
+ 'requestUri ' => 'checkout ' ,
185
+ 'expireCounter ' => $ this ->never (),
186
+ 'dispatchCounter ' => $ this ->never (),
187
+ 'setCustomerIdCounter ' => $ this ->never (),
188
+ ],
189
+ [
190
+ 'refererUri ' => 'index ' ,
191
+ 'requestUri ' => 'index ' ,
192
+ 'expireCounter ' => $ this ->once (),
193
+ 'dispatchCounter ' => $ this ->once (),
194
+ 'setCustomerIdCounter ' => $ this ->once (),
195
+ ],
196
+ ];
197
+ }
117
198
}
0 commit comments