4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Loader;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\FileLocator;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
18 use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
19 use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
21 class PhpFileLoaderTest extends TestCase
23 public function testSupports()
25 $loader = new PhpFileLoader(new ContainerBuilder(), new FileLocator());
27 $this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
28 $this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
29 $this->assertTrue($loader->supports('with_wrong_ext.yml', 'php'), '->supports() returns true if the resource with forced type is loadable');
32 public function testLoad()
34 $loader = new PhpFileLoader($container = new ContainerBuilder(), new FileLocator());
36 $loader->load(__DIR__.'/../Fixtures/php/simple.php');
38 $this->assertEquals('foo', $container->getParameter('foo'), '->load() loads a PHP file resource');
41 public function testConfigServices()
43 $fixtures = realpath(__DIR__.'/../Fixtures');
44 $loader = new PhpFileLoader($container = new ContainerBuilder(), new FileLocator());
45 $loader->load($fixtures.'/config/services9.php');
47 $container->compile();
48 $dumper = new PhpDumper($container);
49 $this->assertStringEqualsFile($fixtures.'/php/services9_compiled.php', str_replace(str_replace('\\', '\\\\', $fixtures.\DIRECTORY_SEPARATOR.'includes'.\DIRECTORY_SEPARATOR), '%path%', $dumper->dump()));
53 * @dataProvider provideConfig
55 public function testConfig($file)
57 $fixtures = realpath(__DIR__.'/../Fixtures');
58 $loader = new PhpFileLoader($container = new ContainerBuilder(), new FileLocator());
59 $loader->load($fixtures.'/config/'.$file.'.php');
61 $container->compile();
63 $dumper = new YamlDumper($container);
64 $this->assertStringEqualsFile($fixtures.'/config/'.$file.'.expected.yml', $dumper->dump());
67 public function provideConfig()
70 yield array('defaults');
71 yield array('instanceof');
72 yield array('prototype');
75 if (\PHP_VERSION_ID >= 70000) {
81 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
82 * @expectedExceptionMessage The service "child_service" cannot have a "parent" and also have "autoconfigure". Try disabling autoconfiguration for the service.
84 public function testAutoConfigureAndChildDefinitionNotAllowed()
86 $fixtures = realpath(__DIR__.'/../Fixtures');
87 $container = new ContainerBuilder();
88 $loader = new PhpFileLoader($container, new FileLocator());
89 $loader->load($fixtures.'/config/services_autoconfigure_with_parent.php');
90 $container->compile();
94 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
95 * @expectedExceptionMessage Invalid factory "factory:method": the `service:method` notation is not available when using PHP-based DI configuration. Use "[ref('factory'), 'method']" instead.
97 public function testFactoryShortNotationNotAllowed()
99 $fixtures = realpath(__DIR__.'/../Fixtures');
100 $container = new ContainerBuilder();
101 $loader = new PhpFileLoader($container, new FileLocator());
102 $loader->load($fixtures.'/config/factory_short_notation.php');
103 $container->compile();