Skip to content

Commit

Permalink
add test assertation class
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Oct 3, 2023
1 parent 2563efa commit 3b2b54d
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/System/Integrate/Testing/ApiResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace System\Integrate\Testing;

use PHPUnit\Framework\Assert;
use System\Http\Response;

/**
* @implements \ArrayAccess<string, mixed>
*/
class ApiResponseTest extends ResponseTest implements \ArrayAccess
{
/**
* @var array<string, mixed>
*/
private array $respone_data;

/**
* @param array<string, mixed> $respone_data
*/
public function __construct(Response $response, $respone_data = [])
{
$this->response = $response;
$this->respone_data = $respone_data;
}

/**
* @return mixed
*/
public function getData()
{
return $this->respone_data['data'];
}

public function offsetExists($offset): bool
{
return array_key_exists($offset, $this->respone_data);
}

#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->respone_data[$offset];
}

public function offsetSet($offset, $value): void
{
$this->respone_data[$offset] = $value;
}

public function offsetUnset($offset): void
{
unset($this->respone_data[$offset]);
}

public function assertDataEmpty(): void
{
Assert::assertEmpty($this->getData());
}

public function assertDataNotEmpty(): void
{
Assert::assertNotEmpty($this->getData());
}

/**
* @param mixed $value
*/
public function assertEqual(string $data_key, $value): void
{
$data_get = data_get($this->respone_data, $data_key);
Assert::assertEquals($data_get, $value);
}

public function assertTrue(string $data_key, string $message = ''): void
{
$data_get = data_get($this->respone_data, $data_key);
Assert::assertTrue($data_get, $message);
}

public function assertFalse(string $data_key, string $message = ''): void
{
$data_get = data_get($this->respone_data, $data_key);
Assert::assertFalse($data_get, $message);
}

public function assertNull(string $data_key, string $message = ''): void
{
$data_get = data_get($this->respone_data, $data_key);
Assert::assertNull($data_get, $message);
}

public function assertNotNull(string $data_key, string $message = ''): void
{
$data_get = data_get($this->respone_data, $data_key);
Assert::assertNotNull($data_get, $message);
}
}
53 changes: 53 additions & 0 deletions src/System/Integrate/Testing/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace System\Integrate\Testing;

use PHPUnit\Framework\Assert;
use System\Http\Response;

class ResponseTest
{
protected Response $response;

public function __construct(Response $response)
{
$this->response = $response;
}

public function getContent(): string
{
return $this->response->getContent();
}

public function assertSee(string $text, string $message = ''): void
{
Assert::assertStringContainsString($text, $this->response->getContent(), $message);
}

public function assertStatusCode(int $code, string $message = ''): void
{
Assert::assertSame($code, $this->response->getStatusCode(), $message);
}

public function assertOk(): void
{
$this->assertStatusCode(Response::HTTP_OK, 'Respone code must return ok');
}

public function assertNoContent(): void
{
$this->assertStatusCode(Response::HTTP_NO_CONTENT, 'Respone code must return no content');
}

public function assertNotFound(): void
{
$this->assertStatusCode(Response::HTTP_NOT_FOUND, 'Respone code must return Not Found');
}

public function assertNotAllowed(): void
{
$this->assertStatusCode(Response::HTTP_METHOD_NOT_ALLOWED, 'Respone code must return Not Allowed');
}
}

0 comments on commit 3b2b54d

Please sign in to comment.