Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/Testing/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class Assert implements Arrayable

protected function __construct(string $component, array $props, string $url, string $version = null, string $path = null)
{
echo "\033[0;31mInertia's built-in 'Assert' library will be removed in a future version of inertia-laravel:\033[0m\n";
echo "\033[0;31m - If you are seeing this error while using \$response->assertInertia(...), please upgrade to Laravel 8.32.0 or higher.\033[0m\n";
echo "\033[0;31m - If you are using the 'Assert' class directly, please adapt your tests to use the 'AssertableInertia' class instead.\033[0m\n";
echo "\033[0;31mFor more information and questions, please see https://github.com/inertiajs/inertia-laravel/pull/338 \033[0m\n\n";
@trigger_error("Inertia's built-in 'Assert' library will be removed in a future version of inertia-laravel: https://github.com/inertiajs/inertia-laravel/pull/338", \E_USER_DEPRECATED);

$this->path = $path;

$this->component = $component;
Expand Down
83 changes: 83 additions & 0 deletions src/Testing/AssertableInertia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Inertia\Testing;

use Illuminate\Testing\Fluent\AssertableJson;
use Illuminate\Testing\TestResponse;
use InvalidArgumentException;
use PHPUnit\Framework\Assert as PHPUnit;
use PHPUnit\Framework\AssertionFailedError;

class AssertableInertia extends AssertableJson
{
/** @var string */
private $component;

/** @var string */
private $url;

/** @var string|null */
private $version;

public static function fromTestResponse(TestResponse $response): self
{
try {
$response->assertViewHas('page');
$page = json_decode(json_encode($response->viewData('page')), true);

PHPUnit::assertIsArray($page);
PHPUnit::assertArrayHasKey('component', $page);
PHPUnit::assertArrayHasKey('props', $page);
PHPUnit::assertArrayHasKey('url', $page);
PHPUnit::assertArrayHasKey('version', $page);
} catch (AssertionFailedError $e) {
PHPUnit::fail('Not a valid Inertia response.');
}

$instance = static::fromArray($page['props']);
$instance->component = $page['component'];
$instance->url = $page['url'];
$instance->version = $page['version'];

return $instance;
}

public function component(string $value = null, $shouldExist = null): self
{
PHPUnit::assertSame($value, $this->component, 'Unexpected Inertia page component.');

if ($shouldExist || (is_null($shouldExist) && config('inertia.testing.ensure_pages_exist', true))) {
try {
app('inertia.testing.view-finder')->find($value);
} catch (InvalidArgumentException $exception) {
PHPUnit::fail(sprintf('Inertia page component file [%s] does not exist.', $value));
}
}

return $this;
}

public function url(string $value): self
{
PHPUnit::assertSame($value, $this->url, 'Unexpected Inertia page url.');

return $this;
}

public function version(string $value): self
{
PHPUnit::assertSame($value, $this->version, 'Unexpected Inertia asset version.');

return $this;
}

public function toArray()
{
return [
'component' => $this->component,
'props' => $this->prop(),
'url' => $this->url,
'version' => $this->version,
];
}
}
11 changes: 10 additions & 1 deletion src/Testing/TestResponseMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
namespace Inertia\Testing;

use Closure;
use Illuminate\Testing\Fluent\AssertableJson;

class TestResponseMacros
{
public function assertInertia()
{
return function (Closure $callback = null) {
$assert = Assert::fromTestResponse($this);
if (class_exists(AssertableJson::class)) {
$assert = AssertableInertia::fromTestResponse($this);
} else {
$assert = Assert::fromTestResponse($this);
}

if (is_null($callback)) {
return $this;
Expand All @@ -24,6 +29,10 @@ public function assertInertia()
public function inertiaPage()
{
return function () {
if (class_exists(AssertableJson::class)) {
return AssertableInertia::fromTestResponse($this)->toArray();
}

return Assert::fromTestResponse($this)->toArray();
};
}
Expand Down
202 changes: 6 additions & 196 deletions tests/Testing/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Collection;
use Illuminate\Testing\Fluent\AssertableJson;
use Inertia\Inertia;
use Inertia\Testing\Assert;
use Inertia\Tests\TestCase;
Expand All @@ -15,146 +16,13 @@

class AssertTest extends TestCase
{
/** @test */
public function the_view_is_served_by_inertia(): void
public function setUp(): void
{
$response = $this->makeMockRequest(
Inertia::render('foo')
);
parent::setUp();

$response->assertInertia();
}

/** @test */
public function the_view_is_not_served_by_inertia(): void
{
$response = $this->makeMockRequest(view('welcome'));
$response->assertOk(); // Make sure we can render the built-in Orchestra 'welcome' view..

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Not a valid Inertia response.');

$response->assertInertia();
}

/** @test */
public function the_component_matches(): void
{
$response = $this->makeMockRequest(
Inertia::render('foo')
);

$response->assertInertia(function (Assert $inertia) {
$inertia->component('foo');
});
}

/** @test */
public function the_component_does_not_match(): void
{
$response = $this->makeMockRequest(
Inertia::render('foo')
);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Unexpected Inertia page component.');

$response->assertInertia(function (Assert $inertia) {
$inertia->component('bar');
});
}

/** @test */
public function the_component_exists_on_the_filesystem(): void
{
$response = $this->makeMockRequest(
Inertia::render('Stubs/ExamplePage')
);

config()->set('inertia.testing.ensure_pages_exist', true);
$response->assertInertia(function (Assert $inertia) {
$inertia->component('Stubs/ExamplePage');
});
}

/** @test */
public function the_component_does_not_exist_on_the_filesystem(): void
{
$response = $this->makeMockRequest(
Inertia::render('foo')
);

config()->set('inertia.testing.ensure_pages_exist', true);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Inertia page component file [foo] does not exist.');

$response->assertInertia(function (Assert $inertia) {
$inertia->component('foo');
});
}

/** @test */
public function it_can_force_enable_the_component_file_existence(): void
{
$response = $this->makeMockRequest(
Inertia::render('foo')
);

config()->set('inertia.testing.ensure_pages_exist', false);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Inertia page component file [foo] does not exist.');

$response->assertInertia(function (Assert $inertia) {
$inertia->component('foo', true);
});
}

/** @test */
public function it_can_force_disable_the_component_file_existence_check(): void
{
$response = $this->makeMockRequest(
Inertia::render('foo')
);

config()->set('inertia.testing.ensure_pages_exist', true);

$response->assertInertia(function (Assert $inertia) {
$inertia->component('foo', false);
});
}

/** @test */
public function the_component_does_not_exist_on_the_filesystem_when_it_does_not_exist_relative_to_any_of_the_given_paths(): void
{
$response = $this->makeMockRequest(
Inertia::render('fixtures/ExamplePage')
);

config()->set('inertia.testing.ensure_pages_exist', true);
config()->set('inertia.testing.page_paths', [realpath(__DIR__)]);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Inertia page component file [fixtures/ExamplePage] does not exist.');

$response->assertInertia(function (Assert $inertia) {
$inertia->component('fixtures/ExamplePage');
});
}

/** @test */
public function the_component_does_not_exist_on_the_filesystem_when_it_does_not_have_one_of_the_configured_extensions(): void
{
$response = $this->makeMockRequest(
Inertia::render('fixtures/ExamplePage')
);

config()->set('inertia.testing.ensure_pages_exist', true);
config()->set('inertia.testing.page_extensions', ['bin', 'exe', 'svg']);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Inertia page component file [fixtures/ExamplePage] does not exist.');

$response->assertInertia(function (Assert $inertia) {
$inertia->component('fixtures/ExamplePage');
});
if (class_exists(AssertableJson::class)) {
$this->markTestSkipped("These tests are not applicable on Laravel 8.32 or newer, as Laravel's built-in AssertableJson is used instead.");
}
}

/** @test */
Expand Down Expand Up @@ -1202,64 +1070,6 @@ public function it_cannot_count_multiple_props_at_once_when_at_least_one_is_miss
});
}

/** @test */
public function the_page_url_matches(): void
{
$response = $this->makeMockRequest(
Inertia::render('foo')
);

$response->assertInertia(function (Assert $inertia) {
$inertia->url('/example-url');
});
}

/** @test */
public function the_page_url_does_not_match(): void
{
$response = $this->makeMockRequest(
Inertia::render('foo')
);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Unexpected Inertia page url.');

$response->assertInertia(function (Assert $inertia) {
$inertia->url('/invalid-page');
});
}

/** @test */
public function the_asset_version_matches(): void
{
Inertia::version('example-version');

$response = $this->makeMockRequest(
Inertia::render('foo')
);

$response->assertInertia(function (Assert $inertia) {
$inertia->version('example-version');
});
}

/** @test */
public function the_asset_version_does_not_match(): void
{
Inertia::version('example-version');

$response = $this->makeMockRequest(
Inertia::render('foo')
);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Unexpected Inertia asset version.');

$response->assertInertia(function (Assert $inertia) {
$inertia->version('different-version');
});
}

/** @test */
public function it_is_macroable(): void
{
Expand Down
Loading