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
2011-01-17 22:28:59 +00:00
use Symfony\Component\DependencyInjection\ContainerInterface ;
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 XmlFileLoaderTest 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-07-05 08:13:01 +01:00
require_once self :: $fixturesPath . '/includes/ProjectWithXsdExtension.php' ;
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testLoad ()
2010-03-18 07:48:03 +00:00
{
2011-03-11 13:50:46 +00:00
$loader = new XmlFileLoader ( new ContainerBuilder (), new FileLocator ( 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 -> load ( 'foo.xml' );
$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 -> assertStringStartsWith ( 'The file "foo.xml" does not exist (in:' , $e -> getMessage (), '->load() throws an InvalidArgumentException if the loaded file does not exist' );
}
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testParseFile ()
2010-03-18 07:48:03 +00:00
{
2011-03-11 13:50:46 +00:00
$loader = new XmlFileLoader ( new ContainerBuilder (), new FileLocator ( self :: $fixturesPath . '/ini' ));
$r = new \ReflectionObject ( $loader );
$m = $r -> getMethod ( 'parseFile' );
$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 , self :: $fixturesPath . '/ini/parameters.ini' );
2010-05-06 12:25:53 +01:00
$this -> fail ( '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file' );
$this -> assertStringStartsWith ( '[ERROR 4] Start tag expected, \'<\' not found (in' , $e -> getMessage (), '->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file' );
}
2011-03-11 13:50:46 +00:00
$loader = new XmlFileLoader ( new ContainerBuilder (), new FileLocator ( self :: $fixturesPath . '/xml' ));
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 , self :: $fixturesPath . '/xml/nonvalid.xml' );
2010-05-06 12:25:53 +01:00
$this -> fail ( '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD' );
2010-05-07 15:09:11 +01:00
} catch ( \Exception $e ) {
2010-05-06 12:25:53 +01:00
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD' );
$this -> assertStringStartsWith ( '[ERROR 1845] Element \'nonvalid\': No matching global declaration available for the validation root. (in' , $e -> getMessage (), '->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD' );
}
2011-03-11 13:50:46 +00:00
$xml = $m -> invoke ( $loader , self :: $fixturesPath . '/xml/services1.xml' );
2010-08-20 22:09:55 +01:00
$this -> assertEquals ( 'Symfony\\Component\\DependencyInjection\\SimpleXMLElement' , get_class ( $xml ), '->parseFile() returns an SimpleXMLElement object' );
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 XmlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/xml' ));
2010-07-16 08:12:58 +01:00
$loader -> load ( 'services2.xml' );
2010-03-18 07:48:03 +00:00
2010-07-16 08:12:58 +01:00
$actual = $container -> getParameterBag () -> all ();
2011-03-15 14:35:40 +00:00
$expected = array ( 'a string' , 'foo' => 'bar' , 'values' => array ( 0 , 'integer' => 4 , 100 => null , 'true' , true , false , 'on' , 'off' , 'float' => 1.3 , 1000.3 , 'a string' , array ( 'foo' , 'bar' )), 'foo_bar' => new Reference ( 'foo_bar' ), 'mixedcase' => array ( 'MixedCaseKey' => 'value' ));
2010-03-18 07:48:03 +00:00
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( $expected , $actual , '->load() converts XML values to PHP ones' );
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 . '/xml' )),
new YamlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/xml' )),
2011-03-11 13:50:46 +00:00
$loader = new XmlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/xml' )),
2010-07-18 10:26:47 +01:00
));
$loader -> setResolver ( $resolver );
2010-07-16 08:12:58 +01:00
$loader -> load ( 'services4.xml' );
2010-05-06 12:25:53 +01:00
2010-07-16 08:12:58 +01:00
$actual = $container -> getParameterBag () -> all ();
2011-03-15 14:35:40 +00:00
$expected = array ( 'a string' , 'foo' => 'bar' , 'values' => array ( true , false ), 'foo_bar' => new Reference ( 'foo_bar' ), 'mixedcase' => array ( 'MixedCaseKey' => 'value' ), 'bar' => '%foo%' , 'imported_from_ini' => true , 'imported_from_yaml' => 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.xml' );
2010-03-18 07:48:03 +00:00
}
2010-05-06 12:25:53 +01:00
public function testLoadAnonymousServices ()
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 XmlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/xml' ));
2010-07-16 08:12:58 +01:00
$loader -> load ( 'services5.xml' );
$services = $container -> getDefinitions ();
2010-05-06 12:25:53 +01:00
$this -> assertEquals ( 3 , count ( $services ), '->load() attributes unique ids to anonymous services' );
$args = $services [ 'foo' ] -> getArguments ();
$this -> assertEquals ( 1 , count ( $args ), '->load() references anonymous services as "normal" ones' );
2010-08-20 22:09:55 +01:00
$this -> assertEquals ( 'Symfony\\Component\\DependencyInjection\\Reference' , get_class ( $args [ 0 ]), '->load() converts anonymous services to references to "normal" services' );
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( isset ( $services [( string ) $args [ 0 ]]), '->load() makes a reference to the created ones' );
$inner = $services [( string ) $args [ 0 ]];
$this -> assertEquals ( 'BarClass' , $inner -> getClass (), '->load() uses the same configuration as for the anonymous ones' );
$args = $inner -> getArguments ();
$this -> assertEquals ( 1 , count ( $args ), '->load() references anonymous services as "normal" ones' );
2010-08-20 22:09:55 +01:00
$this -> assertEquals ( 'Symfony\\Component\\DependencyInjection\\Reference' , get_class ( $args [ 0 ]), '->load() converts anonymous services to references to "normal" services' );
2010-05-06 12:25:53 +01:00
$this -> assertTrue ( isset ( $services [( string ) $args [ 0 ]]), '->load() makes a reference to the created ones' );
$inner = $services [( string ) $args [ 0 ]];
$this -> assertEquals ( 'BazClass' , $inner -> getClass (), '->load() uses the same configuration as for the anonymous ones' );
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 XmlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/xml' ));
2010-07-16 08:12:58 +01:00
$loader -> load ( 'services6.xml' );
$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' );
2011-01-17 22:28:59 +00:00
$this -> assertEquals ( array ( new Reference ( 'baz' , ContainerInterface :: EXCEPTION_ON_INVALID_REFERENCE , false ), 'configure' ), $services [ 'configurator2' ] -> getConfigurator (), '->load() parses the configurator tag' );
2010-05-06 12:25:53 +01:00
$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-10-19 19:55:44 +01:00
$this -> assertNull ( $services [ 'factory_service' ] -> getClass ());
$this -> assertEquals ( 'getInstance' , $services [ 'factory_service' ] -> getFactoryMethod ());
2010-07-04 17:56:48 +01:00
$this -> assertEquals ( 'baz_factory' , $services [ 'factory_service' ] -> getFactoryService ());
2010-07-03 23:35:39 +01:00
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 <service> elements' );
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 testConvertDomElementToArray ()
2010-03-18 07:48:03 +00:00
{
2010-05-06 12:25:53 +01:00
$doc = new \DOMDocument ( " 1.0 " );
$doc -> loadXML ( '<foo>bar</foo>' );
2011-03-11 13:50:46 +00:00
$this -> assertEquals ( 'bar' , XmlFileLoader :: convertDomElementToArray ( $doc -> documentElement ), '::convertDomElementToArray() converts a \DomElement to an array' );
2010-05-06 12:25:53 +01:00
$doc = new \DOMDocument ( " 1.0 " );
$doc -> loadXML ( '<foo foo="bar" />' );
2011-03-11 13:50:46 +00:00
$this -> assertEquals ( array ( 'foo' => 'bar' ), XmlFileLoader :: convertDomElementToArray ( $doc -> documentElement ), '::convertDomElementToArray() converts a \DomElement to an array' );
2010-05-06 12:25:53 +01:00
$doc = new \DOMDocument ( " 1.0 " );
$doc -> loadXML ( '<foo><foo>bar</foo></foo>' );
2011-03-11 13:50:46 +00:00
$this -> assertEquals ( array ( 'foo' => 'bar' ), XmlFileLoader :: convertDomElementToArray ( $doc -> documentElement ), '::convertDomElementToArray() converts a \DomElement to an array' );
2010-05-06 12:25:53 +01:00
$doc = new \DOMDocument ( " 1.0 " );
$doc -> loadXML ( '<foo><foo>bar<foo>bar</foo></foo></foo>' );
2011-03-11 13:50:46 +00:00
$this -> assertEquals ( array ( 'foo' => array ( 'value' => 'bar' , 'foo' => 'bar' )), XmlFileLoader :: convertDomElementToArray ( $doc -> documentElement ), '::convertDomElementToArray() converts a \DomElement to an array' );
2010-05-06 12:25:53 +01:00
$doc = new \DOMDocument ( " 1.0 " );
$doc -> loadXML ( '<foo><foo></foo></foo>' );
2011-03-11 13:50:46 +00:00
$this -> assertEquals ( array ( 'foo' => null ), XmlFileLoader :: convertDomElementToArray ( $doc -> documentElement ), '::convertDomElementToArray() converts a \DomElement to an array' );
2010-05-06 12:25:53 +01:00
$doc = new \DOMDocument ( " 1.0 " );
$doc -> loadXML ( '<foo><foo><!-- foo --></foo></foo>' );
2011-03-11 13:50:46 +00:00
$this -> assertEquals ( array ( 'foo' => null ), XmlFileLoader :: convertDomElementToArray ( $doc -> documentElement ), '::convertDomElementToArray() converts a \DomElement to an array' );
2010-06-17 14:40:44 +01:00
$doc = new \DOMDocument ( " 1.0 " );
$doc -> loadXML ( '<foo><foo foo="bar"/><foo foo="bar"/></foo>' );
2011-03-11 13:50:46 +00:00
$this -> assertEquals ( array ( 'foo' => array ( array ( 'foo' => 'bar' ), array ( 'foo' => 'bar' ))), XmlFileLoader :: convertDomElementToArray ( $doc -> documentElement ), '::convertDomElementToArray() converts a \DomElement to an array' );
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 ());
$container -> registerExtension ( new \ProjectWithXsdExtension ());
2011-03-11 13:50:46 +00:00
$loader = new XmlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/xml' ));
2010-05-06 12:25:53 +01:00
2010-07-05 08:13:01 +01:00
// extension without an XSD
2010-07-16 08:12:58 +01:00
$loader -> load ( 'extensions/services1.xml' );
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-07-05 08:13:01 +01:00
// extension with an XSD
2010-07-16 08:12:58 +01:00
$container = new ContainerBuilder ();
$container -> registerExtension ( new \ProjectExtension ());
$container -> registerExtension ( new \ProjectWithXsdExtension ());
2011-03-11 13:50:46 +00:00
$loader = new XmlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/xml' ));
2010-07-16 08:12:58 +01:00
$loader -> load ( 'extensions/services2.xml' );
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-07-05 08:13:01 +01:00
$this -> assertTrue ( isset ( $services [ 'project.service.bar' ]), '->load() parses extension elements' );
$this -> assertTrue ( isset ( $parameters [ 'project.parameter.bar' ]), '->load() parses extension elements' );
$this -> assertEquals ( 'BAR' , $services [ 'project.service.foo' ] -> getClass (), '->load() parses extension elements' );
$this -> assertEquals ( 'BAR' , $parameters [ 'project.parameter.foo' ], '->load() parses extension elements' );
2011-02-15 06:52:11 +00:00
$container = new ContainerBuilder ();
$container -> registerExtension ( new \ProjectExtension ());
$container -> registerExtension ( new \ProjectWithXsdExtension ());
2011-03-11 13:50:46 +00:00
$loader = new XmlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/xml' ));
2010-07-16 08:12:58 +01:00
2010-07-05 08:13:01 +01:00
// extension with an XSD (does not validate)
try {
2010-07-16 08:12:58 +01:00
$loader -> load ( 'extensions/services3.xml' );
2010-07-05 08:13:01 +01:00
$this -> fail ( '->load() throws an InvalidArgumentException if the configuration does not validate the XSD' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->load() throws an InvalidArgumentException if the configuration does not validate the XSD' );
$this -> assertRegexp ( '/The attribute \'bar\' is not allowed/' , $e -> getMessage (), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD' );
}
// non-registered extension
2010-05-07 15:09:11 +01:00
try {
2010-07-16 08:12:58 +01:00
$loader -> load ( 'extensions/services4.xml' );
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' );
2010-07-05 08:13:01 +01:00
$this -> assertStringStartsWith ( 'There is no extension able to load the configuration for "project:bar" (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
2011-01-06 21:37:24 +00:00
public function testExtensionInPhar ()
{
2011-03-05 10:43:11 +00:00
if ( extension_loaded ( 'suhosin' ) && false === strpos ( ini_get ( 'suhosin.executor.include.whitelist' ), 'phar' )) {
$this -> markTestSkipped ( 'To run this test, add "phar" to the "suhosin.executor.include.whitelist" settings in your php.ini file.' );
}
require_once self :: $fixturesPath . '/includes/ProjectWithXsdExtensionInPhar.phar' ;
2011-01-06 21:37:24 +00:00
// extension with an XSD in PHAR archive
$container = new ContainerBuilder ();
$container -> registerExtension ( new \ProjectWithXsdExtensionInPhar ());
2011-03-11 13:50:46 +00:00
$loader = new XmlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/xml' ));
2011-01-06 21:37:24 +00:00
$loader -> load ( 'extensions/services6.xml' );
// extension with an XSD in PHAR archive (does not validate)
try {
$loader -> load ( 'extensions/services7.xml' );
$this -> fail ( '->load() throws an InvalidArgumentException if the configuration does not validate the XSD' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->load() throws an InvalidArgumentException if the configuration does not validate the XSD' );
$this -> assertRegexp ( '/The attribute \'bar\' is not allowed/' , $e -> getMessage (), '->load() throws an InvalidArgumentException if the configuration does not validate the XSD' );
}
}
2010-07-18 10:26:47 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\Loader\XmlFileLoader :: supports
2010-07-18 10:26:47 +01:00
*/
public function testSupports ()
{
2011-02-09 23:44:02 +00:00
$loader = new XmlFileLoader ( new ContainerBuilder (), new FileLocator ());
2010-07-18 10:26:47 +01:00
$this -> assertTrue ( $loader -> supports ( 'foo.xml' ), '->supports() returns true if the resource is loadable' );
$this -> assertFalse ( $loader -> supports ( 'foo.foo' ), '->supports() returns true if the resource is loadable' );
}
2011-05-28 09:29:32 +01:00
public function testNoNamingConflictsForAnonymousServices ()
{
$container = new ContainerBuilder ();
$loader1 = new XmlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/xml/extension1' ));
$loader1 -> load ( 'services.xml' );
$services = $container -> getDefinitions ();
2011-06-08 18:56:59 +01:00
$this -> assertEquals ( 2 , count ( $services ), '->load() attributes unique ids to anonymous services' );
2011-05-28 09:29:32 +01:00
$loader2 = new XmlFileLoader ( $container , new FileLocator ( self :: $fixturesPath . '/xml/extension2' ));
$loader2 -> load ( 'services.xml' );
$services = $container -> getDefinitions ();
$this -> assertEquals ( 4 , count ( $services ), '->load() attributes unique ids to anonymous services' );
$services = $container -> getDefinitions ();
$args1 = $services [ 'extension1.foo' ] -> getArguments ();
$inner1 = $services [( string ) $args1 [ 0 ]];
$this -> assertEquals ( 'BarClass1' , $inner1 -> getClass (), '->load() uses the same configuration as for the anonymous ones' );
$args2 = $services [ 'extension2.foo' ] -> getArguments ();
$inner2 = $services [( string ) $args2 [ 0 ]];
$this -> assertEquals ( 'BarClass2' , $inner2 -> getClass (), '->load() uses the same configuration as for the anonymous ones' );
}
2010-03-18 07:48:03 +00:00
}