11
11
12
12
namespace Symfony \Component \HttpKernel \Tests \DataCollector ;
13
13
14
+ use Symfony \Component \HttpKernel \HttpKernel ;
15
+ use Symfony \Component \HttpKernel \HttpKernelInterface ;
14
16
use Symfony \Component \HttpKernel \DataCollector \RequestDataCollector ;
17
+ use Symfony \Component \HttpKernel \Event \FilterControllerEvent ;
15
18
use Symfony \Component \HttpFoundation \Request ;
16
19
use Symfony \Component \HttpFoundation \Response ;
17
20
use Symfony \Component \HttpFoundation \Cookie ;
21
+ use Symfony \Component \EventDispatcher \EventDispatcher ;
18
22
19
23
class RequestDataCollectorTest extends \PHPUnit_Framework_TestCase
20
24
{
@@ -50,6 +54,95 @@ public function testCollect(Request $request, Response $response)
50
54
$ this ->assertEquals ('application/json ' ,$ c ->getContentType ());
51
55
}
52
56
57
+ /**
58
+ * Test various types of controller callables.
59
+ *
60
+ * @dataProvider provider
61
+ */
62
+ public function testControllerInspection (Request $ request , Response $ response )
63
+ {
64
+ // make sure we always match the line number
65
+ $ r1 = new \ReflectionMethod ($ this , 'testControllerInspection ' );
66
+ $ r2 = new \ReflectionMethod ($ this , 'staticControllerMethod ' );
67
+ // test name, callable, expected
68
+ $ controllerTests = array (
69
+ array (
70
+ '"Regular" callable ' ,
71
+ array ($ this , 'testControllerInspection ' ),
72
+ array (
73
+ 'class ' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest ' ,
74
+ 'method ' => 'testControllerInspection ' ,
75
+ 'file ' => __FILE__ ,
76
+ 'line ' => $ r1 ->getStartLine ()
77
+ ),
78
+ ),
79
+
80
+ array (
81
+ 'Closure ' ,
82
+ function () { return 'foo ' ; },
83
+ 'Closure ' ,
84
+ ),
85
+
86
+ array (
87
+ 'Static callback as string ' ,
88
+ 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest::staticControllerMethod ' ,
89
+ 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest::staticControllerMethod ' ,
90
+ ),
91
+
92
+ array (
93
+ 'Static callable with instance ' ,
94
+ array ($ this , 'staticControllerMethod ' ),
95
+ array (
96
+ 'class ' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest ' ,
97
+ 'method ' => 'staticControllerMethod ' ,
98
+ 'file ' => __FILE__ ,
99
+ 'line ' => $ r2 ->getStartLine ()
100
+ ),
101
+ ),
102
+
103
+ array (
104
+ 'Static callable with class name ' ,
105
+ array ('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest ' , 'staticControllerMethod ' ),
106
+ array (
107
+ 'class ' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest ' ,
108
+ 'method ' => 'staticControllerMethod ' ,
109
+ 'file ' => __FILE__ ,
110
+ 'line ' => $ r2 ->getStartLine ()
111
+ ),
112
+ ),
113
+
114
+ array (
115
+ 'Callable with instance depending on __call() ' ,
116
+ array ($ this , 'magicMethod ' ),
117
+ array (
118
+ 'class ' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest ' ,
119
+ 'method ' => 'magicMethod ' ,
120
+ 'file ' => 'n/a ' ,
121
+ 'line ' => 'n/a '
122
+ ),
123
+ ),
124
+
125
+ array (
126
+ 'Callable with class name depending on __callStatic() ' ,
127
+ array ('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest ' , 'magicMethod ' ),
128
+ array (
129
+ 'class ' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest ' ,
130
+ 'method ' => 'magicMethod ' ,
131
+ 'file ' => 'n/a ' ,
132
+ 'line ' => 'n/a '
133
+ ),
134
+ ),
135
+ );
136
+
137
+ $ c = new RequestDataCollector ();
138
+
139
+ foreach ($ controllerTests as $ controllerTest ) {
140
+ $ this ->injectController ($ c , $ controllerTest [1 ], $ request );
141
+ $ c ->collect ($ request , $ response );
142
+ $ this ->assertEquals ($ controllerTest [2 ], $ c ->getController (), sprintf ('Testing: %s ' , $ controllerTest [0 ]));
143
+ }
144
+ }
145
+
53
146
public function provider ()
54
147
{
55
148
if (!class_exists ('Symfony\Component\HttpFoundation\Request ' )) {
@@ -71,4 +164,39 @@ public function provider()
71
164
);
72
165
}
73
166
167
+ /**
168
+ * Inject the given controller callable into the data collector.
169
+ */
170
+ protected function injectController ($ collector , $ controller , $ request )
171
+ {
172
+ $ resolver = $ this ->getMock ('Symfony \\Component \\HttpKernel \\Controller \\ControllerResolverInterface ' );
173
+ $ httpKernel = new HttpKernel (new EventDispatcher (), $ resolver );
174
+ $ event = new FilterControllerEvent ($ httpKernel , $ controller , $ request , HttpKernelInterface::MASTER_REQUEST );
175
+ $ collector ->onKernelController ($ event );
176
+ }
177
+
178
+ /**
179
+ * Dummy method used as controller callable
180
+ */
181
+ public static function staticControllerMethod ()
182
+ {
183
+ throw new \LogicException ('Unexpected method call ' );
184
+ }
185
+
186
+ /**
187
+ * Magic method to allow non existing methods to be called and delegated.
188
+ */
189
+ public function __call ($ method , $ args )
190
+ {
191
+ throw new \LogicException ('Unexpected method call ' );
192
+ }
193
+
194
+ /**
195
+ * Magic method to allow non existing methods to be called and delegated.
196
+ */
197
+ public static function __callStatic ($ method , $ args )
198
+ {
199
+ throw new \LogicException ('Unexpected method call ' );
200
+ }
201
+
74
202
}
0 commit comments