7
7
8
8
use Magento \Framework \Exception \LocalizedException ;
9
9
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
10
+ use Magento \TestFramework \Dependency \Reader \ClassScanner ;
10
11
use Magento \TestFramework \Exception \NoSuchActionException ;
11
12
12
13
/**
@@ -24,6 +25,11 @@ class PhpRuleTest extends \PHPUnit\Framework\TestCase
24
25
*/
25
26
private $ objectManagerHelper ;
26
27
28
+ /**
29
+ * @var ClassScanner
30
+ */
31
+ private $ classScanner ;
32
+
27
33
/**
28
34
* @inheritDoc
29
35
* @throws \Exception
@@ -39,13 +45,16 @@ protected function setUp()
39
45
$ whitelist = [];
40
46
41
47
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
48
+ $ this ->classScanner = $ this ->createMock (ClassScanner::class);
49
+
42
50
$ this ->model = $ this ->objectManagerHelper ->getObject (
43
51
PhpRule::class,
44
52
[
45
53
'mapRouters ' => $ mapRoutes ,
46
54
'mapLayoutBlocks ' => $ mapLayoutBlocks ,
47
55
'pluginMap ' => $ pluginMap ,
48
56
'whitelists ' => $ whitelist ,
57
+ 'classScanner ' => $ this ->classScanner
49
58
]
50
59
);
51
60
}
@@ -62,14 +71,20 @@ public function testNonPhpGetDependencyInfo()
62
71
/**
63
72
* @param string $class
64
73
* @param string $content
74
+ * @param int $expectedScans
65
75
* @param array $expected
66
- * @dataProvider getDependencyInfoDataProvider
76
+ *
67
77
* @throws \Exception
78
+ * @dataProvider getDependencyInfoDataProvider
68
79
*/
69
- public function testGetDependencyInfo ($ class , $ content , array $ expected )
80
+ public function testGetDependencyInfo (string $ class , string $ content , int $ expectedScans , array $ expected ): void
70
81
{
71
82
$ file = $ this ->makeMockFilepath ($ class );
72
83
$ module = $ this ->getModuleFromClass ($ class );
84
+ $ this ->classScanner ->expects ($ this ->exactly ($ expectedScans ))
85
+ ->method ('getClassName ' )
86
+ ->with ($ file )
87
+ ->willReturn ($ class );
73
88
$ this ->assertEquals ($ expected , $ this ->model ->getDependencyInfo ($ module , 'php ' , $ file , $ content ));
74
89
}
75
90
@@ -82,11 +97,13 @@ public function getDependencyInfoDataProvider()
82
97
'Extend class in same module ' => [
83
98
'Magento\SomeModule\SomeClass ' ,
84
99
'something extends \Magento\SomeModule\Any\ClassName { ' ,
100
+ 0 ,
85
101
[]
86
102
],
87
103
'Extend class in different module ' => [
88
104
'Magento\AnotherModule\SomeClass ' ,
89
105
'something extends \Magento\SomeModule\Any\ClassName { ' ,
106
+ 1 ,
90
107
[
91
108
[
92
109
'modules ' => ['Magento\SomeModule ' ],
@@ -98,11 +115,13 @@ public function getDependencyInfoDataProvider()
98
115
'getViewFileUrl in same module ' => [
99
116
'Magento\SomeModule\SomeClass ' ,
100
117
'$this->getViewFileUrl("Magento_SomeModule::js/order-by-sku-failure.js") ' ,
118
+ 0 ,
101
119
[]
102
120
],
103
121
'getViewFileUrl in different module ' => [
104
122
'Magento\AnotherModule\SomeClass ' ,
105
123
'$this->getViewFileUrl("Magento_SomeModule::js/order-by-sku-failure.js") ' ,
124
+ 1 ,
106
125
[
107
126
[
108
127
'modules ' => ['Magento\SomeModule ' ],
@@ -114,11 +133,13 @@ public function getDependencyInfoDataProvider()
114
133
'Helper class from same module ' => [
115
134
'Magento\SomeModule\SomeClass ' ,
116
135
'$this->helper("Magento\SomeModule\Any\ClassName") ' ,
136
+ 0 ,
117
137
[]
118
138
],
119
139
'Helper class from another module ' => [
120
140
'Magento\AnotherModule\SomeClass ' ,
121
141
'$this->helper("Magento\SomeModule\Any\ClassName") ' ,
142
+ 1 ,
122
143
[
123
144
[
124
145
'modules ' => ['Magento\SomeModule ' ],
@@ -129,11 +150,14 @@ public function getDependencyInfoDataProvider()
129
150
],
130
151
'getBlock from same module ' => [
131
152
'Magento\SomeModule\SomeClass ' ,
132
- '$this->getLayout()->getBlock( \'block.name \'); ' , []
153
+ '$this->getLayout()->getBlock( \'block.name \'); ' ,
154
+ 0 ,
155
+ []
133
156
],
134
157
'getBlock from another module ' => [
135
158
'Magento\AnotherModule\SomeClass ' ,
136
159
'$this->getLayout()->getBlock( \'block.name \'); ' ,
160
+ 0 ,
137
161
[
138
162
[
139
163
'modules ' => ['Magento\SomeModule ' ],
@@ -145,16 +169,19 @@ public function getDependencyInfoDataProvider()
145
169
'Plugin on class in same module ' => [
146
170
'Magento\Module1\Plugin1 ' ,
147
171
', \Magento\Module1\Subject $variable ' ,
172
+ 0 ,
148
173
[]
149
174
],
150
175
'Plugin depends on arbitrary class in same module ' => [
151
176
'Magento\Module1\Plugin1 ' ,
152
177
', \Magento\Module1\NotSubject $variable ' ,
178
+ 0 ,
153
179
[]
154
180
],
155
181
'Plugin on class in different module ' => [
156
182
'Magento\Module1\Plugin2 ' ,
157
183
'Magento\Module2\Subject ' ,
184
+ 1 ,
158
185
[
159
186
[
160
187
'modules ' => ['Magento\Module2 ' ],
@@ -166,6 +193,7 @@ public function getDependencyInfoDataProvider()
166
193
'Plugin depends on arbitrary class in same module as subject ' => [
167
194
'Magento\Module1\Plugin2 ' ,
168
195
'Magento\Module2\NotSubject ' ,
196
+ 1 ,
169
197
[
170
198
[
171
199
'modules ' => ['Magento\Module2 ' ],
@@ -177,6 +205,7 @@ public function getDependencyInfoDataProvider()
177
205
'Plugin depends on arbitrary class in arbitrary module ' => [
178
206
'Magento\Module1\Plugin2 ' ,
179
207
'Magento\OtherModule\NotSubject ' ,
208
+ 1 ,
180
209
[
181
210
[
182
211
'modules ' => ['Magento\OtherModule ' ],
@@ -322,8 +351,9 @@ private function makeMockFilepath($class)
322
351
* @param string $class
323
352
* @return string
324
353
*/
325
- private function getModuleFromClass ($ class )
354
+ private function getModuleFromClass (string $ class ): string
326
355
{
327
- return substr ($ class , 0 , strpos ($ class , '\\' , 9 )); // (strlen('Magento\\') + 1) === 9
356
+ $ moduleNameLength = strpos ($ class , '\\' , strpos ($ class , '\\' ) + 1 );
357
+ return substr ($ class , 0 , $ moduleNameLength );
328
358
}
329
359
}
0 commit comments