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 .
*/
2010-08-20 22:09:55 +01:00
namespace Symfony\Tests\Component\DependencyInjection\Loader ;
2010-03-18 07:48:03 +00:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\DependencyInjection\ContainerBuilder ;
use Symfony\Component\DependencyInjection\Reference ;
use Symfony\Component\DependencyInjection\Definition ;
2011-02-27 18:34:02 +00:00
use Symfony\Component\Config\Loader\Loader ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader ;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader ;
use Symfony\Component\DependencyInjection\Loader\IniFileLoader ;
2011-02-10 15:15:51 +00:00
use Symfony\Component\Config\Loader\LoaderResolver ;
use Symfony\Component\Config\FileLocator ;
2010-03-18 07:48:03 +00:00
class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
{
2010-05-06 12:25:53 +01:00
static protected $fixturesPath ;
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
static public function setUpBeforeClass ()
2010-03-18 07:48:03 +00:00
{
2010-06-28 08:31:54 +01:00
self :: $fixturesPath = realpath ( __DIR__ . '/../Fixtures/' );
2010-10-02 15:47:35 +01:00
require_once self :: $fixturesPath . '/includes/foo.php' ;
2010-05-06 12:25:53 +01:00
require_once self :: $fixturesPath . '/includes/ProjectExtension.php' ;
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testLoadFile ()
2010-03-18 07:48:03 +00:00
{
2011-03-11 13:50:46 +00:00
$loader = new YamlFileLoader ( new ContainerBuilder (), new FileLocator ( self :: $fixturesPath . '/ini' ));
$r = new \ReflectionObject ( $loader );
$m = $r -> getMethod ( 'loadFile' );
$m -> setAccessible ( true );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2011-03-11 13:50:46 +00:00
$m -> invoke ( $loader , 'foo.yml' );
2010-05-06 12:25:53 +01:00
$this -> fail ( '->load() throws an InvalidArgumentException if the loaded file does not exist' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->load() throws an InvalidArgumentException if the loaded file does not exist' );
$this -> assertEquals ( 'The service file "foo.yml" is not valid.' , $e -> getMessage (), '->load() throws an InvalidArgumentException if the loaded file does not exist' );
}
2010-05-07 15:09:11 +01:00
try {
2011-03-11 13:50:46 +00:00
$m -> invoke ( $loader , 'parameters.ini' );
2010-05-06 12:25:53 +01:00
$this -> fail ( '->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file' );
$this -> assertEquals ( 'The service file "parameters.ini" is not valid.' , $e -> getMessage (), '->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file' );
}
2011-03-11 13:50:46 +00:00
$loader = new YamlFileLoader ( new ContainerBuilder (), new FileLocator ( self :: $fixturesPath . '/yaml' ));
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
foreach ( array ( 'nonvalid1' , 'nonvalid2' ) as $fixture ) {
2010-05-08 14:32:30 +01:00
try {
2011-03-11 13:50:46 +00:00
$m -> invoke ( $loader , $fixture . '.yml' );
2010-05-06 12:25:53 +01:00
$this -> fail ( '->load() throws an InvalidArgumentException if the loaded file does not validate' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->load() throws an InvalidArgumentException if the loaded file does not validate' );
$this -> assertStringMatchesFormat ( 'The service file "nonvalid%d.yml" is not valid.' , $e -> getMessage (), '->load() throws an InvalidArgumentException if the loaded file does not validate' );
}
}
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testLoadParameters ()
2010-03-18 07:48:03 +00:00
{
2010-07-16 08:12:58 +01:00
$container = new ContainerBuilder ();
2011-03-11 13:50:46 +00:00
$loader = new YamlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/yaml' ));
2010-07-16 08:12:58 +01:00
$loader -> load ( 'services2.yml' );
2011-03-16 13:56:26 +00:00
$this -> assertEquals ( array ( 'foo' => 'bar' , 'mixedcase' => array ( 'MixedCaseKey' => 'value' ), 'values' => array ( true , false , 0 , 1000.3 ), 'bar' => 'foo' , 'foo_bar' => new Reference ( 'foo_bar' )), $container -> getParameterBag () -> all (), '->load() converts YAML keys to lowercase' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testLoadImports ()
2010-03-18 07:48:03 +00:00
{
2010-07-16 08:12:58 +01:00
$container = new ContainerBuilder ();
2010-07-18 10:26:47 +01:00
$resolver = new LoaderResolver ( array (
2011-02-09 23:44:02 +00:00
new IniFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/yaml' )),
new XmlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/yaml' )),
2011-03-11 13:50:46 +00:00
$loader = new YamlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/yaml' )),
2010-07-18 10:26:47 +01:00
));
$loader -> setResolver ( $resolver );
2010-07-16 08:12:58 +01:00
$loader -> load ( 'services4.yml' );
2010-05-06 12:25:53 +01:00
2010-07-16 08:12:58 +01:00
$actual = $container -> getParameterBag () -> all ();
2011-03-16 13:56:26 +00:00
$expected = array ( 'foo' => 'bar' , 'values' => array ( true , false ), 'bar' => '%foo%' , 'foo_bar' => new Reference ( 'foo_bar' ), 'mixedcase' => array ( 'MixedCaseKey' => 'value' ), 'imported_from_ini' => true , 'imported_from_xml' => true );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( array_keys ( $expected ), array_keys ( $actual ), '->load() imports and merges imported files' );
2011-04-02 00:50:42 +01:00
// Bad import throws no exception due to ignore_errors value.
$loader -> load ( 'services4_bad_import.yml' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testLoadServices ()
2010-03-18 07:48:03 +00:00
{
2010-07-16 08:12:58 +01:00
$container = new ContainerBuilder ();
2011-03-11 13:50:46 +00:00
$loader = new YamlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/yaml' ));
2010-07-16 08:12:58 +01:00
$loader -> load ( 'services6.yml' );
$services = $container -> getDefinitions ();
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( isset ( $services [ 'foo' ]), '->load() parses service elements' );
2010-08-20 22:09:55 +01:00
$this -> assertEquals ( 'Symfony\\Component\\DependencyInjection\\Definition' , get_class ( $services [ 'foo' ]), '->load() converts service element to Definition instances' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 'FooClass' , $services [ 'foo' ] -> getClass (), '->load() parses the class attribute' );
2011-01-17 22:28:59 +00:00
$this -> assertEquals ( 'container' , $services [ 'scope.container' ] -> getScope ());
$this -> assertEquals ( 'custom' , $services [ 'scope.custom' ] -> getScope ());
$this -> assertEquals ( 'prototype' , $services [ 'scope.prototype' ] -> getScope ());
2010-07-05 08:47:14 +01:00
$this -> assertEquals ( 'getInstance' , $services [ 'constructor' ] -> getFactoryMethod (), '->load() parses the factory_method attribute' );
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( '%path%/foo.php' , $services [ 'file' ] -> getFile (), '->load() parses the file tag' );
$this -> assertEquals ( array ( 'foo' , new Reference ( 'foo' ), array ( true , false )), $services [ 'arguments' ] -> getArguments (), '->load() parses the argument tags' );
$this -> assertEquals ( 'sc_configure' , $services [ 'configurator1' ] -> getConfigurator (), '->load() parses the configurator tag' );
$this -> assertEquals ( array ( new Reference ( 'baz' ), 'configure' ), $services [ 'configurator2' ] -> getConfigurator (), '->load() parses the configurator tag' );
$this -> assertEquals ( array ( 'BazClass' , 'configureStatic' ), $services [ 'configurator3' ] -> getConfigurator (), '->load() parses the configurator tag' );
$this -> assertEquals ( array ( array ( 'setBar' , array ())), $services [ 'method_call1' ] -> getMethodCalls (), '->load() parses the method_call tag' );
$this -> assertEquals ( array ( array ( 'setBar' , array ( 'foo' , new Reference ( 'foo' ), array ( true , false )))), $services [ 'method_call2' ] -> getMethodCalls (), '->load() parses the method_call tag' );
2010-07-04 17:56:48 +01:00
$this -> assertEquals ( 'baz_factory' , $services [ 'factory_service' ] -> getFactoryService ());
2010-07-16 08:12:58 +01:00
$aliases = $container -> getAliases ();
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( isset ( $aliases [ 'alias_for_foo' ]), '->load() parses aliases' );
2011-01-07 14:44:29 +00:00
$this -> assertEquals ( 'foo' , ( string ) $aliases [ 'alias_for_foo' ], '->load() parses aliases' );
$this -> assertTrue ( $aliases [ 'alias_for_foo' ] -> isPublic ());
$this -> assertTrue ( isset ( $aliases [ 'another_alias_for_foo' ]));
$this -> assertEquals ( 'foo' , ( string ) $aliases [ 'another_alias_for_foo' ]);
$this -> assertFalse ( $aliases [ 'another_alias_for_foo' ] -> isPublic ());
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testExtensions ()
2010-03-18 07:48:03 +00:00
{
2010-07-16 08:12:58 +01:00
$container = new ContainerBuilder ();
$container -> registerExtension ( new \ProjectExtension ());
2011-03-11 13:50:46 +00:00
$loader = new YamlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/yaml' ));
2010-07-16 08:12:58 +01:00
$loader -> load ( 'services10.yml' );
2011-01-16 07:12:36 +00:00
$container -> compile ();
2010-07-16 08:12:58 +01:00
$services = $container -> getDefinitions ();
$parameters = $container -> getParameterBag () -> all ();
2010-06-22 16:27:49 +01:00
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( isset ( $services [ 'project.service.bar' ]), '->load() parses extension elements' );
$this -> assertTrue ( isset ( $parameters [ 'project.parameter.bar' ]), '->load() parses extension elements' );
2010-06-22 16:27:49 +01:00
$this -> assertEquals ( 'BAR' , $services [ 'project.service.foo' ] -> getClass (), '->load() parses extension elements' );
$this -> assertEquals ( 'BAR' , $parameters [ 'project.parameter.foo' ], '->load() parses extension elements' );
2010-05-07 15:09:11 +01:00
try {
2010-07-16 08:12:58 +01:00
$loader -> load ( 'services11.yml' );
2010-05-06 12:25:53 +01:00
$this -> fail ( '->load() throws an InvalidArgumentException if the tag is not valid' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->load() throws an InvalidArgumentException if the tag is not valid' );
2011-02-15 04:58:18 +00:00
$this -> assertStringStartsWith ( 'There is no extension able to load the configuration for "foobarfoobar" (in' , $e -> getMessage (), '->load() throws an InvalidArgumentException if the tag is not valid' );
2010-05-06 12:25:53 +01:00
}
2010-03-18 07:48:03 +00:00
}
2010-07-18 10:26:47 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\Loader\YamlFileLoader :: supports
2010-07-18 10:26:47 +01:00
*/
public function testSupports ()
{
2011-02-09 23:44:02 +00:00
$loader = new YamlFileLoader ( new ContainerBuilder (), new FileLocator ());
2010-07-18 10:26:47 +01:00
$this -> assertTrue ( $loader -> supports ( 'foo.yml' ), '->supports() returns true if the resource is loadable' );
$this -> assertFalse ( $loader -> supports ( 'foo.foo' ), '->supports() returns true if the resource is loadable' );
}
2010-10-02 15:47:35 +01:00
2011-02-17 14:31:50 +00:00
public function testNonArrayTagThrowsException ()
{
$loader = new YamlFileLoader ( new ContainerBuilder (), new FileLocator ( self :: $fixturesPath . '/yaml' ));
try {
$loader -> load ( 'badtag1.yml' );
$this -> fail ( '->load() should throw an exception when the tags key of a service is not an array' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->load() throws an InvalidArgumentException if the tags key is not an array' );
$this -> assertStringStartsWith ( 'Parameter "tags" must be an array for service' , $e -> getMessage (), '->load() throws an InvalidArgumentException if the tags key is not an array' );
}
}
public function testTagWithoutNameThrowsException ()
{
$loader = new YamlFileLoader ( new ContainerBuilder (), new FileLocator ( self :: $fixturesPath . '/yaml' ));
try {
$loader -> load ( 'badtag2.yml' );
$this -> fail ( '->load() should throw an exception when a tag is missing the name key' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->load() throws an InvalidArgumentException if a tag is missing the name key' );
2011-06-16 09:37:09 +01:00
$this -> assertStringStartsWith ( 'A "tags" entry is missing a "name" key for service ' , $e -> getMessage (), '->load() throws an InvalidArgumentException if a tag is missing the name key' );
2011-02-17 14:31:50 +00:00
}
}
2010-03-18 07:48:03 +00:00
}