Skip to content

Commit

Permalink
Normalize headers mock (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfilla committed Sep 22, 2021
1 parent cce6a82 commit fe176c8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/NetteTests/Mocks/RequestMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RequestMock extends Request
private ?bool $ajaxMock;

private ?string $rawBodyMock;

private bool $rawBodyMockIsSet;

/**
Expand All @@ -30,9 +30,9 @@ class RequestMock extends Request
private ?string $methodMock;

/**
* @var array<mixed>
* @var array<mixed>|null
*/
private array $headersMock;
private ?array $headersMock;

private bool $isSameSiteMock;

Expand All @@ -56,7 +56,7 @@ public function __construct()
$this->filesMock = [];
$this->methodMock = null;
$this->rawBodyMockIsSet = false;
$this->headersMock = [];
$this->headersMock = null;
$this->isSameSiteMock = true;
$this->postMock = [];
$this->queryMock = null;
Expand Down Expand Up @@ -152,10 +152,21 @@ public function getFile($name)
*/
public function getHeader($header): ?string
{
if (isset($this->headersMock[$header])) {
return $this->headersMock[$header];
if ($this->headersMock === null) {
parent::getHeader($header);
}
return parent::getHeader($header);
return $this->headersMock[$header] ?? null;
}

/**
* @return array<mixed>
*/
public function getHeaders(): array
{
if ($this->headersMock === null) {
return parent::getHeaders();
}
return $this->headersMock;
}

public function setRawBodyMock(?string $body): self
Expand All @@ -181,14 +192,12 @@ public function setMethodMock(string $methodMock): RequestMock
}

/**
* @param mixed $header
* @param mixed $value
* @param array<mixed> $headerMock
* @return $this
*/
public function addHeaderMock($header, $value): RequestMock
public function setHeaderMock(array $headerMock): self
{
if (!isset($this->headersMock[$header])) {
$this->headersMock[$header] = $value;
}
$this->headersMock = $headerMock;
return $this;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/app/src/Presenters/ExamplePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ public function actionQueryMock(): void
}
}

public function actionHeadersMock(): void
{
$this->sendJson($this->getHttpRequest()->getHeaders());
}

}
15 changes: 15 additions & 0 deletions tests/app/tests/Presenters/ExamplePresenterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Wavevision\NetteTests\TestAppTests\Presenters;

use Nette\Application\Responses\JsonResponse;
use Nette\Application\Responses\VoidResponse;
use Nette\Http\IRequest;
use PHPUnit\Framework\AssertionFailedError;
use Wavevision\NetteTests\InvalidState;
use Wavevision\NetteTests\Mocks\InjectRequestMock;
use Wavevision\NetteTests\Runners\PresenterRequest;
use Wavevision\NetteTests\Runners\PresenterResponse;
use Wavevision\NetteTests\TestApp\Models\BrokenSignal;
Expand All @@ -15,6 +17,8 @@
class ExamplePresenterTest extends PresenterTestCase
{

use InjectRequestMock;

public function testFlashMessage(): void
{
$this->assertSame(
Expand Down Expand Up @@ -178,6 +182,17 @@ public function testQueryMock(): void
$this->assertInstanceOf(VoidResponse::class, $presenterResponse->getResponse());
}

public function testHeadersMock(): void
{
$headers = ['4' => '2'];
$this->requestMock->setHeaderMock($headers);
$presenterRequest = new PresenterRequest(ExamplePresenter::class, 'headersMock');
$presenterResponse = $this->runPresenter($presenterRequest);
$response = $presenterResponse->getResponse();
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals($headers, $response->getPayload());
}

private function getTerminatePresenterResponse(): PresenterResponse
{
$presenterRequest = new PresenterRequest(ExamplePresenter::class, 'terminateResponse');
Expand Down

0 comments on commit fe176c8

Please sign in to comment.