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 ;
2014-03-18 16:36:12 +00:00
use Symfony\Component\DependencyInjection\Reference ;
use Symfony\Component\Validator\Validation ;
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-01-24 19:50:31 +00:00
}
2013-10-09 09:42:58 +01:00
/**
* @ expectedException \LogicException
* @ expectedExceptionMessage CSRF protection needs sessions to be enabled .
*/
public function testCsrfProtectionNeedsSessionToBeEnabled ()
{
$this -> createContainerFromFile ( 'csrf_needs_session' );
}
2013-11-30 10:58:54 +00:00
public function testCsrfProtectionForFormsEnablesCsrfProtectionAutomatically ()
2013-10-09 09:42:58 +01:00
{
2013-11-30 10:58:54 +00:00
$container = $this -> createContainerFromFile ( 'csrf' );
$this -> assertTrue ( $container -> hasDefinition ( 'security.csrf.token_manager' ));
2013-10-09 09:42:58 +01:00
}
public function testSecureRandomIsAvailableIfCsrfIsDisabled ()
{
$container = $this -> createContainerFromFile ( 'csrf_disabled' );
$this -> assertTrue ( $container -> hasDefinition ( 'security.secure_random' ));
}
2012-12-20 07:58:18 +00:00
public function testProxies ()
{
$container = $this -> createContainerFromFile ( 'full' );
$this -> assertEquals ( array ( '127.0.0.1' , '10.0.0.1' ), $container -> getParameter ( 'kernel.trusted_proxies' ));
}
2011-01-24 19:50:31 +00:00
2013-02-27 13:12:14 +00:00
public function testHttpMethodOverride ()
{
$container = $this -> createContainerFromFile ( 'full' );
$this -> assertFalse ( $container -> getParameter ( 'kernel.http_method_override' ));
}
2011-01-24 19:50:31 +00:00
public function testEsi ()
{
$container = $this -> createContainerFromFile ( 'full' );
$this -> assertTrue ( $container -> hasDefinition ( 'esi' ), '->registerEsiConfiguration() loads esi.xml' );
}
2013-04-26 14:53:58 +01:00
public function testEnabledProfiler ()
2011-01-24 19:50:31 +00:00
{
2013-04-26 14:53:58 +01:00
$container = $this -> createContainerFromFile ( 'profiler' );
2011-01-24 19:50:31 +00:00
$this -> assertTrue ( $container -> hasDefinition ( 'profiler' ), '->registerProfilerConfiguration() loads profiling.xml' );
$this -> assertTrue ( $container -> hasDefinition ( 'data_collector.config' ), '->registerProfilerConfiguration() loads collectors.xml' );
2013-02-14 15:42:17 +00:00
}
public function testDisabledProfiler ()
{
2013-04-26 14:53:58 +01:00
$container = $this -> createContainerFromFile ( 'full' );
2012-07-13 14:40:24 +01:00
2013-02-14 15:42:17 +00:00
$this -> assertFalse ( $container -> hasDefinition ( 'profiler' ), '->registerProfilerConfiguration() does not load profiling.xml' );
$this -> assertFalse ( $container -> hasDefinition ( 'data_collector.config' ), '->registerProfilerConfiguration() does not load collectors.xml' );
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
}
2013-06-07 23:05:01 +01:00
public function testNullSessionHandler ()
{
$container = $this -> createContainerFromFile ( 'session' );
$this -> assertTrue ( $container -> hasDefinition ( 'session' ), '->registerSessionConfiguration() loads session.xml' );
$this -> assertNull ( $container -> getDefinition ( 'session.storage.native' ) -> getArgument ( 1 ));
$this -> assertNull ( $container -> getDefinition ( 'session.storage.php_bridge' ) -> getArgument ( 0 ));
}
2013-12-25 21:24:54 +00:00
public function testRequest ()
{
$container = $this -> createContainerFromFile ( 'full' );
$this -> assertTrue ( $container -> hasDefinition ( 'request.add_request_formats_listener' ), '->registerRequestConfiguration() loads request.xml' );
$listenerDef = $container -> getDefinition ( 'request.add_request_formats_listener' );
$this -> assertEquals ( array ( 'csv' => array ( 'text/csv' , 'text/plain' ), 'pdf' => array ( 'application/pdf' )), $listenerDef -> getArgument ( 0 ));
}
public function testEmptyRequestFormats ()
{
$container = $this -> createContainerFromFile ( 'request' );
$this -> assertFalse ( $container -> hasDefinition ( 'request.add_request_formats_listener' ), '->registerRequestConfiguration() does not load request.xml when no request formats are defined' );
}
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
2013-12-28 08:32:39 +00:00
// default package should have one HTTP base URL and path package SSL URL
2011-05-21 00:08:03 +01:00
$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' );
2014-08-10 13:44:20 +01:00
$this -> assertEquals ( 'global_hinclude_template' , $container -> getParameter ( 'fragment.renderer.hinclude.global_template' ), '->registerTemplatingConfiguration() registers the global hinclude.js template' );
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 ];
}
}
2013-12-28 07:46:05 +00:00
$files = array_map ( function ( $resource ) { return realpath ( $resource [ 1 ]); }, $resources );
2013-05-13 03:46:20 +01:00
$ref = new \ReflectionClass ( 'Symfony\Component\Validator\Validator' );
2011-01-24 19:50:31 +00:00
$this -> assertContains (
2013-09-18 08:24:07 +01:00
strtr ( dirname ( $ref -> getFileName ()) . '/Resources/translations/validators.en.xlf' , '/' , DIRECTORY_SEPARATOR ),
2012-06-16 12:08:37 +01:00
$files ,
'->registerTranslatorConfiguration() finds Validator translation resources'
);
2013-05-13 03:46:20 +01:00
$ref = new \ReflectionClass ( 'Symfony\Component\Form\Form' );
2012-06-16 12:08:37 +01:00
$this -> assertContains (
2013-09-18 08:24:07 +01:00
strtr ( dirname ( $ref -> getFileName ()) . '/Resources/translations/validators.en.xlf' , '/' , DIRECTORY_SEPARATOR ),
2012-06-16 12:08:37 +01:00
$files ,
'->registerTranslatorConfiguration() finds Form translation resources'
2011-01-24 19:50:31 +00:00
);
2013-05-13 03:46:20 +01:00
$ref = new \ReflectionClass ( 'Symfony\Component\Security\Core\SecurityContext' );
2012-07-15 14:56:45 +01:00
$this -> assertContains (
2013-09-18 08:24:07 +01:00
strtr ( dirname ( $ref -> getFileName ()) . '/Resources/translations/security.en.xlf' , '/' , DIRECTORY_SEPARATOR ),
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 ();
2013-03-11 21:59:35 +00:00
$this -> assertEquals ( array ( '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
2013-05-13 03:46:20 +01:00
$ref = new \ReflectionClass ( 'Symfony\Component\Form\Form' );
2014-03-18 16:36:12 +00:00
$xmlMappings = array ( realpath ( dirname ( $ref -> getFileName ()) . '/Resources/config/validation.xml' ));
$calls = $container -> getDefinition ( 'validator.builder' ) -> getMethodCalls ();
2014-08-06 12:56:27 +01:00
$this -> assertCount ( 7 , $calls );
2014-03-18 17:23:55 +00:00
$this -> assertSame ( 'setConstraintValidatorFactory' , $calls [ 0 ][ 0 ]);
2014-03-18 16:36:12 +00:00
$this -> assertEquals ( array ( new Reference ( 'validator.validator_factory' )), $calls [ 0 ][ 1 ]);
$this -> assertSame ( 'setTranslator' , $calls [ 1 ][ 0 ]);
$this -> assertEquals ( array ( new Reference ( 'translator' )), $calls [ 1 ][ 1 ]);
$this -> assertSame ( 'setTranslationDomain' , $calls [ 2 ][ 0 ]);
$this -> assertSame ( array ( '%validator.translation_domain%' ), $calls [ 2 ][ 1 ]);
$this -> assertSame ( 'addXmlMappings' , $calls [ 3 ][ 0 ]);
$this -> assertSame ( array ( $xmlMappings ), $calls [ 3 ][ 1 ]);
$this -> assertSame ( 'addMethodMapping' , $calls [ 4 ][ 0 ]);
2014-06-03 15:40:53 +01:00
$this -> assertSame ( array ( 'loadValidatorMetadata' ), $calls [ 4 ][ 1 ]);
2014-03-18 17:23:55 +00:00
$this -> assertSame ( 'setMetadataCache' , $calls [ 5 ][ 0 ]);
2014-03-18 16:36:12 +00:00
$this -> assertEquals ( array ( new Reference ( 'validator.mapping.cache.apc' )), $calls [ 5 ][ 1 ]);
2014-08-06 12:56:27 +01:00
$this -> assertSame ( 'setApiVersion' , $calls [ 6 ][ 0 ]);
if ( version_compare ( PHP_VERSION , '5.3.9' , '<' )) {
$this -> assertEquals ( array ( Validation :: API_VERSION_2_4 ), $calls [ 6 ][ 1 ]);
} else {
$this -> assertEquals ( array ( Validation :: API_VERSION_2_5_BC ), $calls [ 6 ][ 1 ]);
}
2014-04-03 17:10:06 +01:00
}
public function testFullyConfiguredValidationService ()
{
if ( ! extension_loaded ( 'apc' )) {
$this -> markTestSkipped ( 'The apc extension is not available.' );
}
$container = $this -> createContainerFromFile ( 'full' );
$this -> assertInstanceOf ( 'Symfony\Component\Validator\ValidatorInterface' , $container -> get ( 'validator' ));
}
public function testValidationService ()
{
$container = $this -> createContainerFromFile ( 'validation_annotations' );
2014-03-18 17:23:55 +00:00
$this -> assertInstanceOf ( 'Symfony\Component\Validator\ValidatorInterface' , $container -> get ( 'validator' ));
2010-06-24 09:40:05 +01:00
}
2011-05-03 13:36:42 +01:00
public function testAnnotations ()
{
$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' );
2014-03-18 16:36:12 +00:00
$calls = $container -> getDefinition ( 'validator.builder' ) -> getMethodCalls ();
2014-08-06 12:56:27 +01:00
$this -> assertCount ( 7 , $calls );
2014-04-02 17:29:34 +01:00
$this -> assertSame ( 'enableAnnotationMapping' , $calls [ 4 ][ 0 ]);
2014-03-18 16:36:12 +00:00
$this -> assertEquals ( array ( new Reference ( 'annotation_reader' )), $calls [ 4 ][ 1 ]);
$this -> assertSame ( 'addMethodMapping' , $calls [ 5 ][ 0 ]);
2014-06-03 15:40:53 +01:00
$this -> assertSame ( array ( 'loadValidatorMetadata' ), $calls [ 5 ][ 1 ]);
2014-03-18 16:36:12 +00:00
// no cache this time
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' ),
));
2014-03-18 16:36:12 +00:00
$calls = $container -> getDefinition ( 'validator.builder' ) -> getMethodCalls ();
2014-08-06 12:56:27 +01:00
$this -> assertCount ( 8 , $calls );
2014-03-18 16:36:12 +00:00
$this -> assertSame ( 'addXmlMappings' , $calls [ 3 ][ 0 ]);
$this -> assertSame ( 'addYamlMappings' , $calls [ 4 ][ 0 ]);
2014-04-02 17:29:34 +01:00
$this -> assertSame ( 'enableAnnotationMapping' , $calls [ 5 ][ 0 ]);
2014-03-18 16:36:12 +00:00
$this -> assertSame ( 'addMethodMapping' , $calls [ 6 ][ 0 ]);
2014-06-03 15:40:53 +01:00
$this -> assertSame ( array ( 'loadValidatorMetadata' ), $calls [ 6 ][ 1 ]);
2014-03-18 16:36:12 +00:00
$xmlMappings = $calls [ 3 ][ 1 ][ 0 ];
$this -> assertCount ( 2 , $xmlMappings );
$this -> assertStringEndsWith ( 'Component' . DIRECTORY_SEPARATOR . 'Form/Resources/config/validation.xml' , $xmlMappings [ 0 ]);
$this -> assertStringEndsWith ( 'TestBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'validation.xml' , $xmlMappings [ 1 ]);
$yamlMappings = $calls [ 4 ][ 1 ][ 0 ];
$this -> assertCount ( 1 , $yamlMappings );
$this -> assertStringEndsWith ( 'TestBundle' . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'validation.yml' , $yamlMappings [ 0 ]);
}
public function testValidationNoStaticMethod ()
{
$container = $this -> createContainerFromFile ( 'validation_no_static_method' );
$calls = $container -> getDefinition ( 'validator.builder' ) -> getMethodCalls ();
2014-08-06 12:56:27 +01:00
$this -> assertCount ( 5 , $calls );
2014-03-18 16:36:12 +00:00
$this -> assertSame ( 'addXmlMappings' , $calls [ 3 ][ 0 ]);
// no cache, no annotations, no static methods
}
2014-08-06 12:56:27 +01:00
public function testValidation2Dot4Api ()
2014-03-18 16:36:12 +00:00
{
$container = $this -> createContainerFromFile ( 'validation_2_4_api' );
$calls = $container -> getDefinition ( 'validator.builder' ) -> getMethodCalls ();
2011-04-11 22:26:11 +01:00
2014-03-18 16:36:12 +00:00
$this -> assertCount ( 6 , $calls );
$this -> assertSame ( 'addXmlMappings' , $calls [ 3 ][ 0 ]);
$this -> assertSame ( 'addMethodMapping' , $calls [ 4 ][ 0 ]);
2014-06-03 15:40:53 +01:00
$this -> assertSame ( array ( 'loadValidatorMetadata' ), $calls [ 4 ][ 1 ]);
2014-03-18 16:36:12 +00:00
$this -> assertSame ( 'setApiVersion' , $calls [ 5 ][ 0 ]);
$this -> assertSame ( array ( Validation :: API_VERSION_2_4 ), $calls [ 5 ][ 1 ]);
2014-10-06 14:29:21 +01:00
$this -> assertSame ( 'Symfony\Component\Validator\ValidatorInterface' , $container -> getParameter ( 'validator.class' ));
2014-03-18 16:36:12 +00:00
// no cache, no annotations
2011-04-11 22:26:11 +01:00
}
2014-08-06 12:56:27 +01:00
public function testValidation2Dot5Api ()
{
$container = $this -> createContainerFromFile ( 'validation_2_5_api' );
$calls = $container -> getDefinition ( 'validator.builder' ) -> getMethodCalls ();
$this -> assertCount ( 6 , $calls );
$this -> assertSame ( 'addXmlMappings' , $calls [ 3 ][ 0 ]);
$this -> assertSame ( 'addMethodMapping' , $calls [ 4 ][ 0 ]);
$this -> assertSame ( array ( 'loadValidatorMetadata' ), $calls [ 4 ][ 1 ]);
$this -> assertSame ( 'setApiVersion' , $calls [ 5 ][ 0 ]);
$this -> assertSame ( array ( Validation :: API_VERSION_2_5 ), $calls [ 5 ][ 1 ]);
2014-10-06 14:29:21 +01:00
$this -> assertSame ( 'Symfony\Component\Validator\Validator\ValidatorInterface' , $container -> getParameter ( 'validator.class' ));
2014-08-06 12:56:27 +01:00
// no cache, no annotations
}
public function testValidation2Dot5BcApi ()
{
$container = $this -> createContainerFromFile ( 'validation_2_5_bc_api' );
$calls = $container -> getDefinition ( 'validator.builder' ) -> getMethodCalls ();
$this -> assertCount ( 6 , $calls );
$this -> assertSame ( 'addXmlMappings' , $calls [ 3 ][ 0 ]);
$this -> assertSame ( 'addMethodMapping' , $calls [ 4 ][ 0 ]);
$this -> assertSame ( array ( 'loadValidatorMetadata' ), $calls [ 4 ][ 1 ]);
$this -> assertSame ( 'setApiVersion' , $calls [ 5 ][ 0 ]);
$this -> assertSame ( array ( Validation :: API_VERSION_2_5_BC ), $calls [ 5 ][ 1 ]);
2014-10-06 14:29:21 +01:00
$this -> assertSame ( 'Symfony\Component\Validator\ValidatorInterface' , $container -> getParameter ( 'validator.class' ));
2014-08-06 12:56:27 +01:00
// no cache, no annotations
}
public function testValidationImplicitApi ()
{
$container = $this -> createContainerFromFile ( 'validation_implicit_api' );
$calls = $container -> getDefinition ( 'validator.builder' ) -> getMethodCalls ();
$this -> assertCount ( 6 , $calls );
$this -> assertSame ( 'addXmlMappings' , $calls [ 3 ][ 0 ]);
$this -> assertSame ( 'addMethodMapping' , $calls [ 4 ][ 0 ]);
$this -> assertSame ( array ( 'loadValidatorMetadata' ), $calls [ 4 ][ 1 ]);
$this -> assertSame ( 'setApiVersion' , $calls [ 5 ][ 0 ]);
// no cache, no annotations
if ( version_compare ( PHP_VERSION , '5.3.9' , '<' )) {
$this -> assertSame ( array ( Validation :: API_VERSION_2_4 ), $calls [ 5 ][ 1 ]);
} else {
$this -> assertSame ( array ( Validation :: API_VERSION_2_5_BC ), $calls [ 5 ][ 1 ]);
}
}
/**
* This feature is equivalent to the implicit api , only that the " api "
* key is explicitly set to " auto " .
*/
public function testValidationAutoApi ()
{
$container = $this -> createContainerFromFile ( 'validation_auto_api' );
$calls = $container -> getDefinition ( 'validator.builder' ) -> getMethodCalls ();
$this -> assertCount ( 6 , $calls );
$this -> assertSame ( 'addXmlMappings' , $calls [ 3 ][ 0 ]);
$this -> assertSame ( 'addMethodMapping' , $calls [ 4 ][ 0 ]);
$this -> assertSame ( array ( 'loadValidatorMetadata' ), $calls [ 4 ][ 1 ]);
$this -> assertSame ( 'setApiVersion' , $calls [ 5 ][ 0 ]);
// no cache, no annotations
if ( version_compare ( PHP_VERSION , '5.3.9' , '<' )) {
$this -> assertSame ( array ( Validation :: API_VERSION_2_4 ), $calls [ 5 ][ 1 ]);
} else {
$this -> assertSame ( array ( Validation :: API_VERSION_2_5_BC ), $calls [ 5 ][ 1 ]);
}
}
2013-10-09 09:42:58 +01:00
public function testFormsCanBeEnabledWithoutCsrfProtection ()
{
$container = $this -> createContainerFromFile ( 'form_no_csrf' );
$this -> assertFalse ( $container -> getParameter ( 'form.type_extension.csrf.enabled' ));
}
public function testFormCsrfFieldNameCanBeSetUnderCsrfSettings ()
{
$container = $this -> createContainerFromFile ( 'form_csrf_sets_field_name' );
$this -> assertTrue ( $container -> getParameter ( 'form.type_extension.csrf.enabled' ));
$this -> assertEquals ( '_custom' , $container -> getParameter ( 'form.type_extension.csrf.field_name' ));
}
public function testFormCsrfFieldNameUnderFormSettingsTakesPrecedence ()
{
$container = $this -> createContainerFromFile ( 'form_csrf_under_form_sets_field_name' );
$this -> assertTrue ( $container -> getParameter ( 'form.type_extension.csrf.enabled' ));
$this -> assertEquals ( '_custom_form' , $container -> getParameter ( 'form.type_extension.csrf.field_name' ));
}
2011-04-11 22:26:11 +01:00
protected function createContainer ( array $data = array ())
{
return new ContainerBuilder ( new ParameterBag ( array_merge ( array (
2014-10-22 19:27:13 +01:00
'kernel.bundles' => array ( 'FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle' ),
'kernel.cache_dir' => __DIR__ ,
'kernel.debug' => false ,
2013-09-12 05:41:55 +01:00
'kernel.environment' => 'test' ,
2014-10-22 19:27:13 +01:00
'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
}