Skip to content
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"laravel/framework": "^5.4|^6.0|^7.0|^8.0"
},
"require-dev": {
"orchestra/testbench": "~3.0"
"orchestra/testbench": "^3.4|^4.0|^5.0",
"phpunit/phpunit": "~5.7|~6.0|^7.0|^8.0|^9.0"
},
"extra": {
"laravel": {
Expand Down
39 changes: 39 additions & 0 deletions ide_helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/** @noinspection PhpUndefinedClassInspection */
/** @noinspection PhpFullyQualifiedNameUsageInspection */
/** @noinspection PhpUnusedAliasInspection */

namespace Illuminate\Testing {

/**
* @see \Inertia\Assertions
*
* @method self assertInertia($component = null, $props = [])
* @method self assertInertiaHas($key, $value = null)
* @method self assertInertiaHasAll(array $bindings)
* @method self assertInertiaMissing($key)
* @method array inertiaProps()
*/
class TestResponse
{
//
}
}

namespace Illuminate\Foundation\Testing {

/**
* @see \Inertia\Assertions
*
* @method self assertInertia($component = null, $props = [])
* @method self assertInertiaHas($key, $value = null)
* @method self assertInertiaHasAll(array $bindings)
* @method self assertInertiaMissing($key)
* @method array inertiaProps()
*/
class TestResponse
{
//
}
}
92 changes: 92 additions & 0 deletions src/Assertions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Inertia;

use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Support\Arr;
use PHPUnit\Framework\Assert as PHPUnit;

class Assertions
{
public function assertInertia()
{
return function ($component = null, $props = []) {
$this->assertViewHas('page');

tap($this->viewData('page'), function ($view) {
PHPUnit::assertArrayHasKey('component', $view);
PHPUnit::assertArrayHasKey('props', $view);
PHPUnit::assertArrayHasKey('url', $view);
PHPUnit::assertArrayHasKey('version', $view);
});

if (! is_null($component)) {
PHPUnit::assertEquals($component, $this->viewData('page')['component']);
}

$this->assertInertiaHasAll($props);

return $this;
};
}

public function inertiaProps()
{
return function () {
$this->assertInertia();

return $this->viewData('page')['props'];
};
}

public function assertInertiaHas()
{
return function ($key, $value = null) {
if (is_array($key)) {
return $this->assertInertiaHasAll($key);
}

if (is_null($value)) {
PHPUnit::assertTrue(Arr::has($this->inertiaProps(), $key));
} elseif ($value instanceof Closure) {
PHPUnit::assertTrue($value(Arr::get($this->inertiaProps(), $key)));
} elseif ($value instanceof Arrayable) {
PHPUnit::assertEquals($value->toArray(), Arr::get($this->inertiaProps(), $key));
} elseif ($value instanceof Responsable) {
PHPUnit::assertEquals($value->toResponse($this)->getData(), Arr::get($this->inertiaProps(), $key));
} else {
PHPUnit::assertEquals($value, Arr::get($this->inertiaProps(), $key));
}

return $this;
};
}

public function assertInertiaHasAll()
{
return function (array $bindings) {
foreach ($bindings as $key => $value) {
if (is_int($key)) {
$this->assertInertiaHas($value);
} else {
$this->assertInertiaHas($key, $value);
}
}

return $this;
};
}

public function assertInertiaMissing()
{
return function ($key) {
$this->assertInertia();

PHPUnit::assertFalse(Arr::has($this->inertiaProps(), $key));

return $this;
};
}
}
27 changes: 27 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Inertia;

use LogicException;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Testing\TestResponse;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Illuminate\Foundation\Testing\TestResponse as LegacyTestResponse;

class ServiceProvider extends BaseServiceProvider
{
Expand All @@ -24,6 +28,10 @@ public function boot()
$this->registerRouterMacro();
$this->registerMiddleware();
$this->shareValidationErrors();

if (App::runningUnitTests()) {
$this->registerTestingMacros();
}
}

protected function registerBladeDirective()
Expand Down Expand Up @@ -54,6 +62,25 @@ protected function registerMiddleware()
$this->app[Kernel::class]->pushMiddleware(Middleware::class);
}

protected function registerTestingMacros()
{
// Laravel >= 7.0
if (class_exists(TestResponse::class)) {
TestResponse::mixin(new Assertions());

return;
}

// Laravel <= 6.0
if (class_exists(LegacyTestResponse::class)) {
LegacyTestResponse::mixin(new Assertions());

return;
}

throw new LogicException('Could not detect TestResponse class.');
}

protected function shareValidationErrors()
{
if (Inertia::getShared('errors')) {
Expand Down
Loading