2010-03-18 07:48:03 +00:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2011-01-15 13:29:43 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-04-07 02:07:59 +01:00
*
2010-03-18 07:48:03 +00:00
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\DependencyInjection\Tests\Dumper ;
2010-03-18 07:48:03 +00:00
2017-02-08 07:24:27 +00:00
use PHPUnit\Framework\TestCase ;
2017-04-17 18:50:34 +01:00
use Psr\Container\ContainerInterface ;
2016-08-12 19:34:27 +01:00
use Symfony\Component\Config\FileLocator ;
2016-12-13 17:37:51 +00:00
use Symfony\Component\DependencyInjection\Argument\IteratorArgument ;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator ;
2017-02-26 17:31:03 +00:00
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument ;
2017-09-17 10:49:41 +01:00
use Symfony\Component\DependencyInjection\ChildDefinition ;
2017-02-26 17:31:03 +00:00
use Symfony\Component\DependencyInjection\ContainerBuilder ;
2017-05-26 00:10:31 +01:00
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface ;
2018-07-26 10:03:18 +01:00
use Symfony\Component\DependencyInjection\Definition ;
2017-02-26 17:31:03 +00:00
use Symfony\Component\DependencyInjection\Dumper\PhpDumper ;
2016-10-22 17:25:15 +01:00
use Symfony\Component\DependencyInjection\EnvVarProcessorInterface ;
2018-07-24 10:19:17 +01:00
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException ;
2018-07-26 10:06:28 +01:00
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader ;
2017-12-06 12:34:05 +00:00
use Symfony\Component\DependencyInjection\Parameter ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag ;
use Symfony\Component\DependencyInjection\Reference ;
2017-02-06 23:00:41 +00:00
use Symfony\Component\DependencyInjection\ServiceLocator ;
2017-07-29 14:29:10 +01:00
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition ;
2018-07-26 10:06:28 +01:00
use Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator ;
2017-04-06 09:48:39 +01:00
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber ;
2018-07-26 10:06:28 +01:00
use Symfony\Component\DependencyInjection\TypedReference ;
2016-04-10 19:38:01 +01:00
use Symfony\Component\DependencyInjection\Variable ;
use Symfony\Component\ExpressionLanguage\Expression ;
2010-03-18 07:48:03 +00:00
2016-09-10 18:39:00 +01:00
require_once __DIR__ . '/../Fixtures/includes/classes.php' ;
2017-02-08 07:24:27 +00:00
class PhpDumperTest extends TestCase
2010-03-18 07:48:03 +00:00
{
2012-07-09 13:50:58 +01:00
protected static $fixturesPath ;
2010-03-18 07:48:03 +00:00
2012-07-09 13:50:58 +01:00
public static function setUpBeforeClass ()
2010-05-06 12:25:53 +01:00
{
2010-06-28 08:31:54 +01:00
self :: $fixturesPath = realpath ( __DIR__ . '/../Fixtures/' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
public function testDump ()
{
2016-11-25 13:27:11 +00:00
$container = new ContainerBuilder ();
$container -> compile ();
$dumper = new PhpDumper ( $container );
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services1.php' , $dumper -> dump (), '->dump() dumps an empty container as an empty PHP class' );
2013-05-22 20:21:30 +01:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services1-1.php' , $dumper -> dump ( array ( 'class' => 'Container' , 'base_class' => 'AbstractContainer' , 'namespace' => 'Symfony\Component\DependencyInjection\Dump' )), '->dump() takes a class and a base_class options' );
2011-10-18 13:52:35 +01:00
}
public function testDumpOptimizationString ()
{
$definition = new Definition ();
$definition -> setClass ( 'stdClass' );
$definition -> addArgument ( array (
'only dot' => '.' ,
'concatenation as value' => '.\'\'.' ,
'concatenation from the start value' => '\'\'.' ,
'.' => 'dot as a key' ,
'.\'\'.' => 'concatenation as a key' ,
2014-09-21 19:53:12 +01:00
'\'\'.' => 'concatenation from the start key' ,
2015-03-21 10:51:07 +00:00
'optimize concatenation' => 'string1%some_string%string2' ,
'optimize concatenation with empty string' => 'string1%empty_value%string2' ,
2011-10-18 13:52:35 +01:00
'optimize concatenation from the start' => '%empty_value%start' ,
'optimize concatenation at the end' => 'end%empty_value%' ,
2017-10-12 12:28:41 +01:00
'new line' => " string with \n new line " ,
2011-10-18 13:52:35 +01:00
));
2017-09-17 10:49:41 +01:00
$definition -> setPublic ( true );
2011-10-18 13:52:35 +01:00
$container = new ContainerBuilder ();
2012-12-28 09:40:13 +00:00
$container -> setResourceTracking ( false );
2011-10-18 13:52:35 +01:00
$container -> setDefinition ( 'test' , $definition );
$container -> setParameter ( 'empty_value' , '' );
$container -> setParameter ( 'some_string' , '-' );
$container -> compile ();
2010-05-06 12:25:53 +01:00
$dumper = new PhpDumper ( $container );
2011-10-18 13:52:35 +01:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services10.php' , $dumper -> dump (), '->dump() dumps an empty container as an empty PHP class' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
2014-11-30 15:07:59 +00:00
public function testDumpRelativeDir ()
{
$definition = new Definition ();
$definition -> setClass ( 'stdClass' );
$definition -> addArgument ( '%foo%' );
2014-12-04 18:27:52 +00:00
$definition -> addArgument ( array ( '%foo%' => '%buz%/' ));
2017-09-17 10:49:41 +01:00
$definition -> setPublic ( true );
2014-11-30 15:07:59 +00:00
$container = new ContainerBuilder ();
$container -> setDefinition ( 'test' , $definition );
2018-07-05 12:24:53 +01:00
$container -> setParameter ( 'foo' , 'wiz' . \dirname ( __DIR__ ));
2015-03-06 19:37:36 +00:00
$container -> setParameter ( 'bar' , __DIR__ );
2014-11-30 15:07:59 +00:00
$container -> setParameter ( 'baz' , '%bar%/PhpDumperTest.php' );
2018-07-05 12:24:53 +01:00
$container -> setParameter ( 'buz' , \dirname ( \dirname ( __DIR__ )));
2014-11-30 15:07:59 +00:00
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services12.php' , $dumper -> dump ( array ( 'file' => __FILE__ )), '->dump() dumps __DIR__ relative strings' );
}
2018-01-11 07:05:20 +00:00
public function testDumpCustomContainerClassWithoutConstructor ()
{
$container = new ContainerBuilder ();
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/custom_container_class_without_constructor.php' , $dumper -> dump ( array ( 'base_class' => 'NoConstructorContainer' , 'namespace' => 'Symfony\Component\DependencyInjection\Tests\Fixtures\Container' )));
}
public function testDumpCustomContainerClassConstructorWithoutArguments ()
{
$container = new ContainerBuilder ();
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/custom_container_class_constructor_without_arguments.php' , $dumper -> dump ( array ( 'base_class' => 'ConstructorWithoutArgumentsContainer' , 'namespace' => 'Symfony\Component\DependencyInjection\Tests\Fixtures\Container' )));
}
public function testDumpCustomContainerClassWithOptionalArgumentLessConstructor ()
{
$container = new ContainerBuilder ();
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/custom_container_class_with_optional_constructor_arguments.php' , $dumper -> dump ( array ( 'base_class' => 'ConstructorWithOptionalArgumentsContainer' , 'namespace' => 'Symfony\Component\DependencyInjection\Tests\Fixtures\Container' )));
}
public function testDumpCustomContainerClassWithMandatoryArgumentLessConstructor ()
{
$container = new ContainerBuilder ();
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/custom_container_class_with_mandatory_constructor_arguments.php' , $dumper -> dump ( array ( 'base_class' => 'ConstructorWithMandatoryArgumentsContainer' , 'namespace' => 'Symfony\Component\DependencyInjection\Tests\Fixtures\Container' )));
}
2010-06-27 17:28:29 +01:00
/**
2016-04-10 19:38:01 +01:00
* @ dataProvider provideInvalidParameters
2013-11-25 08:44:14 +00:00
* @ expectedException \InvalidArgumentException
2010-06-27 17:28:29 +01:00
*/
2016-04-10 19:38:01 +01:00
public function testExportParameters ( $parameters )
2010-06-27 17:28:29 +01:00
{
2016-11-25 13:27:11 +00:00
$container = new ContainerBuilder ( new ParameterBag ( $parameters ));
$container -> compile ();
$dumper = new PhpDumper ( $container );
2010-06-27 17:28:29 +01:00
$dumper -> dump ();
}
2016-04-10 19:38:01 +01:00
public function provideInvalidParameters ()
{
return array (
array ( array ( 'foo' => new Definition ( 'stdClass' ))),
2016-07-22 15:17:18 +01:00
array ( array ( 'foo' => new Expression ( 'service("foo").foo() ~ (container.hasParameter("foo") ? parameter("foo") : "default")' ))),
2016-04-10 19:38:01 +01:00
array ( array ( 'foo' => new Reference ( 'foo' ))),
array ( array ( 'foo' => new Variable ( 'foo' ))),
);
}
2010-05-06 12:25:53 +01:00
public function testAddParameters ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$container = include self :: $fixturesPath . '/containers/container8.php' ;
2016-11-25 13:27:11 +00:00
$container -> compile ();
2010-05-06 12:25:53 +01:00
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services8.php' , $dumper -> dump (), '->dump() dumps parameters' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
2016-11-25 13:27:11 +00:00
/**
* @ group legacy
2017-12-31 05:33:21 +00:00
* @ expectedDeprecation Dumping an uncompiled ContainerBuilder is deprecated since Symfony 3.3 and will not be supported anymore in 4.0 . Compile the container beforehand .
2016-11-25 13:27:11 +00:00
*/
public function testAddServiceWithoutCompilation ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$container = include self :: $fixturesPath . '/containers/container9.php' ;
$dumper = new PhpDumper ( $container );
2018-07-26 12:13:39 +01:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services9.php' , str_replace ( str_replace ( '\\' , '\\\\' , self :: $fixturesPath . \DIRECTORY_SEPARATOR . 'includes' . \DIRECTORY_SEPARATOR ), '%path%' , $dumper -> dump ()), '->dump() dumps services' );
2016-11-25 13:27:11 +00:00
}
2010-05-06 12:25:53 +01:00
2016-11-25 13:27:11 +00:00
public function testAddService ()
{
2013-01-05 08:50:11 +00:00
$container = include self :: $fixturesPath . '/containers/container9.php' ;
$container -> compile ();
$dumper = new PhpDumper ( $container );
2018-07-26 12:13:39 +01:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services9_compiled.php' , str_replace ( str_replace ( '\\' , '\\\\' , self :: $fixturesPath . \DIRECTORY_SEPARATOR . 'includes' . \DIRECTORY_SEPARATOR ), '%path%' , $dumper -> dump ()), '->dump() dumps services' );
2013-01-05 08:50:11 +00:00
2016-11-25 13:27:11 +00:00
$container = new ContainerBuilder ();
2017-09-17 10:49:41 +01:00
$container -> register ( 'foo' , 'FooClass' ) -> addArgument ( new \stdClass ()) -> setPublic ( true );
2016-11-25 13:27:11 +00:00
$container -> compile ();
$dumper = new PhpDumper ( $container );
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$dumper -> dump ();
$this -> fail ( '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2011-12-05 18:05:21 +00:00
$this -> assertInstanceOf ( '\Symfony\Component\DependencyInjection\Exception\RuntimeException' , $e , '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources' );
$this -> assertEquals ( 'Unable to dump a service container if a parameter is an object or a resource.' , $e -> getMessage (), '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
}
2010-04-08 09:43:42 +01:00
2017-07-24 20:11:47 +01:00
public function testDumpAsFiles ()
{
$container = include self :: $fixturesPath . '/containers/container9.php' ;
2017-11-08 09:48:34 +00:00
$container -> getDefinition ( 'bar' ) -> addTag ( 'hot' );
2017-07-24 20:11:47 +01:00
$container -> compile ();
$dumper = new PhpDumper ( $container );
2017-11-08 09:48:34 +00:00
$dump = print_r ( $dumper -> dump ( array ( 'as_files' => true , 'file' => __DIR__ , 'hot_path_tag' => 'hot' )), true );
2018-07-26 12:19:56 +01:00
if ( '\\' === \DIRECTORY_SEPARATOR ) {
2017-07-24 20:11:47 +01:00
$dump = str_replace ( '\\\\Fixtures\\\\includes\\\\foo.php' , '/Fixtures/includes/foo.php' , $dump );
}
$this -> assertStringMatchesFormatFile ( self :: $fixturesPath . '/php/services9_as_files.txt' , $dump );
}
2014-09-27 17:06:30 +01:00
public function testServicesWithAnonymousFactories ()
{
$container = include self :: $fixturesPath . '/containers/container19.php' ;
2016-11-25 13:27:11 +00:00
$container -> compile ();
2014-09-27 17:06:30 +01:00
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services19.php' , $dumper -> dump (), '->dump() dumps services with anonymous factories' );
}
2016-03-14 19:21:02 +00:00
public function testAddServiceIdWithUnsupportedCharacters ()
2013-07-19 08:14:04 +01:00
{
2016-03-14 19:21:02 +00:00
$class = 'Symfony_DI_PhpDumper_Test_Unsupported_Characters' ;
2013-07-19 08:14:04 +01:00
$container = new ContainerBuilder ();
2017-09-17 10:49:41 +01:00
$container -> register ( 'bar$' , 'FooClass' ) -> setPublic ( true );
$container -> register ( 'bar$!' , 'FooClass' ) -> setPublic ( true );
2016-11-25 13:27:11 +00:00
$container -> compile ();
2013-07-19 08:14:04 +01:00
$dumper = new PhpDumper ( $container );
2016-03-14 19:21:02 +00:00
eval ( '?>' . $dumper -> dump ( array ( 'class' => $class )));
$this -> assertTrue ( method_exists ( $class , 'getBarService' ));
$this -> assertTrue ( method_exists ( $class , 'getBar2Service' ));
}
public function testConflictingServiceIds ()
{
$class = 'Symfony_DI_PhpDumper_Test_Conflicting_Service_Ids' ;
$container = new ContainerBuilder ();
2017-09-17 10:49:41 +01:00
$container -> register ( 'foo_bar' , 'FooClass' ) -> setPublic ( true );
$container -> register ( 'foobar' , 'FooClass' ) -> setPublic ( true );
2016-11-25 13:27:11 +00:00
$container -> compile ();
2016-03-14 19:21:02 +00:00
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => $class )));
$this -> assertTrue ( method_exists ( $class , 'getFooBarService' ));
$this -> assertTrue ( method_exists ( $class , 'getFoobar2Service' ));
}
public function testConflictingMethodsWithParent ()
{
$class = 'Symfony_DI_PhpDumper_Test_Conflicting_Method_With_Parent' ;
$container = new ContainerBuilder ();
2017-09-17 10:49:41 +01:00
$container -> register ( 'bar' , 'FooClass' ) -> setPublic ( true );
$container -> register ( 'foo_bar' , 'FooClass' ) -> setPublic ( true );
2016-11-25 13:27:11 +00:00
$container -> compile ();
2016-03-14 19:21:02 +00:00
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array (
'class' => $class ,
'base_class' => 'Symfony\Component\DependencyInjection\Tests\Fixtures\containers\CustomContainer' ,
)));
$this -> assertTrue ( method_exists ( $class , 'getBar2Service' ));
$this -> assertTrue ( method_exists ( $class , 'getFoobar2Service' ));
2013-07-19 08:14:04 +01:00
}
2015-12-02 16:08:27 +00:00
/**
* @ dataProvider provideInvalidFactories
2016-09-02 19:13:13 +01:00
* @ expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
2015-12-02 16:08:27 +00:00
* @ expectedExceptionMessage Cannot dump definition
*/
public function testInvalidFactories ( $factory )
{
$container = new ContainerBuilder ();
$def = new Definition ( 'stdClass' );
2017-09-17 10:49:41 +01:00
$def -> setPublic ( true );
2015-12-02 16:08:27 +00:00
$def -> setFactory ( $factory );
$container -> setDefinition ( 'bar' , $def );
2016-11-25 13:27:11 +00:00
$container -> compile ();
2015-12-02 16:08:27 +00:00
$dumper = new PhpDumper ( $container );
$dumper -> dump ();
}
public function provideInvalidFactories ()
{
return array (
array ( array ( '' , 'method' )),
array ( array ( 'class' , '' )),
array ( array ( '...' , 'method' )),
array ( array ( 'class' , '...' )),
);
}
2013-06-05 10:51:05 +01:00
public function testAliases ()
{
$container = include self :: $fixturesPath . '/containers/container9.php' ;
2017-01-14 09:13:01 +00:00
$container -> setParameter ( 'foo_bar' , 'foo_bar' );
2013-06-05 10:51:05 +01:00
$container -> compile ();
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Aliases' )));
$container = new \Symfony_DI_PhpDumper_Test_Aliases ();
2017-01-14 09:13:01 +00:00
$foo = $container -> get ( 'foo' );
2013-06-05 10:51:05 +01:00
$this -> assertSame ( $foo , $container -> get ( 'alias_for_foo' ));
$this -> assertSame ( $foo , $container -> get ( 'alias_for_alias' ));
}
2013-07-07 13:58:42 +01:00
public function testFrozenContainerWithoutAliases ()
{
$container = new ContainerBuilder ();
$container -> compile ();
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Frozen_No_Aliases' )));
$container = new \Symfony_DI_PhpDumper_Test_Frozen_No_Aliases ();
$this -> assertFalse ( $container -> has ( 'foo' ));
}
2017-01-14 09:13:01 +00:00
/**
* @ group legacy
2017-10-07 22:38:34 +01:00
* @ expectedDeprecation The " decorator_service " service is already initialized , replacing it is deprecated since Symfony 3.3 and will fail in 4.0 .
2017-01-14 09:13:01 +00:00
*/
2010-05-06 12:25:53 +01:00
public function testOverrideServiceWhenUsingADumpedContainer ()
{
2017-10-07 22:38:34 +01:00
require_once self :: $fixturesPath . '/php/services9_compiled.php' ;
2010-04-08 09:43:42 +01:00
2010-05-06 12:25:53 +01:00
$container = new \ProjectServiceContainer ();
2017-10-07 22:38:34 +01:00
$container -> get ( 'decorator_service' );
$container -> set ( 'decorator_service' , $decorator = new \stdClass ());
2010-11-12 08:49:59 +00:00
2017-10-07 22:38:34 +01:00
$this -> assertSame ( $decorator , $container -> get ( 'decorator_service' ), '->set() overrides an already defined service' );
2010-11-12 08:49:59 +00:00
}
2013-09-12 07:52:33 +01:00
2015-11-03 23:41:24 +00:00
public function testDumpAutowireData ()
{
$container = include self :: $fixturesPath . '/containers/container24.php' ;
2016-11-25 13:27:11 +00:00
$container -> compile ();
2015-11-03 23:41:24 +00:00
$dumper = new PhpDumper ( $container );
2017-01-10 07:04:52 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services24.php' , $dumper -> dump ());
2015-11-03 23:41:24 +00:00
}
2016-06-06 12:11:27 +01:00
2017-11-24 11:48:20 +00:00
public function testEnvInId ()
{
$container = include self :: $fixturesPath . '/containers/container_env_in_id.php' ;
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_env_in_id.php' , $dumper -> dump ());
}
2016-08-12 19:34:27 +01:00
public function testEnvParameter ()
{
2016-10-22 17:25:15 +01:00
$rand = mt_rand ();
putenv ( 'Baz=' . $rand );
2016-08-12 19:34:27 +01:00
$container = new ContainerBuilder ();
$loader = new YamlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/yaml' ));
$loader -> load ( 'services26.yml' );
2016-10-22 17:25:15 +01:00
$container -> setParameter ( 'env(json_file)' , self :: $fixturesPath . '/array.json' );
2016-08-12 19:34:27 +01:00
$container -> compile ();
$dumper = new PhpDumper ( $container );
2016-10-22 17:25:15 +01:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services26.php' , $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_EnvParameters' , 'file' => self :: $fixturesPath . '/php/services26.php' )));
require self :: $fixturesPath . '/php/services26.php' ;
$container = new \Symfony_DI_PhpDumper_Test_EnvParameters ();
$this -> assertSame ( $rand , $container -> getParameter ( 'baz' ));
$this -> assertSame ( array ( 123 , 'abc' ), $container -> getParameter ( 'json' ));
$this -> assertSame ( 'sqlite:///foo/bar/var/data.db' , $container -> getParameter ( 'db_dsn' ));
putenv ( 'Baz' );
}
public function testResolvedBase64EnvParameters ()
{
$container = new ContainerBuilder ();
$container -> setParameter ( 'env(foo)' , base64_encode ( 'world' ));
$container -> setParameter ( 'hello' , '%env(base64:foo)%' );
$container -> compile ( true );
$expected = array (
2018-09-21 13:47:38 +01:00
'env(foo)' => 'd29ybGQ=' ,
'hello' => 'world' ,
2016-10-22 17:25:15 +01:00
);
$this -> assertSame ( $expected , $container -> getParameterBag () -> all ());
}
public function testDumpedBase64EnvParameters ()
{
$container = new ContainerBuilder ();
$container -> setParameter ( 'env(foo)' , base64_encode ( 'world' ));
$container -> setParameter ( 'hello' , '%env(base64:foo)%' );
$container -> compile ();
$dumper = new PhpDumper ( $container );
$dumper -> dump ();
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_base64_env.php' , $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Base64Parameters' )));
require self :: $fixturesPath . '/php/services_base64_env.php' ;
$container = new \Symfony_DI_PhpDumper_Test_Base64Parameters ();
$this -> assertSame ( 'world' , $container -> getParameter ( 'hello' ));
}
public function testCustomEnvParameters ()
{
$container = new ContainerBuilder ();
$container -> setParameter ( 'env(foo)' , str_rot13 ( 'world' ));
$container -> setParameter ( 'hello' , '%env(rot13:foo)%' );
2017-09-17 10:49:41 +01:00
$container -> register ( Rot13EnvVarProcessor :: class ) -> addTag ( 'container.env_var_processor' ) -> setPublic ( true );
2016-10-22 17:25:15 +01:00
$container -> compile ();
$dumper = new PhpDumper ( $container );
$dumper -> dump ();
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_rot13_env.php' , $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Rot13Parameters' )));
require self :: $fixturesPath . '/php/services_rot13_env.php' ;
$container = new \Symfony_DI_PhpDumper_Test_Rot13Parameters ();
$this -> assertSame ( 'world' , $container -> getParameter ( 'hello' ));
}
public function testFileEnvProcessor ()
{
$container = new ContainerBuilder ();
$container -> setParameter ( 'env(foo)' , __FILE__ );
$container -> setParameter ( 'random' , '%env(file:foo)%' );
$container -> compile ( true );
$this -> assertStringEqualsFile ( __FILE__ , $container -> getParameter ( 'random' ));
2016-08-12 19:34:27 +01:00
}
/**
* @ expectedException \Symfony\Component\DependencyInjection\Exception\EnvParameterException
2017-05-30 18:42:15 +01:00
* @ expectedExceptionMessage Environment variables " FOO " are never used . Please , check your container ' s configuration .
2016-08-12 19:34:27 +01:00
*/
public function testUnusedEnvParameter ()
{
$container = new ContainerBuilder ();
$container -> getParameter ( 'env(FOO)' );
$container -> compile ();
$dumper = new PhpDumper ( $container );
$dumper -> dump ();
}
2016-10-22 17:25:15 +01:00
/**
* @ expectedException \Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException
* @ expectedExceptionMessage Circular reference detected for parameter " env(resolve:DUMMY_ENV_VAR) " ( " env(resolve:DUMMY_ENV_VAR) " > " env(resolve:DUMMY_ENV_VAR) " ) .
*/
public function testCircularDynamicEnv ()
{
$container = new ContainerBuilder ();
$container -> setParameter ( 'foo' , '%bar%' );
$container -> setParameter ( 'bar' , '%env(resolve:DUMMY_ENV_VAR)%' );
$container -> compile ();
$dumper = new PhpDumper ( $container );
$dump = $dumper -> dump ( array ( 'class' => $class = __FUNCTION__ ));
eval ( '?>' . $dump );
$container = new $class ();
putenv ( 'DUMMY_ENV_VAR=%foo%' );
try {
$container -> getParameter ( 'bar' );
} finally {
putenv ( 'DUMMY_ENV_VAR' );
}
}
2016-05-26 15:08:43 +01:00
public function testInlinedDefinitionReferencingServiceContainer ()
{
$container = new ContainerBuilder ();
$container -> register ( 'foo' , 'stdClass' ) -> addMethodCall ( 'add' , array ( new Reference ( 'service_container' ))) -> setPublic ( false );
2017-09-17 10:49:41 +01:00
$container -> register ( 'bar' , 'stdClass' ) -> addArgument ( new Reference ( 'foo' )) -> setPublic ( true );
2016-05-26 15:08:43 +01:00
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services13.php' , $dumper -> dump (), '->dump() dumps inline definitions which reference service_container' );
}
2016-11-19 11:21:18 +00:00
2018-05-24 15:05:16 +01:00
public function testNonSharedLazyDefinitionReferences ()
{
$container = new ContainerBuilder ();
$container -> register ( 'foo' , 'stdClass' ) -> setShared ( false ) -> setLazy ( true );
$container -> register ( 'bar' , 'stdClass' ) -> addArgument ( new Reference ( 'foo' , ContainerBuilder :: EXCEPTION_ON_INVALID_REFERENCE , false ));
$container -> compile ();
$dumper = new PhpDumper ( $container );
$dumper -> setProxyDumper ( new \DummyProxyDumper ());
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_non_shared_lazy.php' , $dumper -> dump ());
}
2016-11-19 11:21:18 +00:00
public function testInitializePropertiesBeforeMethodCalls ()
{
require_once self :: $fixturesPath . '/includes/classes.php' ;
$container = new ContainerBuilder ();
2017-09-17 10:49:41 +01:00
$container -> register ( 'foo' , 'stdClass' ) -> setPublic ( true );
2016-11-19 11:21:18 +00:00
$container -> register ( 'bar' , 'MethodCallClass' )
2017-09-17 10:49:41 +01:00
-> setPublic ( true )
2016-11-19 11:21:18 +00:00
-> setProperty ( 'simple' , 'bar' )
-> setProperty ( 'complex' , new Reference ( 'foo' ))
-> addMethodCall ( 'callMe' );
$container -> compile ();
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Properties_Before_Method_Calls' )));
$container = new \Symfony_DI_PhpDumper_Test_Properties_Before_Method_Calls ();
$this -> assertTrue ( $container -> get ( 'bar' ) -> callPassed (), '->dump() initializes properties before method calls' );
}
2016-09-10 18:39:00 +01:00
public function testCircularReferenceAllowanceForLazyServices ()
{
$container = new ContainerBuilder ();
2017-09-17 10:49:41 +01:00
$container -> register ( 'foo' , 'stdClass' ) -> addArgument ( new Reference ( 'bar' )) -> setPublic ( true );
$container -> register ( 'bar' , 'stdClass' ) -> setLazy ( true ) -> addArgument ( new Reference ( 'foo' )) -> setPublic ( true );
2016-09-10 18:39:00 +01:00
$container -> compile ();
$dumper = new PhpDumper ( $container );
2018-07-24 10:19:17 +01:00
$dumper -> setProxyDumper ( new \DummyProxyDumper ());
2016-09-10 18:39:00 +01:00
$dumper -> dump ();
2017-03-18 09:10:35 +00:00
$this -> addToAssertionCount ( 1 );
2016-09-10 18:39:00 +01:00
$dumper = new PhpDumper ( $container );
2018-08-08 12:42:34 +01:00
$message = 'Circular reference detected for service "foo", path: "foo -> bar -> foo". Try running "composer require symfony/proxy-manager-bridge".' ;
2018-07-24 10:19:17 +01:00
if ( method_exists ( $this , 'expectException' )) {
$this -> expectException ( ServiceCircularReferenceException :: class );
$this -> expectExceptionMessage ( $message );
} else {
$this -> setExpectedException ( ServiceCircularReferenceException :: class , $message );
}
2017-03-18 09:10:35 +00:00
2018-07-24 10:19:17 +01:00
$dumper -> dump ();
2016-09-10 18:39:00 +01:00
}
2016-12-13 17:37:51 +00:00
2018-06-09 20:00:10 +01:00
public function testDedupLazyProxy ()
{
$container = new ContainerBuilder ();
$container -> register ( 'foo' , 'stdClass' ) -> setLazy ( true ) -> setPublic ( true );
$container -> register ( 'bar' , 'stdClass' ) -> setLazy ( true ) -> setPublic ( true );
$container -> compile ();
$dumper = new PhpDumper ( $container );
$dumper -> setProxyDumper ( new \DummyProxyDumper ());
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_dedup_lazy_proxy.php' , $dumper -> dump ());
}
2016-12-13 17:37:51 +00:00
public function testLazyArgumentProvideGenerator ()
{
require_once self :: $fixturesPath . '/includes/classes.php' ;
$container = new ContainerBuilder ();
2017-09-17 10:49:41 +01:00
$container -> register ( 'lazy_referenced' , 'stdClass' ) -> setPublic ( true );
2016-12-13 17:37:51 +00:00
$container
-> register ( 'lazy_context' , 'LazyContext' )
2017-09-17 10:49:41 +01:00
-> setPublic ( true )
2017-05-19 22:03:53 +01:00
-> setArguments ( array (
new IteratorArgument ( array ( 'k1' => new Reference ( 'lazy_referenced' ), 'k2' => new Reference ( 'service_container' ))),
new IteratorArgument ( array ()),
))
2016-12-13 17:37:51 +00:00
;
$container -> compile ();
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Lazy_Argument_Provide_Generator' )));
$container = new \Symfony_DI_PhpDumper_Test_Lazy_Argument_Provide_Generator ();
$lazyContext = $container -> get ( 'lazy_context' );
$this -> assertInstanceOf ( RewindableGenerator :: class , $lazyContext -> lazyValues );
2017-05-19 22:03:53 +01:00
$this -> assertInstanceOf ( RewindableGenerator :: class , $lazyContext -> lazyEmptyValues );
$this -> assertCount ( 2 , $lazyContext -> lazyValues );
$this -> assertCount ( 0 , $lazyContext -> lazyEmptyValues );
2016-12-13 17:37:51 +00:00
$i = - 1 ;
foreach ( $lazyContext -> lazyValues as $k => $v ) {
switch ( ++ $i ) {
case 0 :
$this -> assertEquals ( 'k1' , $k );
2017-04-21 10:31:17 +01:00
$this -> assertInstanceOf ( 'stdCLass' , $v );
2016-12-13 17:37:51 +00:00
break ;
2017-04-21 10:31:17 +01:00
case 1 :
2016-12-13 17:37:51 +00:00
$this -> assertEquals ( 'k2' , $k );
2017-04-21 10:31:17 +01:00
$this -> assertInstanceOf ( 'Symfony_DI_PhpDumper_Test_Lazy_Argument_Provide_Generator' , $v );
2016-12-13 17:37:51 +00:00
break ;
}
}
2017-05-19 22:03:53 +01:00
$this -> assertEmpty ( iterator_to_array ( $lazyContext -> lazyEmptyValues ));
2016-12-13 17:37:51 +00:00
}
2016-12-15 07:56:11 +00:00
2017-01-10 07:04:52 +00:00
public function testNormalizedId ()
{
$container = include self :: $fixturesPath . '/containers/container33.php' ;
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services33.php' , $dumper -> dump ());
}
2017-02-28 13:19:28 +00:00
2017-02-27 14:50:16 +00:00
public function testDumpContainerBuilderWithFrozenConstructorIncludingPrivateServices ()
{
$container = new ContainerBuilder ();
2017-09-17 10:49:41 +01:00
$container -> register ( 'foo_service' , 'stdClass' ) -> setArguments ( array ( new Reference ( 'baz_service' ))) -> setPublic ( true );
$container -> register ( 'bar_service' , 'stdClass' ) -> setArguments ( array ( new Reference ( 'baz_service' ))) -> setPublic ( true );
2017-02-27 14:50:16 +00:00
$container -> register ( 'baz_service' , 'stdClass' ) -> setPublic ( false );
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_private_frozen.php' , $dumper -> dump ());
}
2017-02-26 17:31:03 +00:00
public function testServiceLocator ()
{
$container = new ContainerBuilder ();
$container -> register ( 'foo_service' , ServiceLocator :: class )
2017-09-17 10:49:41 +01:00
-> setPublic ( true )
2017-02-26 17:31:03 +00:00
-> addArgument ( array (
'bar' => new ServiceClosureArgument ( new Reference ( 'bar_service' )),
'baz' => new ServiceClosureArgument ( new TypedReference ( 'baz_service' , 'stdClass' )),
2017-03-09 08:35:59 +00:00
'nil' => $nil = new ServiceClosureArgument ( new Reference ( 'nil' )),
2017-02-26 17:31:03 +00:00
))
;
2017-04-29 18:21:19 +01:00
// no method calls
2017-09-17 10:49:41 +01:00
$container -> register ( 'translator.loader_1' , 'stdClass' ) -> setPublic ( true );
2017-04-29 18:21:19 +01:00
$container -> register ( 'translator.loader_1_locator' , ServiceLocator :: class )
-> setPublic ( false )
-> addArgument ( array (
'translator.loader_1' => new ServiceClosureArgument ( new Reference ( 'translator.loader_1' )),
));
$container -> register ( 'translator_1' , StubbedTranslator :: class )
2017-09-17 10:49:41 +01:00
-> setPublic ( true )
2017-04-29 18:21:19 +01:00
-> addArgument ( new Reference ( 'translator.loader_1_locator' ));
// one method calls
2017-09-17 10:49:41 +01:00
$container -> register ( 'translator.loader_2' , 'stdClass' ) -> setPublic ( true );
2017-04-29 18:21:19 +01:00
$container -> register ( 'translator.loader_2_locator' , ServiceLocator :: class )
-> setPublic ( false )
-> addArgument ( array (
'translator.loader_2' => new ServiceClosureArgument ( new Reference ( 'translator.loader_2' )),
));
$container -> register ( 'translator_2' , StubbedTranslator :: class )
2017-09-17 10:49:41 +01:00
-> setPublic ( true )
2017-04-29 18:21:19 +01:00
-> addArgument ( new Reference ( 'translator.loader_2_locator' ))
-> addMethodCall ( 'addResource' , array ( 'db' , new Reference ( 'translator.loader_2' ), 'nl' ));
// two method calls
2017-09-17 10:49:41 +01:00
$container -> register ( 'translator.loader_3' , 'stdClass' ) -> setPublic ( true );
2017-04-29 18:21:19 +01:00
$container -> register ( 'translator.loader_3_locator' , ServiceLocator :: class )
-> setPublic ( false )
-> addArgument ( array (
'translator.loader_3' => new ServiceClosureArgument ( new Reference ( 'translator.loader_3' )),
));
$container -> register ( 'translator_3' , StubbedTranslator :: class )
2017-09-17 10:49:41 +01:00
-> setPublic ( true )
2017-04-29 18:21:19 +01:00
-> addArgument ( new Reference ( 'translator.loader_3_locator' ))
-> addMethodCall ( 'addResource' , array ( 'db' , new Reference ( 'translator.loader_3' ), 'nl' ))
-> addMethodCall ( 'addResource' , array ( 'db' , new Reference ( 'translator.loader_3' ), 'en' ));
2017-03-09 08:35:59 +00:00
$nil -> setValues ( array ( null ));
2017-09-17 10:49:41 +01:00
$container -> register ( 'bar_service' , 'stdClass' ) -> setArguments ( array ( new Reference ( 'baz_service' ))) -> setPublic ( true );
2017-02-26 17:31:03 +00:00
$container -> register ( 'baz_service' , 'stdClass' ) -> setPublic ( false );
$container -> compile ();
$dumper = new PhpDumper ( $container );
2017-03-06 08:56:20 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_locator.php' , $dumper -> dump ());
2017-02-26 17:31:03 +00:00
}
2017-02-21 22:24:03 +00:00
public function testServiceSubscriber ()
{
$container = new ContainerBuilder ();
2017-04-06 09:48:39 +01:00
$container -> register ( 'foo_service' , TestServiceSubscriber :: class )
2017-09-17 10:49:41 +01:00
-> setPublic ( true )
2017-02-21 22:24:03 +00:00
-> setAutowired ( true )
2017-04-17 18:50:34 +01:00
-> addArgument ( new Reference ( ContainerInterface :: class ))
2017-02-21 22:24:03 +00:00
-> addTag ( 'container.service_subscriber' , array (
2017-03-27 19:07:18 +01:00
'key' => 'bar' ,
2017-04-06 09:48:39 +01:00
'id' => TestServiceSubscriber :: class ,
2017-02-21 22:24:03 +00:00
))
;
2017-09-17 10:49:41 +01:00
$container -> register ( TestServiceSubscriber :: class , TestServiceSubscriber :: class ) -> setPublic ( true );
2017-07-29 14:29:10 +01:00
$container -> register ( CustomDefinition :: class , CustomDefinition :: class )
-> setPublic ( false );
2017-02-21 22:24:03 +00:00
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_subscriber.php' , $dumper -> dump ());
}
2017-05-26 00:10:31 +01:00
2017-05-25 11:05:01 +01:00
public function testPrivateWithIgnoreOnInvalidReference ()
{
require_once self :: $fixturesPath . '/includes/classes.php' ;
$container = new ContainerBuilder ();
$container -> register ( 'not_invalid' , 'BazClass' )
-> setPublic ( false );
$container -> register ( 'bar' , 'BarClass' )
2017-09-17 10:49:41 +01:00
-> setPublic ( true )
2017-05-26 00:10:31 +01:00
-> addMethodCall ( 'setBaz' , array ( new Reference ( 'not_invalid' , SymfonyContainerInterface :: IGNORE_ON_INVALID_REFERENCE )));
$container -> compile ();
2017-05-25 11:05:01 +01:00
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Private_With_Ignore_On_Invalid_Reference' )));
$container = new \Symfony_DI_PhpDumper_Test_Private_With_Ignore_On_Invalid_Reference ();
$this -> assertInstanceOf ( 'BazClass' , $container -> get ( 'bar' ) -> getBaz ());
}
2017-06-12 12:31:43 +01:00
public function testArrayParameters ()
{
$container = new ContainerBuilder ();
$container -> setParameter ( 'array_1' , array ( 123 ));
$container -> setParameter ( 'array_2' , array ( __DIR__ ));
$container -> register ( 'bar' , 'BarClass' )
2017-09-17 10:49:41 +01:00
-> setPublic ( true )
2017-08-17 10:23:46 +01:00
-> addMethodCall ( 'setBaz' , array ( '%array_1%' , '%array_2%' , '%%array_1%%' , array ( 123 )));
2017-06-12 12:31:43 +01:00
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_array_params.php' , str_replace ( '\\\\Dumper' , '/Dumper' , $dumper -> dump ( array ( 'file' => self :: $fixturesPath . '/php/services_array_params.php' ))));
}
2017-07-12 14:09:21 +01:00
2017-07-24 21:27:32 +01:00
public function testExpressionReferencingPrivateService ()
{
$container = new ContainerBuilder ();
$container -> register ( 'private_bar' , 'stdClass' )
-> setPublic ( false );
$container -> register ( 'private_foo' , 'stdClass' )
-> setPublic ( false );
$container -> register ( 'public_foo' , 'stdClass' )
2017-09-17 10:49:41 +01:00
-> setPublic ( true )
2017-07-24 21:27:32 +01:00
-> addArgument ( new Expression ( 'service("private_foo")' ));
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_private_in_expression.php' , $dumper -> dump ());
}
2017-08-30 08:27:55 +01:00
public function testUninitializedReference ()
{
$container = include self :: $fixturesPath . '/containers/container_uninitialized_ref.php' ;
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_uninitialized_ref.php' , $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Uninitialized_Reference' )));
require self :: $fixturesPath . '/php/services_uninitialized_ref.php' ;
$container = new \Symfony_DI_PhpDumper_Test_Uninitialized_Reference ();
$bar = $container -> get ( 'bar' );
$this -> assertNull ( $bar -> foo1 );
$this -> assertNull ( $bar -> foo2 );
$this -> assertNull ( $bar -> foo3 );
$this -> assertNull ( $bar -> closures [ 0 ]());
$this -> assertNull ( $bar -> closures [ 1 ]());
$this -> assertNull ( $bar -> closures [ 2 ]());
$this -> assertSame ( array (), iterator_to_array ( $bar -> iter ));
$container = new \Symfony_DI_PhpDumper_Test_Uninitialized_Reference ();
$container -> get ( 'foo1' );
$container -> get ( 'baz' );
$bar = $container -> get ( 'bar' );
$this -> assertEquals ( new \stdClass (), $bar -> foo1 );
$this -> assertNull ( $bar -> foo2 );
$this -> assertEquals ( new \stdClass (), $bar -> foo3 );
$this -> assertEquals ( new \stdClass (), $bar -> closures [ 0 ]());
$this -> assertNull ( $bar -> closures [ 1 ]());
$this -> assertEquals ( new \stdClass (), $bar -> closures [ 2 ]());
$this -> assertEquals ( array ( 'foo1' => new \stdClass (), 'foo3' => new \stdClass ()), iterator_to_array ( $bar -> iter ));
}
2017-11-27 20:00:44 +00:00
/**
* @ dataProvider provideAlmostCircular
*/
public function testAlmostCircular ( $visibility )
2017-11-04 20:24:26 +00:00
{
$container = include self :: $fixturesPath . '/containers/container_almost_circular.php' ;
$container -> compile ();
$dumper = new PhpDumper ( $container );
2017-11-27 20:00:44 +00:00
$container = 'Symfony_DI_PhpDumper_Test_Almost_Circular_' . ucfirst ( $visibility );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_almost_circular_' . $visibility . '.php' , $dumper -> dump ( array ( 'class' => $container )));
2017-11-04 20:24:26 +00:00
2017-11-27 20:00:44 +00:00
require self :: $fixturesPath . '/php/services_almost_circular_' . $visibility . '.php' ;
2017-11-04 20:24:26 +00:00
2017-11-27 20:00:44 +00:00
$container = new $container ();
2017-11-05 17:53:02 +00:00
2017-11-27 20:00:44 +00:00
$foo = $container -> get ( 'foo' );
2017-11-05 17:53:02 +00:00
$this -> assertSame ( $foo , $foo -> bar -> foobar -> foo );
2017-11-27 20:00:44 +00:00
$foo2 = $container -> get ( 'foo2' );
$this -> assertSame ( $foo2 , $foo2 -> bar -> foobar -> foo );
2017-11-05 17:53:02 +00:00
2017-11-27 20:00:44 +00:00
$this -> assertSame ( array (), ( array ) $container -> get ( 'foobar4' ));
2017-12-01 13:14:10 +00:00
$foo5 = $container -> get ( 'foo5' );
$this -> assertSame ( $foo5 , $foo5 -> bar -> foo );
2018-07-24 10:19:17 +01:00
$manager = $container -> get ( 'manager' );
$this -> assertEquals ( new \stdClass (), $manager );
$manager = $container -> get ( 'manager2' );
$this -> assertEquals ( new \stdClass (), $manager );
2017-11-27 20:00:44 +00:00
}
2017-11-04 20:24:26 +00:00
2017-11-27 20:00:44 +00:00
public function provideAlmostCircular ()
{
yield array ( 'public' );
yield array ( 'private' );
2018-09-04 08:23:21 +01:00
}
public function testDeepServiceGraph ()
{
$container = new ContainerBuilder ();
$loader = new YamlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/yaml' ));
$loader -> load ( 'services_deep_graph.yml' );
$container -> compile ();
$dumper = new PhpDumper ( $container );
$dumper -> dump ();
2018-09-06 22:58:48 +01:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_deep_graph.php' , $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Deep_Graph' )));
require self :: $fixturesPath . '/php/services_deep_graph.php' ;
$container = new \Symfony_DI_PhpDumper_Test_Deep_Graph ();
$this -> assertInstanceOf ( FooForDeepGraph :: class , $container -> get ( 'foo' ));
$this -> assertEquals (( object ) array ( 'p2' => ( object ) array ( 'p3' => ( object ) array ())), $container -> get ( 'foo' ) -> bClone );
2017-11-04 20:24:26 +00:00
}
2017-11-08 09:48:34 +00:00
public function testHotPathOptimizations ()
{
$container = include self :: $fixturesPath . '/containers/container_inline_requires.php' ;
$container -> setParameter ( 'inline_requires' , true );
$container -> compile ();
$dumper = new PhpDumper ( $container );
2017-11-27 20:00:44 +00:00
$dump = $dumper -> dump ( array ( 'hot_path_tag' => 'container.hot_path' , 'inline_class_loader_parameter' => 'inline_requires' , 'file' => self :: $fixturesPath . '/php/services_inline_requires.php' ));
2018-07-26 12:19:56 +01:00
if ( '\\' === \DIRECTORY_SEPARATOR ) {
2017-11-08 09:48:34 +00:00
$dump = str_replace ( " ' \\ \\ includes \\ \\ HotPath \\ \\ " , " '/includes/HotPath/ " , $dump );
}
2017-11-27 20:00:44 +00:00
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_inline_requires.php' , $dump );
2017-11-08 09:48:34 +00:00
}
2017-07-10 19:09:41 +01:00
public function testDumpHandlesLiteralClassWithRootNamespace ()
{
$container = new ContainerBuilder ();
2017-09-17 10:49:41 +01:00
$container -> register ( 'foo' , '\\stdClass' ) -> setPublic ( true );
2017-07-12 14:09:21 +01:00
$container -> compile ();
2017-07-10 19:09:41 +01:00
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Literal_Class_With_Root_Namespace' )));
$container = new \Symfony_DI_PhpDumper_Test_Literal_Class_With_Root_Namespace ();
$this -> assertInstanceOf ( 'stdClass' , $container -> get ( 'foo' ));
}
2017-07-17 10:57:18 +01:00
2017-12-06 12:34:05 +00:00
public function testDumpHandlesObjectClassNames ()
{
$container = new ContainerBuilder ( new ParameterBag ( array (
'class' => 'stdClass' ,
)));
$container -> setDefinition ( 'foo' , new Definition ( new Parameter ( 'class' )));
$container -> setDefinition ( 'bar' , new Definition ( 'stdClass' , array (
new Reference ( 'foo' ),
))) -> setPublic ( true );
$container -> setParameter ( 'inline_requires' , true );
$container -> compile ();
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array (
'class' => 'Symfony_DI_PhpDumper_Test_Object_Class_Name' ,
'inline_class_loader_parameter' => 'inline_requires' ,
)));
$container = new \Symfony_DI_PhpDumper_Test_Object_Class_Name ();
$this -> assertInstanceOf ( 'stdClass' , $container -> get ( 'bar' ));
}
2018-06-30 08:08:55 +01:00
public function testUninitializedSyntheticReference ()
{
$container = new ContainerBuilder ();
$container -> register ( 'foo' , 'stdClass' ) -> setPublic ( true ) -> setSynthetic ( true );
$container -> register ( 'bar' , 'stdClass' ) -> setPublic ( true ) -> setShared ( false )
-> setProperty ( 'foo' , new Reference ( 'foo' , ContainerBuilder :: IGNORE_ON_UNINITIALIZED_REFERENCE ));
$container -> compile ();
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array (
'class' => 'Symfony_DI_PhpDumper_Test_UninitializedSyntheticReference' ,
'inline_class_loader_parameter' => 'inline_requires' ,
)));
$container = new \Symfony_DI_PhpDumper_Test_UninitializedSyntheticReference ();
$this -> assertEquals (( object ) array ( 'foo' => null ), $container -> get ( 'bar' ));
$container -> set ( 'foo' , ( object ) array ( 123 ));
$this -> assertEquals (( object ) array ( 'foo' => ( object ) array ( 123 )), $container -> get ( 'bar' ));
}
2017-09-05 12:45:10 +01:00
/**
* @ group legacy
2017-10-07 22:38:34 +01:00
* @ expectedDeprecation The " private " service is private , getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0 . You should either make the service public , or stop using the container directly and use dependency injection instead .
* @ expectedDeprecation The " private_alias " service is private , getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0 . You should either make the service public , or stop using the container directly and use dependency injection instead .
* @ expectedDeprecation The " decorated_private " service is private , getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0 . You should either make the service public , or stop using the container directly and use dependency injection instead .
* @ expectedDeprecation The " decorated_private_alias " service is private , getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0 . You should either make the service public , or stop using the container directly and use dependency injection instead .
* @ expectedDeprecation The " private_not_inlined " service is private , getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0 . You should either make the service public , or stop using the container directly and use dependency injection instead .
* @ expectedDeprecation The " private_not_removed " service is private , getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0 . You should either make the service public , or stop using the container directly and use dependency injection instead .
* @ expectedDeprecation The " private_child " service is private , getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0 . You should either make the service public , or stop using the container directly and use dependency injection instead .
* @ expectedDeprecation The " private_parent " service is private , getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0 . You should either make the service public , or stop using the container directly and use dependency injection instead .
2017-09-05 12:45:10 +01:00
*/
public function testLegacyPrivateServices ()
{
$container = new ContainerBuilder ();
$loader = new YamlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/yaml' ));
$loader -> load ( 'services_legacy_privates.yml' );
2017-09-17 10:49:41 +01:00
$container -> setDefinition ( 'private_child' , new ChildDefinition ( 'foo' ));
$container -> setDefinition ( 'private_parent' , new ChildDefinition ( 'private' ));
2017-09-05 12:45:10 +01:00
$container -> getDefinition ( 'private' ) -> setPrivate ( true );
$container -> getDefinition ( 'private_not_inlined' ) -> setPrivate ( true );
$container -> getDefinition ( 'private_not_removed' ) -> setPrivate ( true );
$container -> getDefinition ( 'decorated_private' ) -> setPrivate ( true );
$container -> getDefinition ( 'private_child' ) -> setPrivate ( true );
$container -> getAlias ( 'decorated_private_alias' ) -> setPrivate ( true );
$container -> getAlias ( 'private_alias' ) -> setPrivate ( true );
$container -> compile ();
$dumper = new PhpDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/php/services_legacy_privates.php' , $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Legacy_Privates' , 'file' => self :: $fixturesPath . '/php/services_legacy_privates.php' )));
require self :: $fixturesPath . '/php/services_legacy_privates.php' ;
$container = new \Symfony_DI_PhpDumper_Test_Legacy_Privates ();
$container -> get ( 'private' );
$container -> get ( 'private_alias' );
$container -> get ( 'alias_to_private' );
$container -> get ( 'decorated_private' );
$container -> get ( 'decorated_private_alias' );
$container -> get ( 'private_not_inlined' );
$container -> get ( 'private_not_removed' );
$container -> get ( 'private_child' );
$container -> get ( 'private_parent' );
$container -> get ( 'public_child' );
}
2017-07-17 10:57:18 +01:00
/**
* This test checks the trigger of a deprecation note and should not be removed in major releases .
*
* @ group legacy
* @ expectedDeprecation The " foo " service is deprecated . You should stop using it , as it will soon be removed .
*/
public function testPrivateServiceTriggersDeprecation ()
{
$container = new ContainerBuilder ();
$container -> register ( 'foo' , 'stdClass' )
-> setPublic ( false )
-> setDeprecated ( true );
$container -> register ( 'bar' , 'stdClass' )
-> setPublic ( true )
-> setProperty ( 'foo' , new Reference ( 'foo' ));
$container -> compile ();
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Private_Service_Triggers_Deprecation' )));
$container = new \Symfony_DI_PhpDumper_Test_Private_Service_Triggers_Deprecation ();
$container -> get ( 'bar' );
}
2017-08-12 10:16:19 +01:00
/**
* @ group legacy
2017-12-31 06:25:36 +00:00
* @ expectedDeprecation Parameter names will be made case sensitive in Symfony 4.0 . Using " foo " instead of " Foo " is deprecated since Symfony 3.4 .
* @ expectedDeprecation Parameter names will be made case sensitive in Symfony 4.0 . Using " FOO " instead of " Foo " is deprecated since Symfony 3.4 .
* @ expectedDeprecation Parameter names will be made case sensitive in Symfony 4.0 . Using " bar " instead of " BAR " is deprecated since Symfony 3.4 .
2017-08-12 10:16:19 +01:00
*/
public function testParameterWithMixedCase ()
{
$container = new ContainerBuilder ( new ParameterBag ( array ( 'Foo' => 'bar' , 'BAR' => 'foo' )));
$container -> compile ();
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Parameter_With_Mixed_Case' )));
$container = new \Symfony_DI_PhpDumper_Test_Parameter_With_Mixed_Case ();
$this -> assertSame ( 'bar' , $container -> getParameter ( 'foo' ));
$this -> assertSame ( 'bar' , $container -> getParameter ( 'FOO' ));
$this -> assertSame ( 'foo' , $container -> getParameter ( 'bar' ));
$this -> assertSame ( 'foo' , $container -> getParameter ( 'BAR' ));
}
/**
* @ group legacy
2017-12-31 06:25:36 +00:00
* @ expectedDeprecation Parameter names will be made case sensitive in Symfony 4.0 . Using " FOO " instead of " foo " is deprecated since Symfony 3.4 .
2017-08-12 10:16:19 +01:00
*/
public function testParameterWithLowerCase ()
{
$container = new ContainerBuilder ( new ParameterBag ( array ( 'foo' => 'bar' )));
$container -> compile ();
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Parameter_With_Lower_Case' )));
$container = new \Symfony_DI_PhpDumper_Test_Parameter_With_Lower_Case ();
$this -> assertSame ( 'bar' , $container -> getParameter ( 'FOO' ));
}
2018-03-01 13:42:47 +00:00
/**
* @ group legacy
* @ expectedDeprecation Service identifiers will be made case sensitive in Symfony 4.0 . Using " foo " instead of " Foo " is deprecated since Symfony 3.3 .
* @ expectedDeprecation The " Foo " service is deprecated . You should stop using it , as it will soon be removed .
*/
public function testReferenceWithLowerCaseId ()
{
$container = new ContainerBuilder ();
$container -> register ( 'Bar' , 'stdClass' ) -> setProperty ( 'foo' , new Reference ( 'foo' )) -> setPublic ( true );
$container -> register ( 'Foo' , 'stdClass' ) -> setDeprecated ();
$container -> compile ();
$dumper = new PhpDumper ( $container );
eval ( '?>' . $dumper -> dump ( array ( 'class' => 'Symfony_DI_PhpDumper_Test_Reference_With_Lower_Case_Id' )));
$container = new \Symfony_DI_PhpDumper_Test_Reference_With_Lower_Case_Id ();
$this -> assertEquals (( object ) array ( 'foo' => ( object ) array ()), $container -> get ( 'Bar' ));
}
2010-03-18 07:48:03 +00:00
}
2016-10-22 17:25:15 +01:00
class Rot13EnvVarProcessor implements EnvVarProcessorInterface
{
public function getEnv ( $prefix , $name , \Closure $getEnv )
{
return str_rot13 ( $getEnv ( $name ));
}
public static function getProvidedTypes ()
{
return array ( 'rot13' => 'string' );
}
}
2018-09-06 22:58:48 +01:00
class FooForDeepGraph
{
public $bClone ;
public function __construct ( \stdClass $a , \stdClass $b )
{
// clone to verify that $b has been fully initialized before
$this -> bClone = clone $b ;
}
}