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