2025-12-29 02:57:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Browser;
|
|
|
|
|
|
|
|
|
|
use Laravel\Dusk\Browser;
|
2025-12-29 23:36:15 +01:00
|
|
|
use Tests\DuskTestCase;
|
2025-12-29 02:57:25 +01:00
|
|
|
|
|
|
|
|
trait LoginHelpers
|
|
|
|
|
{
|
2025-12-29 23:36:15 +01:00
|
|
|
protected static $testPlanner = null;
|
|
|
|
|
protected static $testEmail = null;
|
|
|
|
|
protected static $testPassword = 'password';
|
|
|
|
|
|
|
|
|
|
protected function ensureTestPlannerExists(): void
|
|
|
|
|
{
|
|
|
|
|
// Always create a fresh planner for each test class to avoid session conflicts
|
|
|
|
|
if (self::$testPlanner === null || !self::$testPlanner->exists) {
|
|
|
|
|
// Generate unique email for this test run
|
|
|
|
|
self::$testEmail = fake()->unique()->safeEmail();
|
|
|
|
|
|
|
|
|
|
self::$testPlanner = \App\Models\Planner::factory()->create([
|
|
|
|
|
'email' => self::$testEmail,
|
|
|
|
|
'password' => \Illuminate\Support\Facades\Hash::make(self::$testPassword),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
protected function loginAndNavigate(Browser $browser, string $page = '/dashboard'): Browser
|
|
|
|
|
{
|
2025-12-29 23:36:15 +01:00
|
|
|
$this->ensureTestPlannerExists();
|
|
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
// Clear browser session and cookies to start fresh
|
|
|
|
|
$browser->driver->manage()->deleteAllCookies();
|
|
|
|
|
|
|
|
|
|
return $browser->visit('http://dishplanner_app:8000/login')
|
2025-12-29 23:36:15 +01:00
|
|
|
->waitFor('input[id="email"]', DuskTestCase::TIMEOUT_SHORT)
|
2025-12-29 02:57:25 +01:00
|
|
|
->clear('input[id="email"]')
|
2025-12-29 23:36:15 +01:00
|
|
|
->type('input[id="email"]', self::$testEmail)
|
2025-12-29 02:57:25 +01:00
|
|
|
->clear('input[id="password"]')
|
2025-12-29 23:36:15 +01:00
|
|
|
->type('input[id="password"]', self::$testPassword)
|
2025-12-29 02:57:25 +01:00
|
|
|
->press('Login')
|
2025-12-29 23:36:15 +01:00
|
|
|
->waitForLocation('/dashboard', DuskTestCase::TIMEOUT_MEDIUM) // Wait for successful login redirect
|
|
|
|
|
->pause(DuskTestCase::PAUSE_SHORT) // Brief pause for any initialization
|
2025-12-29 02:57:25 +01:00
|
|
|
->visit('http://dishplanner_app:8000' . $page)
|
2025-12-29 23:36:15 +01:00
|
|
|
->pause(DuskTestCase::PAUSE_MEDIUM); // Let Livewire components initialize
|
2025-12-29 02:57:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function loginAndGoToDishes(Browser $browser): Browser
|
|
|
|
|
{
|
|
|
|
|
return $this->loginAndNavigate($browser, '/dishes');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function loginAndGoToUsers(Browser $browser): Browser
|
|
|
|
|
{
|
|
|
|
|
return $this->loginAndNavigate($browser, '/users');
|
|
|
|
|
}
|
2025-12-29 23:36:15 +01:00
|
|
|
|
|
|
|
|
protected function loginAndGoToSchedule(Browser $browser): Browser
|
|
|
|
|
{
|
|
|
|
|
return $this->loginAndNavigate($browser, '/schedule');
|
|
|
|
|
}
|
2025-12-29 02:57:25 +01:00
|
|
|
}
|