Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Enhancement: Leverage AbstractHttpControllerTestCase #307

Merged
merged 3 commits into from
Jan 23, 2015
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;
Copy link
Member Author

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 :trollface:.


$moduleMapper
->expects($this->once())
->method('findAll')
->with(
$this->equalTo(10),
$this->equalTo('created_at'),
$this->equalTo('DESC')
)
->willReturn([])
;
Copy link
Member Author

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 :trollface:.


$userMapper = $this->getMockBuilder(User::class)->getMock();

$userMapper
->expects($this->once())
->method('findAll')
->with(
$this->equalTo(16),
$this->equalTo('created_at'),
$this->equalTo('DESC')
)
->willReturn([])
;
Copy link
Member Author

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 :trollface:.


$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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public static function setConfig($config)
static::$config = $config;
}

/**
* @return mixed
*/
public static function getConfig()
{
return static::$config;
}

/**
* @return ServiceManager
*/
Expand Down