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/Compiler/SerializerPassTest.php

96 lines
3.5 KiB
PHP
Raw Normal View History

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;
2017-02-08 07:24:27 +00:00
use PHPUnit\Framework\TestCase;
2017-04-12 10:13:52 +01:00
use Symfony\Component\DependencyInjection\ContainerBuilder;
2013-01-20 18:39:49 +00:00
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass;
/**
2014-12-21 17:00:50 +00:00
* Tests for the SerializerPass class.
2013-07-01 13:24:43 +01:00
*
* @group legacy
*
2013-01-20 18:39:49 +00:00
* @author Javier Lopez <f12loalf@gmail.com>
*/
2017-02-08 07:24:27 +00:00
class SerializerPassTest extends TestCase
2013-01-20 18:39:49 +00:00
{
public function testThrowExceptionWhenNoNormalizers()
{
2016-12-19 09:02:29 +00:00
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds'))->getMock();
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
$this->{method_exists($this, $_ = 'expectException') ? $_ : '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()
{
2016-12-19 09:02:29 +00:00
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
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
2017-04-12 10:13:52 +01:00
$container->expects($this->any())
2013-01-20 18:39:49 +00:00
->method('getDefinition')
->will($this->returnValue($definition));
2013-07-01 13:24:43 +01:00
$this->{method_exists($this, $_ = 'expectException') ? $_ : '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()
{
2017-04-12 10:13:52 +01:00
$container = new ContainerBuilder();
2013-01-20 18:39:49 +00:00
2017-04-12 10:13:52 +01:00
$definition = $container->register('serializer')->setArguments(array(null, null));
$container->register('n2')->addTag('serializer.normalizer', array('priority' => 100))->addTag('serializer.encoder', array('priority' => 100));
$container->register('n1')->addTag('serializer.normalizer', array('priority' => 200))->addTag('serializer.encoder', array('priority' => 200));
$container->register('n3')->addTag('serializer.normalizer')->addTag('serializer.encoder');
2013-01-20 18:39:49 +00:00
$serializerPass = new SerializerPass();
2017-04-12 10:13:52 +01:00
$serializerPass->process($container);
2013-07-01 13:24:43 +01:00
2017-04-12 10:13:52 +01:00
$expected = array(
new Reference('n1'),
new Reference('n2'),
new Reference('n3'),
2013-01-20 18:39:49 +00:00
);
2017-04-12 10:13:52 +01:00
$this->assertEquals($expected, $definition->getArgument(0));
$this->assertEquals($expected, $definition->getArgument(1));
2013-01-20 18:39:49 +00:00
}
}