Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertion Tests & Implementation #51

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Testing/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Inertia\Testing;

use Inertia\Testing\TestResponse;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
/**
* Create the test response instance from the given response.
*
* @param \Illuminate\Http\Response $response
*
* @return \Illuminate\Foundation\Testing\TestResponse
*/
protected function createTestResponse($response)
{
return TestResponse::fromBaseResponse($response);
}
}
57 changes: 57 additions & 0 deletions src/Testing/TestResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Inertia\Testing;

use Illuminate\Support\Arr;
use PHPUnit\Framework\Assert as PHPUnit;
use Illuminate\Foundation\Testing\TestResponse as BaseResponse;

class TestResponse extends BaseResponse
{
public function assertHasProp($prop)
{
PHPUnit::assertTrue(Arr::has($this->props(), $prop));

return $this;
}

public function assertPropValue($prop, $value)
{
$this->assertHasProp($prop);

if (is_callable($value)) {
$value($this->props($prop));
} else {
PHPUnit::assertEquals($this->props($prop), $value);
}

return $this;
}

public function assertPropCount($prop, $count)
{
$this->assertHasProp($prop);

PHPUnit::assertCount($count, $this->props($prop));

return $this;
}

public function assertComponent($component)
{
PHPUnit::assertEquals($component, $this->original['page']['component']);

return $this;
}

protected function props($key = null)
{
$props = json_decode(json_encode($this->original['page']['props']), JSON_OBJECT_AS_ARRAY);

if ($key) {
return Arr::get($props, $key);
}

return $props;
}
}
56 changes: 56 additions & 0 deletions tests/TestResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use Illuminate\Http\Response;
use Inertia\Testing\TestResponse;
use PHPUnit\Framework\TestCase;

class TestResponseTest extends TestCase
{
public function test_assert_has_prop()
{
$response = $this->makeMockResponse([
'prop' => 'value'
]);

$response->assertHasProp('prop');
}

public function test_assert_prop_value()
{
$response = $this->makeMockResponse([
'prop' => 'value'
]);

$response->assertPropValue('prop', 'value');
}

public function test_assert_prop_count()
{
$response = $this->makeMockResponse([
'prop' => [1, 2, 3],
]);

$response->assertPropCount('prop', 3);
}

public function test_assert_component()
{
$response = $this->makeMockResponse();

$response->assertComponent('mock');
}

private function makeMockResponse($content = [])
{
$baseResponse = tap(new Response, function ($response) use ($content) {
$response->setContent([
'page' => [
'props' => $content,
'component' => 'mock'
],
]);
});

return TestResponse::fromBaseResponse($baseResponse);
}
}