11
11
12
12
namespace Symfony \Bridge \PhpUnit \Tests \DeprecationErrorHandler ;
13
13
14
+ use Composer \Autoload \ClassLoader ;
14
15
use PHPUnit \Framework \TestCase ;
15
16
use Symfony \Bridge \PhpUnit \DeprecationErrorHandler ;
16
17
use Symfony \Bridge \PhpUnit \DeprecationErrorHandler \Deprecation ;
17
18
use Symfony \Bridge \PhpUnit \Legacy \SymfonyTestsListenerForV5 ;
18
19
19
20
class DeprecationTest extends TestCase
20
21
{
22
+ public static function setUpBeforeClass (): void
23
+ {
24
+ $ vendorDir = self ::getVendorDir ();
25
+
26
+ mkdir ($ vendorDir .'/myfakevendor/myfakepackage1 ' , 0777 , true );
27
+ mkdir ($ vendorDir .'/myfakevendor/myfakepackage2 ' );
28
+ touch ($ vendorDir .'/myfakevendor/myfakepackage1/MyFakeFile1.php ' );
29
+ touch ($ vendorDir .'/myfakevendor/myfakepackage1/MyFakeFile2.php ' );
30
+ touch ($ vendorDir .'/myfakevendor/myfakepackage2/MyFakeFile.php ' );
31
+ }
32
+
33
+ private static function getVendorDir (): string
34
+ {
35
+ $ reflection = new \ReflectionClass (ClassLoader::class);
36
+
37
+ return \dirname ($ reflection ->getFileName (), 2 );
38
+ }
39
+
21
40
public function testItCanDetermineTheClassWhereTheDeprecationHappened ()
22
41
{
23
42
$ deprecation = new Deprecation ('💩 ' , $ this ->debugBacktrace (), __FILE__ );
@@ -119,19 +138,19 @@ public function testItTakesMutesDeprecationFromPhpUnitFiles()
119
138
$ this ->assertTrue ($ deprecation ->isMuted ());
120
139
}
121
140
122
- public function providerIsSelf (): array
141
+ public function providerGetTypeDetectsSelf (): array
123
142
{
124
143
return [
125
- 'not_from_vendors_file ' => [true , '' , 'MyClass1 ' , '' ],
126
- 'nonexistent_file ' => [false , '' , 'MyClass1 ' , 'dummy_vendor_path ' ],
144
+ 'not_from_vendors_file ' => [Deprecation:: TYPE_SELF , '' , 'MyClass1 ' , '' ],
145
+ 'nonexistent_file ' => [Deprecation:: TYPE_UNDETERMINED , '' , 'MyClass1 ' , 'dummy_vendor_path ' ],
127
146
'serialized_trace_without_triggering_file ' => [
128
- true ,
147
+ Deprecation:: TYPE_SELF ,
129
148
serialize (['class ' => '' , 'method ' => '' , 'deprecation ' => '' , 'files_stack ' => []]),
130
149
SymfonyTestsListenerForV5::class,
131
150
'' ,
132
151
],
133
152
'serialized_trace_with_not_from_vendors_triggering_file ' => [
134
- true ,
153
+ Deprecation:: TYPE_SELF ,
135
154
serialize ([
136
155
'class ' => '' ,
137
156
'method ' => '' ,
@@ -143,7 +162,7 @@ public function providerIsSelf(): array
143
162
'' ,
144
163
],
145
164
'serialized_trace_with_nonexistent_triggering_file ' => [
146
- false ,
165
+ Deprecation:: TYPE_UNDETERMINED ,
147
166
serialize ([
148
167
'class ' => '' ,
149
168
'method ' => '' ,
@@ -158,16 +177,72 @@ public function providerIsSelf(): array
158
177
}
159
178
160
179
/**
161
- * @dataProvider providerIsSelf
180
+ * @dataProvider providerGetTypeDetectsSelf
162
181
*/
163
- public function testIsSelf ( bool $ expectedIsSelf , string $ message , string $ traceClass , string $ file ): void
182
+ public function testGetTypeDetectsSelf ( string $ expectedType , string $ message , string $ traceClass , string $ file ): void
164
183
{
165
184
$ trace = [
166
185
['class ' => 'MyClass1 ' , 'function ' => 'myMethod ' ],
167
186
['class ' => $ traceClass , 'function ' => 'myMethod ' ],
168
187
];
169
188
$ deprecation = new Deprecation ($ message , $ trace , $ file );
170
- $ this ->assertEquals ($ expectedIsSelf , $ deprecation ->getType () === Deprecation::TYPE_SELF );
189
+ $ this ->assertEquals ($ expectedType , $ deprecation ->getType ());
190
+ }
191
+
192
+ public function providerGetTypeUsesRightTrace (): array
193
+ {
194
+ $ vendorDir = self ::getVendorDir ();
195
+
196
+ return [
197
+ 'no_file_in_stack ' => [Deprecation::TYPE_DIRECT , '' , [['function ' => 'myfunc1 ' ], ['function ' => 'myfunc2 ' ]]],
198
+ 'files_in_stack_from_various_packages ' => [
199
+ Deprecation::TYPE_INDIRECT ,
200
+ '' ,
201
+ [
202
+ ['function ' => 'myfunc1 ' , 'file ' => $ vendorDir .'/myfakevendor/myfakepackage1/MyFakeFile1.php ' ],
203
+ ['function ' => 'myfunc2 ' , 'file ' => $ vendorDir .'/myfakevendor/myfakepackage2/MyFakeFile.php ' ],
204
+ ],
205
+ ],
206
+ 'serialized_stack_files_from_same_package ' => [
207
+ Deprecation::TYPE_DIRECT ,
208
+ serialize ([
209
+ 'deprecation ' => 'My deprecation message ' ,
210
+ 'class ' => 'MyClass ' ,
211
+ 'method ' => 'myMethod ' ,
212
+ 'files_stack ' => [
213
+ $ vendorDir .'/myfakevendor/myfakepackage1/MyFakeFile1.php ' ,
214
+ $ vendorDir .'/myfakevendor/myfakepackage1/MyFakeFile2.php ' ,
215
+ ],
216
+ ]),
217
+ [['function ' => 'myfunc1 ' ], ['class ' => SymfonyTestsListenerForV5::class, 'method ' => 'mymethod ' ]],
218
+ ],
219
+ 'serialized_stack_files_from_various_packages ' => [
220
+ Deprecation::TYPE_INDIRECT ,
221
+ serialize ([
222
+ 'deprecation ' => 'My deprecation message ' ,
223
+ 'class ' => 'MyClass ' ,
224
+ 'method ' => 'myMethod ' ,
225
+ 'files_stack ' => [
226
+ $ vendorDir .'/myfakevendor/myfakepackage1/MyFakeFile1.php ' ,
227
+ $ vendorDir .'/myfakevendor/myfakepackage2/MyFakeFile.php ' ,
228
+ ],
229
+ ]),
230
+ [['function ' => 'myfunc1 ' ], ['class ' => SymfonyTestsListenerForV5::class, 'method ' => 'mymethod ' ]],
231
+ ],
232
+ ];
233
+ }
234
+
235
+ /**
236
+ * @dataProvider providerGetTypeUsesRightTrace
237
+ */
238
+ public function testGetTypeUsesRightTrace (string $ expectedType , string $ message , array $ trace ): void
239
+ {
240
+ $ deprecation = new Deprecation (
241
+ $ message ,
242
+ $ trace ,
243
+ self ::getVendorDir ().'/myfakevendor/myfakepackage2/MyFakeFile.php '
244
+ );
245
+ $ this ->assertEquals ($ expectedType , $ deprecation ->getType ());
171
246
}
172
247
173
248
/**
@@ -178,4 +253,22 @@ public function debugBacktrace(): array
178
253
{
179
254
return debug_backtrace ();
180
255
}
256
+
257
+ private static function removeDir ($ dir ): void
258
+ {
259
+ $ files = glob ($ dir .'/* ' );
260
+ foreach ($ files as $ file ) {
261
+ if (is_file ($ file )) {
262
+ unlink ($ file );
263
+ } else {
264
+ self ::removeDir ($ file );
265
+ }
266
+ }
267
+ rmdir ($ dir );
268
+ }
269
+
270
+ public static function tearDownAfterClass (): void
271
+ {
272
+ self ::removeDir (self ::getVendorDir ().'/myfakevendor ' );
273
+ }
181
274
}
0 commit comments