|
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;
|
| 18 | +use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerForV5; |
17 | 19 |
|
18 | 20 | class DeprecationTest extends TestCase
|
19 | 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 | + |
20 | 40 | public function testItCanDetermineTheClassWhereTheDeprecationHappened()
|
21 | 41 | {
|
22 | 42 | $deprecation = new Deprecation('💩', $this->debugBacktrace(), __FILE__);
|
@@ -118,12 +138,135 @@ public function testItTakesMutesDeprecationFromPhpUnitFiles()
|
118 | 138 | $this->assertTrue($deprecation->isMuted());
|
119 | 139 | }
|
120 | 140 |
|
| 141 | + public function providerGetTypeDetectsSelf(): array |
| 142 | + { |
| 143 | + foreach (get_declared_classes() as $class) { |
| 144 | + if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) { |
| 145 | + $r = new \ReflectionClass($class); |
| 146 | + $v = \dirname(\dirname($r->getFileName())); |
| 147 | + if (file_exists($v.'/composer/installed.json')) { |
| 148 | + $loader = require $v.'/autoload.php'; |
| 149 | + $reflection = new \ReflectionClass($loader); |
| 150 | + $prop = $reflection->getProperty('prefixDirsPsr4'); |
| 151 | + $prop->setAccessible(true); |
| 152 | + $currentValue = $prop->getValue($loader); |
| 153 | + $currentValue['Symfony\\Bridge\\PhpUnit\\'] = [realpath(__DIR__.'/../..')]; |
| 154 | + $prop->setValue($loader, $currentValue); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return [ |
| 160 | + 'not_from_vendors_file' => [Deprecation::TYPE_SELF, '', 'MyClass1', ''], |
| 161 | + 'nonexistent_file' => [Deprecation::TYPE_UNDETERMINED, '', 'MyClass1', 'dummy_vendor_path'], |
| 162 | + 'serialized_trace_with_nonexistent_triggering_file' => [ |
| 163 | + Deprecation::TYPE_UNDETERMINED, |
| 164 | + serialize([ |
| 165 | + 'class' => '', |
| 166 | + 'method' => '', |
| 167 | + 'deprecation' => '', |
| 168 | + 'triggering_file' => 'dummy_vendor_path', |
| 169 | + 'files_stack' => [], |
| 170 | + ]), |
| 171 | + SymfonyTestsListenerForV5::class, |
| 172 | + '', |
| 173 | + ], |
| 174 | + ]; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @dataProvider providerGetTypeDetectsSelf |
| 179 | + */ |
| 180 | + public function testGetTypeDetectsSelf(string $expectedType, string $message, string $traceClass, string $file): void |
| 181 | + { |
| 182 | + $trace = [ |
| 183 | + ['class' => 'MyClass1', 'function' => 'myMethod'], |
| 184 | + ['class' => $traceClass, 'function' => 'myMethod'], |
| 185 | + ]; |
| 186 | + $deprecation = new Deprecation($message, $trace, $file); |
| 187 | + $this->assertEquals($expectedType, $deprecation->getType()); |
| 188 | + } |
| 189 | + |
| 190 | + public function providerGetTypeUsesRightTrace(): array |
| 191 | + { |
| 192 | + $vendorDir = self::getVendorDir(); |
| 193 | + |
| 194 | + return [ |
| 195 | + 'no_file_in_stack' => [Deprecation::TYPE_DIRECT, '', [['function' => 'myfunc1'], ['function' => 'myfunc2']]], |
| 196 | + 'files_in_stack_from_various_packages' => [ |
| 197 | + Deprecation::TYPE_INDIRECT, |
| 198 | + '', |
| 199 | + [ |
| 200 | + ['function' => 'myfunc1', 'file' => $vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php'], |
| 201 | + ['function' => 'myfunc2', 'file' => $vendorDir.'/myfakevendor/myfakepackage2/MyFakeFile.php'], |
| 202 | + ], |
| 203 | + ], |
| 204 | + 'serialized_stack_files_from_same_package' => [ |
| 205 | + Deprecation::TYPE_DIRECT, |
| 206 | + serialize([ |
| 207 | + 'deprecation' => 'My deprecation message', |
| 208 | + 'class' => 'MyClass', |
| 209 | + 'method' => 'myMethod', |
| 210 | + 'files_stack' => [ |
| 211 | + $vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php', |
| 212 | + $vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile2.php', |
| 213 | + ], |
| 214 | + ]), |
| 215 | + [['function' => 'myfunc1'], ['class' => SymfonyTestsListenerForV5::class, 'method' => 'mymethod']], |
| 216 | + ], |
| 217 | + 'serialized_stack_files_from_various_packages' => [ |
| 218 | + Deprecation::TYPE_INDIRECT, |
| 219 | + serialize([ |
| 220 | + 'deprecation' => 'My deprecation message', |
| 221 | + 'class' => 'MyClass', |
| 222 | + 'method' => 'myMethod', |
| 223 | + 'files_stack' => [ |
| 224 | + $vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php', |
| 225 | + $vendorDir.'/myfakevendor/myfakepackage2/MyFakeFile.php', |
| 226 | + ], |
| 227 | + ]), |
| 228 | + [['function' => 'myfunc1'], ['class' => SymfonyTestsListenerForV5::class, 'method' => 'mymethod']], |
| 229 | + ], |
| 230 | + ]; |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * @dataProvider providerGetTypeUsesRightTrace |
| 235 | + */ |
| 236 | + public function testGetTypeUsesRightTrace(string $expectedType, string $message, array $trace): void |
| 237 | + { |
| 238 | + $deprecation = new Deprecation( |
| 239 | + $message, |
| 240 | + $trace, |
| 241 | + self::getVendorDir().'/myfakevendor/myfakepackage2/MyFakeFile.php' |
| 242 | + ); |
| 243 | + $this->assertEquals($expectedType, $deprecation->getType()); |
| 244 | + } |
| 245 | + |
121 | 246 | /**
|
122 | 247 | * This method is here to simulate the extra level from the piece of code
|
123 |
| - * triggering an error to the error handler |
| 248 | + * triggering an error to the error handler. |
124 | 249 | */
|
125 | 250 | public function debugBacktrace(): array
|
126 | 251 | {
|
127 | 252 | return debug_backtrace();
|
128 | 253 | }
|
| 254 | + |
| 255 | + private static function removeDir($dir): void |
| 256 | + { |
| 257 | + $files = glob($dir.'/*'); |
| 258 | + foreach ($files as $file) { |
| 259 | + if (is_file($file)) { |
| 260 | + unlink($file); |
| 261 | + } else { |
| 262 | + self::removeDir($file); |
| 263 | + } |
| 264 | + } |
| 265 | + rmdir($dir); |
| 266 | + } |
| 267 | + |
| 268 | + public static function tearDownAfterClass(): void |
| 269 | + { |
| 270 | + self::removeDir(self::getVendorDir().'/myfakevendor'); |
| 271 | + } |
129 | 272 | }
|
0 commit comments