Skip to content

Commit

Permalink
Session test (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet authored Oct 25, 2024
1 parent e1e7b84 commit c549981
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 11 deletions.
6 changes: 5 additions & 1 deletion demo-app/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace App\Models;

use Illuminate\Auth\Authenticatable as AuthTrait;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

class User extends Model
class User extends Model implements Authenticatable
{
use AuthTrait;

public $timestamps = false;

protected $appends = ['avatar'];
Expand Down
23 changes: 13 additions & 10 deletions demo-app/resources/js/Pages/Container.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ export default function Container({ children }) {
const { props } = usePage();

return (
<div className="mx-auto max-w-3xl px-4 py-6 sm:px-6 lg:px-8">
{props.flash.message && (
<div className="mb-4 border-l-4 border-green-400 bg-green-50 p-4">
<p className="text-sm text-green-600">
{props.flash.message}
</p>
</div>
)}
{children}
</div>
<>
<p>Logged in as: {props.auth.user?.id ?? '-'}</p>
<div className="mx-auto max-w-3xl px-4 py-6 sm:px-6 lg:px-8">
{props.flash.message && (
<div className="mb-4 border-l-4 border-green-400 bg-green-50 p-4">
<p className="text-sm text-green-600">
{props.flash.message}
</p>
</div>
)}
{children}
</div>
</>
);
};
1 change: 1 addition & 0 deletions demo-app/resources/js/Pages/Container.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<template>
<p>Logged in as: {{ $page.props.auth.user?.id ?? '-' }}</p>
<div class="mx-auto max-w-3xl px-4 py-6 sm:px-6 lg:px-8">
<div
v-if="$page.props.flash.message"
Expand Down
7 changes: 7 additions & 0 deletions demo-app/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

use App\Models\Role;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Inertia\Inertia;

Route::get('/login', function () {
Auth::loginUsingId(1);

return redirect('/users');
});

// Edit a user
Route::get('/users/{user}/edit', function (User $user) {
return Inertia::modal('EditUser', [
Expand Down
42 changes: 42 additions & 0 deletions demo-app/tests/Browser/SessionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Tests\Browser;

use App\Models\User;
use Illuminate\Support\Str;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tests\DuskTestCase;

class SessionTest extends DuskTestCase
{
#[DataProvider('booleanProvider')]
#[Test]
public function it_can_submit_a_form_and_redirect_and_keep_the_user_logged_in(bool $navigate)
{
$this->browse(function (Browser $browser) use ($navigate) {
$firstUser = User::orderBy('name')->first();

$browser
->loginAs($firstUser)
->visit('/users'.($navigate ? '?navigate=1' : ''))
->waitForFirstUser()
->assertSee("Logged in as: {$firstUser->id}")
->click("@edit-user-{$firstUser->id}")
->waitForTextIn('.im-modal-content', 'Edit User')
->assertSee("Logged in as: {$firstUser->id}")
->type('name', $newName = Str::random(10))
->press('Save')
->waitForText('User updated successfully!')
->waitUntilMissing('.im-dialog')
->assertPathIs('/users')
->assertSee("Logged in as: {$firstUser->id}")
->assertAuthenticatedAs($firstUser);

$this->assertDatabaseHas('users', [
'id' => $firstUser->id,
'name' => $newName,
]);
});
}
}

0 comments on commit c549981

Please sign in to comment.