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-08-10 12:45:16 +01:00
use Symfony\Component\Config\FileLocator ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\DependencyInjection\ContainerBuilder ;
2017-08-30 08:27:55 +01:00
use Symfony\Component\DependencyInjection\ContainerInterface ;
2017-08-10 12:54:24 +01:00
use Symfony\Component\DependencyInjection\Definition ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\DependencyInjection\Dumper\YamlDumper ;
2017-08-10 12:45:16 +01:00
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader ;
2017-08-30 08:27:55 +01:00
use Symfony\Component\DependencyInjection\Reference ;
2017-02-06 17:23:56 +00:00
use Symfony\Component\Yaml\Parser ;
2016-02-15 17:40:28 +00:00
use Symfony\Component\Yaml\Yaml ;
2010-03-18 07:48:03 +00:00
2017-02-08 07:24:27 +00:00
class YamlDumperTest extends TestCase
2010-03-18 07:48:03 +00:00
{
2012-07-09 13:50:58 +01:00
protected static $fixturesPath ;
2010-05-06 12:25:53 +01: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
}
public function testDump ()
{
2010-07-15 14:11:33 +01:00
$dumper = new YamlDumper ( $container = new ContainerBuilder ());
2010-05-06 12:25:53 +01:00
2016-02-16 08:56:31 +00:00
$this -> assertEqualYamlStructure ( file_get_contents ( self :: $fixturesPath . '/yaml/services1.yml' ), $dumper -> dump (), '->dump() dumps an empty container as an empty YAML file' );
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' ;
$dumper = new YamlDumper ( $container );
2016-02-16 08:56:31 +00:00
$this -> assertEqualYamlStructure ( file_get_contents ( self :: $fixturesPath . '/yaml/services8.yml' ), $dumper -> dump (), '->dump() dumps parameters' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testAddService ()
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 YamlDumper ( $container );
2018-07-26 12:13:39 +01:00
$this -> assertEqualYamlStructure ( str_replace ( '%path%' , self :: $fixturesPath . \DIRECTORY_SEPARATOR . 'includes' . \DIRECTORY_SEPARATOR , file_get_contents ( self :: $fixturesPath . '/yaml/services9.yml' )), $dumper -> dump (), '->dump() dumps services' );
2010-05-06 12:25:53 +01:00
2010-07-15 14:11:33 +01:00
$dumper = new YamlDumper ( $container = new ContainerBuilder ());
2017-09-17 10:49:41 +01:00
$container -> register ( 'foo' , 'FooClass' ) -> addArgument ( new \stdClass ()) -> setPublic ( true );
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 ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\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-03-18 07:48:03 +00:00
}
2015-11-03 23:41:24 +00:00
public function testDumpAutowireData ()
{
$container = include self :: $fixturesPath . '/containers/container24.php' ;
$dumper = new YamlDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/yaml/services24.yml' , $dumper -> dump ());
}
2016-02-16 06:09:38 +00:00
2017-08-10 12:45:16 +01:00
public function testDumpLoad ()
{
$container = new ContainerBuilder ();
$loader = new YamlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/yaml' ));
$loader -> load ( 'services_dump_load.yml' );
2017-08-30 08:27:55 +01:00
$this -> assertEquals ( array ( new Reference ( 'bar' , ContainerInterface :: IGNORE_ON_UNINITIALIZED_REFERENCE )), $container -> getDefinition ( 'foo' ) -> getArguments ());
2017-08-10 12:45:16 +01:00
$dumper = new YamlDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/yaml/services_dump_load.yml' , $dumper -> dump ());
}
2017-08-10 12:54:24 +01:00
public function testInlineServices ()
{
$container = new ContainerBuilder ();
$container -> register ( 'foo' , 'Class1' )
2017-09-17 10:49:41 +01:00
-> setPublic ( true )
2017-08-10 12:54:24 +01:00
-> addArgument (( new Definition ( 'Class2' ))
-> addArgument ( new Definition ( 'Class2' ))
)
;
$dumper = new YamlDumper ( $container );
$this -> assertStringEqualsFile ( self :: $fixturesPath . '/yaml/services_inline.yml' , $dumper -> dump ());
}
2017-02-06 17:23:56 +00:00
private function assertEqualYamlStructure ( $expected , $yaml , $message = '' )
2016-02-15 17:40:28 +00:00
{
2017-02-06 17:23:56 +00:00
$parser = new Parser ();
$this -> assertEquals ( $parser -> parse ( $expected , Yaml :: PARSE_CUSTOM_TAGS ), $parser -> parse ( $yaml , Yaml :: PARSE_CUSTOM_TAGS ), $message );
2016-02-15 17:40:28 +00:00
}
2010-03-18 07:48:03 +00:00
}