diff --git a/phpstan.neon b/phpstan.neon index 6bbf8839..0c22a36d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,8 +6,6 @@ parameters: paths: - src - tests - excludePaths: - - src/Testing/Concerns ignoreErrors: - '#Call to an undefined method.*TestResponse.*::assertInertia\(\)#' - '#Call to an undefined method.*TestResponse.*::inertiaPage\(\)#' diff --git a/src/Inertia.php b/src/Inertia.php index 3ce6294b..2de1b074 100644 --- a/src/Inertia.php +++ b/src/Inertia.php @@ -16,7 +16,6 @@ * @method static void resolveUrlUsing(\Closure|null $urlResolver = null) * @method static \Inertia\ScrollProp scroll(mixed $value, string $wrapper = 'data', \Inertia\ProvidesScrollMetadata|callable|null $metadata = null) * @method static \Inertia\OptionalProp optional(callable $callback) - * @method static \Inertia\LazyProp lazy(callable $callback) * @method static \Inertia\DeferProp defer(callable $callback, string $group = 'default') * @method static \Inertia\AlwaysProp always(mixed $value) * @method static \Inertia\MergeProp merge(mixed $value) diff --git a/src/LazyProp.php b/src/LazyProp.php deleted file mode 100644 index da53cf31..00000000 --- a/src/LazyProp.php +++ /dev/null @@ -1,36 +0,0 @@ -callback = $callback; - } - - /** - * Resolve the property value. - * - * @return mixed - */ - public function __invoke() - { - return App::call($this->callback); - } -} diff --git a/src/Response.php b/src/Response.php index 86ea9f44..7fa450e5 100644 --- a/src/Response.php +++ b/src/Response.php @@ -379,7 +379,6 @@ public function resolvePropertyInstances(array $props, Request $request, ?string $resolveViaApp = collect([ Closure::class, - LazyProp::class, OptionalProp::class, DeferProp::class, AlwaysProp::class, @@ -565,7 +564,7 @@ protected function resolveMergeMatchingKeys(Collection $mergeProps): array } /** - * Resolve deferred props configuration for client-side lazy loading. + * Resolve deferred props configuration for client-side loading. * * @return array */ diff --git a/src/ResponseFactory.php b/src/ResponseFactory.php index d3a7626b..80ff7a9c 100644 --- a/src/ResponseFactory.php +++ b/src/ResponseFactory.php @@ -167,16 +167,6 @@ public function encryptHistory($encrypt = true): void $this->encryptHistory = $encrypt; } - /** - * Create a lazy property. - * - * @deprecated Use `optional` instead. - */ - public function lazy(callable $callback): LazyProp - { - return new LazyProp($callback); - } - /** * Create an optional property. */ diff --git a/src/Testing/Concerns/Debugging.php b/src/Testing/Concerns/Debugging.php deleted file mode 100644 index f1ecc611..00000000 --- a/src/Testing/Concerns/Debugging.php +++ /dev/null @@ -1,24 +0,0 @@ -prop($prop)); - - return $this; - } - - public function dd(?string $prop = null): void - { - dd($this->prop($prop)); - } - - abstract protected function prop(?string $key = null); -} diff --git a/src/Testing/Concerns/Has.php b/src/Testing/Concerns/Has.php deleted file mode 100644 index 8d27f9f3..00000000 --- a/src/Testing/Concerns/Has.php +++ /dev/null @@ -1,121 +0,0 @@ -prop($key), - sprintf('Inertia property [%s] does not have the expected size.', $this->dotPath($key)) - ); - - return $this; - } - - public function hasAll($key): self - { - $keys = is_array($key) ? $key : func_get_args(); - - foreach ($keys as $prop => $count) { - if (is_int($prop)) { - $this->has($count); - } else { - $this->has($prop, $count); - } - } - - return $this; - } - - /** - * @param mixed $value - * @return $this - */ - public function has(string $key, $value = null, ?Closure $scope = null): self - { - PHPUnit::assertTrue( - Arr::has($this->prop(), $key), - sprintf('Inertia property [%s] does not exist.', $this->dotPath($key)) - ); - - $this->interactsWith($key); - - if (is_int($value) && ! is_null($scope)) { - $path = $this->dotPath($key); - - $prop = $this->prop($key); - if ($prop instanceof Collection) { - $prop = $prop->all(); - } - - PHPUnit::assertTrue($value > 0, sprintf('Cannot scope directly onto the first entry of property [%s] when asserting that it has a size of 0.', $path)); - PHPUnit::assertIsArray($prop, sprintf('Direct scoping is currently unsupported for non-array like properties such as [%s].', $path)); - $this->count($key, $value); - - return $this->scope($key.'.'.array_keys($prop)[0], $scope); - } - - if (is_callable($value)) { - $this->scope($key, $value); - } elseif (! is_null($value)) { - $this->count($key, $value); - } - - return $this; - } - - public function missingAll($key): self - { - $keys = is_array($key) ? $key : func_get_args(); - - foreach ($keys as $prop) { - $this->misses($prop); - } - - return $this; - } - - public function missing(string $key): self - { - $this->interactsWith($key); - - PHPUnit::assertNotTrue( - Arr::has($this->prop(), $key), - sprintf('Inertia property [%s] was found while it was expected to be missing.', $this->dotPath($key)) - ); - - return $this; - } - - public function missesAll($key): self - { - return $this->missingAll( - is_array($key) ? $key : func_get_args() - ); - } - - public function misses(string $key): self - { - return $this->missing($key); - } - - abstract protected function prop(?string $key = null); - - abstract protected function dotPath(string $key): string; - - abstract protected function interactsWith(string $key): void; - - abstract protected function scope(string $key, Closure $callback); -} diff --git a/src/Testing/Concerns/Interaction.php b/src/Testing/Concerns/Interaction.php deleted file mode 100644 index eb97dcbc..00000000 --- a/src/Testing/Concerns/Interaction.php +++ /dev/null @@ -1,45 +0,0 @@ -interacted, true)) { - $this->interacted[] = $prop; - } - } - - public function interacted(): void - { - PHPUnit::assertSame( - [], - array_diff(array_keys($this->prop()), $this->interacted), - $this->path - ? sprintf('Unexpected Inertia properties were found in scope [%s].', $this->path) - : 'Unexpected Inertia properties were found on the root level.' - ); - } - - public function etc(): self - { - $this->interacted = array_keys($this->prop()); - - return $this; - } - - abstract protected function prop(?string $key = null); -} diff --git a/src/Testing/Concerns/Matching.php b/src/Testing/Concerns/Matching.php deleted file mode 100644 index feb0f4fd..00000000 --- a/src/Testing/Concerns/Matching.php +++ /dev/null @@ -1,78 +0,0 @@ - $value) { - $this->where($key, $value); - } - - return $this; - } - - public function where(string $key, $expected): self - { - $this->has($key); - - $actual = $this->prop($key); - - if ($expected instanceof Closure) { - PHPUnit::assertTrue( - $expected(is_array($actual) ? Collection::make($actual) : $actual), - sprintf('Inertia property [%s] was marked as invalid using a closure.', $this->dotPath($key)) - ); - - return $this; - } - - if ($expected instanceof Arrayable) { - $expected = $expected->toArray(); - } elseif ($expected instanceof ResourceResponse || $expected instanceof JsonResource) { - $expected = json_decode(json_encode($expected->toResponse(request())->getData()), true); - } - - $this->ensureSorted($expected); - $this->ensureSorted($actual); - - PHPUnit::assertSame( - $expected, - $actual, - sprintf('Inertia property [%s] does not match the expected value.', $this->dotPath($key)) - ); - - return $this; - } - - protected function ensureSorted(&$value): void - { - if (! is_array($value)) { - return; - } - - foreach ($value as &$arg) { - $this->ensureSorted($arg); - } - - ksort($value); - } - - abstract protected function dotPath(string $key): string; - - abstract protected function prop(?string $key = null); - - abstract public function has(string $key, $value = null, ?Closure $scope = null); -} diff --git a/src/Testing/Concerns/PageObject.php b/src/Testing/Concerns/PageObject.php deleted file mode 100644 index cfc016b9..00000000 --- a/src/Testing/Concerns/PageObject.php +++ /dev/null @@ -1,60 +0,0 @@ -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; - } - - protected function prop(?string $key = null) - { - return Arr::get($this->props, $key); - } - - 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(): array - { - return [ - 'component' => $this->component, - 'props' => $this->props, - 'url' => $this->url, - 'version' => $this->version, - 'encryptHistory' => $this->encryptHistory, - 'clearHistory' => $this->clearHistory, - ]; - } -} diff --git a/tests/LazyPropTest.php b/tests/LazyPropTest.php deleted file mode 100644 index f1127328..00000000 --- a/tests/LazyPropTest.php +++ /dev/null @@ -1,27 +0,0 @@ -assertSame('A lazy value', $lazyProp()); - } - - public function test_can_resolve_bindings_when_invoked(): void - { - $lazyProp = new LazyProp(function (Request $request) { - return $request; - }); - - $this->assertInstanceOf(Request::class, $lazyProp()); - } -} diff --git a/tests/OptionalPropTest.php b/tests/OptionalPropTest.php index 932c803b..8551f206 100644 --- a/tests/OptionalPropTest.php +++ b/tests/OptionalPropTest.php @@ -10,10 +10,10 @@ class OptionalPropTest extends TestCase public function test_can_invoke(): void { $optionalProp = new OptionalProp(function () { - return 'A lazy value'; + return 'An optional value'; }); - $this->assertSame('A lazy value', $optionalProp()); + $this->assertSame('An optional value', $optionalProp()); } public function test_can_resolve_bindings_when_invoked(): void diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index eac6101f..75230a55 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -15,7 +15,6 @@ use Inertia\ComponentNotFoundException; use Inertia\DeferProp; use Inertia\Inertia; -use Inertia\LazyProp; use Inertia\MergeProp; use Inertia\OptionalProp; use Inertia\ResponseFactory; @@ -262,16 +261,6 @@ public function test_can_flush_shared_data(): void $this->assertSame([], Inertia::getShared()); } - public function test_can_create_lazy_prop(): void - { - $factory = new ResponseFactory; - $lazyProp = $factory->lazy(function () { - return 'A lazy value'; - }); - - $this->assertInstanceOf(LazyProp::class, $lazyProp); - } - public function test_can_create_deferred_prop(): void { $factory = new ResponseFactory; diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 425d87d0..52c0d576 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -14,8 +14,8 @@ use Inertia\AlwaysProp; use Inertia\DeferProp; use Inertia\Inertia; -use Inertia\LazyProp; use Inertia\MergeProp; +use Inertia\OptionalProp; use Inertia\ProvidesInertiaProperties; use Inertia\ProvidesScrollMetadata; use Inertia\RenderContext; @@ -645,7 +645,7 @@ public function test_resource_response(): void $this->assertSame('123', $page->version); } - public function test_lazy_callable_resource_response(): void + public function test_optional_callable_resource_response(): void { $request = Request::create('/users', 'GET'); $request->headers->add(['X-Inertia' => 'true']); @@ -670,7 +670,7 @@ public function test_lazy_callable_resource_response(): void }); } - public function test_lazy_callable_resource_partial_response(): void + public function test_optional_callable_resource_partial_response(): void { $request = Request::create('/users', 'GET'); $request->headers->add(['X-Inertia' => 'true']); @@ -695,7 +695,7 @@ public function test_lazy_callable_resource_partial_response(): void }); } - public function test_lazy_resource_response(): void + public function test_optional_resource_response(): void { $request = Request::create('/users', 'GET', ['page' => 1]); $request->headers->add(['X-Inertia' => 'true']); @@ -747,7 +747,7 @@ public function test_lazy_resource_response(): void }); } - public function test_nested_lazy_resource_response(): void + public function test_nested_optional_resource_response(): void { $request = Request::create('/users', 'GET', ['page' => 1]); $request->headers->add(['X-Inertia' => 'true']); @@ -904,7 +904,7 @@ public function test_nested_partial_props(): void $props = [ 'auth' => [ - 'user' => new LazyProp(function () { + 'user' => new OptionalProp(function () { return [ 'name' => 'Jonathan Reinink', 'email' => 'jonathan@example.com', @@ -940,7 +940,7 @@ public function test_exclude_nested_props_from_partial_response(): void $props = [ 'auth' => [ - 'user' => new LazyProp(function () { + 'user' => new OptionalProp(function () { return [ 'name' => 'Jonathan Reinink', 'email' => 'jonathan@example.com', @@ -963,42 +963,42 @@ public function test_exclude_nested_props_from_partial_response(): void $this->assertSame('value', $page->props->auth->refresh_token); } - public function test_lazy_props_are_not_included_by_default(): void + public function test_optional_props_are_not_included_by_default(): void { $request = Request::create('/users', 'GET'); $request->headers->add(['X-Inertia' => 'true']); - $lazyProp = new LazyProp(function () { - return 'A lazy value'; + $optionalProp = new OptionalProp(function () { + return 'An optional value'; }); - $response = new Response('Users', ['users' => [], 'lazy' => $lazyProp], 'app', '123'); + $response = new Response('Users', ['users' => [], 'optional' => $optionalProp], 'app', '123'); /** @var JsonResponse $response */ $response = $response->toResponse($request); $page = $response->getData(); $this->assertSame([], $page->props->users); - $this->assertFalse(property_exists($page->props, 'lazy')); + $this->assertFalse(property_exists($page->props, 'optional')); } - public function test_lazy_props_are_included_in_partial_reload(): void + public function test_optional_props_are_included_in_partial_reload(): void { $request = Request::create('/users', 'GET'); $request->headers->add(['X-Inertia' => 'true']); $request->headers->add(['X-Inertia-Partial-Component' => 'Users']); - $request->headers->add(['X-Inertia-Partial-Data' => 'lazy']); + $request->headers->add(['X-Inertia-Partial-Data' => 'optional']); - $lazyProp = new LazyProp(function () { - return 'A lazy value'; + $optionalProp = new OptionalProp(function () { + return 'An optional value'; }); - $response = new Response('Users', ['users' => [], 'lazy' => $lazyProp], 'app', '123'); + $response = new Response('Users', ['users' => [], 'optional' => $optionalProp], 'app', '123'); /** @var JsonResponse $response */ $response = $response->toResponse($request); $page = $response->getData(); $this->assertFalse(property_exists($page->props, 'users')); - $this->assertSame('A lazy value', $page->props->lazy); + $this->assertSame('An optional value', $page->props->optional); } public function test_defer_arrayable_props_are_resolved_in_partial_reload(): void @@ -1035,7 +1035,7 @@ public function test_always_props_are_included_on_partial_reload(): void $request->headers->add(['X-Inertia-Partial-Data' => 'data']); $props = [ - 'user' => new LazyProp(function () { + 'user' => new OptionalProp(function () { return [ 'name' => 'Jonathan Reinink', 'email' => 'jonathan@example.com', diff --git a/tests/Testing/AssertableInertiaTest.php b/tests/Testing/AssertableInertiaTest.php index c4e6644c..b1af38c9 100644 --- a/tests/Testing/AssertableInertiaTest.php +++ b/tests/Testing/AssertableInertiaTest.php @@ -219,13 +219,13 @@ public function test_reloading_a_visit(): void $this->assertTrue($called); } - public function test_lazy_props_can_be_evaluated(): void + public function test_optional_props_can_be_evaluated(): void { $response = $this->makeMockRequest( Inertia::render('foo', [ 'foo' => 'bar', - 'lazy1' => Inertia::lazy(fn () => 'baz'), - 'lazy2' => Inertia::lazy(fn () => 'qux'), + 'optional1' => Inertia::optional(fn () => 'baz'), + 'optional2' => Inertia::optional(fn () => 'qux'), ]) ); @@ -233,13 +233,13 @@ public function test_lazy_props_can_be_evaluated(): void $response->assertInertia(function ($inertia) use (&$called) { $inertia->where('foo', 'bar'); - $inertia->missing('lazy1'); - $inertia->missing('lazy2'); + $inertia->missing('optional1'); + $inertia->missing('optional2'); - $result = $inertia->reloadOnly('lazy1', function ($inertia) use (&$called) { + $result = $inertia->reloadOnly('optional1', function ($inertia) use (&$called) { $inertia->missing('foo'); - $inertia->where('lazy1', 'baz'); - $inertia->missing('lazy2'); + $inertia->where('optional1', 'baz'); + $inertia->missing('optional2'); $called = true; }); @@ -249,13 +249,13 @@ public function test_lazy_props_can_be_evaluated(): void $this->assertTrue($called); } - public function test_lazy_props_can_be_evaluated_with_except(): void + public function test_optional_props_can_be_evaluated_with_except(): void { $response = $this->makeMockRequest( Inertia::render('foo', [ 'foo' => 'bar', - 'lazy1' => Inertia::lazy(fn () => 'baz'), - 'lazy2' => Inertia::lazy(fn () => 'qux'), + 'optional1' => Inertia::optional(fn () => 'baz'), + 'optional2' => Inertia::optional(fn () => 'qux'), ]) ); @@ -263,13 +263,13 @@ public function test_lazy_props_can_be_evaluated_with_except(): void $response->assertInertia(function (AssertableInertia $inertia) use (&$called) { $inertia->where('foo', 'bar'); - $inertia->missing('lazy1'); - $inertia->missing('lazy2'); + $inertia->missing('optional1'); + $inertia->missing('optional2'); - $inertia->reloadExcept('lazy1', function ($inertia) use (&$called) { + $inertia->reloadExcept('optional1', function ($inertia) use (&$called) { $inertia->where('foo', 'bar'); - $inertia->missing('lazy1'); - $inertia->where('lazy2', 'qux'); + $inertia->missing('optional1'); + $inertia->where('optional2', 'qux'); $called = true; }); });