Version 1
[yaffs-website] / vendor / symfony / http-kernel / Tests / DependencyInjection / FragmentRendererPassTest.php
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\HttpKernel\Tests\DependencyInjection;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Reference;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpKernel\DependencyInjection\FragmentRendererPass;
18 use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
19
20 class FragmentRendererPassTest extends TestCase
21 {
22     /**
23      * @group legacy
24      */
25     public function testLegacyFragmentRedererWithoutAlias()
26     {
27         // no alias
28         $services = array(
29             'my_content_renderer' => array(array()),
30         );
31
32         $renderer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
33         $renderer
34             ->expects($this->once())
35             ->method('addMethodCall')
36             ->with('addRenderer', array(new Reference('my_content_renderer')))
37         ;
38
39         $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
40         $definition->expects($this->atLeastOnce())
41             ->method('getClass')
42             ->will($this->returnValue('Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService'));
43         $definition
44             ->expects($this->once())
45             ->method('isPublic')
46             ->will($this->returnValue(true))
47         ;
48
49         $builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
50         $builder->expects($this->any())
51             ->method('hasDefinition')
52             ->will($this->returnValue(true));
53
54         // We don't test kernel.fragment_renderer here
55         $builder->expects($this->atLeastOnce())
56             ->method('findTaggedServiceIds')
57             ->will($this->returnValue($services));
58
59         $builder->expects($this->atLeastOnce())
60             ->method('getDefinition')
61             ->will($this->onConsecutiveCalls($renderer, $definition));
62
63         $pass = new FragmentRendererPass();
64         $pass->process($builder);
65     }
66
67     /**
68      * Tests that content rendering not implementing FragmentRendererInterface
69      * trigger an exception.
70      *
71      * @expectedException \InvalidArgumentException
72      */
73     public function testContentRendererWithoutInterface()
74     {
75         // one service, not implementing any interface
76         $services = array(
77             'my_content_renderer' => array(array('alias' => 'foo')),
78         );
79
80         $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
81
82         $builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
83         $builder->expects($this->any())
84             ->method('hasDefinition')
85             ->will($this->returnValue(true));
86
87         // We don't test kernel.fragment_renderer here
88         $builder->expects($this->atLeastOnce())
89             ->method('findTaggedServiceIds')
90             ->will($this->returnValue($services));
91
92         $builder->expects($this->atLeastOnce())
93             ->method('getDefinition')
94             ->will($this->returnValue($definition));
95
96         $pass = new FragmentRendererPass();
97         $pass->process($builder);
98     }
99
100     public function testValidContentRenderer()
101     {
102         $services = array(
103             'my_content_renderer' => array(array('alias' => 'foo')),
104         );
105
106         $renderer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
107         $renderer
108             ->expects($this->once())
109             ->method('addMethodCall')
110             ->with('addRendererService', array('foo', 'my_content_renderer'))
111         ;
112
113         $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
114         $definition->expects($this->atLeastOnce())
115             ->method('getClass')
116             ->will($this->returnValue('Symfony\Component\HttpKernel\Tests\DependencyInjection\RendererService'));
117         $definition
118             ->expects($this->once())
119             ->method('isPublic')
120             ->will($this->returnValue(true))
121         ;
122
123         $builder = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('hasDefinition', 'findTaggedServiceIds', 'getDefinition'))->getMock();
124         $builder->expects($this->any())
125             ->method('hasDefinition')
126             ->will($this->returnValue(true));
127
128         // We don't test kernel.fragment_renderer here
129         $builder->expects($this->atLeastOnce())
130             ->method('findTaggedServiceIds')
131             ->will($this->returnValue($services));
132
133         $builder->expects($this->atLeastOnce())
134             ->method('getDefinition')
135             ->will($this->onConsecutiveCalls($renderer, $definition));
136
137         $pass = new FragmentRendererPass();
138         $pass->process($builder);
139     }
140 }
141
142 class RendererService implements FragmentRendererInterface
143 {
144     public function render($uri, Request $request = null, array $options = array())
145     {
146     }
147
148     public function getName()
149     {
150         return 'test';
151     }
152 }