|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\DependencyInjection\Tests\Compiler; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; |
| 15 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 16 | +use Symfony\Component\DependencyInjection\Reference; |
| 17 | + |
| 18 | +class PriorityTaggedServiceTraitTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + public function testThatCacheWarmersAreProcessedInPriorityOrder() |
| 21 | + { |
| 22 | + $services = array( |
| 23 | + 'my_service1' => array('my_custom_tag' => array('priority' => 100)), |
| 24 | + 'my_service2' => array('my_custom_tag' => array('priority' => 200)), |
| 25 | + 'my_service3' => array('my_custom_tag' => array('priority' => -501)), |
| 26 | + 'my_service4' => array('my_custom_tag' => array()), |
| 27 | + 'my_service5' => array('my_custom_tag' => array('priority' => -1)), |
| 28 | + 'my_service6' => array('my_custom_tag' => array('priority' => -500)), |
| 29 | + 'my_service7' => array('my_custom_tag' => array('priority' => -499)), |
| 30 | + 'my_service8' => array('my_custom_tag' => array('priority' => 1)), |
| 31 | + 'my_service9' => array('my_custom_tag' => array('priority' => -2)), |
| 32 | + 'my_service10' => array('my_custom_tag' => array('priority' => -1000)), |
| 33 | + 'my_service11' => array('my_custom_tag' => array('priority' => -1001)), |
| 34 | + 'my_service12' => array('my_custom_tag' => array('priority' => -1002)), |
| 35 | + 'my_service13' => array('my_custom_tag' => array('priority' => -1003)), |
| 36 | + ); |
| 37 | + |
| 38 | + $container = new ContainerBuilder(); |
| 39 | + |
| 40 | + foreach ($services as $id => $tags) { |
| 41 | + $definition = $container->register($id); |
| 42 | + |
| 43 | + foreach ($tags as $name => $attributes) { |
| 44 | + $definition->addTag($name, $attributes); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + $expected = array( |
| 49 | + new Reference('my_service2'), |
| 50 | + new Reference('my_service1'), |
| 51 | + new Reference('my_service8'), |
| 52 | + new Reference('my_service4'), |
| 53 | + new Reference('my_service5'), |
| 54 | + new Reference('my_service9'), |
| 55 | + new Reference('my_service7'), |
| 56 | + new Reference('my_service6'), |
| 57 | + new Reference('my_service3'), |
| 58 | + new Reference('my_service10'), |
| 59 | + new Reference('my_service11'), |
| 60 | + new Reference('my_service12'), |
| 61 | + new Reference('my_service13'), |
| 62 | + ); |
| 63 | + |
| 64 | + $priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation(); |
| 65 | + |
| 66 | + $this->assertEquals($expected, $priorityTaggedServiceTraitImplementation->test('my_custom_tag', $container)); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +class PriorityTaggedServiceTraitImplementation |
| 71 | +{ |
| 72 | + use PriorityTaggedServiceTrait; |
| 73 | + |
| 74 | + public function test($tagName, ContainerBuilder $container) |
| 75 | + { |
| 76 | + return $this->findAndSortTaggedServices($tagName, $container); |
| 77 | + } |
| 78 | +} |
0 commit comments