2010-06-07 08:08:35 +01:00
< ? php
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-06-07 08:08:35 +01:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2010-07-09 09:05:26 +01:00
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection ;
2010-06-07 08:08:35 +01:00
2010-07-09 09:05:26 +01:00
use Symfony\Bundle\FrameworkBundle\Tests\TestCase ;
2010-09-20 20:01:41 +01:00
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\DependencyInjection\ContainerBuilder ;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag ;
2010-06-07 08:08:35 +01:00
2011-01-24 19:50:31 +00:00
abstract class FrameworkExtensionTest extends TestCase
2010-06-07 08:08:35 +01:00
{
2011-01-24 19:50:31 +00:00
abstract protected function loadFromFile ( ContainerBuilder $container , $file );
public function testCsrfProtection ()
{
$container = $this -> createContainerFromFile ( 'full' );
2011-04-28 10:28:44 +01:00
$def = $container -> getDefinition ( 'form.type_extension.csrf' );
$this -> assertTrue ( $def -> getArgument ( 0 ));
$this -> assertEquals ( '_csrf' , $def -> getArgument ( 1 ));
2011-04-28 10:26:10 +01:00
$this -> assertEquals ( 's3cr3t' , $container -> getParameterBag () -> resolveValue ( $container -> findDefinition ( 'form.csrf_provider' ) -> getArgument ( 1 )));
2011-01-24 19:50:31 +00:00
}
public function testEsi ()
{
$container = $this -> createContainerFromFile ( 'full' );
$this -> assertTrue ( $container -> hasDefinition ( 'esi' ), '->registerEsiConfiguration() loads esi.xml' );
}
public function testProfiler ()
{
$container = $this -> createContainerFromFile ( 'full' );
$this -> assertTrue ( $container -> hasDefinition ( 'profiler' ), '->registerProfilerConfiguration() loads profiling.xml' );
$this -> assertTrue ( $container -> hasDefinition ( 'data_collector.config' ), '->registerProfilerConfiguration() loads collectors.xml' );
2011-04-28 10:26:10 +01:00
$this -> assertTrue ( $container -> getDefinition ( 'profiler_listener' ) -> getArgument ( 2 ));
2011-01-24 19:50:31 +00:00
}
public function testRouter ()
{
$container = $this -> createContainerFromFile ( 'full' );
$this -> assertTrue ( $container -> hasDefinition ( 'router.real' ), '->registerRouterConfiguration() loads routing.xml' );
2011-04-23 14:27:35 +01:00
$arguments = $container -> getDefinition ( 'router.real' ) -> getArguments ();
$this -> assertEquals ( $container -> getParameter ( 'kernel.root_dir' ) . '/config/routing.xml' , $arguments [ 1 ], '->registerRouterConfiguration() sets routing resource' );
$this -> assertEquals ( 'xml' , $arguments [ 2 ][ 'resource_type' ], '->registerRouterConfiguration() sets routing resource type' );
2011-01-24 19:50:31 +00:00
$this -> assertTrue ( $container -> getDefinition ( 'router.cache_warmer' ) -> hasTag ( 'kernel.cache_warmer' ), '->registerRouterConfiguration() tags router cache warmer if cache warming is set' );
$this -> assertEquals ( 'router.cached' , ( string ) $container -> getAlias ( 'router' ), '->registerRouterConfiguration() changes router alias to cached if cache warming is set' );
}
/**
2011-02-17 15:03:48 +00:00
* @ expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
2011-01-24 19:50:31 +00:00
*/
public function testRouterRequiresResourceOption ()
2010-06-07 08:08:35 +01:00
{
2011-01-24 19:50:31 +00:00
$container = $this -> createContainer ();
2010-09-20 20:01:41 +01:00
$loader = new FrameworkExtension ();
2011-02-15 04:58:18 +00:00
$loader -> load ( array ( array ( 'router' => true )), $container );
2011-01-24 19:50:31 +00:00
}
public function testSession ()
{
$container = $this -> createContainerFromFile ( 'full' );
$this -> assertTrue ( $container -> hasDefinition ( 'session' ), '->registerSessionConfiguration() loads session.xml' );
2011-04-28 10:26:10 +01:00
$this -> assertEquals ( 'fr' , $container -> getDefinition ( 'session' ) -> getArgument ( 1 ));
2011-01-24 19:50:31 +00:00
$this -> assertTrue ( $container -> getDefinition ( 'session' ) -> hasMethodCall ( 'start' ));
$this -> assertEquals ( 'session.storage.native' , ( string ) $container -> getAlias ( 'session.storage' ));
2011-03-02 18:51:17 +00:00
2011-04-22 11:35:09 +01:00
$options = $container -> getParameter ( 'session.storage.options' );
2011-01-24 19:50:31 +00:00
$this -> assertEquals ( '_SYMFONY' , $options [ 'name' ]);
$this -> assertEquals ( 86400 , $options [ 'lifetime' ]);
$this -> assertEquals ( '/' , $options [ 'path' ]);
$this -> assertEquals ( 'example.com' , $options [ 'domain' ]);
$this -> assertTrue ( $options [ 'secure' ]);
$this -> assertTrue ( $options [ 'httponly' ]);
}
public function testTemplating ()
{
$container = $this -> createContainerFromFile ( 'full' );
2010-06-07 08:08:35 +01:00
2011-01-24 19:50:31 +00:00
$this -> assertTrue ( $container -> hasDefinition ( 'templating.name_parser' ), '->registerTemplatingConfiguration() loads templating.xml' );
2011-03-30 18:05:22 +01:00
$arguments = $container -> getDefinition ( 'templating.helper.assets' ) -> getArguments ();
$this -> assertEquals ( 'SomeVersionScheme' , $arguments [ 2 ]);
$this -> assertEquals ( array ( 'http://cdn.example.com' ), $arguments [ 1 ]);
2010-06-16 13:19:46 +01:00
2011-01-24 19:50:31 +00:00
$this -> assertTrue ( $container -> getDefinition ( 'templating.cache_warmer.template_paths' ) -> hasTag ( 'kernel.cache_warmer' ), '->registerTemplatingConfiguration() tags templating cache warmer if cache warming is set' );
$this -> assertEquals ( 'templating.locator.cached' , ( string ) $container -> getAlias ( 'templating.locator' ), '->registerTemplatingConfiguration() changes templating.locator alias to cached if cache warming is set' );
$this -> assertEquals ( 'templating.engine.delegating' , ( string ) $container -> getAlias ( 'templating' ), '->registerTemplatingConfiguration() configures delegating loader if multiple engines are provided' );
2011-04-02 16:05:02 +01:00
$this -> assertEquals ( $container -> getDefinition ( 'templating.loader.chain' ), $container -> getDefinition ( 'templating.loader.wrapped' ), '->registerTemplatingConfiguration() configures loader chain if multiple loaders are provided' );
$this -> assertEquals ( $container -> getDefinition ( 'templating.loader' ), $container -> getDefinition ( 'templating.loader.cache' ), '->registerTemplatingConfiguration() configures the loader to use cache' );
2011-04-28 10:26:10 +01:00
$this -> assertEquals ( '/path/to/cache' , $container -> getDefinition ( 'templating.loader.cache' ) -> getArgument ( 1 ));
2011-02-10 21:05:04 +00:00
$this -> assertEquals ( array ( 'php' , 'twig' ), $container -> getParameter ( 'templating.engines' ), '->registerTemplatingConfiguration() sets a templating.engines parameter' );
2011-01-24 19:50:31 +00:00
}
public function testTranslator ()
{
$container = $this -> createContainerFromFile ( 'full' );
$this -> assertTrue ( $container -> hasDefinition ( 'translator.real' ), '->registerTranslatorConfiguration() loads translation.xml' );
$this -> assertSame ( $container -> getDefinition ( 'translator.real' ), $container -> getDefinition ( 'translator' ), '->registerTranslatorConfiguration() redefines translator service from identity to real translator' );
2011-04-23 10:34:17 +01:00
$resources = array ();
foreach ( $container -> getDefinition ( 'translator.real' ) -> getMethodCalls () as $call ) {
if ( 'addResource' == $call [ 0 ]) {
$resources [] = $call [ 1 ];
}
}
2011-01-24 19:50:31 +00:00
$this -> assertContains (
realpath ( __DIR__ . '/../../Resources/translations/validators.fr.xliff' ),
2011-04-23 10:34:17 +01:00
array_map ( function ( $resource ) use ( $resources ) { return realpath ( $resource [ 1 ]); }, $resources ),
2011-01-24 19:50:31 +00:00
'->registerTranslatorConfiguration() finds FrameworkExtension translation resources'
);
2011-03-30 18:16:13 +01:00
$calls = $container -> getDefinition ( 'translator.real' ) -> getMethodCalls ();
$this -> assertEquals ( 'fr' , $calls [ 0 ][ 1 ][ 0 ]);
2011-01-24 19:50:31 +00:00
}
/**
2011-02-17 15:03:48 +00:00
* @ expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
2011-01-24 19:50:31 +00:00
*/
public function testTemplatingRequiresAtLeastOneEngine ()
{
$container = $this -> createContainer ();
2010-09-20 20:01:41 +01:00
$loader = new FrameworkExtension ();
2011-02-15 04:58:18 +00:00
$loader -> load ( array ( array ( 'templating' => null )), $container );
2011-01-24 19:50:31 +00:00
}
public function testValidation ()
{
$container = $this -> createContainerFromFile ( 'full' );
2010-06-16 13:19:46 +01:00
2011-01-24 19:50:31 +00:00
$this -> assertTrue ( $container -> hasDefinition ( 'validator' ), '->registerValidationConfiguration() loads validator.xml' );
$this -> assertTrue ( $container -> hasDefinition ( 'validator.mapping.loader.xml_files_loader' ), '->registerValidationConfiguration() defines the XML loader' );
$this -> assertTrue ( $container -> hasDefinition ( 'validator.mapping.loader.yaml_files_loader' ), '->registerValidationConfiguration() defines the YAML loader' );
2010-06-07 08:08:35 +01:00
2011-04-28 10:26:10 +01:00
$xmlFiles = $container -> getDefinition ( 'validator.mapping.loader.xml_files_loader' ) -> getArgument ( 0 );
2010-06-24 09:40:05 +01:00
2011-01-24 19:50:31 +00:00
$this -> assertContains (
realpath ( __DIR__ . '/../../../../Component/Form/Resources/config/validation.xml' ),
array_map ( 'realpath' , $xmlFiles ),
'->registerValidationConfiguration() adds Form validation.xml to XML loader'
);
2010-06-24 09:40:05 +01:00
}
2011-01-24 19:50:31 +00:00
public function testValidationAnnotations ()
{
$container = $this -> createContainerFromFile ( 'validation_annotations' );
$this -> assertTrue ( $container -> hasDefinition ( 'validator.mapping.loader.annotation_loader' ), '->registerValidationConfiguration() defines the annotation loader' );
2011-04-28 22:26:27 +01:00
$loaders = $container -> getDefinition ( 'validator.mapping.loader.loader_chain' ) -> getArgument ( 0 );
$found = false ;
foreach ( $loaders as $loader ) {
if ( 'validator.mapping.loader.annotation_loader' === ( string ) $loader ) {
$found = true ;
}
}
$this -> assertTrue ( $found , 'validator.mapping.loader.annotation_loader is added to the loader chain.' );
2011-01-24 19:50:31 +00:00
}
2011-04-11 22:26:11 +01:00
public function testValidationPaths ()
2010-08-10 14:55:05 +01:00
{
2011-04-11 22:26:11 +01:00
require_once __DIR__ . " /Fixtures/TestBundle/TestBundle.php " ;
2011-04-15 20:12:02 +01:00
2011-04-11 22:26:11 +01:00
$container = $this -> createContainerFromFile ( 'validation_annotations' , array (
'kernel.bundles' => array ( 'TestBundle' => 'Symfony\Bundle\FrameworkBundle\Tests\TestBundle' ),
));
2011-04-28 10:26:10 +01:00
$yamlArgs = $container -> getDefinition ( 'validator.mapping.loader.yaml_files_loader' ) -> getArgument ( 0 );
$this -> assertEquals ( 1 , count ( $yamlArgs ));
$this -> assertStringEndsWith ( 'TestBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'validation.yml' , $yamlArgs [ 0 ]);
2011-04-11 22:26:11 +01:00
2011-04-28 10:26:10 +01:00
$xmlArgs = $container -> getDefinition ( 'validator.mapping.loader.xml_files_loader' ) -> getArgument ( 0 );
$this -> assertEquals ( 2 , count ( $xmlArgs ));
$this -> assertStringEndsWith ( 'Component/Form/Resources/config/validation.xml' , $xmlArgs [ 0 ]);
$this -> assertStringEndsWith ( 'TestBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'validation.xml' , $xmlArgs [ 1 ]);
2011-04-11 22:26:11 +01:00
}
protected function createContainer ( array $data = array ())
{
return new ContainerBuilder ( new ParameterBag ( array_merge ( array (
2011-04-04 10:08:30 +01:00
'kernel.bundles' => array ( 'FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle' ),
2011-01-24 19:50:31 +00:00
'kernel.cache_dir' => __DIR__ ,
2010-08-10 20:44:04 +01:00
'kernel.compiled_classes' => array (),
2011-01-24 19:50:31 +00:00
'kernel.debug' => false ,
'kernel.environment' => 'test' ,
'kernel.name' => 'kernel' ,
'kernel.root_dir' => __DIR__ ,
2011-04-11 22:26:11 +01:00
), $data )));
2010-08-10 14:55:05 +01:00
}
2011-01-24 19:50:31 +00:00
2011-04-11 22:26:11 +01:00
protected function createContainerFromFile ( $file , $data = array ())
2011-01-24 19:50:31 +00:00
{
2011-04-11 22:26:11 +01:00
$container = $this -> createContainer ( $data );
2011-01-24 19:50:31 +00:00
$container -> registerExtension ( new FrameworkExtension ());
$this -> loadFromFile ( $container , $file );
$container -> getCompilerPassConfig () -> setOptimizationPasses ( array ());
$container -> getCompilerPassConfig () -> setRemovingPasses ( array ());
$container -> compile ();
return $container ;
}
2010-06-07 08:08:35 +01:00
}