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' );
$this -> assertTrue ( $container -> getParameter ( 'form.csrf_protection.enabled' ));
$this -> assertEquals ( '_csrf' , $container -> getParameter ( 'form.csrf_protection.field_name' ));
$this -> assertEquals ( 's3cr3t' , $container -> getParameter ( 'form.csrf_protection.secret' ));
}
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-03-16 14:50:48 +00:00
$arguments = $container -> getDefinition ( 'profiler_listener' ) -> getArguments ();
$this -> assertTrue ( $arguments [ 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' );
$this -> assertEquals ( $container -> getParameter ( 'kernel.root_dir' ) . '/config/routing.xml' , $container -> getParameter ( 'routing.resource' ), '->registerRouterConfiguration() sets routing resource' );
2011-02-10 15:26:13 +00:00
$this -> assertEquals ( 'xml' , $container -> getParameter ( 'router.options.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-03-30 16:26:33 +01:00
$arguments = $container -> getDefinition ( 'session' ) -> getArguments ();
$this -> assertEquals ( 'fr' , $arguments [ 1 ]);
2011-01-24 19:50:31 +00:00
$this -> assertTrue ( $container -> getDefinition ( 'session' ) -> hasMethodCall ( 'start' ));
$this -> assertEquals ( 'Session' , $container -> getParameter ( 'session.class' ));
$this -> assertEquals ( 'session.storage.native' , ( string ) $container -> getAlias ( 'session.storage' ));
2011-03-02 18:51:17 +00:00
2011-01-24 19:50:31 +00:00
$options = $container -> getParameter ( 'session.storage.native.options' );
$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 testSessionPdo ()
{
$container = $this -> createContainerFromFile ( 'session_pdo' );
$options = $container -> getParameter ( 'session.storage.pdo.options' );
$this -> assertEquals ( 'session.storage.pdo' , ( string ) $container -> getAlias ( 'session.storage' ));
$this -> assertEquals ( 'table' , $options [ 'db_table' ]);
$this -> assertEquals ( 'id' , $options [ 'db_id_col' ]);
$this -> assertEquals ( 'data' , $options [ 'db_data_col' ]);
$this -> assertEquals ( 'time' , $options [ 'db_time_col' ]);
}
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' );
$this -> assertEquals ( 'templating.loader.chain' , ( string ) $container -> getAlias ( 'templating.loader' ), '->registerTemplatingConfiguration() configures loader chain if multiple loaders are provided' );
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' );
$this -> assertContains (
realpath ( __DIR__ . '/../../Resources/translations/validators.fr.xliff' ),
array_map ( function ( $resource ) { return $resource [ 1 ]; }, $container -> getParameter ( 'translation.resources' )),
'->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-01-24 19:50:31 +00:00
$xmlLoaderArgs = $container -> getDefinition ( 'validator.mapping.loader.xml_files_loader' ) -> getArguments ();
$xmlFiles = $xmlLoaderArgs [ 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
$this -> assertFalse ( $container -> hasDefinition ( 'validator.mapping.loader.annotation_loader' ), '->registerValidationConfiguration() does not define the annotation loader unless needed' );
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' );
$namespaces = $container -> getParameter ( 'validator.annotations.namespaces' );
$this -> assertEquals ( 'Symfony\\Component\\Validator\\Constraints\\' , $namespaces [ 'validation' ], '->registerValidationConfiguration() loads the default "validation" namespace' );
$this -> assertEquals ( 'Application\\Validator\\Constraints\\' , $namespaces [ 'app' ], '->registerValidationConfiguration() loads custom validation namespaces' );
}
protected function createContainer ()
2010-08-10 14:55:05 +01:00
{
return new ContainerBuilder ( new ParameterBag ( array (
2011-03-28 19:38:22 +01:00
'kernel.bundles' => array ( 'Framework' => '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__ ,
2010-08-10 14:55:05 +01:00
)));
}
2011-01-24 19:50:31 +00:00
protected function createContainerFromFile ( $file )
{
$container = $this -> createContainer ();
$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
}