This repository has been archived by the owner on May 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 159
Enhancement: Leverage AbstractHttpControllerTestCase #307
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,58 +4,116 @@ | |
|
||
use Application\Controller; | ||
use ApplicationTest\Integration\Util\Bootstrap; | ||
use User\Mapper\User; | ||
use Zend\Http; | ||
use Zend\Mvc\Controller\ControllerManager; | ||
use Zend\Mvc\MvcEvent; | ||
use Zend\Mvc\Router\RouteMatch; | ||
use Zend\Mvc\Router\Http\TreeRouteStack as HttpRouter; | ||
use PHPUnit_Framework_TestCase; | ||
use Zend\Paginator; | ||
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; | ||
use ZfModule\Mapper\Module; | ||
|
||
class IndexControllerTest extends PHPUnit_Framework_TestCase | ||
class IndexControllerTest extends AbstractHttpControllerTestCase | ||
{ | ||
/** | ||
* @var Controller\IndexController; | ||
*/ | ||
protected $controller; | ||
protected $request; | ||
protected $response; | ||
|
||
/** | ||
* @var RouteMatch | ||
*/ | ||
protected $routeMatch; | ||
|
||
protected $event; | ||
|
||
protected function setUp() | ||
{ | ||
$serviceManager = Bootstrap::getServiceManager(); | ||
|
||
/* @var ControllerManager $controllerManager */ | ||
$controllerManager = $serviceManager->get('ControllerManager'); | ||
$this->controller = $controllerManager->get('Application\Controller\Index'); | ||
|
||
$this->request = new Http\Request(); | ||
$this->routeMatch = new RouteMatch(['controller' => 'index']); | ||
$this->event = new MvcEvent(); | ||
$config = $serviceManager->get('Config'); | ||
$routerConfig = isset($config['router']) ? $config['router'] : []; | ||
$router = HttpRouter::factory($routerConfig); | ||
$this->event->setRouter($router); | ||
$this->event->setRouteMatch($this->routeMatch); | ||
$this->controller->setEvent($this->event); | ||
$this->controller->setServiceLocator($serviceManager); | ||
parent::setUp(); | ||
|
||
$this->setApplicationConfig(Bootstrap::getConfig()); | ||
} | ||
|
||
public function testIndexActionCanBeAccessed() | ||
{ | ||
$this->routeMatch->setParam('action', 'index'); | ||
$moduleMapper = $this->getMockBuilder(Module::class)->getMock(); | ||
|
||
$moduleMapper | ||
->expects($this->once()) | ||
->method('pagination') | ||
->with( | ||
$this->equalTo(1), | ||
$this->equalTo(Controller\IndexController::MODULES_PER_PAGE), | ||
$this->equalTo(null), | ||
$this->equalTo('created_at'), | ||
$this->equalTo('DESC') | ||
) | ||
->willReturn(new Paginator\Paginator(new Paginator\Adapter\Null())) | ||
; | ||
|
||
$moduleMapper | ||
->expects($this->once()) | ||
->method('getTotal') | ||
->willReturn(0) | ||
; | ||
|
||
$moduleMapper | ||
->expects($this->once()) | ||
->method('findAll') | ||
->with( | ||
$this->equalTo(10), | ||
$this->equalTo('created_at'), | ||
$this->equalTo('DESC') | ||
) | ||
->willReturn([]) | ||
; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed to mock database access in view layer . |
||
|
||
$userMapper = $this->getMockBuilder(User::class)->getMock(); | ||
|
||
$userMapper | ||
->expects($this->once()) | ||
->method('findAll') | ||
->with( | ||
$this->equalTo(16), | ||
$this->equalTo('created_at'), | ||
$this->equalTo('DESC') | ||
) | ||
->willReturn([]) | ||
; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed to mock database access in view layer . |
||
|
||
$this->getApplicationServiceLocator() | ||
->setAllowOverride(true) | ||
->setService( | ||
'zfmodule_mapper_module', | ||
$moduleMapper | ||
) | ||
->setService( | ||
'zfcuser_user_mapper', | ||
$userMapper | ||
) | ||
; | ||
|
||
$this->dispatch('/'); | ||
|
||
$this->assertControllerName('Application\Controller\Index'); | ||
$this->assertActionName('index'); | ||
$this->assertResponseStatusCode(Http\Response::STATUS_CODE_200); | ||
} | ||
|
||
public function testFeedActionCanBeAccessed() | ||
{ | ||
$moduleMapper = $this->getMockBuilder(Module::class)->getMock(); | ||
|
||
$moduleMapper | ||
->expects($this->once()) | ||
->method('pagination') | ||
->with( | ||
$this->equalTo(1), | ||
$this->equalTo(Controller\IndexController::MODULES_PER_PAGE), | ||
$this->equalTo(null), | ||
$this->equalTo('created_at'), | ||
$this->equalTo('DESC') | ||
) | ||
->willReturn([]) | ||
; | ||
|
||
$this->controller->dispatch($this->request); | ||
$this->getApplicationServiceLocator() | ||
->setAllowOverride(true) | ||
->setService( | ||
'zfmodule_mapper_module', | ||
$moduleMapper | ||
) | ||
; | ||
|
||
/* @var Response $response */ | ||
$response = $this->controller->getResponse(); | ||
$this->dispatch('/feed'); | ||
|
||
$this->assertEquals(Http\Response::STATUS_CODE_200, $response->getStatusCode()); | ||
$this->assertControllerName('Application\Controller\Index'); | ||
$this->assertActionName('feed'); | ||
$this->assertResponseStatusCode(Http\Response::STATUS_CODE_200); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
module/Application/test/ApplicationTest/Integration/Controller/SearchControllerTest.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace ApplicationTest\Integration\Controller; | ||
|
||
use ApplicationTest\Integration\Util\Bootstrap; | ||
use Zend\Http; | ||
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; | ||
use ZfModule\Mapper; | ||
|
||
class SearchControllerTest extends AbstractHttpControllerTestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->setApplicationConfig(Bootstrap::getConfig()); | ||
} | ||
|
||
public function testIndexActionCanBeAccessed() | ||
{ | ||
$moduleMapper = $this->getMockBuilder(Mapper\Module::class)->getMock(); | ||
|
||
$moduleMapper | ||
->expects($this->once()) | ||
->method('findByLike') | ||
->with($this->equalTo(null)) | ||
->willReturn([]) | ||
; | ||
|
||
$this->getApplicationServiceLocator() | ||
->setAllowOverride(true) | ||
->setService( | ||
'zfmodule_mapper_module', | ||
$moduleMapper | ||
) | ||
; | ||
|
||
$this->dispatch('/live-search'); | ||
|
||
$this->assertControllerName('Application\Controller\Search'); | ||
$this->assertActionName('index'); | ||
$this->assertResponseStatusCode(Http\Response::STATUS_CODE_200); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needed to mock database access in view layer .