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

Testing lazy props #595

Open
wants to merge 3 commits into
base: 0.x
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Inertia;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Foundation\Http\Events\RequestHandled;
use LogicException;
use Inertia\Ssr\Gateway;
use ReflectionException;
Expand All @@ -13,6 +15,7 @@
use Inertia\Testing\TestResponseMacros;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Illuminate\Foundation\Testing\TestResponse as LegacyTestResponse;
use Inertia\Testing\Listeners\RequestHandledListener;

class ServiceProvider extends BaseServiceProvider
{
Expand All @@ -38,6 +41,10 @@ public function register(): void
$app['config']->get('inertia.testing.page_extensions')
);
});

$this->app->bind('inertia.testing.request', function ($app) {
return null;
});
}

public function boot(): void
Expand All @@ -47,6 +54,11 @@ public function boot(): void
$this->publishes([
__DIR__.'/../config/inertia.php' => config_path('inertia.php'),
]);

if (app()->environment('testing') === true) {
$dispatcher = app(Dispatcher::class);
$dispatcher->listen(RequestHandled::class, RequestHandledListener::class);
}
}

protected function registerBladeDirectives(): void
Expand Down
38 changes: 38 additions & 0 deletions src/Testing/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Closure;
use const E_USER_DEPRECATED;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Testing\TestResponse;
use Illuminate\Foundation\Testing\TestResponse as LegacyTestResponse;
use Illuminate\Support\Traits\Macroable;
use PHPUnit\Framework\Assert as PHPUnit;
use Illuminate\Contracts\Support\Arrayable;
Expand Down Expand Up @@ -89,4 +92,39 @@ public static function fromTestResponse($response): self

return new self($page['component'], $page['props'], $page['url'], $page['version']);
}

public function requestProp(string $prop, Closure $assert = null): self
{
$request = app('inertia.testing.request');
if ($request === null) {
PHPUnit::fail('Unable to catch the previous request via the `\Illuminate\Foundation\Http\Events\RequestHandled` event using listener. If you are using Event::fake(), please use Event::fakeExcept(RequestHandled::class) instead when calling requestProp if you want to block other events.');
}

$request->headers->add([
'X-Inertia-Partial-Component' => $this->component,
'X-Inertia-Partial-Data' => $prop,
]);


$kernel = app()->make(Kernel::class);
$response = $kernel->handle($request);

$kernel->terminate($request, $response);

$testResponseClass = TestResponse::class;
if (class_exists($testResponseClass) == false) {
$testResponseClass = LegacyTestResponse::class;
}

$testResponse = $testResponseClass::fromBaseResponse($response);
$testResponse->assertInertia(function (Assert $page) use ($prop, $assert) {
$page->has($prop);

if ($assert !== null) {
$assert($page);
}
});

return $this;
}
}
38 changes: 38 additions & 0 deletions src/Testing/AssertableInertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace Inertia\Testing;

use InvalidArgumentException;
use Closure;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Testing\Fakes\Fake;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Assert as PHPUnit;
use PHPUnit\Framework\AssertionFailedError;
Expand Down Expand Up @@ -71,6 +75,40 @@ public function version(string $value): self
return $this;
}

public function requestProp(string $prop, Closure $assert = null): self
{
$request = app('inertia.testing.request');
if ($request === null) {
if (Event::getFacadeRoot() instanceof Fake) {
PHPUnit::fail('Unable to listen to the `\Illuminate\Foundation\Http\Events\RequestHandled` event. Please use Event::fakeExcept(RequestHandled::class) when using requestProp if you want to block other events.');
}

PHPUnit::fail('Unable to catch the previous request via the `\Illuminate\Foundation\Http\Events\RequestHandled` event using listener.');
}

$request->headers->add([
'X-Inertia-Partial-Component' => $this->component,
'X-Inertia-Partial-Data' => $prop,
]);


$kernel = app()->make(Kernel::class);
$response = $kernel->handle($request);

$kernel->terminate($request, $response);

$testResponse = TestResponse::fromBaseResponse($response);
$testResponse->assertInertia(function (AssertableInertia $page) use ($prop, $assert) {
$page->has($prop);

if($assert !== null){
$assert($page);
}
});

return $this;
}

public function toArray()
{
return [
Expand Down
15 changes: 15 additions & 0 deletions src/Testing/Listeners/RequestHandledListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Inertia\Testing\Listeners;

use Illuminate\Foundation\Http\Events\RequestHandled;

class RequestHandledListener
{
public function handle(RequestHandled $event): void
{
app()->bind('inertia.testing.request', function () use ($event) {
return $event->request;
});
}
}
33 changes: 33 additions & 0 deletions tests/Testing/AssertableInertiaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,37 @@ public function the_asset_version_does_not_match(): void
$inertia->version('different-version');
});
}

/** @test */
public function the_lazy_prop_can_be_fetch_via_the_request_prop_method(): void
{
$response = $this->makeMockRequest(
Inertia::render('foo', [
'bar' => Inertia::lazy(function () {
return 'foobar';
}),
])
);
$response->assertInertia(function ($inertia) {
$inertia->missing('bar');

$inertia->requestProp('bar', function ($subInertia) {
$subInertia->where('bar', 'foobar');
});
});
}

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

$this->expectException(AssertionFailedError::class);

$response->assertInertia(function ($inertia) {
$inertia->requestProp('bar');
});
}
}
25 changes: 25 additions & 0 deletions tests/Testing/Listeners/RequestHandledListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Inertia\Tests\Testing\Listeners;

use Illuminate\Foundation\Http\Events\RequestHandled;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Event;
use Inertia\Tests\TestCase;

class RequestHandledListener extends TestCase
{
/** @test */
public function the_request_is_stored()
{
$request = new Request();
$response = new Response();

$this->assertNull(app('inertia.testing.request'));

Event::dispatch(new RequestHandled($request, $response));

$this->assertSame($request, app('inertia.testing.request'));
}
}
Loading