2013-01-20 18:39:49 +00:00
|
|
|
<?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\Compiler;
|
|
|
|
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for the SerializerPass class
|
2013-07-01 13:24:43 +01:00
|
|
|
*
|
2013-01-20 18:39:49 +00:00
|
|
|
* @author Javier Lopez <f12loalf@gmail.com>
|
|
|
|
*/
|
|
|
|
class SerializerPassTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testThrowExceptionWhenNoNormalizers()
|
|
|
|
{
|
2014-09-29 16:33:55 +01:00
|
|
|
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder', array('hasDefinition', 'findTaggedServiceIds'));
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
$container->expects($this->once())
|
|
|
|
->method('hasDefinition')
|
|
|
|
->with('serializer')
|
|
|
|
->will($this->returnValue(true));
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
$container->expects($this->once())
|
|
|
|
->method('findTaggedServiceIds')
|
|
|
|
->with('serializer.normalizer')
|
|
|
|
->will($this->returnValue(array()));
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
$this->setExpectedException('RuntimeException');
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
$serializerPass = new SerializerPass();
|
|
|
|
$serializerPass->process($container);
|
|
|
|
}
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
public function testThrowExceptionWhenNoEncoders()
|
|
|
|
{
|
|
|
|
$definition = $this->getMock('Symfony\Component\DependencyInjection\Definition');
|
2014-09-29 16:33:55 +01:00
|
|
|
$container = $this->getMock(
|
|
|
|
'Symfony\Component\DependencyInjection\ContainerBuilder',
|
|
|
|
array('hasDefinition', 'findTaggedServiceIds', 'getDefinition')
|
|
|
|
);
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
$container->expects($this->once())
|
|
|
|
->method('hasDefinition')
|
|
|
|
->with('serializer')
|
|
|
|
->will($this->returnValue(true));
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
$container->expects($this->any())
|
|
|
|
->method('findTaggedServiceIds')
|
|
|
|
->will($this->onConsecutiveCalls(
|
|
|
|
array('n' => array('serializer.normalizer')),
|
|
|
|
array()
|
|
|
|
));
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
$container->expects($this->once())
|
|
|
|
->method('getDefinition')
|
|
|
|
->will($this->returnValue($definition));
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
$this->setExpectedException('RuntimeException');
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
$serializerPass = new SerializerPass();
|
|
|
|
$serializerPass->process($container);
|
|
|
|
}
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
public function testServicesAreOrderedAccordingToPriority()
|
|
|
|
{
|
2014-09-21 19:53:12 +01:00
|
|
|
$services = array(
|
2013-01-20 18:39:49 +00:00
|
|
|
'n3' => array('tag' => array()),
|
|
|
|
'n1' => array('tag' => array('priority' => 200)),
|
2014-09-21 19:53:12 +01:00
|
|
|
'n2' => array('tag' => array('priority' => 100)),
|
2013-01-20 18:39:49 +00:00
|
|
|
);
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2014-09-21 19:53:12 +01:00
|
|
|
$expected = array(
|
2013-01-20 18:39:49 +00:00
|
|
|
new Reference('n1'),
|
|
|
|
new Reference('n2'),
|
2014-09-21 19:53:12 +01:00
|
|
|
new Reference('n3'),
|
2013-01-20 18:39:49 +00:00
|
|
|
);
|
|
|
|
|
2014-09-29 16:33:55 +01:00
|
|
|
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder', array('findTaggedServiceIds'));
|
2013-01-20 18:39:49 +00:00
|
|
|
|
|
|
|
$container->expects($this->atLeastOnce())
|
|
|
|
->method('findTaggedServiceIds')
|
|
|
|
->will($this->returnValue($services));
|
|
|
|
|
|
|
|
$serializerPass = new SerializerPass();
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
$method = new \ReflectionMethod(
|
2013-07-01 13:24:43 +01:00
|
|
|
'Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass',
|
2013-01-20 18:39:49 +00:00
|
|
|
'findAndSortTaggedServices'
|
|
|
|
);
|
2014-07-27 09:25:12 +01:00
|
|
|
$method->setAccessible(true);
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
$actual = $method->invoke($serializerPass, 'tag', $container);
|
2013-07-01 13:24:43 +01:00
|
|
|
|
2013-01-20 18:39:49 +00:00
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
}
|