Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / class-loader / Tests / ClassCollectionLoaderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\ClassLoader\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\ClassLoader\ClassCollectionLoader;
16 use Symfony\Component\ClassLoader\Tests\Fixtures\DeclaredClass;
17 use Symfony\Component\ClassLoader\Tests\Fixtures\WarmedClass;
18
19 require_once __DIR__.'/Fixtures/ClassesWithParents/GInterface.php';
20 require_once __DIR__.'/Fixtures/ClassesWithParents/CInterface.php';
21 require_once __DIR__.'/Fixtures/ClassesWithParents/B.php';
22 require_once __DIR__.'/Fixtures/ClassesWithParents/A.php';
23
24 /**
25  * @group legacy
26  */
27 class ClassCollectionLoaderTest extends TestCase
28 {
29     public function testTraitDependencies()
30     {
31         require_once __DIR__.'/Fixtures/deps/traits.php';
32
33         $r = new \ReflectionClass('Symfony\Component\ClassLoader\ClassCollectionLoader');
34         $m = $r->getMethod('getOrderedClasses');
35         $m->setAccessible(true);
36
37         $ordered = $m->invoke(null, array('CTFoo'));
38
39         $this->assertEquals(
40             array('TD', 'TC', 'TB', 'TA', 'TZ', 'CTFoo'),
41             array_map(function ($class) { return $class->getName(); }, $ordered)
42         );
43
44         $ordered = $m->invoke(null, array('CTBar'));
45
46         $this->assertEquals(
47             array('TD', 'TZ', 'TC', 'TB', 'TA', 'CTBar'),
48             array_map(function ($class) { return $class->getName(); }, $ordered)
49         );
50     }
51
52     /**
53      * @dataProvider getDifferentOrders
54      */
55     public function testClassReordering(array $classes)
56     {
57         $expected = array(
58             'ClassesWithParents\\GInterface',
59             'ClassesWithParents\\CInterface',
60             'ClassesWithParents\\B',
61             'ClassesWithParents\\A',
62         );
63
64         $r = new \ReflectionClass('Symfony\Component\ClassLoader\ClassCollectionLoader');
65         $m = $r->getMethod('getOrderedClasses');
66         $m->setAccessible(true);
67
68         $ordered = $m->invoke(null, $classes);
69
70         $this->assertEquals($expected, array_map(function ($class) { return $class->getName(); }, $ordered));
71     }
72
73     public function getDifferentOrders()
74     {
75         return array(
76             array(array(
77                 'ClassesWithParents\\A',
78                 'ClassesWithParents\\CInterface',
79                 'ClassesWithParents\\GInterface',
80                 'ClassesWithParents\\B',
81             )),
82             array(array(
83                 'ClassesWithParents\\B',
84                 'ClassesWithParents\\A',
85                 'ClassesWithParents\\CInterface',
86             )),
87             array(array(
88                 'ClassesWithParents\\CInterface',
89                 'ClassesWithParents\\B',
90                 'ClassesWithParents\\A',
91             )),
92             array(array(
93                 'ClassesWithParents\\A',
94             )),
95         );
96     }
97
98     /**
99      * @dataProvider getDifferentOrdersForTraits
100      */
101     public function testClassWithTraitsReordering(array $classes)
102     {
103         require_once __DIR__.'/Fixtures/ClassesWithParents/ATrait.php';
104         require_once __DIR__.'/Fixtures/ClassesWithParents/BTrait.php';
105         require_once __DIR__.'/Fixtures/ClassesWithParents/CTrait.php';
106         require_once __DIR__.'/Fixtures/ClassesWithParents/D.php';
107         require_once __DIR__.'/Fixtures/ClassesWithParents/E.php';
108
109         $expected = array(
110             'ClassesWithParents\\GInterface',
111             'ClassesWithParents\\CInterface',
112             'ClassesWithParents\\ATrait',
113             'ClassesWithParents\\BTrait',
114             'ClassesWithParents\\CTrait',
115             'ClassesWithParents\\B',
116             'ClassesWithParents\\A',
117             'ClassesWithParents\\D',
118             'ClassesWithParents\\E',
119         );
120
121         $r = new \ReflectionClass('Symfony\Component\ClassLoader\ClassCollectionLoader');
122         $m = $r->getMethod('getOrderedClasses');
123         $m->setAccessible(true);
124
125         $ordered = $m->invoke(null, $classes);
126
127         $this->assertEquals($expected, array_map(function ($class) { return $class->getName(); }, $ordered));
128     }
129
130     public function getDifferentOrdersForTraits()
131     {
132         return array(
133             array(array(
134                 'ClassesWithParents\\E',
135                 'ClassesWithParents\\ATrait',
136             )),
137             array(array(
138                 'ClassesWithParents\\E',
139             )),
140         );
141     }
142
143     public function testFixClassWithTraitsOrdering()
144     {
145         require_once __DIR__.'/Fixtures/ClassesWithParents/CTrait.php';
146         require_once __DIR__.'/Fixtures/ClassesWithParents/F.php';
147         require_once __DIR__.'/Fixtures/ClassesWithParents/G.php';
148
149         $classes = array(
150             'ClassesWithParents\\F',
151             'ClassesWithParents\\G',
152         );
153
154         $expected = array(
155             'ClassesWithParents\\CTrait',
156             'ClassesWithParents\\F',
157             'ClassesWithParents\\G',
158         );
159
160         $r = new \ReflectionClass('Symfony\Component\ClassLoader\ClassCollectionLoader');
161         $m = $r->getMethod('getOrderedClasses');
162         $m->setAccessible(true);
163
164         $ordered = $m->invoke(null, $classes);
165
166         $this->assertEquals($expected, array_map(function ($class) { return $class->getName(); }, $ordered));
167     }
168
169     /**
170      * @dataProvider getFixNamespaceDeclarationsData
171      */
172     public function testFixNamespaceDeclarations($source, $expected)
173     {
174         $this->assertEquals('<?php '.$expected, ClassCollectionLoader::fixNamespaceDeclarations('<?php '.$source));
175     }
176
177     public function getFixNamespaceDeclarationsData()
178     {
179         return array(
180             array("namespace;\nclass Foo {}\n", "namespace\n{\nclass Foo {}\n}"),
181             array("namespace Foo;\nclass Foo {}\n", "namespace Foo\n{\nclass Foo {}\n}"),
182             array("namespace   Bar ;\nclass Foo {}\n", "namespace Bar\n{\nclass Foo {}\n}"),
183             array("namespace Foo\Bar;\nclass Foo {}\n", "namespace Foo\Bar\n{\nclass Foo {}\n}"),
184             array("namespace Foo\Bar\Bar\n{\nclass Foo {}\n}\n", "namespace Foo\Bar\Bar\n{\nclass Foo {}\n}"),
185             array("namespace\n{\nclass Foo {}\n}\n", "namespace\n{\nclass Foo {}\n}"),
186         );
187     }
188
189     /**
190      * @dataProvider getFixNamespaceDeclarationsDataWithoutTokenizer
191      */
192     public function testFixNamespaceDeclarationsWithoutTokenizer($source, $expected)
193     {
194         ClassCollectionLoader::enableTokenizer(false);
195         $this->assertEquals('<?php '.$expected, ClassCollectionLoader::fixNamespaceDeclarations('<?php '.$source));
196         ClassCollectionLoader::enableTokenizer(true);
197     }
198
199     public function getFixNamespaceDeclarationsDataWithoutTokenizer()
200     {
201         return array(
202             array("namespace;\nclass Foo {}\n", "namespace\n{\nclass Foo {}\n}\n"),
203             array("namespace Foo;\nclass Foo {}\n", "namespace Foo\n{\nclass Foo {}\n}\n"),
204             array("namespace   Bar ;\nclass Foo {}\n", "namespace   Bar\n{\nclass Foo {}\n}\n"),
205             array("namespace Foo\Bar;\nclass Foo {}\n", "namespace Foo\Bar\n{\nclass Foo {}\n}\n"),
206             array("namespace Foo\Bar\Bar\n{\nclass Foo {}\n}\n", "namespace Foo\Bar\Bar\n{\nclass Foo {}\n}\n"),
207             array("\nnamespace\n{\nclass Foo {}\n\$namespace=123;}\n", "\nnamespace\n{\nclass Foo {}\n\$namespace=123;}\n"),
208         );
209     }
210
211     /**
212      * @expectedException \InvalidArgumentException
213      */
214     public function testUnableToLoadClassException()
215     {
216         if (is_file($file = sys_get_temp_dir().'/foo.php')) {
217             unlink($file);
218         }
219
220         ClassCollectionLoader::load(array('SomeNotExistingClass'), sys_get_temp_dir(), 'foo', false);
221     }
222
223     public function testCommentStripping()
224     {
225         if (is_file($file = __DIR__.'/bar.php')) {
226             unlink($file);
227         }
228         spl_autoload_register($r = function ($class) {
229             if (0 === strpos($class, 'Namespaced') || 0 === strpos($class, 'Pearlike_')) {
230                 @require_once __DIR__.'/Fixtures/'.str_replace(array('\\', '_'), '/', $class).'.php';
231             }
232         });
233
234         $strictTypes = \defined('HHVM_VERSION') ? '' : "\nnamespace {require __DIR__.'/Fixtures/Namespaced/WithStrictTypes.php';}";
235
236         ClassCollectionLoader::load(
237             array('Namespaced\\WithComments', 'Pearlike_WithComments', 'Namespaced\\WithDirMagic', 'Namespaced\\WithFileMagic', 'Namespaced\\WithHaltCompiler', $strictTypes ? 'Namespaced\\WithStrictTypes' : 'Namespaced\\WithComments'),
238             __DIR__,
239             'bar',
240             false
241         );
242
243         spl_autoload_unregister($r);
244
245         $this->assertEquals(<<<'EOF'
246 namespace Namespaced
247 {
248 class WithComments
249 {
250 public static $loaded = true;
251 }
252 $string ='string should not be   modified {$string}';
253 $heredoc = (<<<HD
254
255
256 Heredoc should not be   modified {$string}
257
258
259 HD
260 );
261 $nowdoc =<<<'ND'
262
263
264 Nowdoc should not be   modified {$string}
265
266
267 ND
268 ;
269 }
270 namespace
271 {
272 class Pearlike_WithComments
273 {
274 public static $loaded = true;
275 }
276 }
277 namespace {require __DIR__.'/Fixtures/Namespaced/WithDirMagic.php';}
278 namespace {require __DIR__.'/Fixtures/Namespaced/WithFileMagic.php';}
279 namespace {require __DIR__.'/Fixtures/Namespaced/WithHaltCompiler.php';}
280 EOF
281             .$strictTypes,
282             str_replace(array("<?php \n", '\\\\'), array('', '/'), file_get_contents($file))
283         );
284
285         unlink($file);
286     }
287
288     public function testInline()
289     {
290         $this->assertTrue(class_exists(WarmedClass::class, true));
291
292         @unlink($cache = sys_get_temp_dir().'/inline.php');
293
294         $classes = array(WarmedClass::class);
295         $excluded = array(DeclaredClass::class);
296
297         ClassCollectionLoader::inline($classes, $cache, $excluded);
298
299         $this->assertSame(<<<'EOTXT'
300 <?php 
301 namespace Symfony\Component\ClassLoader\Tests\Fixtures
302 {
303 interface WarmedInterface
304 {
305 }
306 }
307 namespace Symfony\Component\ClassLoader\Tests\Fixtures
308 {
309 class WarmedClass extends DeclaredClass implements WarmedInterface
310 {
311 }
312 }
313 EOTXT
314             , file_get_contents($cache)
315         );
316
317         unlink($cache);
318     }
319 }