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' );
2011-05-13 10:57:05 +01:00
$this -> assertTrue ( $container -> getParameter ( 'form.type_extension.csrf.enabled' ));
2012-04-13 13:11:09 +01:00
$this -> assertEquals ( '%form.type_extension.csrf.enabled%' , $def -> getArgument ( 1 ));
2011-05-13 10:57:05 +01:00
$this -> assertEquals ( '_csrf' , $container -> getParameter ( 'form.type_extension.csrf.field_name' ));
2012-04-13 13:11:09 +01:00
$this -> assertEquals ( '%form.type_extension.csrf.field_name%' , $def -> getArgument ( 2 ));
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
}
2012-12-20 07:58:18 +00:00
public function testProxies ()
{
$container = $this -> createContainerFromFile ( 'full' );
$this -> assertTrue ( $container -> getParameter ( 'kernel.trust_proxy_headers' ));
$this -> assertEquals ( array ( '127.0.0.1' , '10.0.0.1' ), $container -> getParameter ( 'kernel.trusted_proxies' ));
}
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-05-13 10:57:05 +01:00
$this -> assertTrue ( $container -> getParameter ( 'profiler_listener.only_exceptions' ));
$this -> assertEquals ( '%profiler_listener.only_exceptions%' , $container -> getDefinition ( 'profiler_listener' ) -> getArgument ( 2 ));
2012-07-13 14:40:24 +01:00
$calls = $container -> getDefinition ( 'profiler' ) -> getMethodCalls ();
$this -> assertEquals ( 'disable' , $calls [ 0 ][ 0 ]);
2011-01-24 19:50:31 +00:00
}
public function testRouter ()
{
$container = $this -> createContainerFromFile ( 'full' );
2011-07-18 09:10:04 +01:00
$this -> assertTrue ( $container -> has ( 'router' ), '->registerRouterConfiguration() loads routing.xml' );
$arguments = $container -> findDefinition ( 'router' ) -> getArguments ();
2011-05-13 10:57:05 +01:00
$this -> assertEquals ( $container -> getParameter ( 'kernel.root_dir' ) . '/config/routing.xml' , $container -> getParameter ( 'router.resource' ), '->registerRouterConfiguration() sets routing resource' );
$this -> assertEquals ( '%router.resource%' , $arguments [ 1 ], '->registerRouterConfiguration() sets routing resource' );
2011-04-23 14:27:35 +01:00
$this -> assertEquals ( 'xml' , $arguments [ 2 ][ 'resource_type' ], '->registerRouterConfiguration() sets routing resource type' );
2011-01-24 19:50:31 +00:00
}
/**
2013-01-05 17:52:40 +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-10-05 10:09:51 +01:00
$this -> assertEquals ( 'fr' , $container -> getParameter ( 'kernel.default_locale' ));
2012-03-03 09:03:04 +00:00
$this -> assertEquals ( 'session.storage.native' , ( string ) $container -> getAlias ( 'session.storage' ));
$this -> assertEquals ( 'session.handler.native_file' , ( string ) $container -> getAlias ( 'session.handler' ));
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' ]);
2012-02-23 10:53:14 +00:00
$this -> assertEquals ( 86400 , $options [ 'cookie_lifetime' ]);
$this -> assertEquals ( '/' , $options [ 'cookie_path' ]);
$this -> assertEquals ( 'example.com' , $options [ 'cookie_domain' ]);
$this -> assertTrue ( $options [ 'cookie_secure' ]);
$this -> assertTrue ( $options [ 'cookie_httponly' ]);
2012-03-21 03:22:23 +00:00
$this -> assertEquals ( 108 , $options [ 'gc_divisor' ]);
$this -> assertEquals ( 1 , $options [ 'gc_probability' ]);
$this -> assertEquals ( 90000 , $options [ 'gc_maxlifetime' ]);
2012-04-13 09:34:00 +01:00
$this -> assertEquals ( '/path/to/sessions' , $container -> getParameter ( 'session.save_path' ));
2011-01-24 19:50:31 +00:00
}
2012-02-29 08:44:47 +00:00
public function testSessionDeprecatedMergeFull ()
{
$container = $this -> createContainerFromFile ( 'deprecated_merge_full' );
$this -> assertTrue ( $container -> hasDefinition ( 'session' ), '->registerSessionConfiguration() loads session.xml' );
$options = $container -> getParameter ( 'session.storage.options' );
$this -> assertEquals ( '_SYMFONY' , $options [ 'name' ]);
$this -> assertEquals ( 86400 , $options [ 'cookie_lifetime' ]);
$this -> assertEquals ( '/' , $options [ 'cookie_path' ]);
$this -> assertEquals ( 'example.com' , $options [ 'cookie_domain' ]);
$this -> assertTrue ( $options [ 'cookie_secure' ]);
$this -> assertTrue ( $options [ 'cookie_httponly' ]);
}
public function testSessionDeprecatedMergePartial ()
{
$container = $this -> createContainerFromFile ( 'deprecated_merge_partial' );
$this -> assertTrue ( $container -> hasDefinition ( 'session' ), '->registerSessionConfiguration() loads session.xml' );
$options = $container -> getParameter ( 'session.storage.options' );
$this -> assertEquals ( '_SYMFONY' , $options [ 'name' ]);
$this -> assertEquals ( 86400 , $options [ 'cookie_lifetime' ]);
$this -> assertEquals ( '/' , $options [ 'cookie_path' ]);
$this -> assertEquals ( 'sf2.example.com' , $options [ 'cookie_domain' ]);
$this -> assertFalse ( $options [ 'cookie_secure' ]);
$this -> assertTrue ( $options [ 'cookie_httponly' ]);
}
2011-01-24 19:50:31 +00:00
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-05-21 00:08:03 +01:00
2011-10-24 19:17:43 +01:00
$this -> assertEquals ( 'request' , $container -> getDefinition ( 'templating.helper.assets' ) -> getScope (), '->registerTemplatingConfiguration() sets request scope on assets helper if one or more packages are request-scoped' );
2011-09-26 18:19:08 +01:00
2011-05-21 00:08:03 +01:00
// default package should have one http base url and path package ssl url
$this -> assertTrue ( $container -> hasDefinition ( 'templating.asset.default_package.http' ));
$package = $container -> getDefinition ( 'templating.asset.default_package.http' );
$this -> assertInstanceOf ( 'Symfony\\Component\\DependencyInjection\\DefinitionDecorator' , $package );
$this -> assertEquals ( 'templating.asset.url_package' , $package -> getParent ());
$arguments = array_values ( $package -> getArguments ());
$this -> assertEquals ( array ( 'http://cdn.example.com' ), $arguments [ 0 ]);
$this -> assertEquals ( 'SomeVersionScheme' , $arguments [ 1 ]);
2011-09-19 07:55:18 +01:00
$this -> assertEquals ( '%%s?%%s' , $arguments [ 2 ]);
2011-05-21 00:08:03 +01:00
$this -> assertTrue ( $container -> hasDefinition ( 'templating.asset.default_package.ssl' ));
$package = $container -> getDefinition ( 'templating.asset.default_package.ssl' );
$this -> assertInstanceOf ( 'Symfony\\Component\\DependencyInjection\\DefinitionDecorator' , $package );
$this -> assertEquals ( 'templating.asset.path_package' , $package -> getParent ());
2010-06-16 13:19:46 +01:00
2011-01-24 19:50:31 +00:00
$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-05-13 10:57:05 +01:00
$this -> assertEquals ( '%templating.loader.cache.path%' , $container -> getDefinition ( 'templating.loader.cache' ) -> getArgument ( 1 ));
$this -> assertEquals ( '/path/to/cache' , $container -> getParameter ( 'templating.loader.cache.path' ));
2011-02-10 21:05:04 +00:00
$this -> assertEquals ( array ( 'php' , 'twig' ), $container -> getParameter ( 'templating.engines' ), '->registerTemplatingConfiguration() sets a templating.engines parameter' );
2011-06-22 07:27:40 +01:00
$this -> assertEquals ( array ( 'FrameworkBundle:Form' , 'theme1' , 'theme2' ), $container -> getParameter ( 'templating.helper.form.resources' ), '->registerTemplatingConfiguration() registers the theme and adds the base theme' );
2011-01-24 19:50:31 +00:00
}
2011-09-26 18:19:08 +01:00
public function testTemplatingAssetsHelperScopeDependsOnPackageArgumentScopes ()
{
$container = $this -> createContainerFromFile ( 'templating_url_package' );
2011-10-24 19:17:43 +01:00
$this -> assertNotEquals ( 'request' , $container -> getDefinition ( 'templating.helper.assets' ) -> getScope (), '->registerTemplatingConfiguration() does not set request scope on assets helper if no packages are request-scoped' );
2011-09-26 18:19:08 +01:00
}
2011-01-24 19:50:31 +00:00
public function testTranslator ()
{
$container = $this -> createContainerFromFile ( 'full' );
2011-07-19 11:05:51 +01:00
$this -> assertTrue ( $container -> hasDefinition ( 'translator.default' ), '->registerTranslatorConfiguration() loads translation.xml' );
$this -> assertEquals ( 'translator.default' , ( string ) $container -> getAlias ( 'translator' ), '->registerTranslatorConfiguration() redefines translator service from identity to real translator' );
2011-01-24 19:50:31 +00:00
2011-04-23 10:34:17 +01:00
$resources = array ();
2011-07-19 11:05:51 +01:00
foreach ( $container -> getDefinition ( 'translator.default' ) -> getMethodCalls () as $call ) {
2011-04-23 10:34:17 +01:00
if ( 'addResource' == $call [ 0 ]) {
$resources [] = $call [ 1 ];
}
}
2012-08-25 21:54:18 +01:00
$rootDirectory = str_replace ( '/' , DIRECTORY_SEPARATOR , realpath ( __DIR__ . '/../../../../..' ) . '/' );
$files = array_map ( function ( $resource ) use ( $rootDirectory , $resources ) { return str_replace ( $rootDirectory , '' , realpath ( $resource [ 1 ])); }, $resources );
2011-01-24 19:50:31 +00:00
$this -> assertContains (
2012-08-25 21:54:18 +01:00
str_replace ( '/' , DIRECTORY_SEPARATOR , 'Symfony/Component/Validator/Resources/translations/validators.en.xlf' ),
2012-06-16 12:08:37 +01:00
$files ,
'->registerTranslatorConfiguration() finds Validator translation resources'
);
$this -> assertContains (
2012-08-25 21:54:18 +01:00
str_replace ( '/' , DIRECTORY_SEPARATOR , 'Symfony/Component/Form/Resources/translations/validators.en.xlf' ),
2012-06-16 12:08:37 +01:00
$files ,
'->registerTranslatorConfiguration() finds Form translation resources'
2011-01-24 19:50:31 +00:00
);
2012-07-15 14:56:45 +01:00
$this -> assertContains (
2013-01-07 19:57:06 +00:00
'Symfony/Component/Security/Resources/translations/security.en.xlf' ,
2012-07-15 14:56:45 +01:00
$files ,
'->registerTranslatorConfiguration() finds Security translation resources'
);
2011-01-24 19:50:31 +00:00
2011-07-19 11:05:51 +01:00
$calls = $container -> getDefinition ( 'translator.default' ) -> getMethodCalls ();
2011-03-30 18:16:13 +01:00
$this -> assertEquals ( 'fr' , $calls [ 0 ][ 1 ][ 0 ]);
2011-01-24 19:50:31 +00:00
}
/**
2013-01-05 17:52:40 +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-05-13 10:57:05 +01:00
$xmlFiles = $container -> getParameter ( 'validator.mapping.loader.xml_files_loader.mapping_files' );
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-05-03 13:36:42 +01:00
public function testAnnotations ()
{
2011-05-24 22:16:10 +01:00
if ( ! class_exists ( 'Doctrine\\Common\\Version' )) {
$this -> markTestSkipped ( 'Doctrine is not available.' );
}
2011-05-03 13:36:42 +01:00
$container = $this -> createContainerFromFile ( 'full' );
2011-05-24 12:29:44 +01:00
$this -> assertEquals ( $container -> getParameter ( 'kernel.cache_dir' ) . '/annotations' , $container -> getDefinition ( 'annotations.file_cache_reader' ) -> getArgument ( 1 ));
$this -> assertInstanceOf ( 'Doctrine\Common\Annotations\FileCacheReader' , $container -> get ( 'annotation_reader' ));
2011-05-03 13:36:42 +01:00
}
2011-07-12 18:01:49 +01:00
public function testFileLinkFormat ()
{
$container = $this -> createContainerFromFile ( 'full' );
$this -> assertEquals ( 'file%link%format' , $container -> getParameter ( 'templating.helper.code.file_link_format' ));
}
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-06-23 13:07:53 +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-05-13 10:57:05 +01:00
$yamlArgs = $container -> getParameter ( 'validator.mapping.loader.yaml_files_loader.mapping_files' );
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 1 , $yamlArgs );
2011-04-28 10:26:10 +01:00
$this -> assertStringEndsWith ( 'TestBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'validation.yml' , $yamlArgs [ 0 ]);
2011-04-11 22:26:11 +01:00
2011-05-13 10:57:05 +01:00
$xmlArgs = $container -> getParameter ( 'validator.mapping.loader.xml_files_loader.mapping_files' );
2012-01-18 13:42:47 +00:00
$this -> assertCount ( 2 , $xmlArgs );
2012-03-02 16:54:28 +00:00
$this -> assertStringEndsWith ( 'Component' . DIRECTORY_SEPARATOR . 'Form/Resources/config/validation.xml' , $xmlArgs [ 0 ]);
2011-04-28 10:26:10 +01:00
$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
}