Relax some mocks

This commit is contained in:
Nicolas Grekas 2017-04-12 11:13:52 +02:00
parent 766ae29f85
commit 61be73394e
4 changed files with 63 additions and 98 deletions

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass;
@ -19,37 +20,22 @@ class AddCacheWarmerPassTest extends TestCase
{ {
public function testThatCacheWarmersAreProcessedInPriorityOrder() public function testThatCacheWarmersAreProcessedInPriorityOrder()
{ {
$services = array( $container = new ContainerBuilder();
'my_cache_warmer_service1' => array(0 => array('priority' => 100)),
'my_cache_warmer_service2' => array(0 => array('priority' => 200)),
'my_cache_warmer_service3' => array(0 => array()),
);
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); $definition = $container->register('cache_warmer')->addArgument(null);
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds', 'getDefinition', 'hasDefinition'))->getMock(); $container->register('my_cache_warmer_service1')->addTag('kernel.cache_warmer', array('priority' => 100));
$container->register('my_cache_warmer_service2')->addTag('kernel.cache_warmer', array('priority' => 200));
$container->expects($this->atLeastOnce()) $container->register('my_cache_warmer_service3')->addTag('kernel.cache_warmer');
->method('findTaggedServiceIds')
->will($this->returnValue($services));
$container->expects($this->atLeastOnce())
->method('getDefinition')
->with('cache_warmer')
->will($this->returnValue($definition));
$container->expects($this->atLeastOnce())
->method('hasDefinition')
->with('cache_warmer')
->will($this->returnValue(true));
$definition->expects($this->once())
->method('replaceArgument')
->with(0, array(
new Reference('my_cache_warmer_service2'),
new Reference('my_cache_warmer_service1'),
new Reference('my_cache_warmer_service3'),
));
$addCacheWarmerPass = new AddCacheWarmerPass(); $addCacheWarmerPass = new AddCacheWarmerPass();
$addCacheWarmerPass->process($container); $addCacheWarmerPass->process($container);
$expected = array(
new Reference('my_cache_warmer_service2'),
new Reference('my_cache_warmer_service1'),
new Reference('my_cache_warmer_service3'),
);
$this->assertEquals($expected, $definition->getArgument(0));
} }
public function testThatCompilerPassIsIgnoredIfThereIsNoCacheWarmerDefinition() public function testThatCompilerPassIsIgnoredIfThereIsNoCacheWarmerDefinition()

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ConfigCachePass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ConfigCachePass;
@ -19,33 +20,22 @@ class ConfigCachePassTest extends TestCase
{ {
public function testThatCheckersAreProcessedInPriorityOrder() public function testThatCheckersAreProcessedInPriorityOrder()
{ {
$services = array( $container = new ContainerBuilder();
'checker_2' => array(0 => array('priority' => 100)),
'checker_1' => array(0 => array('priority' => 200)),
'checker_3' => array(0 => array()),
);
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock(); $definition = $container->register('config_cache_factory')->addArgument(null);
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds', 'getDefinition', 'hasDefinition'))->getMock(); $container->register('checker_2')->addTag('config_cache.resource_checker', array('priority' => 100));
$container->register('checker_1')->addTag('config_cache.resource_checker', array('priority' => 200));
$container->expects($this->atLeastOnce()) $container->register('checker_3')->addTag('config_cache.resource_checker');
->method('findTaggedServiceIds')
->will($this->returnValue($services));
$container->expects($this->atLeastOnce())
->method('getDefinition')
->with('config_cache_factory')
->will($this->returnValue($definition));
$definition->expects($this->once())
->method('replaceArgument')
->with(0, array(
new Reference('checker_1'),
new Reference('checker_2'),
new Reference('checker_3'),
));
$pass = new ConfigCachePass(); $pass = new ConfigCachePass();
$pass->process($container); $pass->process($container);
$expected = array(
new Reference('checker_1'),
new Reference('checker_2'),
new Reference('checker_3'),
);
$this->assertEquals($expected, $definition->getArgument(0));
} }
public function testThatCheckersCanBeMissing() public function testThatCheckersCanBeMissing()

View File

@ -13,42 +13,42 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
class PropertyInfoPassTest extends TestCase class PropertyInfoPassTest extends TestCase
{ {
public function testServicesAreOrderedAccordingToPriority() /**
* @dataProvider provideTags
*/
public function testServicesAreOrderedAccordingToPriority($index, $tag)
{ {
$services = array( $container = new ContainerBuilder();
'n3' => array(array()),
'n1' => array(array('priority' => 200)), $definition = $container->register('property_info')->setArguments(array(null, null, null, null));
'n2' => array(array('priority' => 100)), $container->register('n2')->addTag($tag, array('priority' => 100));
); $container->register('n1')->addTag($tag, array('priority' => 200));
$container->register('n3')->addTag($tag);
$propertyInfoPass = new PropertyInfoPass();
$propertyInfoPass->process($container);
$expected = array( $expected = array(
new Reference('n1'), new Reference('n1'),
new Reference('n2'), new Reference('n2'),
new Reference('n3'), new Reference('n3'),
); );
$this->assertEquals($expected, $definition->getArgument($index));
}
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds'))->getMock(); public function provideTags()
{
$container return array(
->expects($this->any()) array(0, 'property_info.list_extractor'),
->method('findTaggedServiceIds') array(1, 'property_info.type_extractor'),
->will($this->returnValue($services)); array(2, 'property_info.description_extractor'),
array(3, 'property_info.access_extractor'),
$propertyInfoPass = new PropertyInfoPass();
$method = new \ReflectionMethod(
'Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\PropertyInfoPass',
'findAndSortTaggedServices'
); );
$method->setAccessible(true);
$actual = $method->invoke($propertyInfoPass, 'tag', $container);
$this->assertEquals($expected, $actual);
} }
public function testReturningEmptyArrayWhenNoService() public function testReturningEmptyArrayWhenNoService()

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass;
@ -59,7 +60,7 @@ class SerializerPassTest extends TestCase
array() array()
)); ));
$container->expects($this->once()) $container->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->will($this->returnValue($definition)); ->will($this->returnValue($definition));
@ -71,34 +72,22 @@ class SerializerPassTest extends TestCase
public function testServicesAreOrderedAccordingToPriority() public function testServicesAreOrderedAccordingToPriority()
{ {
$services = array( $container = new ContainerBuilder();
'n3' => array(array()),
'n1' => array(array('priority' => 200)),
'n2' => array(array('priority' => 100)),
);
$expected = array( $definition = $container->register('serializer')->setArguments(array(null, null));
new Reference('n1'), $container->register('n2')->addTag('serializer.normalizer', array('priority' => 100))->addTag('serializer.encoder', array('priority' => 100));
new Reference('n2'), $container->register('n1')->addTag('serializer.normalizer', array('priority' => 200))->addTag('serializer.encoder', array('priority' => 200));
new Reference('n3'), $container->register('n3')->addTag('serializer.normalizer')->addTag('serializer.encoder');
);
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds'))->getMock();
$container->expects($this->any())
->method('findTaggedServiceIds')
->will($this->returnValue($services));
$serializerPass = new SerializerPass(); $serializerPass = new SerializerPass();
$serializerPass->process($container);
$method = new \ReflectionMethod( $expected = array(
'Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass', new Reference('n1'),
'findAndSortTaggedServices' new Reference('n2'),
new Reference('n3'),
); );
$method->setAccessible(true); $this->assertEquals($expected, $definition->getArgument(0));
$this->assertEquals($expected, $definition->getArgument(1));
$actual = $method->invoke($serializerPass, 'tag', $container);
$this->assertEquals($expected, $actual);
} }
} }