Skip to content

[FrameworkBundle][2.3] Add tests for the Controller class #18203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,122 @@ public function testForward()
$response = $controller->forward('a_controller');
$this->assertEquals('xml--fr', $response->getContent());
}

public function testGenerateUrl()
{
$router = $this->getMock('Symfony\Component\Routing\RouterInterface');
$router->expects($this->once())->method('generate')->willReturn('/foo');

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->at(0))->method('get')->will($this->returnValue($router));

$controller = new Controller();
$controller->setContainer($container);

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

public function testRedirect()
{
$controller = new Controller();
$response = $controller->redirect('http://dunglas.fr', 301);

$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertSame('http://dunglas.fr', $response->getTargetUrl());
$this->assertSame(301, $response->getStatusCode());
}

public function testRenderViewTemplating()
{
$templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
$templating->expects($this->once())->method('render')->willReturn('bar');

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->at(0))->method('get')->will($this->returnValue($templating));

$controller = new Controller();
$controller->setContainer($container);

$this->assertEquals('bar', $controller->renderView('foo'));
}

public function testRenderTemplating()
{
$templating = $this->getMock('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
$templating->expects($this->once())->method('renderResponse')->willReturn(new Response('bar'));

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->at(0))->method('get')->will($this->returnValue($templating));

$controller = new Controller();
$controller->setContainer($container);

$this->assertEquals('bar', $controller->render('foo')->getContent());
}

public function testStreamTemplating()
{
$templating = $this->getMock('Symfony\Component\Routing\RouterInterface');

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->at(0))->method('get')->will($this->returnValue($templating));

$controller = new Controller();
$controller->setContainer($container);

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

public function testCreateNotFoundException()
{
$controller = new Controller();

$this->assertInstanceOf('Symfony\Component\HttpKernel\Exception\NotFoundHttpException', $controller->createNotFoundException());
}

public function testCreateForm()
{
$form = $this->getMock('Symfony\Component\Form\FormInterface');

$formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
$formFactory->expects($this->once())->method('create')->willReturn($form);

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->at(0))->method('get')->will($this->returnValue($formFactory));

$controller = new Controller();
$controller->setContainer($container);

$this->assertEquals($form, $controller->createForm('foo'));
}

public function testCreateFormBuilder()
{
$formBuilder = $this->getMock('Symfony\Component\Form\FormBuilderInterface');

$formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
$formFactory->expects($this->once())->method('createBuilder')->willReturn($formBuilder);

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->at(0))->method('get')->will($this->returnValue($formFactory));

$controller = new Controller();
$controller->setContainer($container);

$this->assertEquals($formBuilder, $controller->createFormBuilder('foo'));
}

public function testGetDoctrine()
{
$doctrine = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->at(0))->method('has')->will($this->returnValue(true));
$container->expects($this->at(1))->method('get')->will($this->returnValue($doctrine));

$controller = new Controller();
$controller->setContainer($container);

$this->assertEquals($doctrine, $controller->getDoctrine());
}
}