Created
November 20, 2016 12:33
-
-
Save JavierCane/a27be29b593d2b387459ac7cc67f8b1a to your computer and use it in GitHub Desktop.
Example of framework coupled controller test. Entire repo: https://github.com/CodelyTV/coupled-code-example/blob/master/src/AppBundle/Tests/Controller/CourseControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Tests\Controller; | |
use AppBundle\Controller\CourseController; | |
use Doctrine\Bundle\DoctrineBundle\Registry; | |
use Doctrine\ORM\AbstractQuery; | |
use Doctrine\ORM\EntityManager; | |
use Doctrine\ORM\QueryBuilder; | |
use PHPUnit_Framework_TestCase; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
final class CourseControllerTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testGetCoursesFilteredByLevel() | |
{ | |
$fromLevel = 0; | |
$request = new Request(['from_level' => $fromLevel]); | |
$container = \Mockery::mock(ContainerInterface::class); | |
$doctrine = \Mockery::mock(Registry::class); | |
$entityManager = \Mockery::mock(EntityManager::class); | |
$queryBuilder = \Mockery::mock(QueryBuilder::class); | |
$query = \Mockery::mock(AbstractQuery::class); | |
$container->shouldReceive('has') | |
->once() | |
->with('doctrine') | |
->andReturn(true); | |
$container->shouldReceive('get') | |
->once() | |
->with('doctrine') | |
->andReturn($doctrine); | |
$doctrine->shouldReceive('getEntityManager') | |
->once() | |
->withNoArgs() | |
->andReturn($entityManager); | |
$entityManager->shouldReceive('createQueryBuilder') | |
->once() | |
->withNoArgs() | |
->andReturn($queryBuilder); | |
$queryBuilder->shouldReceive('select') | |
->once() | |
->with('c', 'v') | |
->andReturn($queryBuilder); | |
$queryBuilder->shouldReceive('from') | |
->once() | |
->with('AppBundle\Model\Course', 'c') | |
->andReturn($queryBuilder); | |
$queryBuilder->shouldReceive('where') | |
->once() | |
->with('c.level', '>', $fromLevel) | |
->andReturn($queryBuilder); | |
$queryBuilder->shouldReceive('getQuery') | |
->once() | |
->withNoArgs() | |
->andReturn($query); | |
$query->shouldReceive('execute') | |
->once() | |
->withNoArgs() | |
->andReturn( | |
[ | |
[ | |
'title' => 'Codely mola', | |
'level' => 2, | |
], | |
[ | |
'title' => 'Aprende a decir basicamente como Javi', | |
'level' => 5, | |
], | |
] | |
); | |
$controller = new CourseController(); | |
$controller->setContainer($container); | |
$controllerResult = $controller->getCourseAction($request); | |
$this->assertEquals( | |
[ | |
[ | |
'title' => 'Codely mola', | |
'level' => 2, | |
], | |
[ | |
'title' => 'Aprende a decir basicamente como Javi', | |
'level' => 5, | |
], | |
], | |
$controllerResult | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment