This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
Fabien Potencier d2e9f14597 merged branch schmittjoh/routerFix (PR #1720)
Commits
-------

9bcce9f fix tests
fc4787a fix non-extensible router

Discussion
----------

Router fix

Right now, the router is hard to overwrite (you need always a compiler pass). This commit fixes this.

---------------------------------------------------------------------------

by fabpot at 2011/07/18 01:15:36 -0700

Why do you need a complier pass to override the router?

---------------------------------------------------------------------------

by schmittjoh at 2011/07/18 01:47:47 -0700

How would you suggest to overwrite it?

Basically, I want to do something like this:

```yml
services:
    router:
         parent: router.default
         class: MyClass
         calls:
             - [moreDeps, []]
```

---------------------------------------------------------------------------

by Seldaek at 2011/07/18 05:07:19 -0700

Then maybe we should somehow support redefining services with the same name while keeping the old one as parent, otherwise we need this foo.default for every service out there?

---------------------------------------------------------------------------

by fabpot at 2011/07/18 06:30:34 -0700

as @Seldeak said, why do that for the router and not all services?

---------------------------------------------------------------------------

by schmittjoh at 2011/07/18 06:38:39 -0700

I have designed the SecurityBundle this way where extension is encouraged.

---------------------------------------------------------------------------

by schmittjoh at 2011/07/18 11:15:57 -0700

I should add that this is mainly a problem for services where you still want to use the semantic configuration that is provided by the bundle. For services which are not configured by the extension, this is not so much of an issue.

Anyway, if you don't want to merge it, just close the PR. I have no problem with using a compiler pass.

---------------------------------------------------------------------------

by fabpot at 2011/07/18 11:55:11 -0700

We already have such a case with translator and translator.real. I will review the existing services to see where it makes sense to implement the same strategy.

---------------------------------------------------------------------------

by Seldaek at 2011/07/18 12:20:55 -0700

I guess you'd do it anyway, but we should pick a winner between .real and .default

---------------------------------------------------------------------------

by lsmith77 at 2011/07/18 12:26:52 -0700

I would prefer ".default" as ".real" always confused me.
2011-07-19 12:06:28 +02:00

256 lines
13 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
abstract class FrameworkExtensionTest extends TestCase
{
abstract protected function loadFromFile(ContainerBuilder $container, $file);
public function testCsrfProtection()
{
$container = $this->createContainerFromFile('full');
$def = $container->getDefinition('form.type_extension.csrf');
$this->assertTrue($container->getParameter('form.type_extension.csrf.enabled'));
$this->assertEquals('%form.type_extension.csrf.enabled%', $def->getArgument(0));
$this->assertEquals('_csrf', $container->getParameter('form.type_extension.csrf.field_name'));
$this->assertEquals('%form.type_extension.csrf.field_name%', $def->getArgument(1));
$this->assertEquals('s3cr3t', $container->getParameterBag()->resolveValue($container->findDefinition('form.csrf_provider')->getArgument(1)));
}
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');
$this->assertTrue($container->getParameter('profiler_listener.only_exceptions'));
$this->assertEquals('%profiler_listener.only_exceptions%', $container->getDefinition('profiler_listener')->getArgument(2));
}
public function testRouter()
{
$container = $this->createContainerFromFile('full');
$this->assertTrue($container->has('router'), '->registerRouterConfiguration() loads routing.xml');
$arguments = $container->findDefinition('router')->getArguments();
$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');
$this->assertEquals('xml', $arguments[2]['resource_type'], '->registerRouterConfiguration() sets routing resource type');
}
/**
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testRouterRequiresResourceOption()
{
$container = $this->createContainer();
$loader = new FrameworkExtension();
$loader->load(array(array('router' => true)), $container);
}
public function testSession()
{
$container = $this->createContainerFromFile('full');
$this->assertTrue($container->hasDefinition('session'), '->registerSessionConfiguration() loads session.xml');
$this->assertEquals('fr', $container->getParameter('session.default_locale'));
$this->assertEquals('%session.default_locale%', $container->getDefinition('session')->getArgument(1));
$this->assertTrue($container->getDefinition('session_listener')->getArgument(1));
$this->assertEquals('session.storage.native', (string) $container->getAlias('session.storage'));
$options = $container->getParameter('session.storage.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 testTemplating()
{
$container = $this->createContainerFromFile('full');
$this->assertTrue($container->hasDefinition('templating.name_parser'), '->registerTemplatingConfiguration() loads templating.xml');
// 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]);
$this->assertNull($arguments[2]);
$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());
$this->assertEquals('templating.engine.delegating', (string) $container->getAlias('templating'), '->registerTemplatingConfiguration() configures delegating loader if multiple engines are provided');
$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');
$this->assertEquals('%templating.loader.cache.path%', $container->getDefinition('templating.loader.cache')->getArgument(1));
$this->assertEquals('/path/to/cache', $container->getParameter('templating.loader.cache.path'));
$this->assertEquals(array('php', 'twig'), $container->getParameter('templating.engines'), '->registerTemplatingConfiguration() sets a templating.engines parameter');
$this->assertEquals(array('FrameworkBundle:Form', 'theme1', 'theme2'), $container->getParameter('templating.helper.form.resources'), '->registerTemplatingConfiguration() registers the theme and adds the base theme');
}
public function testTranslator()
{
$container = $this->createContainerFromFile('full');
$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');
$resources = array();
foreach ($container->getDefinition('translator.default')->getMethodCalls() as $call) {
if ('addResource' == $call[0]) {
$resources[] = $call[1];
}
}
$this->assertContains(
realpath(__DIR__.'/../../Resources/translations/validators.fr.xliff'),
array_map(function($resource) use ($resources) { return realpath($resource[1]); }, $resources),
'->registerTranslatorConfiguration() finds FrameworkExtension translation resources'
);
$calls = $container->getDefinition('translator.default')->getMethodCalls();
$this->assertEquals('fr', $calls[0][1][0]);
}
/**
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testTemplatingRequiresAtLeastOneEngine()
{
$container = $this->createContainer();
$loader = new FrameworkExtension();
$loader->load(array(array('templating' => null)), $container);
}
public function testValidation()
{
$container = $this->createContainerFromFile('full');
$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');
$xmlFiles = $container->getParameter('validator.mapping.loader.xml_files_loader.mapping_files');
$this->assertContains(
realpath(__DIR__.'/../../../../Component/Form/Resources/config/validation.xml'),
array_map('realpath', $xmlFiles),
'->registerValidationConfiguration() adds Form validation.xml to XML loader'
);
}
public function testAnnotations()
{
if (!class_exists('Doctrine\\Common\\Version')) {
$this->markTestSkipped('Doctrine is not available.');
}
$container = $this->createContainerFromFile('full');
$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'));
}
public function testFileLinkFormat()
{
$container = $this->createContainerFromFile('full');
$this->assertEquals('file%link%format', $container->getParameter('templating.helper.code.file_link_format'));
}
public function testValidationAnnotations()
{
$container = $this->createContainerFromFile('validation_annotations');
$this->assertTrue($container->hasDefinition('validator.mapping.loader.annotation_loader'), '->registerValidationConfiguration() defines the annotation loader');
$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.');
}
public function testValidationPaths()
{
require_once __DIR__."/Fixtures/TestBundle/TestBundle.php";
$container = $this->createContainerFromFile('validation_annotations', array(
'kernel.bundles' => array('TestBundle' => 'Symfony\Bundle\FrameworkBundle\Tests\TestBundle'),
));
$yamlArgs = $container->getParameter('validator.mapping.loader.yaml_files_loader.mapping_files');
$this->assertEquals(1, count($yamlArgs));
$this->assertStringEndsWith('TestBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'validation.yml', $yamlArgs[0]);
$xmlArgs = $container->getParameter('validator.mapping.loader.xml_files_loader.mapping_files');
$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]);
}
protected function createContainer(array $data = array())
{
return new ContainerBuilder(new ParameterBag(array_merge(array(
'kernel.bundles' => array('FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'),
'kernel.cache_dir' => __DIR__,
'kernel.compiled_classes' => array(),
'kernel.debug' => false,
'kernel.environment' => 'test',
'kernel.name' => 'kernel',
'kernel.root_dir' => __DIR__,
), $data)));
}
protected function createContainerFromFile($file, $data = array())
{
$container = $this->createContainer($data);
$container->registerExtension(new FrameworkExtension());
$this->loadFromFile($container, $file);
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->compile();
return $container;
}
}