35
35
use Symfony \Component \Security \Core \User \User ;
36
36
use Symfony \Component \Security \Csrf \CsrfTokenManagerInterface ;
37
37
use Symfony \Component \Serializer \SerializerInterface ;
38
- use Symfony \Bundle \FrameworkBundle \Templating \EngineInterface ;
39
38
40
39
/**
41
40
* @author Kévin Dunglas <dunglas@gmail.com>
41
+ *
42
+ * @requires PHP 7
42
43
*/
43
44
class ControllerTraitTest extends \PHPUnit_Framework_TestCase
44
45
{
45
46
use ControllerTrait {
46
47
getSerializer as traitGetSerializer;
47
- getTemplating as traitGetTemplating;
48
48
}
49
49
50
50
private $ token ;
51
51
private $ serializer ;
52
52
private $ flashBag ;
53
53
private $ isGranted ;
54
- private $ templating ;
55
54
private $ twig ;
56
55
private $ formFactory ;
57
56
58
57
protected function getRouter ()
59
58
{
60
- $ router = $ this ->getMock (RouterInterface::class);
59
+ $ router = $ this ->getMockBuilder (RouterInterface::class)-> getMock ( );
61
60
$ router ->expects ($ this ->once ())->method ('generate ' )->willReturn ('/foo ' );
62
61
63
62
return $ router ;
@@ -77,7 +76,7 @@ protected function getRequestStack()
77
76
78
77
protected function getHttpKernel ()
79
78
{
80
- $ kernel = $ this ->getMock (HttpKernelInterface::class);
79
+ $ kernel = $ this ->getMockBuilder (HttpKernelInterface::class)-> getMock ( );
81
80
$ kernel ->expects ($ this ->once ())->method ('handle ' )->will ($ this ->returnCallback (function (Request $ request ) {
82
81
return new Response ($ request ->getRequestFormat ().'-- ' .$ request ->getLocale ());
83
82
}));
@@ -96,37 +95,28 @@ protected function getSerializer()
96
95
97
96
protected function getSession ()
98
97
{
99
- $ session = $ this ->getMock (Session::class);
98
+ $ session = $ this ->getMockBuilder (Session::class)-> getMock ( );
100
99
$ session ->expects ($ this ->once ())->method ('getFlashBag ' )->willReturn ($ this ->flashBag );
101
100
102
101
return $ session ;
103
102
}
104
103
105
104
protected function getAuthorizationChecker ()
106
105
{
107
- $ authorizationChecker = $ this ->getMock (AuthorizationCheckerInterface::class);
106
+ $ authorizationChecker = $ this ->getMockBuilder (AuthorizationCheckerInterface::class)-> getMock ( );
108
107
$ authorizationChecker ->expects ($ this ->once ())->method ('isGranted ' )->willReturn ($ this ->isGranted );
109
108
110
109
return $ authorizationChecker ;
111
110
}
112
111
113
- protected function getTemplating ()
114
- {
115
- if ($ this ->templating ) {
116
- return $ this ->templating ;
117
- }
118
-
119
- return $ this ->traitGetTemplating ();
120
- }
121
-
122
112
protected function getTwig ()
123
113
{
124
114
return $ this ->twig ;
125
115
}
126
116
127
117
protected function getDoctrine ()
128
118
{
129
- return $ this ->getMock (ManagerRegistry::class);
119
+ return $ this ->getMockBuilder (ManagerRegistry::class)-> getMock ( );
130
120
}
131
121
132
122
protected function getFormFactory ()
@@ -136,7 +126,7 @@ protected function getFormFactory()
136
126
137
127
protected function getTokenStorage ()
138
128
{
139
- $ tokenStorage = $ this ->getMock (TokenStorageInterface::class);
129
+ $ tokenStorage = $ this ->getMockBuilder (TokenStorageInterface::class)-> getMock ( );
140
130
$ tokenStorage
141
131
->expects ($ this ->once ())
142
132
->method ('getToken ' )
@@ -147,7 +137,7 @@ protected function getTokenStorage()
147
137
148
138
protected function getCsrfTokenManager ()
149
139
{
150
- $ tokenManager = $ this ->getMock (CsrfTokenManagerInterface::class);
140
+ $ tokenManager = $ this ->getMockBuilder (CsrfTokenManagerInterface::class)-> getMock ( );
151
141
$ tokenManager ->expects ($ this ->once ())->method ('isTokenValid ' )->willReturn (true );
152
142
153
143
return $ tokenManager ;
@@ -213,7 +203,7 @@ public function testJson()
213
203
214
204
public function testJsonWithSerializer ()
215
205
{
216
- $ this ->serializer = $ this ->getMock (SerializerInterface::class);
206
+ $ this ->serializer = $ this ->getMockBuilder (SerializerInterface::class)-> getMock ( );
217
207
$ this ->serializer
218
208
->expects ($ this ->once ())
219
209
->method ('serialize ' )
@@ -227,7 +217,7 @@ public function testJsonWithSerializer()
227
217
228
218
public function testJsonWithSerializerContextOverride ()
229
219
{
230
- $ this ->serializer = $ this ->getMock (SerializerInterface::class);
220
+ $ this ->serializer = $ this ->getMockBuilder (SerializerInterface::class)-> getMock ( );
231
221
$ this ->serializer
232
222
->expects ($ this ->once ())
233
223
->method ('serialize ' )
@@ -244,7 +234,6 @@ public function testJsonWithSerializerContextOverride()
244
234
public function testAddFlash ()
245
235
{
246
236
$ this ->flashBag = new FlashBag ();
247
-
248
237
$ this ->addFlash ('foo ' , 'bar ' );
249
238
250
239
$ this ->assertSame (array ('bar ' ), $ this ->flashBag ->get ('foo ' ));
@@ -267,50 +256,24 @@ public function testDenyAccessUnlessGranted()
267
256
$ this ->denyAccessUnlessGranted ('foo ' );
268
257
}
269
258
270
- public function testRenderViewTemplating ()
271
- {
272
- $ this ->templating = $ this ->getMock (EngineInterface::class);
273
- $ this ->templating ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
274
-
275
- $ this ->assertEquals ('bar ' , $ this ->renderView ('foo ' ));
276
- }
277
-
278
259
public function testRenderViewTwig ()
279
260
{
280
- $ this ->templating = false ;
281
261
$ this ->twig = $ this ->getMockBuilder (\Twig_Environment::class)->disableOriginalConstructor ()->getMock ();
282
262
$ this ->twig ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
283
263
284
264
$ this ->assertEquals ('bar ' , $ this ->renderView ('foo ' ));
285
265
}
286
266
287
- public function testRenderTemplating ()
288
- {
289
- $ this ->templating = $ this ->getMock (EngineInterface::class);
290
- $ this ->templating ->expects ($ this ->once ())->method ('renderResponse ' )->willReturn (new Response ('bar ' ));
291
-
292
- $ this ->assertEquals ('bar ' , $ this ->render ('foo ' )->getContent ());
293
- }
294
-
295
267
public function testRenderTwig ()
296
268
{
297
- $ this ->templating = false ;
298
269
$ this ->twig = $ this ->getMockBuilder (\Twig_Environment::class)->disableOriginalConstructor ()->getMock ();
299
270
$ this ->twig ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
300
271
301
272
$ this ->assertEquals ('bar ' , $ this ->render ('foo ' )->getContent ());
302
273
}
303
274
304
- public function testStreamTemplating ()
305
- {
306
- $ this ->templating = $ this ->getMock (EngineInterface::class);
307
-
308
- $ this ->assertInstanceOf (StreamedResponse::class, $ this ->stream ('foo ' ));
309
- }
310
-
311
275
public function testStreamTwig ()
312
276
{
313
- $ this ->templating = false ;
314
277
$ this ->twig = $ this ->getMockBuilder (\Twig_Environment::class)->disableOriginalConstructor ()->getMock ();
315
278
316
279
$ this ->assertInstanceOf (StreamedResponse::class, $ this ->stream ('foo ' ));
@@ -328,19 +291,19 @@ public function testCreateAccessDeniedException()
328
291
329
292
public function testCreateForm ()
330
293
{
331
- $ form = $ this ->getMock (FormInterface::class);
294
+ $ form = $ this ->getMockBuilder (FormInterface::class)-> getMock ( );
332
295
333
- $ this ->formFactory = $ this ->getMock (FormFactoryInterface::class);
296
+ $ this ->formFactory = $ this ->getMockBuilder (FormFactoryInterface::class)-> getMock ( );
334
297
$ this ->formFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ form );
335
298
336
299
$ this ->assertEquals ($ form , $ this ->createForm ('foo ' ));
337
300
}
338
301
339
302
public function testCreateFormBuilder ()
340
303
{
341
- $ formBuilder = $ this ->getMock (FormBuilderInterface::class);
304
+ $ formBuilder = $ this ->getMockBuilder (FormBuilderInterface::class)-> getMock ( );
342
305
343
- $ this ->formFactory = $ this ->getMock (FormFactoryInterface::class);
306
+ $ this ->formFactory = $ this ->getMockBuilder (FormFactoryInterface::class)-> getMock ( );
344
307
$ this ->formFactory ->expects ($ this ->once ())->method ('createBuilder ' )->willReturn ($ formBuilder );
345
308
346
309
$ this ->assertEquals ($ formBuilder , $ this ->createFormBuilder ('foo ' ));
0 commit comments