|
| 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\Bundle\WebProfilerBundle\Tests\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +use Symfony\Bundle\WebProfilerBundle\DependencyInjection\Compiler\ProfilerPass; |
| 15 | +use Symfony\Component\DependencyInjection\Definition; |
| 16 | + |
| 17 | +class ProfilerPassTest extends \PHPUnit_Framework_TestCase |
| 18 | +{ |
| 19 | + private $profilerDefinition; |
| 20 | + |
| 21 | + protected function setUp() |
| 22 | + { |
| 23 | + $this->profilerDefinition = new Definition('ProfilerClass'); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Tests that collectors that specify a template but no "id" will throw |
| 28 | + * an exception (both are needed if the template is specified). |
| 29 | + * |
| 30 | + * Thus, a fully-valid tag looks something like this: |
| 31 | + * |
| 32 | + * <tag name="data_collector" template="YourBundle:Collector:templatename" id="your_collector_name" /> |
| 33 | + */ |
| 34 | + public function testTemplateNoIdThrowsException() |
| 35 | + { |
| 36 | + // one service, with a template key, but no id |
| 37 | + $services = array( |
| 38 | + 'my_collector_service' => array(0 => array('template' => 'foo')), |
| 39 | + ); |
| 40 | + |
| 41 | + $builder = $this->createContainerMock($services); |
| 42 | + |
| 43 | + $this->setExpectedException('InvalidArgumentException'); |
| 44 | + |
| 45 | + $profilerPass = new ProfilerPass(); |
| 46 | + $profilerPass->process($builder); |
| 47 | + } |
| 48 | + |
| 49 | + public function testValidCollector() |
| 50 | + { |
| 51 | + // one service, with a template key, but no id |
| 52 | + $services = array( |
| 53 | + 'my_collector_service' => array(0 => array('template' => 'foo', 'id' => 'my_collector')), |
| 54 | + ); |
| 55 | + |
| 56 | + $container = $this->createContainerMock($services); |
| 57 | + |
| 58 | + // fake the getDefinition() to return a Profiler definition |
| 59 | + $container->expects($this->atLeastOnce()) |
| 60 | + ->method('getDefinition'); |
| 61 | + |
| 62 | + // assert that the data_collector.templates parameter should be set |
| 63 | + $container->expects($this->once()) |
| 64 | + ->method('setParameter') |
| 65 | + ->with('data_collector.templates', array('my_collector_service' => array('my_collector', 'foo'))); |
| 66 | + |
| 67 | + $profilerPass = new ProfilerPass(); |
| 68 | + $profilerPass->process($container); |
| 69 | + |
| 70 | + // grab the method calls off of the "profiler" definition |
| 71 | + $methodCalls = $this->profilerDefinition->getMethodCalls(); |
| 72 | + $this->assertCount(1, $methodCalls); |
| 73 | + $this->assertEquals('add', $methodCalls[0][0]); // grab the method part of the first call |
| 74 | + } |
| 75 | + |
| 76 | + private function createContainerMock($services) |
| 77 | + { |
| 78 | + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'getDefinition', 'findTaggedServiceIds', 'setParameter'))->getMock(); |
| 79 | + $container->expects($this->any()) |
| 80 | + ->method('hasDefinition') |
| 81 | + ->with($this->equalTo('profiler')) |
| 82 | + ->will($this->returnValue(true)); |
| 83 | + $container->expects($this->any()) |
| 84 | + ->method('getDefinition') |
| 85 | + ->will($this->returnValue($this->profilerDefinition)); |
| 86 | + $container->expects($this->atLeastOnce()) |
| 87 | + ->method('findTaggedServiceIds') |
| 88 | + ->will($this->returnValue($services)); |
| 89 | + |
| 90 | + return $container; |
| 91 | + } |
| 92 | +} |
0 commit comments