8000 fix controller tests · symfony/symfony@6ac0be8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ac0be8

Browse files
committed
fix controller tests
* move test methods from `TestController` class to the test case class * add missing public method stubs to `TestController` (all methods in the base `Controller` class from the FrameworkBundle are `protected` since Symfony 3.0)
1 parent 96eacb4 commit 6ac0be8

File tree

1 file changed

+105
-50
lines changed

1 file changed

+105
-50
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php

Lines changed: 105 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Component\HttpFoundation\RequestStack;
1919
use Symfony\Component\HttpFoundation\Response;
2020
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
21+
use Symfony\Component\HttpFoundation\StreamedResponse;
22+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2123
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
2224
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2325
use Symfony\Component\Security\Core\User\User;
@@ -261,44 +263,6 @@ public function testIsCsrfTokenValid()
261263

262264
$this->assertTrue($controller->isCsrfTokenValid('foo', 'bar'));
263265
}
264-
}
265-
266-
class TestController extends Controller
267-
{
268-
public function forward($controller, array $path = array(), array $query = array())
269-
{
270-
return parent::forward($controller, $path, $query);
271-
}
272-
273-
public function getUser()
274-
{
275-
return parent::getUser();
276-
}
277-
278-
public function isGranted($attributes, $object = null)
279-
{
280-
return parent::isGranted($attributes, $object);
281-
}
282-
283-
public function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
284-
{
285-
parent::denyAccessUnlessGranted($attributes, $object, $message);
286-
}
287-
288-
public function redirectToRoute($route, array $parameters = array(), $status = 302)
289-
{
290-
return parent::redirectToRoute($route, $parameters, $status);
291-
}
292-
293-
public function addFlash($type, $message)
294-
{
295-
parent::addFlash($type, $message);
296-
}
297-
298-
public function isCsrfTokenValid($id, $token)
299-
{
300-
return parent::isCsrfTokenValid($id, $token);
301-
}
302266

303267
public function testGenerateUrl()
304268
{
@@ -308,15 +272,15 @@ public function testGenerateUrl()
308272
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
309273
$container->expects($this->at(0))->method('get')->will($this->returnValue($router));
310274

311-
$controller = new Controller();
275+
$controller = new TestController();
312276
$controller->setContainer($container);
313277

314278
$this->assertEquals('/foo', $controller->generateUrl('foo'));
315279
}
316280

317281
public function testRedirect()
318282
{
319-
$controller = new Controller();
283+
$controller = new TestController();
320284
$response = $controller->redirect('http://dunglas.fr', 301);
321285

322286
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
@@ -330,9 +294,10 @@ public function testRenderViewTemplating()
330294
$templating->expects($this->once())->method('render')->willReturn('bar');
331295

332296
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
333-
$container->expects($this->at(0))->method('get')->will($this->returnValue($templating));
297+
$container->expects($this->at(0))->method('has')->willReturn(true);
298+
$container->expects($this->at(1))->method('get')->will($this->returnValue($templating));
334299

335-
$controller = new Controller();
300+
$controller = new TestController();
336301
$controller->setContainer($container);
337302

338303
$this->assertEquals('bar', $controller->renderView('foo'));
@@ -344,9 +309,10 @@ public function testRenderTemplating()
344309
$templating->expects($this->once())->method('renderResponse')->willReturn(new Response('bar'));
345310

346311
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
347-
$container->expects($this->at(0))->method('get')->will($this->returnValue($templating));
312+
$container->expects($this->at(0))->method('has')->willReturn(true);
313+
$container->expects($this->at(1))->method('get')->will($this->returnValue($templating));
348314

349-
$controller = new Controller();
315+
$controller = new TestController();
350316
$controller->setContainer($container);
351317

352318
$this->assertEquals('bar', $controller->render('foo')->getContent());
@@ -357,17 +323,18 @@ public function testStreamTemplating()
357323
$templating = $this->getMock('Symfony\Component\Routing\RouterInterface');
358324

359325
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
360-
$container->expects($this->at(0))->method('get')->will($this->returnValue($templating));
326+
$container->expects($this->at(0))->method('has')->willReturn(true);
327+
$container->expects($this->at(1))->method('get')->will($this->returnValue($templating));
361328

362-
$controller = new Controller();
329+
$controller = new TestController();
363330
$controller->setContainer($container);
364331

365332
$this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $controller->stream('foo'));
366333
}
367334

