2026-08-15 15:10:26 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
use App\Livewire\Counter;
|
2026-08-15 15:10:26 +02:00
|
|
|
use App\Models\Tracker;
|
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
2026-08-16 00:57:07 +02:00
|
|
|
use Livewire\Livewire;
|
2026-08-15 15:10:26 +02:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
class TrackerTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
public function test_a_fresh_install_asks_for_a_starting_value(): void
|
2026-08-15 15:10:26 +02:00
|
|
|
{
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->assertSet('needsOnboarding', true)
|
|
|
|
|
->assertSee('Starting Value');
|
2026-08-15 15:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
public function test_initialise_creates_the_counter(): void
|
2026-08-15 15:10:26 +02:00
|
|
|
{
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->set('value', 47)
|
|
|
|
|
->call('initialise')
|
|
|
|
|
->assertSet('needsOnboarding', false)
|
|
|
|
|
->assertSet('count', 47);
|
2026-08-15 15:10:26 +02:00
|
|
|
|
|
|
|
|
$tracker = Tracker::first();
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
$this->assertNotNull($tracker);
|
|
|
|
|
$this->assertSame(47, $tracker->count);
|
2026-08-15 15:10:26 +02:00
|
|
|
$this->assertSame('Counter', $tracker->label);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
public function test_a_counter_can_start_at_zero(): void
|
2026-08-15 15:10:26 +02:00
|
|
|
{
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->set('value', 0)
|
|
|
|
|
->call('initialise')
|
|
|
|
|
->assertSet('needsOnboarding', false)
|
|
|
|
|
->assertSet('count', 0);
|
2026-08-15 15:10:26 +02:00
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
$this->assertSame(0, Tracker::first()->count);
|
2026-08-15 15:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
public function test_a_zero_count_does_not_reopen_onboarding(): void
|
2026-08-15 15:10:26 +02:00
|
|
|
{
|
2026-08-16 00:57:07 +02:00
|
|
|
Tracker::factory()->create(['count' => 0]);
|
2026-08-15 15:10:26 +02:00
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->assertSet('needsOnboarding', false);
|
2026-08-15 15:10:26 +02:00
|
|
|
}
|
2026-08-15 21:34:19 +02:00
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
public function test_initialise_rejects_a_negative_starting_value(): void
|
2026-08-15 21:34:19 +02:00
|
|
|
{
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->set('value', -5)
|
|
|
|
|
->call('initialise')
|
|
|
|
|
->assertHasErrors('value')
|
|
|
|
|
->assertSet('needsOnboarding', true);
|
2026-08-15 21:34:19 +02:00
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
$this->assertSame(0, Tracker::count());
|
|
|
|
|
}
|
2026-08-15 21:34:19 +02:00
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
public function test_an_existing_counter_skips_onboarding(): void
|
|
|
|
|
{
|
|
|
|
|
Tracker::factory()->create(['count' => 9]);
|
2026-08-15 21:34:19 +02:00
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->assertSet('needsOnboarding', false)
|
|
|
|
|
->assertSet('count', 9);
|
2026-08-15 21:34:19 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
public function test_initialising_twice_does_not_create_a_second_counter(): void
|
2026-08-15 21:34:19 +02:00
|
|
|
{
|
2026-08-16 00:57:07 +02:00
|
|
|
$component = Livewire::test(Counter::class)->set('value', 5);
|
|
|
|
|
|
|
|
|
|
Tracker::factory()->create(['count' => 12]);
|
|
|
|
|
|
|
|
|
|
$component->call('initialise')->assertSet('count', 12);
|
|
|
|
|
|
|
|
|
|
$this->assertSame(1, Tracker::count());
|
2026-08-15 21:34:19 +02:00
|
|
|
}
|
2026-08-15 15:10:26 +02:00
|
|
|
}
|