Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Loader / FileLoaderTest.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\DependencyInjection\Tests\Loader;
13
14 use Psr\Container\ContainerInterface as PsrContainerInterface;
15 use PHPUnit\Framework\TestCase;
16 use Symfony\Component\Config\FileLocator;
17 use Symfony\Component\Config\Loader\LoaderResolver;
18 use Symfony\Component\DependencyInjection\ContainerBuilder;
19 use Symfony\Component\DependencyInjection\ContainerInterface;
20 use Symfony\Component\DependencyInjection\Definition;
21 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
22 use Symfony\Component\DependencyInjection\Loader\FileLoader;
23 use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
24 use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
25 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
26 use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
27 use Symfony\Component\DependencyInjection\Reference;
28 use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\MissingParent;
29 use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\AnotherSub\DeeperBaz;
30 use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\OtherDir\Baz;
31 use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo;
32 use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\FooInterface;
33 use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar;
34 use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\BarInterface;
35
36 class FileLoaderTest extends TestCase
37 {
38     protected static $fixturesPath;
39
40     public static function setUpBeforeClass()
41     {
42         self::$fixturesPath = realpath(__DIR__.'/../');
43     }
44
45     public function testImportWithGlobPattern()
46     {
47         $container = new ContainerBuilder();
48         $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath));
49
50         $resolver = new LoaderResolver(array(
51             new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/ini')),
52             new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')),
53             new PhpFileLoader($container, new FileLocator(self::$fixturesPath.'/php')),
54             new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
55         ));
56
57         $loader->setResolver($resolver);
58         $loader->import('{F}ixtures/{xml,yaml}/services2.{yml,xml}');
59
60         $actual = $container->getParameterBag()->all();
61         $expected = array(
62             'a string',
63             'foo' => 'bar',
64             'values' => array(
65                 0,
66                 'integer' => 4,
67                 100 => null,
68                 'true',
69                 true,
70                 false,
71                 'on',
72                 'off',
73                 'float' => 1.3,
74                 1000.3,
75                 'a string',
76                 array('foo', 'bar'),
77             ),
78             'mixedcase' => array('MixedCaseKey' => 'value'),
79             'constant' => PHP_EOL,
80             'bar' => '%foo%',
81             'escape' => '@escapeme',
82             'foo_bar' => new Reference('foo_bar'),
83         );
84
85         $this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
86     }
87
88     public function testRegisterClasses()
89     {
90         $container = new ContainerBuilder();
91         $container->setParameter('sub_dir', 'Sub');
92         $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
93
94         $loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\\', 'Prototype/%sub_dir%/*');
95
96         $this->assertEquals(
97             array('service_container', Bar::class),
98             array_keys($container->getDefinitions())
99         );
100         $this->assertEquals(
101             array(
102                 PsrContainerInterface::class,
103                 ContainerInterface::class,
104                 BarInterface::class,
105             ),
106             array_keys($container->getAliases())
107         );
108     }
109
110     public function testRegisterClassesWithExclude()
111     {
112         $container = new ContainerBuilder();
113         $container->setParameter('other_dir', 'OtherDir');
114         $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
115
116         $loader->registerClasses(
117             new Definition(),
118             'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\',
119             'Prototype/*',
120             // load everything, except OtherDir/AnotherSub & Foo.php
121             'Prototype/{%other_dir%/AnotherSub,Foo.php}'
122         );
123
124         $this->assertTrue($container->has(Bar::class));
125         $this->assertTrue($container->has(Baz::class));
126         $this->assertFalse($container->has(Foo::class));
127         $this->assertFalse($container->has(DeeperBaz::class));
128
129         $this->assertEquals(
130             array(
131                 PsrContainerInterface::class,
132                 ContainerInterface::class,
133                 BarInterface::class,
134             ),
135             array_keys($container->getAliases())
136         );
137     }
138
139     public function testNestedRegisterClasses()
140     {
141         $container = new ContainerBuilder();
142         $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
143
144         $prototype = new Definition();
145         $prototype->setPublic(true)->setPrivate(true);
146         $loader->registerClasses($prototype, 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\', 'Prototype/*');
147
148         $this->assertTrue($container->has(Bar::class));
149         $this->assertTrue($container->has(Baz::class));
150         $this->assertTrue($container->has(Foo::class));
151
152         $this->assertEquals(
153             array(
154                 PsrContainerInterface::class,
155                 ContainerInterface::class,
156                 FooInterface::class,
157             ),
158             array_keys($container->getAliases())
159         );
160
161         $alias = $container->getAlias(FooInterface::class);
162         $this->assertSame(Foo::class, (string) $alias);
163         $this->assertFalse($alias->isPublic());
164         $this->assertFalse($alias->isPrivate());
165     }
166
167     public function testMissingParentClass()
168     {
169         $container = new ContainerBuilder();
170         $container->setParameter('bad_classes_dir', 'BadClasses');
171         $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
172
173         $loader->registerClasses(
174             (new Definition())->setPublic(false),
175             'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\\',
176             'Prototype/%bad_classes_dir%/*'
177         );
178
179         $this->assertTrue($container->has(MissingParent::class));
180
181         $this->assertSame(
182             array('While discovering services from namespace "Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\", an error was thrown when processing the class "Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\MissingParent": "Class Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\MissingClass not found".'),
183             $container->getDefinition(MissingParent::class)->getErrors()
184         );
185     }
186
187     /**
188      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
189      * @expectedExceptionMessageRegExp /Expected to find class "Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\Prototype\\Bar" in file ".+" while importing services from resource "Prototype\/Sub\/\*", but it was not found\! Check the namespace prefix used with the resource/
190      */
191     public function testRegisterClassesWithBadPrefix()
192     {
193         $container = new ContainerBuilder();
194         $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
195
196         // the Sub is missing from namespace prefix
197         $loader->registerClasses(new Definition(), 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\', 'Prototype/Sub/*');
198     }
199
200     /**
201      * @dataProvider getIncompatibleExcludeTests
202      */
203     public function testRegisterClassesWithIncompatibleExclude($resourcePattern, $excludePattern)
204     {
205         $container = new ContainerBuilder();
206         $loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
207
208         try {
209             $loader->registerClasses(
210                 new Definition(),
211                 'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\',
212                 $resourcePattern,
213                 $excludePattern
214             );
215         } catch (InvalidArgumentException $e) {
216             $this->assertEquals(
217                 sprintf('Invalid "exclude" pattern when importing classes for "Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\": make sure your "exclude" pattern (%s) is a subset of the "resource" pattern (%s)', $excludePattern, $resourcePattern),
218                 $e->getMessage()
219             );
220         }
221     }
222
223     public function getIncompatibleExcludeTests()
224     {
225         yield array('Prototype/*', 'yaml/*', false);
226         yield array('Prototype/OtherDir/*', 'Prototype/*', false);
227     }
228 }
229
230 class TestFileLoader extends FileLoader
231 {
232     public function load($resource, $type = null)
233     {
234         return $resource;
235     }
236
237     public function supports($resource, $type = null)
238     {
239         return false;
240     }
241 }