3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Customer \Test \Unit \Controller \Plugin ;
7
8
9
+ use Closure ;
10
+ use Magento \Customer \Controller \AccountInterface ;
8
11
use Magento \Customer \Controller \Plugin \Account ;
9
12
use Magento \Customer \Model \Session ;
10
13
use Magento \Framework \App \ActionFlag ;
11
14
use Magento \Framework \App \ActionInterface ;
12
- use Magento \Framework \App \Action \AbstractAction ;
13
15
use Magento \Framework \App \Request \Http ;
16
+ use Magento \Framework \App \Request \Http as HttpRequest ;
14
17
use Magento \Framework \Controller \ResultInterface ;
15
18
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
19
+ use PHPUnit \Framework \MockObject \MockObject ;
20
+ use PHPUnit \Framework \TestCase ;
16
21
17
- class AccountTest extends \ PHPUnit \ Framework \ TestCase
22
+ class AccountTest extends TestCase
18
23
{
19
24
/**
20
25
* @var string
@@ -27,110 +32,98 @@ class AccountTest extends \PHPUnit\Framework\TestCase
27
32
protected $ plugin ;
28
33
29
34
/**
30
- * @var Session | \PHPUnit_Framework_MockObject_MockObject
35
+ * @var Session|MockObject
31
36
*/
32
- protected $ session ;
37
+ protected $ sessionMock ;
33
38
34
39
/**
35
- * @var AbstractAction | \PHPUnit_Framework_MockObject_MockObject
40
+ * @var AccountInterface|MockObject
36
41
*/
37
- protected $ subject ;
42
+ protected $ actionMock ;
38
43
39
44
/**
40
- * @var Http | \PHPUnit_Framework_MockObject_MockObject
45
+ * @var Http|MockObject
41
46
*/
42
- protected $ request ;
47
+ protected $ requestMock ;
43
48
44
49
/**
45
- * @var ActionFlag | \PHPUnit_Framework_MockObject_MockObject
50
+ * @var ActionFlag|MockObject
46
51
*/
47
- protected $ actionFlag ;
52
+ protected $ actionFlagMock ;
48
53
49
54
/**
50
- * @var ResultInterface|\PHPUnit_Framework_MockObject_MockObject
55
+ * @var ResultInterface|MockObject
51
56
*/
52
- private $ resultInterface ;
57
+ private $ resultMock ;
53
58
54
59
protected function setUp ()
55
60
{
56
- $ this ->session = $ this ->getMockBuilder (\ Magento \ Customer \ Model \ Session::class)
61
+ $ this ->sessionMock = $ this ->getMockBuilder (Session::class)
57
62
->disableOriginalConstructor ()
58
- ->setMethods ([
59
- 'setNoReferer ' ,
60
- 'unsNoReferer ' ,
61
- 'authenticate ' ,
62
- ])
63
+ ->setMethods (['setNoReferer ' , 'unsNoReferer ' , 'authenticate ' ])
63
64
->getMock ();
64
65
65
- $ this ->subject = $ this ->getMockBuilder (AbstractAction::class)
66
- ->setMethods ([
67
- 'getActionFlag ' ,
68
- ])
66
+ $ this ->actionMock = $ this ->getMockBuilder (AccountInterface::class)
67
+ ->setMethods (['getActionFlag ' ])
69
68
->disableOriginalConstructor ()
70
69
->getMockForAbstractClass ();
71
70
72
- $ this ->request = $ this ->getMockBuilder (\ Magento \ Framework \ App \ Request \Http ::class)
71
+ $ this ->requestMock = $ this ->getMockBuilder (HttpRequest ::class)
73
72
->disableOriginalConstructor ()
74
- ->setMethods ([
75
- 'getActionName ' ,
76
- ])
73
+ ->setMethods (['getActionName ' ])
77
74
->getMock ();
78
75
79
- $ this ->resultInterface = $ this ->getMockBuilder (ResultInterface::class)
76
+ $ this ->resultMock = $ this ->getMockBuilder (ResultInterface::class)
80
77
->getMockForAbstractClass ();
81
78
82
- $ this ->actionFlag = $ this ->getMockBuilder (\ Magento \ Framework \ App \ ActionFlag::class)
79
+ $ this ->actionFlagMock = $ this ->getMockBuilder (ActionFlag::class)
83
80
->disableOriginalConstructor ()
84
81
->getMock ();
85
82
}
86
83
87
84
/**
88
85
* @param string $action
89
86
* @param array $allowedActions
90
- * @param boolean $isActionAllowed
91
- * @param boolean $isAuthenticated
87
+ * @param boolean $isAllowed
92
88
*
93
- * @dataProvider beforeDispatchDataProvider
89
+ * @dataProvider beforeExecuteDataProvider
94
90
*/
95
- public function testBeforeDispatch (
96
- $ action ,
97
- $ allowedActions ,
98
- $ isActionAllowed ,
99
- $ isAuthenticated
91
+ public function testAroundExecuteInterruptsOriginalCallWhenNotAllowed (
92
+ string $ action ,
93
+ array $ allowedActions ,
94
+ bool $ isAllowed
100
95
) {
101
- $ this ->request ->expects ($ this ->once ())
96
+ /** @var callable|MockObject $proceedMock */
97
+ $ proceedMock = $ this ->getMockBuilder (\stdClass::class)
98
+ ->setMethods (['__invoke ' ])
99
+ ->getMock ();
100
+
101
+ $ closureMock = Closure::fromCallable ($ proceedMock );
102
+
103
+ $ this ->requestMock ->expects ($ this ->once ())
102
104
->method ('getActionName ' )
103
105
->willReturn ($ action );
104
106
105
- if ($ isActionAllowed ) {
106
- $ this ->session ->expects ($ this ->once ())
107
- ->method ('setNoReferer ' )
108
- ->with (true )
109
- ->willReturnSelf ();
107
+ if ($ isAllowed ) {
108
+ $ proceedMock ->expects ($ this ->once ())->method ('__invoke ' )->willReturn ($ this ->resultMock );
110
109
} else {
111
- $ this ->session ->expects ($ this ->once ())
112
- ->method ('authenticate ' )
113
- ->willReturn ($ isAuthenticated );
114
- if (!$ isAuthenticated ) {
115
- $ this ->subject ->expects ($ this ->once ())
116
- ->method ('getActionFlag ' )
117
- ->willReturn ($ this ->actionFlag );
118
-
119
- $ this ->actionFlag ->expects ($ this ->once ())
120
- ->method ('set ' )
121
- ->with ('' , ActionInterface::FLAG_NO_DISPATCH , true )
122
- ->willReturnSelf ();
123
- }
110
+ $ proceedMock ->expects ($ this ->never ())->method ('__invoke ' );
124
111
}
125
112
126
- $ plugin = new Account ($ this ->session , $ allowedActions );
127
- $ plugin ->beforeDispatch ($ this ->subject , $ this ->request );
113
+ $ plugin = new Account ($ this ->requestMock , $ this ->sessionMock , $ allowedActions );
114
+ $ result = $ plugin ->aroundExecute ($ this ->actionMock , $ closureMock );
115
+
116
+ if ($ isAllowed ) {
117
+ $ this ->assertSame ($ this ->resultMock , $ result );
118
+ } else {
119
+ $ this ->assertNull ($ result );
120
+ }
128
121
}
129
122
130
123
/**
131
124
* @return array
132
125
*/
133
- public function beforeDispatchDataProvider ()
126
+ public function beforeExecuteDataProvider ()
134
127
{
135
128
return [
136
129
[
@@ -165,24 +158,4 @@ public function beforeDispatchDataProvider()
165
158
],
166
159
];
167
160
}
168
-
169
- public function testAfterDispatch ()
170
- {
171
- $ this ->session ->expects ($ this ->once ())
172
- ->method ('unsNoReferer ' )
173
- ->with (false )
174
- ->willReturnSelf ();
175
-
176
- $ plugin = (new ObjectManager ($ this ))->getObject (
177
- Account::class,
178
- [
179
- 'session ' => $ this ->session ,
180
- 'allowedActions ' => ['testaction ' ]
181
- ]
182
- );
183
- $ this ->assertSame (
184
- $ this ->resultInterface ,
185
- $ plugin ->afterDispatch ($ this ->subject , $ this ->resultInterface , $ this ->request )
186
- );
187
- }
188
161
}
0 commit comments