File: DeprecationTest.php

package info (click to toggle)
symfony 5.4.23%2Bdfsg-1%2Bdeb12u4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 78,212 kB
  • sloc: php: 845,908; xml: 5,982; sh: 590; javascript: 578; makefile: 155; pascal: 84
file content (280 lines) | stat: -rw-r--r-- 10,543 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerForV7;

class DeprecationTest extends TestCase
{
    private static $vendorDir;
    private static $prefixDirsPsr4;

    private static function getVendorDir()
    {
        if (null !== self::$vendorDir) {
            return self::$vendorDir;
        }

        foreach (get_declared_classes() as $class) {
            if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
                $r = new \ReflectionClass($class);
                $vendorDir = \dirname($r->getFileName(), 2);
                if (file_exists($vendorDir.'/composer/installed.json') && @mkdir($vendorDir.'/myfakevendor/myfakepackage1', 0777, true)) {
                    break;
                }
            }
        }

        self::$vendorDir = $vendorDir;
        @mkdir($vendorDir.'/myfakevendor/myfakepackage2');
        touch($vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php');
        touch($vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile2.php');
        touch($vendorDir.'/myfakevendor/myfakepackage2/MyFakeFile.php');

        return self::$vendorDir;
    }

    public function testItCanDetermineTheClassWhereTheDeprecationHappened()
    {
        $deprecation = new Deprecation('💩', $this->debugBacktrace(), __FILE__);
        $this->assertTrue($deprecation->originatesFromAnObject());
        $this->assertSame(self::class, $deprecation->originatingClass());
        $this->assertSame(__FUNCTION__, $deprecation->originatingMethod());
    }

    public function testLegacyTestMethodIsDetectedAsSuch()
    {
        $deprecation = new Deprecation('💩', $this->debugBacktrace(), __FILE__);
        $this->assertTrue($deprecation->isLegacy('whatever'));
    }

    public function testItCanBeConvertedToAString()
    {
        $deprecation = new Deprecation('💩', $this->debugBacktrace(), __FILE__);
        $this->assertStringContainsString('💩', $deprecation->toString());
        $this->assertStringContainsString(__FUNCTION__, $deprecation->toString());
    }

    /**
     * @dataProvider mutedProvider
     */
    public function testItMutesOnlySpecificErrorMessagesWhenTheCallingCodeIsInPhpunit($muted, $callingClass, $message)
    {
        $trace = $this->debugBacktrace();
        array_unshift($trace, ['class' => $callingClass]);
        array_unshift($trace, ['class' => DeprecationErrorHandler::class]);
        $deprecation = new Deprecation($message, $trace, 'should_not_matter.php');
        $this->assertSame($muted, $deprecation->isMuted());
    }

    public static function mutedProvider()
    {
        yield 'not from phpunit, and not a whitelisted message' => [
            false,
            \My\Source\Code::class,
            'Self deprecating humor is deprecated by itself',
        ];
        yield 'from phpunit, but not a whitelisted message' => [
            false,
            \PHPUnit\Random\Piece\Of\Code::class,
            'Self deprecating humor is deprecated by itself',
        ];
        yield 'whitelisted message, but not from phpunit' => [
            false,
            \My\Source\Code::class,
            'Function ReflectionType::__toString() is deprecated',
        ];
        yield 'from phpunit and whitelisted message' => [
            true,
            \PHPUnit\Random\Piece\Of\Code::class,
            'Function ReflectionType::__toString() is deprecated',
        ];
    }

    public function testNotMutedIfNotCalledFromAClassButARandomFile()
    {
        $deprecation = new Deprecation(
            'Function ReflectionType::__toString() is deprecated',
            [
                ['file' => 'should_not_matter.php'],
                ['file' => 'should_not_matter_either.php'],
            ],
            'my-procedural-controller.php'
        );
        $this->assertFalse($deprecation->isMuted());
    }

    public function testItTakesMutesDeprecationFromPhpUnitFiles()
    {
        $deprecation = new Deprecation(
            'Function ReflectionType::__toString() is deprecated',
            [
                ['file' => 'should_not_matter.php'],
                ['file' => 'should_not_matter_either.php'],
            ],
            'random_path'.\DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR.'phpunit'.\DIRECTORY_SEPARATOR.'whatever.php'
        );
        $this->assertTrue($deprecation->isMuted());
    }

    public static function providerGetTypeDetectsSelf()
    {
        return [
            'not_from_vendors_file' => [Deprecation::TYPE_SELF, '', 'MyClass1', __FILE__],
            'nonexistent_file' => [Deprecation::TYPE_UNDETERMINED, '', 'MyClass1', 'dummy_vendor_path'],
            'serialized_trace_with_nonexistent_triggering_file' => [
                Deprecation::TYPE_UNDETERMINED,
                serialize([
                    'class' => '',
                    'method' => '',
                    'deprecation' => '',
                    'triggering_file' => 'dummy_vendor_path',
                    'files_stack' => [],
                ]),
                SymfonyTestsListenerForV7::class,
                '',
            ],
        ];
    }

    /**
     * @dataProvider providerGetTypeDetectsSelf
     */
    public function testGetTypeDetectsSelf(string $expectedType, string $message, string $traceClass, string $file)
    {
        $this->markTestSkipped('type_undetermined instead of type_self in Debian as of 2020/01/06');
        $trace = [
            ['class' => 'MyClass1', 'function' => 'myMethod'],
            ['function' => 'trigger_error'],
            ['class' => SymfonyTestsListenerTrait::class, 'function' => 'endTest'],
            ['class' => $traceClass, 'function' => 'myMethod'],
        ];
        $deprecation = new Deprecation($message, $trace, $file);
        $this->assertSame($expectedType, $deprecation->getType());
    }

    public static function providerGetTypeUsesRightTrace()
    {
        $vendorDir = self::getVendorDir();
        $fakeTrace = [
            ['function' => 'trigger_error'],
            ['class' => SymfonyTestsListenerTrait::class, 'function' => 'endTest'],
            ['class' => SymfonyTestsListenerForV7::class, 'function' => 'endTest'],
        ];

        return [
            'no_file_in_stack' => [Deprecation::TYPE_DIRECT, '', [['function' => 'myfunc1'], ['function' => 'myfunc2']]],
            'files_in_stack_from_various_packages' => [
                Deprecation::TYPE_INDIRECT,
                '',
                [
                    ['function' => 'myfunc1', 'file' => $vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php'],
                    ['function' => 'myfunc2', 'file' => $vendorDir.'/myfakevendor/myfakepackage2/MyFakeFile.php'],
                ],
            ],
            'serialized_stack_files_from_same_package' => [
                Deprecation::TYPE_DIRECT,
                serialize([
                    'deprecation' => 'My deprecation message',
                    'class' => 'MyClass',
                    'method' => 'myMethod',
                    'files_stack' => [
                        $vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php',
                        $vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile2.php',
                    ],
                ]),
                $fakeTrace,
            ],
            'serialized_stack_files_from_various_packages' => [
                Deprecation::TYPE_INDIRECT,
                serialize([
                    'deprecation' => 'My deprecation message',
                    'class' => 'MyClass',
                    'method' => 'myMethod',
                    'files_stack' => [
                        $vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php',
                        $vendorDir.'/myfakevendor/myfakepackage2/MyFakeFile.php',
                    ],
                ]),
                $fakeTrace,
            ],
        ];
    }

    /**
     * @dataProvider providerGetTypeUsesRightTrace
     * @group composer
     */
    public function testGetTypeUsesRightTrace(string $expectedType, string $message, array $trace)
    {
        $deprecation = new Deprecation(
            $message,
            $trace,
            self::getVendorDir().'/myfakevendor/myfakepackage2/MyFakeFile.php'
        );
        $this->assertSame($expectedType, $deprecation->getType());
    }

    /**
     * This method is here to simulate the extra level from the piece of code
     * triggering an error to the error handler.
     */
    public function debugBacktrace()
    {
        return debug_backtrace();
    }

    private static function removeDir($dir)
    {
        $files = glob($dir.'/*');
        foreach ($files as $file) {
            if (is_file($file)) {
                unlink($file);
            } else {
                self::removeDir($file);
            }
        }
        rmdir($dir);
    }

    public static function setupBeforeClass(): void
    {
        foreach (get_declared_classes() as $class) {
            if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
                $r = new \ReflectionClass($class);
                $v = \dirname($r->getFileName(), 2);
                if (file_exists($v.'/composer/installed.json')) {
                    $loader = require $v.'/autoload.php';
                    $reflection = new \ReflectionClass($loader);
                    $prop = $reflection->getProperty('prefixDirsPsr4');
                    $prop->setAccessible(true);
                    $currentValue = $prop->getValue($loader);
                    self::$prefixDirsPsr4[] = [$prop, $loader, $currentValue];
                    $currentValue['Symfony\\Bridge\\PhpUnit\\'] = [realpath(__DIR__.'/../..')];
                    $prop->setValue($loader, $currentValue);
                }
            }
        }
    }

    public static function tearDownAfterClass(): void
    {
        foreach (self::$prefixDirsPsr4 as [$prop, $loader, $prefixDirsPsr4]) {
            $prop->setValue($loader, $prefixDirsPsr4);
        }

        self::removeDir(self::getVendorDir().'/myfakevendor');
    }
}