Skip to content

Commit 6067b7c

Browse files
committed
Add basic tests
1 parent 33b4cdb commit 6067b7c

File tree

6 files changed

+102
-4
lines changed

6 files changed

+102
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor
22
.DS_Store
3+
.phpunit.result.cache
34
composer.lock

composer.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
"Tests\\": "tests/"
2222
}
2323
},
24-
"require": {
25-
"illuminate/contracts": "^5.8",
26-
"illuminate/support": "^5.8"
27-
},
2824
"require-dev": {
2925
"orchestra/testbench": "~3.0"
3026
},

phpunit.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.4/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
forceCoversAnnotation="true"
6+
beStrictAboutCoversAnnotation="true"
7+
beStrictAboutOutputDuringTests="true"
8+
beStrictAboutTodoAnnotatedTests="true"
9+
verbose="true">
10+
<testsuites>
11+
<testsuite name="default">
12+
<directory suffix="Test.php">tests</directory>
13+
</testsuite>
14+
</testsuites>
15+
<filter>
16+
<whitelist processUncoveredFilesFromWhitelist="true">
17+
<directory suffix=".php">src</directory>
18+
</whitelist>
19+
</filter>
20+
</phpunit>

tests/ResponseTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Inertia\Response;
6+
use Illuminate\View\View;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Http\JsonResponse;
9+
10+
class ResponseTest extends TestCase
11+
{
12+
public function test_server_response()
13+
{
14+
$request = Request::create('/user/123', 'GET');
15+
16+
$response = new Response(
17+
'User/Edit',
18+
['user' => ['name' => 'Jonathan']],
19+
'app',
20+
['foo' => 'bar'],
21+
'123'
22+
);
23+
24+
$response = $response->toResponse($request);
25+
$page = $response->getData()['page'];
26+
27+
$this->assertInstanceOf(View::class, $response);
28+
$this->assertSame('User/Edit', $page['component']);
29+
$this->assertSame('Jonathan', $page['props']['user']['name']);
30+
$this->assertSame('bar', $page['props']['foo']);
31+
$this->assertSame('/user/123', $page['url']);
32+
$this->assertSame('123', $page['version']);
33+
$this->assertSame('<div id="app" data-page="{&quot;component&quot;:&quot;User\/Edit&quot;,&quot;props&quot;:{&quot;foo&quot;:&quot;bar&quot;,&quot;user&quot;:{&quot;name&quot;:&quot;Jonathan&quot;}},&quot;url&quot;:&quot;\/user\/123&quot;,&quot;version&quot;:&quot;123&quot;}"></div>'."\n", $response->render());
34+
}
35+
36+
public function test_xhr_response()
37+
{
38+
$request = Request::create('/user/123', 'GET');
39+
$request->headers->add(['X-Inertia' => 'true']);
40+
41+
$response = new Response(
42+
'User/Edit',
43+
['user' => ['name' => 'Jonathan']],
44+
'app',
45+
['foo' => 'bar'],
46+
'123'
47+
);
48+
49+
$response = $response->toResponse($request);
50+
$page = $response->getData();
51+
52+
$this->assertInstanceOf(JsonResponse::class, $response);
53+
$this->assertSame('User/Edit', $page->component);
54+
$this->assertSame('Jonathan', $page->props->user->name);
55+
$this->assertSame('bar', $page->props->foo);
56+
$this->assertSame('/user/123', $page->url);
57+
$this->assertSame('123', $page->version);
58+
}
59+
}

tests/TestCase.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Illuminate\Support\Facades\View;
6+
use Orchestra\Testbench\TestCase as Orchestra;
7+
8+
abstract class TestCase extends Orchestra
9+
{
10+
protected function getPackageProviders($app)
11+
{
12+
return ['Inertia\ServiceProvider'];
13+
}
14+
15+
public function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
View::addLocation(__DIR__.'/views');
20+
}
21+
}

tests/views/app.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@inertia

0 commit comments

Comments
 (0)