Skip to content

Commit

Permalink
Optimize test traits
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Apr 6, 2024
1 parent 160ed2b commit 961992f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 22 deletions.
2 changes: 1 addition & 1 deletion tests/TestCase/Action/Home/HomeActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testAction(): void
$response = $this->app->handle($request);

$this->assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
$this->assertResponseContains($response, 'Welcome!');
$this->assertResponseContains('Welcome!', $response);
}

public function testPageNotFound(): void
Expand Down
6 changes: 1 addition & 5 deletions tests/Traits/AppTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

trait AppTestTrait
{
use ArrayTestTrait;
use ContainerTestTrait;
use HttpTestTrait;
use HttpJsonTestTrait;
Expand All @@ -30,10 +31,5 @@ protected function setUpApp(): void
$this->app = $container->get(App::class);

$this->setUpContainer($container);

/** @phpstan-ignore-next-line */
if (method_exists($this, 'setUpDatabase')) {
$this->setUpDatabase(__DIR__ . '/../../resources/schema/schema.sql');
}
}
}
40 changes: 40 additions & 0 deletions tests/Traits/ArrayTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Test\Traits;

/**
* Array Test Trait.
*/
trait ArrayTestTrait
{
/**
* Read array value with dot notation.
*
* @param array $data The array
* @param string $path The path
* @param mixed $default The default return value#
*
* @return mixed The value from the array or the default value
*/
protected function getArrayValue(array $data, string $path, mixed $default = null): mixed
{
$currentValue = $data;
$keyPaths = (array)explode('.', $path);

foreach ($keyPaths as $currentKey) {
if (isset($currentValue->$currentKey)) {
$currentValue = $currentValue->$currentKey;
continue;
}
if (isset($currentValue[$currentKey])) {
$currentValue = $currentValue[$currentKey];
continue;
}

return $default;
}

// @phpstan-ignore-next-line
return $currentValue === null ? $default : $currentValue;
}
}
9 changes: 3 additions & 6 deletions tests/Traits/ContainerTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
*/
trait ContainerTestTrait
{
/**
* @var ContainerInterface
*/
protected $container;
protected ?ContainerInterface $container;

/**
* Setup DI container.
Expand Down Expand Up @@ -48,9 +45,9 @@ protected function setUpContainer(ContainerInterface $container = null): void
*
* @return void
*/
protected function setContainerValue(string $name, $value): void
protected function setContainerValue(string $name, mixed $value): void
{
if (method_exists($this->container, 'set')) {
if (isset($this->container) && method_exists($this->container, 'set')) {
$this->container->set($name, $value);

return;
Expand Down
13 changes: 8 additions & 5 deletions tests/Traits/HttpJsonTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ trait HttpJsonTestTrait
*
* @return ServerRequestInterface
*/
protected function createJsonRequest(string $method, $uri, array $data = null): ServerRequestInterface
{
protected function createJsonRequest(
string $method,
string|UriInterface $uri,
array $data = null
): ServerRequestInterface {
$request = $this->createRequest($method, $uri);

if ($data !== null) {
Expand All @@ -36,12 +39,12 @@ protected function createJsonRequest(string $method, $uri, array $data = null):
/**
* Verify that the specified array is an exact match for the returned JSON.
*
* @param ResponseInterface $response The response
* @param array $expected The expected array
* @param ResponseInterface $response The response
*
* @return void
*/
protected function assertJsonData(ResponseInterface $response, array $expected): void
protected function assertJsonData(array $expected, ResponseInterface $response): void
{
$data = $this->getJsonData($response);

Expand Down Expand Up @@ -84,7 +87,7 @@ protected function assertJsonContentType(ResponseInterface $response): void
*
* @return void
*/
protected function assertJsonValue($expected, string $path, ResponseInterface $response)
protected function assertJsonValue(mixed $expected, string $path, ResponseInterface $response): void
{
$this->assertSame($expected, $this->getArrayValue($this->getJsonData($response), $path));
}
Expand Down
13 changes: 8 additions & 5 deletions tests/Traits/HttpTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ trait HttpTestTrait
*
* @return ServerRequestInterface The request
*/
protected function createRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
protected function createRequest(
string $method,
string|UriInterface $uri,
array $serverParams = []
): ServerRequestInterface {
if (!$this->container instanceof ContainerInterface) {
throw new RuntimeException('DI container not found');
}
Expand Down Expand Up @@ -81,15 +84,15 @@ protected function createResponse(int $code = 200, string $reasonPhrase = ''): R
/**
* Assert that the response body contains a string.
*
* @param string $expected The expected string
* @param ResponseInterface $response The response
* @param string $needle The expected string
*
* @return void
*/
protected function assertResponseContains(ResponseInterface $response, string $needle): void
protected function assertResponseContains(string $expected, ResponseInterface $response): void
{
$body = (string)$response->getBody();

$this->assertStringContainsString($needle, $body);
$this->assertStringContainsString($expected, $body);
}
}

0 comments on commit 961992f

Please sign in to comment.