Skip to content

Commit f697368

Browse files
Migrate to AssertableJson (Laravel-core) (#338)
* Migrate to AssertableJson (Laravel-core)
1 parent 667335e commit f697368

File tree

6 files changed

+322
-198
lines changed

6 files changed

+322
-198
lines changed

src/Testing/Assert.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ class Assert implements Arrayable
3434

3535
protected function __construct(string $component, array $props, string $url, string $version = null, string $path = null)
3636
{
37+
echo "\033[0;31mInertia's built-in 'Assert' library will be removed in a future version of inertia-laravel:\033[0m\n";
38+
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";
39+
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";
40+
echo "\033[0;31mFor more information and questions, please see https://github.com/inertiajs/inertia-laravel/pull/338 \033[0m\n\n";
41+
@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);
42+
3743
$this->path = $path;
3844

3945
$this->component = $component;

src/Testing/AssertableInertia.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Inertia\Testing;
4+
5+
use Illuminate\Testing\Fluent\AssertableJson;
6+
use Illuminate\Testing\TestResponse;
7+
use InvalidArgumentException;
8+
use PHPUnit\Framework\Assert as PHPUnit;
9+
use PHPUnit\Framework\AssertionFailedError;
10+
11+
class AssertableInertia extends AssertableJson
12+
{
13+
/** @var string */
14+
private $component;
15+
16+
/** @var string */
17+
private $url;
18+
19+
/** @var string|null */
20+
private $version;
21+
22+
public static function fromTestResponse(TestResponse $response): self
23+
{
24+
try {
25+
$response->assertViewHas('page');
26+
$page = json_decode(json_encode($response->viewData('page')), true);
27+
28+
PHPUnit::assertIsArray($page);
29+
PHPUnit::assertArrayHasKey('component', $page);
30+
PHPUnit::assertArrayHasKey('props', $page);
31+
PHPUnit::assertArrayHasKey('url', $page);
32+
PHPUnit::assertArrayHasKey('version', $page);
33+
} catch (AssertionFailedError $e) {
34+
PHPUnit::fail('Not a valid Inertia response.');
35+
}
36+
37+
$instance = static::fromArray($page['props']);
38+
$instance->component = $page['component'];
39+
$instance->url = $page['url'];
40+
$instance->version = $page['version'];
41+
42+
return $instance;
43+
}
44+
45+
public function component(string $value = null, $shouldExist = null): self
46+
{
47+
PHPUnit::assertSame($value, $this->component, 'Unexpected Inertia page component.');
48+
49+
if ($shouldExist || (is_null($shouldExist) && config('inertia.testing.ensure_pages_exist', true))) {
50+
try {
51+
app('inertia.testing.view-finder')->find($value);
52+
} catch (InvalidArgumentException $exception) {
53+
PHPUnit::fail(sprintf('Inertia page component file [%s] does not exist.', $value));
54+
}
55+
}
56+
57+
return $this;
58+
}
59+
60+
public function url(string $value): self
61+
{
62+
PHPUnit::assertSame($value, $this->url, 'Unexpected Inertia page url.');
63+
64+
return $this;
65+
}
66+
67+
public function version(string $value): self
68+
{
69+
PHPUnit::assertSame($value, $this->version, 'Unexpected Inertia asset version.');
70+
71+
return $this;
72+
}
73+
74+
public function toArray()
75+
{
76+
return [
77+
'component' => $this->component,
78+
'props' => $this->prop(),
79+
'url' => $this->url,
80+
'version' => $this->version,
81+
];
82+
}
83+
}

src/Testing/TestResponseMacros.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
namespace Inertia\Testing;
44

55
use Closure;
6+
use Illuminate\Testing\Fluent\AssertableJson;
67

78
class TestResponseMacros
89
{
910
public function assertInertia()
1011
{
1112
return function (Closure $callback = null) {
12-
$assert = Assert::fromTestResponse($this);
13+
if (class_exists(AssertableJson::class)) {
14+
$assert = AssertableInertia::fromTestResponse($this);
15+
} else {
16+
$assert = Assert::fromTestResponse($this);
17+
}
1318

1419
if (is_null($callback)) {
1520
return $this;
@@ -24,6 +29,10 @@ public function assertInertia()
2429
public function inertiaPage()
2530
{
2631
return function () {
32+
if (class_exists(AssertableJson::class)) {
33+
return AssertableInertia::fromTestResponse($this)->toArray();
34+
}
35+
2736
return Assert::fromTestResponse($this)->toArray();
2837
};
2938
}

tests/Testing/AssertTest.php

Lines changed: 6 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Foundation\Auth\User;
88
use Illuminate\Http\Resources\Json\JsonResource;
99
use Illuminate\Support\Collection;
10+
use Illuminate\Testing\Fluent\AssertableJson;
1011
use Inertia\Inertia;
1112
use Inertia\Testing\Assert;
1213
use Inertia\Tests\TestCase;
@@ -15,146 +16,13 @@
1516

1617
class AssertTest extends TestCase
1718
{
18-
/** @test */
19-
public function the_view_is_served_by_inertia(): void
19+
public function setUp(): void
2020
{
21-
$response = $this->makeMockRequest(
22-
Inertia::render('foo')
23-
);
21+
parent::setUp();
2422

25-
$response->assertInertia();
26-
}
27-
28-
/** @test */
29-
public function the_view_is_not_served_by_inertia(): void
30-
{
31-
$response = $this->makeMockRequest(view('welcome'));
32-
$response->assertOk(); // Make sure we can render the built-in Orchestra 'welcome' view..
33-
34-
$this->expectException(AssertionFailedError::class);
35-
$this->expectExceptionMessage('Not a valid Inertia response.');
36-
37-
$response->assertInertia();
38-
}
39-
40-
/** @test */
41-
public function the_component_matches(): void
42-
{
43-
$response = $this->makeMockRequest(
44-
Inertia::render('foo')
45-
);
46-
47-
$response->assertInertia(function (Assert $inertia) {
48-
$inertia->component('foo');
49-
});
50-
}
51-
52-
/** @test */
53-
public function the_component_does_not_match(): void
54-
{
55-
$response = $this->makeMockRequest(
56-
Inertia::render('foo')
57-
);
58-
59-
$this->expectException(AssertionFailedError::class);
60-
$this->expectExceptionMessage('Unexpected Inertia page component.');
61-
62-
$response->assertInertia(function (Assert $inertia) {
63-
$inertia->component('bar');
64-
});
65-
}
66-
67-
/** @test */
68-
public function the_component_exists_on_the_filesystem(): void
69-
{
70-
$response = $this->makeMockRequest(
71-
Inertia::render('Stubs/ExamplePage')
72-
);
73-
74-
config()->set('inertia.testing.ensure_pages_exist', true);
75-
$response->assertInertia(function (Assert $inertia) {
76-
$inertia->component('Stubs/ExamplePage');
77-
});
78-
}
79-
80-
/** @test */
81-
public function the_component_does_not_exist_on_the_filesystem(): void
82-
{
83-
$response = $this->makeMockRequest(
84-
Inertia::render('foo')
85-
);
86-
87-
config()->set('inertia.testing.ensure_pages_exist', true);
88-
$this->expectException(AssertionFailedError::class);
89-
$this->expectExceptionMessage('Inertia page component file [foo] does not exist.');
90-
91-
$response->assertInertia(function (Assert $inertia) {
92-
$inertia->component('foo');
93-
});
94-
}
95-
96-
/** @test */
97-
public function it_can_force_enable_the_component_file_existence(): void
98-
{
99-
$response = $this->makeMockRequest(
100-
Inertia::render('foo')
101-
);
102-
103-
config()->set('inertia.testing.ensure_pages_exist', false);
104-
$this->expectException(AssertionFailedError::class);
105-
$this->expectExceptionMessage('Inertia page component file [foo] does not exist.');
106-
107-
$response->assertInertia(function (Assert $inertia) {
108-
$inertia->component('foo', true);
109-
});
110-
}
111-
112-
/** @test */
113-
public function it_can_force_disable_the_component_file_existence_check(): void
114-
{
115-
$response = $this->makeMockRequest(
116-
Inertia::render('foo')
117-
);
118-
119-
config()->set('inertia.testing.ensure_pages_exist', true);
120-
121-
$response->assertInertia(function (Assert $inertia) {
122-
$inertia->component('foo', false);
123-
});
124-
}
125-
126-
/** @test */
127-
public function the_component_does_not_exist_on_the_filesystem_when_it_does_not_exist_relative_to_any_of_the_given_paths(): void
128-
{
129-
$response = $this->makeMockRequest(
130-
Inertia::render('fixtures/ExamplePage')
131-
);
132-
133-
config()->set('inertia.testing.ensure_pages_exist', true);
134-
config()->set('inertia.testing.page_paths', [realpath(__DIR__)]);
135-
$this->expectException(AssertionFailedError::class);
136-
$this->expectExceptionMessage('Inertia page component file [fixtures/ExamplePage] does not exist.');
137-
138-
$response->assertInertia(function (Assert $inertia) {
139-
$inertia->component('fixtures/ExamplePage');
140-
});
141-
}
142-
143-
/** @test */
144-
public function the_component_does_not_exist_on_the_filesystem_when_it_does_not_have_one_of_the_configured_extensions(): void
145-
{
146-
$response = $this->makeMockRequest(
147-
Inertia::render('fixtures/ExamplePage')
148-
);
149-
150-
config()->set('inertia.testing.ensure_pages_exist', true);
151-
config()->set('inertia.testing.page_extensions', ['bin', 'exe', 'svg']);
152-
$this->expectException(AssertionFailedError::class);
153-
$this->expectExceptionMessage('Inertia page component file [fixtures/ExamplePage] does not exist.');
154-
155-
$response->assertInertia(function (Assert $inertia) {
156-
$inertia->component('fixtures/ExamplePage');
157-
});
23+
if (class_exists(AssertableJson::class)) {
24+
$this->markTestSkipped("These tests are not applicable on Laravel 8.32 or newer, as Laravel's built-in AssertableJson is used instead.");
25+
}
15826
}
15927

16028
/** @test */
@@ -1202,64 +1070,6 @@ public function it_cannot_count_multiple_props_at_once_when_at_least_one_is_miss
12021070
});
12031071
}
12041072

1205-
/** @test */
1206-
public function the_page_url_matches(): void
1207-
{
1208-
$response = $this->makeMockRequest(
1209-
Inertia::render('foo')
1210-
);
1211-
1212-
$response->assertInertia(function (Assert $inertia) {
1213-
$inertia->url('/example-url');
1214-
});
1215-
}
1216-
1217-
/** @test */
1218-
public function the_page_url_does_not_match(): void
1219-
{
1220-
$response = $this->makeMockRequest(
1221-
Inertia::render('foo')
1222-
);
1223-
1224-
$this->expectException(AssertionFailedError::class);
1225-
$this->expectExceptionMessage('Unexpected Inertia page url.');
1226-
1227-
$response->assertInertia(function (Assert $inertia) {
1228-
$inertia->url('/invalid-page');
1229-
});
1230-
}
1231-
1232-
/** @test */
1233-
public function the_asset_version_matches(): void
1234-
{
1235-
Inertia::version('example-version');
1236-
1237-
$response = $this->makeMockRequest(
1238-
Inertia::render('foo')
1239-
);
1240-
1241-
$response->assertInertia(function (Assert $inertia) {
1242-
$inertia->version('example-version');
1243-
});
1244-
}
1245-
1246-
/** @test */
1247-
public function the_asset_version_does_not_match(): void
1248-
{
1249-
Inertia::version('example-version');
1250-
1251-
$response = $this->makeMockRequest(
1252-
Inertia::render('foo')
1253-
);
1254-
1255-
$this->expectException(AssertionFailedError::class);
1256-
$this->expectExceptionMessage('Unexpected Inertia asset version.');
1257-
1258-
$response->assertInertia(function (Assert $inertia) {
1259-
$inertia->version('different-version');
1260-
});
1261-
}
1262-
12631073
/** @test */
12641074
public function it_is_macroable(): void
12651075
{

0 commit comments

Comments
 (0)