368335
public function testCreateNotFoundException()
369336
{
370-
$controller = new Controller();
337+
$controller = new TestController();
371338

372339
$this->assertInstanceOf('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', $controller->createNotFoundException());
373340
}
@@ -382,7 +349,7 @@ public function testCreateForm()
382349
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
383350
$container->expects($this->at(0))->method('get')->will($this->returnValue($formFactory));
384351

385-
$controller = new Controller();
352+
$controller = new TestController();
386353
$controller->setContainer($container);
387354

388355
$this->assertEquals($form, $controller->createForm('foo'));
@@ -398,7 +365,7 @@ public function testCreateFormBuilder()
398365
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
399366
$container->expects($this->at(0))->method('get')->will($this->returnValue($formFactory));
400367

401-
$controller = new Controller();
368+
$controller = new TestController();
402369
$controller->setContainer($container);
403370

404371
$this->assertEquals($formBuilder, $controller->createFormBuilder('foo'));
@@ -412,9 +379,97 @@ public function testGetDoctrine()
412379
$container->expects($this->at(0))->method('has')->will($this->returnValue(true));
413380
$container->expects($this->at(1))->method('get')->will($this->returnValue($doctrine));
414381

415-
$controller = new Controller();
382+
$controller = new TestController();
416383
$controller->setContainer($container);
417384

418385
$this->assertEquals($doctrine, $controller->getDoctrine());
419386
}
420387
}
388+
389+
class TestController extends Controller
390+
{
391+
public function generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
392+
{
393+
return parent::generateUrl($route, $parameters, $referenceType);
394+
}
395+
396+
public function redirect($url, $status = 302)
397+
{
398+
return parent::redirect($url, $status);
399+
}
400+
401+
public function forward($controller, array $path = array(), array $query = array())
402+
{
403+
return parent::forward($controller, $path, $query);
404+
}
405+
406+
public function getUser()
407+
{
408+
return parent::getUser();
409+
}
410+
411+
public function isGranted($attributes, $object = null)
412+
{
413+
return parent::isGranted($attributes, $object);
414+
}
415+
416+
public function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
417+
{
418+
parent::denyAccessUnlessGranted($attributes, $object, $message);
419+
}
420+
421+
public function redirectToRoute($route, array $parameters = array(), $status = 302)
422+
{
423+
return parent::redirectToRoute($route, $parameters, $status);
424+
}
425+
426+
public function addFlash($type, $message)
427+
{
428+
parent::addFlash($type, $message);
429+
}
430+
431+
public function isCsrfTokenValid($id, $token)
432+
{
433+
return parent::isCsrfTokenValid($id, $token);
434+
}
435+
436+
public function renderView($view, array $parameters = array())
437+
{
438+
return parent::renderView($view, $parameters);
439+
}
440+
441+
public function render($view, array $parameters = array(), Response $response = null)
442+
{
443+
return parent::render($view, $parameters, $response);
444+
}
445+
446+
public function stream($view, array $parameters = array(), StreamedResponse $response = null)
447+
{
448+
return parent::stream($view, $parameters, $response);
449+
}
450+
451+
public function createNotFoundException($message = 'Not Found', \Exception $previous = null)
452+
{
453+
return parent::createNotFoundException($message, $previous);
454+
}
455+
456+
public function createAccessDeniedException($message = 'Access Denied.', \Exception $previous = null)
457+
{
458+
return parent::createAccessDeniedException($message, $previous);
459+
}
460+
461+
public function createForm($type, $data = null, array $options = array())
462+
{
463+
return parent::createForm($type, $data, $options);
464+
}
465+
466+
public function createFormBuilder($data = null, array $options = array())
467+
{
468+
return parent::createFormBuilder($data, $options);
469+
}
470+
471+
public function getDoctrine()
472+
{
473+
return parent::getDoctrine();
474+
}
475+
}

0 commit comments

Comments
 (0)
0