Provide global request and response mocks

Prior to this change one had to overwrite setupIcingaMock or setUp to set
a specific mock as request or response. Now it is possible to do this for a
specific test without overwriting any method.
This commit is contained in:
Johannes Meyer 2014-09-09 09:24:38 +02:00
parent f53519c54d
commit 1137c010a8
1 changed files with 34 additions and 13 deletions

View File

@ -151,30 +151,51 @@ namespace Icinga\Test {
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->setupIcingaMock();
$requestMock = Mockery::mock('Icinga\Web\Request');
$requestMock->shouldReceive('getPathInfo')->andReturn('')
->shouldReceive('getBaseUrl')->andReturn('/')
->shouldReceive('getQuery')->andReturn(array());
$this->setupIcingaMock($requestMock);
} }
/** /**
* Setup mock object for the application's bootstrap * Setup mock object for the application's bootstrap
*
* @param Zend_Controller_Request_Abstract $request The request to be returned by
* Icinga::app()->getFrontController()->getRequest()
*/ */
protected function setupIcingaMock(Zend_Controller_Request_Abstract $request) protected function setupIcingaMock()
{ {
$requestMock = Mockery::mock('Icinga\Web\Request')->shouldDeferMissing();
$requestMock->shouldReceive('getPathInfo')->andReturn('')->byDefault()
->shouldReceive('getBaseUrl')->andReturn('/')->byDefault()
->shouldReceive('getQuery')->andReturn(array())->byDefault();
$responseMock = Mockery::mock('Icinga\Web\Response')->shouldDeferMissing();
// Can't express this as demeter chains. See: https://github.com/padraic/mockery/issues/59
$bootstrapMock = Mockery::mock('Icinga\Application\ApplicationBootstrap')->shouldDeferMissing(); $bootstrapMock = Mockery::mock('Icinga\Application\ApplicationBootstrap')->shouldDeferMissing();
$bootstrapMock->shouldReceive('getFrontController->getRequest')->andReturnUsing( $bootstrapMock->shouldReceive('getFrontController')->andReturn($bootstrapMock)
function () use ($request) { return $request; } ->shouldReceive('getApplicationDir')->andReturn(self::$appDir)
)->shouldReceive('getApplicationDir')->andReturn(self::$appDir); ->shouldReceive('getRequest')->andReturn($requestMock)
->shouldReceive('getResponse')->andReturn($responseMock);
Icinga::setApp($bootstrapMock, true); Icinga::setApp($bootstrapMock, true);
} }
/**
* Return the currently active request mock object
*
* @return Icinga\Web\Request
*/
public function getRequestMock()
{
return Icinga::app()->getFrontController()->getRequest();
}
/**
* Return the currently active response mock object
*
* @return Icinga\Web\Response
*/
public function getResponseMock()
{
return Icinga::app()->getFrontController()->getResponse();
}
/** /**
* Create Zend_Config for database configuration * Create Zend_Config for database configuration
* *