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

[1.x] Adds tests for model route binding #84

Merged
merged 3 commits into from
Dec 27, 2023
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"require": {
"php": "^8.1",
"laravel/framework": "^10.27|^11.0",
"laravel/framework": "^10.38.2|^11.0",
"livewire/livewire": "^3.0"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ function usesFileUploads(): UsesOptions
/**
* Indicate that the component supports pagination.
*/
function usesPagination(string $view = null, string $theme = null): UsesOptions
function usesPagination(?string $view = null, ?string $theme = null): UsesOptions
{
return (new UsesOptions)->usesPagination($view, $theme);
}
Expand Down
2 changes: 1 addition & 1 deletion src/CompileContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function resolveListeners(Component $component): array
/**
* Register an event listener on the context.
*/
public function listen(Closure|array|string $listenersOrEventName, Closure|string $handler = null): void
public function listen(Closure|array|string $listenersOrEventName, Closure|string|null $handler = null): void
{
if (is_array($listenersOrEventName)) {
$listeners = $this->registerInlineListeners($listenersOrEventName);
Expand Down
4 changes: 2 additions & 2 deletions src/FragmentAlias.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FragmentAlias
/**
* Encode the given fragment's component name and path into a base64 embedded alias.
*/
public static function encode(string $componentName, string $path, string $basePath = null): string
public static function encode(string $componentName, string $path, ?string $basePath = null): string
{
$basePath = $basePath ?? static::$basePath ?? base_path();

Expand All @@ -25,7 +25,7 @@ public static function encode(string $componentName, string $path, string $baseP
/**
* Resolve the given fragment's component name and path from a base64 embedded alias.
*/
public static function decode(string $alias, string $basePath = null): ?array
public static function decode(string $alias, ?string $basePath = null): ?array
{
if (! static::isFragment($alias)) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Options/StateOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function reactive(): static
/**
* Indicate the state should be tracked in the URL.
*/
public function url(string $as = null, bool $history = null, bool $keep = null): static
public function url(?string $as = null, ?bool $history = null, ?bool $keep = null): static
{
return $this->attribute(Url::class, as: $as, history: $history, keep: $keep);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Options/UsesOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function usesFileUploads(): static
/**
* Indicate that the component should be compiled with pagination support.
*/
public function usesPagination(string $view = null, string $theme = null): static
public function usesPagination(?string $view = null, ?string $theme = null): static
{
CompileContext::instance()->paginationView = $view;
CompileContext::instance()->paginationTheme = $theme;
Expand Down
2 changes: 1 addition & 1 deletion src/VoltManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function withQueryParams(array $params): static
/**
* Set the currently logged in user for the application.
*/
public function actingAs(Authenticatable $user, string $driver = null): static
public function actingAs(Authenticatable $user, ?string $driver = null): static
{
$this->manager->actingAs($user, $driver);

Expand Down
37 changes: 37 additions & 0 deletions tests/Feature/FunctionalComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,43 @@ public function render()
->assertOk();
});

it('test 200s model route binding on full page components', function (string $route, string $uri) {
User::create([
'name' => 'taylor',
'email' => 'taylor@laravel.com',
'password' => 'password',
]);

Volt::route('/users/'.$route, 'navigate.with-model-route-binding');

$this->get('/users/'.$uri)
->assertStatus(200)
->assertSee('Volt 1 using mount.');
})->with([
['{user}', '1'],
['{user:id}', '1'],
['{user:name}', 'taylor'],
['{user:email}', 'taylor@laravel.com'],
]);

it('test 404s model route binding on full page components', function (string $route, string $uri) {
User::create([
'name' => 'taylor',
'email' => 'taylor@laravel.com',
'password' => 'password',
]);

Volt::route('/users/'.$route, 'navigate.with-model-route-binding');

$this->get('/users/'.$uri)
->assertStatus(404);
})->with([
['{user}', '2'],
['{user:id}', '2'],
['{user:name}', 'nuno'],
['{user:email}', 'nuno@laravel.com'],
]);

test('user imports on bottom do not conflict', function () {
User::create([
'name' => 'Taylor',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use function Livewire\Volt\mount;
use function Livewire\Volt\state;
use Tests\Fixtures\User;

state('user');

mount(fn (User $user) => $this->user = $user);

?>

<div>
<div>
Volt {{ $user->id }} using mount.
</div>
</div>