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 .
2010-03-18 07:48:03 +00:00
* ( c ) Fabien Potencier < fabien . potencier @ symfony - project . 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 ;
use Symfony\Component\DependencyInjection\Loader\Loader ;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader ;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader ;
use Symfony\Component\DependencyInjection\Loader\IniFileLoader ;
use Symfony\Component\DependencyInjection\Loader\LoaderResolver ;
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
{
2010-07-16 08:12:58 +01:00
$loader = new ProjectLoader3 ( new ContainerBuilder (), self :: $fixturesPath . '/ini' );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
try {
2010-05-06 12:25:53 +01:00
$loader -> loadFile ( 'foo.yml' );
$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 {
2010-05-06 12:25:53 +01:00
$loader -> loadFile ( 'parameters.ini' );
$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' );
}
2010-07-16 08:12:58 +01:00
$loader = new ProjectLoader3 ( new ContainerBuilder (), 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 {
2010-05-06 12:25:53 +01:00
$loader -> loadFile ( $fixture . '.yml' );
$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 ();
$loader = new ProjectLoader3 ( $container , self :: $fixturesPath . '/yaml' );
$loader -> load ( 'services2.yml' );
$this -> assertEquals ( array ( 'foo' => 'bar' , '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 (
new IniFileLoader ( $container , self :: $fixturesPath . '/yaml' ),
new XmlFileLoader ( $container , self :: $fixturesPath . '/yaml' ),
$loader = new ProjectLoader3 ( $container , self :: $fixturesPath . '/yaml' ),
));
$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 ();
2010-05-06 12:25:53 +01:00
$expected = array ( 'foo' => 'bar' , 'values' => array ( true , false ), 'bar' => '%foo%' , 'foo_bar' => new Reference ( 'foo_bar' ), 'imported_from_ini' => true , 'imported_from_xml' => true );
$this -> assertEquals ( array_keys ( $expected ), array_keys ( $actual ), '->load() imports and merges imported files' );
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 ();
$loader = new ProjectLoader3 ( $container , self :: $fixturesPath . '/yaml' );
$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' );
$this -> assertTrue ( $services [ 'shared' ] -> isShared (), '->load() parses the shared attribute' );
$this -> assertFalse ( $services [ 'non_shared' ] -> isShared (), '->load() parses the shared attribute' );
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' );
$this -> assertEquals ( 'foo' , $aliases [ 'alias_for_foo' ], '->load() parses aliases' );
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 ());
$loader = new ProjectLoader3 ( $container , self :: $fixturesPath . '/yaml' );
$loader -> load ( 'services10.yml' );
$container -> freeze ();
$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' );
$this -> assertStringStartsWith ( 'There is no extension able to load the configuration for "foobar.foobar" (in' , $e -> getMessage (), '->load() throws an InvalidArgumentException if the tag is not valid' );
}
2010-05-07 15:09:11 +01:00
try {
2010-07-16 08:12:58 +01:00
$loader -> load ( 'services12.yml' );
2010-05-06 12:25:53 +01:00
$this -> fail ( '->load() throws an InvalidArgumentException if an extension is not loaded' );
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 an extension is not loaded' );
$this -> assertStringStartsWith ( 'The "foobar" tag is not valid (in' , $e -> getMessage (), '->load() throws an InvalidArgumentException if an extension is not loaded' );
}
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 ()
{
$loader = new YamlFileLoader ( new ContainerBuilder ());
$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
public function testLoadInterfaceInjectors ()
{
$container = new ContainerBuilder ();
$loader = new ProjectLoader3 ( $container , self :: $fixturesPath . '/yaml' );
$loader -> load ( 'interfaces1.yml' );
$interfaces = $container -> getInterfaceInjectors ( 'FooClass' );
$this -> assertEquals ( 1 , count ( $interfaces ), '->load() parses interfaces' );
$interface = $interfaces [ 'FooClass' ];
$this -> assertTrue ( $interface -> hasMethodCall ( 'setBar' ), '->load() parses interfaces elements' );
}
2010-03-18 07:48:03 +00:00
}
class ProjectLoader3 extends YamlFileLoader
{
2010-05-06 12:25:53 +01:00
public function loadFile ( $file )
{
return parent :: loadFile ( $file );
}
2010-03-18 07:48:03 +00:00
}