Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Oct 8, 2023
1 parent d53bae2 commit eba1858
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/System/Integrate/Testing/TaseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,65 @@
namespace System\Integrate\Testing;

use PHPUnit\Framework\TestCase as BaseTestCase;
use System\Http\Request;
use System\Http\Response;
use System\Integrate\Application;
use System\Integrate\Http\Karnel;

class TestCase extends BaseTestCase
{
protected Application $app;
protected Karnel $karnel;
protected string $class;

protected function setUp(): void
{
// create app
$this->karnel = $this->app->make(Karnel::class);
}

protected function tearDown(): void
{
$this->app->flush();
unset($this->app);
unset($this->karnel);
}

/**
* @param array<string, string>|string $call call the given function using the given parameters
* @param array<string, string> $params
*/
protected function json($call, array $params = []): TestJsonResponse
{
$data = $this->app->call($call, $params);
$response = new Response($data);
if (array_key_exists('code', $data)) {
$response->setResponeCode((int) $data['code']);
}
if (array_key_exists('headers', $data)) {
$response->setHeaders($data['headers']);
}

return new TestJsonResponse($response);
}

/**
* @param array<string, string> $parameter
*/
protected function get(string $url, array $parameter = []): TestResponse
{
return new TestResponse(
$this->karnel->handle(new Request($url, $parameter, [], [], [], [], [], 'GET'))
);
}

/**
* @param array<string, string> $post
*/
protected function post(string $url, array $post): TestResponse
{
return new TestResponse(
$this->karnel->handle(new Request($url, [], $post, [], [], [], [], 'POST'))
);
}
}
168 changes: 168 additions & 0 deletions tests/Integrate/Testing/TestJsonResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

declare(strict_types=1);

namespace System\Test\Integrate\Testing;

use PHPUnit\Framework\TestCase;
use System\Http\Response;
use System\Integrate\Testing\TestJsonResponse;

final class TestJsonResponseTest extends TestCase
{
/**
* @test
*/
public function itCanTestResponeseAsArray()
{
$response = new TestJsonResponse(new Response([
'status'=> 'ok',
'code' => 200,
'data' => [
'test' => 'success',
],
'error' => null,
]));
$response['test'] = 'test';

$this->assertEquals('ok', $response['status']);
$this->assertEquals('test', $response['test']);
}

/**
* @test
*/
public function itCanTestResponeseAssert()
{
$response = new TestJsonResponse(new Response([
'status'=> 'ok',
'code' => 200,
'data' => [
'test' => 'success',
],
'error' => null,
]));

$this->assertEquals(['test' => 'success'], $response->getData());
$this->assertEquals('ok', $response['status']);
}

/**
* @test
*/
public function itCanTestResponeseAssertEqual()
{
$response = new TestJsonResponse(new Response([
'status'=> 'ok',
'code' => 200,
'data' => [
'test' => 'success',
],
'error' => null,
]));

$response->assertEqual('data.test', 'success');
}

/**
* @test
*/
public function itCanTestResponeseAssertTrue()
{
$response = new TestJsonResponse(new Response([
'status'=> 'ok',
'code' => 200,
'data' => [
'test' => true,
],
'error' => null,
]));

$response->assertTrue('data.test');
}

/**
* @test
*/
public function itCanTestResponeseAssertFalse()
{
$response = new TestJsonResponse(new Response([
'status'=> 'ok',
'code' => 200,
'data' => [
'test' => false,
],
'error' => null,
]));

$response->assertFalse('data.test');
}

/**
* @test
*/
public function itCanTestResponeseAssertNull()
{
$response = new TestJsonResponse(new Response([
'status'=> 'ok',
'code' => 200,
'data' => [
'test' => false,
],
'error' => null,
]));

$response->assertNull('error');
}

/**
* @test
*/
public function itCanTestResponeseAssertNotNull()
{
$response = new TestJsonResponse(new Response([
'status'=> 'ok',
'code' => 200,
'data' => [
'test' => false,
],
'error' => [
'test' => 'some erroe',
],
]));

$response->assertNotNull('error');
}

/**
* @test
*/
public function itCanTestResponeseAssertEmpty()
{
$response = new TestJsonResponse(new Response([
'status'=> 'ok',
'code' => 200,
'data' => [],
'error' => null,
]));

$response->assertEmpty('error');
}

/**
* @test
*/
public function itCanTestResponeseAssertNotEmpty()
{
$response = new TestJsonResponse(new Response([
'status'=> 'ok',
'code' => 200,
'data' => [
'test' => false,
],
'error' => null,
]));

$response->assertNotEmpty('error');
}
}
24 changes: 24 additions & 0 deletions tests/Integrate/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace System\Test\Integrate\Testing;

use PHPUnit\Framework\TestCase;
use System\Http\Response;
use System\Integrate\Testing\TestResponse;

final class TestResponseTest extends TestCase
{
/**
* @test
*/
public function itCanTestResponeseAssert()
{
$response = new TestResponse(new Response('test', 200, []));

$this->assertEquals('test', $response->getContent());
$response->assertSee('test');
$response->assertStatusCode(200);
}
}
52 changes: 52 additions & 0 deletions tests/Integrate/Testing/Traits/ResponseStatusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace System\Test\Integrate\Testing\Traits;

use PHPUnit\Framework\TestCase;
use System\Http\Response;
use System\Integrate\Testing\TestResponse;

final class ResponseStatusTrait extends TestCase
{
/**
* @test
*/
public function itCanTestResponeseAssertOk()
{
$response = new TestResponse(new Response('test', 200, []));

$response->assertOk();
}

/**
* @test
*/
public function itCanTestResponeseAssertNoContent()
{
$response = new TestResponse(new Response('', 204, []));

$response->assertNoContent();
}

/**
* @test
*/
public function itCanTestResponeseAssertNotFound()
{
$response = new TestResponse(new Response('', 404, []));

$response->assertNotFound();
}

/**
* @test
*/
public function itCanTestResponeseAssertNotAllowed()
{
$response = new TestResponse(new Response('', 404, []));

$response->assertNotFound();
}
}

0 comments on commit eba1858

Please sign in to comment.