BaseTestCase: Refactor request and response mocking

This commit is contained in:
Johannes Meyer 2023-07-04 16:40:19 +02:00
parent 08bfc4f596
commit 82d39be642
1 changed files with 23 additions and 29 deletions

View File

@ -4,6 +4,8 @@
namespace Icinga\Test { namespace Icinga\Test {
use Exception; use Exception;
use Icinga\Web\Request;
use Icinga\Web\Response;
use ipl\I18n\NoopTranslator; use ipl\I18n\NoopTranslator;
use ipl\I18n\StaticTranslator; use ipl\I18n\StaticTranslator;
use RuntimeException; use RuntimeException;
@ -92,26 +94,28 @@ namespace Icinga\Test {
), ),
); );
/** @var Request */
private $requestMock;
/** @var Response */
private $responseMock;
/** /**
* Setup MVC bootstrapping and ensure that the Icinga-Mock gets reinitialized * Setup MVC bootstrapping and ensure that the Icinga-Mock gets reinitialized
*/ */
public function setUp(): void public function setUp(): void
{ {
parent::setUp(); parent::setUp();
$this->setupRequestMock();
$this->setupResponseMock();
StaticTranslator::$instance = new NoopTranslator(); StaticTranslator::$instance = new NoopTranslator();
$this->setupIcingaMock();
} }
/** private function setupRequestMock()
* Setup mock object for the application's bootstrap
*
* @return Mockery\Mock
*/
protected function setupIcingaMock()
{ {
$requestMock = Mockery::mock('Icinga\Web\Request')->shouldDeferMissing(); $this->requestMock = Mockery::mock('Icinga\Web\Request')->shouldDeferMissing();
$requestMock->shouldReceive('getPathInfo')->andReturn('')->byDefault() $this->requestMock->shouldReceive('getPathInfo')->andReturn('')->byDefault()
->shouldReceive('getBaseUrl')->andReturn('/')->byDefault() ->shouldReceive('getBaseUrl')->andReturn('/')->byDefault()
->shouldReceive('getQuery')->andReturn(array())->byDefault() ->shouldReceive('getQuery')->andReturn(array())->byDefault()
->shouldReceive('getParam')->with(Mockery::type('string'), Mockery::type('string')) ->shouldReceive('getParam')->with(Mockery::type('string'), Mockery::type('string'))
@ -119,43 +123,33 @@ namespace Icinga\Test {
return $default; return $default;
})->byDefault(); })->byDefault();
$responseMock = Mockery::mock('Icinga\Web\Response')->shouldDeferMissing(); Icinga::app()->setRequest($this->requestMock);
// Can't express this as demeter chains. See: https://github.com/padraic/mockery/issues/59 }
$bootstrapMock = Mockery::mock('Icinga\Application\ApplicationBootstrap')->shouldDeferMissing();
$libDir = dirname(self::$libDir);
$bootstrapMock->shouldReceive('getFrontController')->andReturn($bootstrapMock)
->shouldReceive('getApplicationDir')->andReturn(self::$appDir)
->shouldReceive('getLibraryDir')->andReturnUsing(function ($subdir = null) use ($libDir) {
if ($subdir !== null) {
$libDir .= '/' . ltrim($subdir, '/');
}
return $libDir;
})
->shouldReceive('getRequest')->andReturn($requestMock)
->shouldReceive('getResponse')->andReturn($responseMock);
Icinga::setApp($bootstrapMock, true); private function setupResponseMock()
return $bootstrapMock; {
$this->responseMock = Mockery::mock('Icinga\Web\Response')->shouldDeferMissing();
Icinga::app()->setResponse($this->responseMock);
} }
/** /**
* Return the currently active request mock object * Return the currently active request mock object
* *
* @return Icinga\Web\Request * @return Request
*/ */
public function getRequestMock() public function getRequestMock()
{ {
return Icinga::app()->getRequest(); return $this->requestMock;
} }
/** /**
* Return the currently active response mock object * Return the currently active response mock object
* *
* @return Icinga\Web\Response * @return Response
*/ */
public function getResponseMock() public function getResponseMock()
{ {
return Icinga::app()->getFrontController()->getResponse(); return $this->responseMock;
} }
/** /**