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 PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
class CounterTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
|
|
|
|
private function tracker(int $count = 0): Tracker
|
|
|
|
|
{
|
|
|
|
|
return Tracker::factory()->create(['count' => $count]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
public function test_the_page_renders_the_current_count(): void
|
|
|
|
|
{
|
|
|
|
|
$this->tracker(42);
|
|
|
|
|
|
|
|
|
|
$this->get('/')
|
|
|
|
|
->assertOk()
|
|
|
|
|
->assertSee('42');
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-16 12:23:28 +02:00
|
|
|
public function test_the_edit_form_renders_with_both_actions(): void
|
|
|
|
|
{
|
|
|
|
|
$this->tracker(42);
|
|
|
|
|
|
|
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->call('edit')
|
|
|
|
|
->assertSee('Set Value')
|
|
|
|
|
->assertSee('[EXECUTE]')
|
|
|
|
|
->assertSee('[ABORT]');
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-15 15:10:26 +02:00
|
|
|
public function test_increment_adds_exactly_one(): void
|
|
|
|
|
{
|
|
|
|
|
$tracker = $this->tracker(5);
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->call('increment')
|
|
|
|
|
->assertSet('count', 6);
|
2026-08-15 15:10:26 +02:00
|
|
|
|
|
|
|
|
$this->assertSame(6, $tracker->refresh()->count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_increments_accumulate(): void
|
|
|
|
|
{
|
|
|
|
|
$tracker = $this->tracker();
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->call('increment')
|
|
|
|
|
->call('increment')
|
|
|
|
|
->call('increment')
|
|
|
|
|
->assertSet('count', 3);
|
2026-08-15 15:10:26 +02:00
|
|
|
|
|
|
|
|
$this->assertSame(3, $tracker->refresh()->count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_count_can_be_set_to_an_absolute_value(): void
|
|
|
|
|
{
|
|
|
|
|
$tracker = $this->tracker(3);
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->call('edit')
|
|
|
|
|
->set('value', 250)
|
|
|
|
|
->call('save')
|
|
|
|
|
->assertSet('count', 250)
|
|
|
|
|
->assertSet('editing', false);
|
2026-08-15 15:10:26 +02:00
|
|
|
|
|
|
|
|
$this->assertSame(250, $tracker->refresh()->count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_count_can_be_set_to_zero(): void
|
|
|
|
|
{
|
|
|
|
|
$tracker = $this->tracker(42);
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->call('edit')
|
|
|
|
|
->set('value', 0)
|
|
|
|
|
->call('save');
|
2026-08-15 15:10:26 +02:00
|
|
|
|
|
|
|
|
$this->assertSame(0, $tracker->refresh()->count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array<string, array{mixed}>
|
|
|
|
|
*/
|
|
|
|
|
public static function invalidCounts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'negative' => [-1],
|
|
|
|
|
'above unsigned int ceiling' => [4294967296],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[DataProvider('invalidCounts')]
|
2026-08-16 00:57:07 +02:00
|
|
|
public function test_save_rejects_invalid_values(mixed $value): void
|
2026-08-15 15:10:26 +02:00
|
|
|
{
|
|
|
|
|
$tracker = $this->tracker(7);
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->call('edit')
|
|
|
|
|
->set('value', $value)
|
|
|
|
|
->call('save')
|
|
|
|
|
->assertHasErrors('value');
|
2026-08-15 15:10:26 +02:00
|
|
|
|
|
|
|
|
$this->assertSame(7, $tracker->refresh()->count);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
public function test_cancel_discards_the_edit(): void
|
2026-08-15 15:10:26 +02:00
|
|
|
{
|
2026-08-16 00:57:07 +02:00
|
|
|
$tracker = $this->tracker(9);
|
2026-08-15 15:10:26 +02:00
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->call('edit')
|
|
|
|
|
->set('value', 500)
|
|
|
|
|
->call('cancel')
|
|
|
|
|
->assertSet('count', 9)
|
|
|
|
|
->assertSet('editing', false);
|
2026-08-15 15:10:26 +02:00
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
$this->assertSame(9, $tracker->refresh()->count);
|
2026-08-15 15:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
public function test_increment_stops_at_the_column_ceiling(): void
|
2026-08-15 15:10:26 +02:00
|
|
|
{
|
2026-08-16 00:57:07 +02:00
|
|
|
$tracker = $this->tracker(Counter::MAX_COUNT);
|
2026-08-15 15:10:26 +02:00
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
Livewire::test(Counter::class)
|
|
|
|
|
->call('increment')
|
|
|
|
|
->assertSet('count', Counter::MAX_COUNT);
|
2026-08-15 15:10:26 +02:00
|
|
|
|
2026-08-16 00:57:07 +02:00
|
|
|
$this->assertSame(Counter::MAX_COUNT, $tracker->refresh()->count);
|
2026-08-15 15:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_count_is_cast_to_an_integer(): void
|
|
|
|
|
{
|
|
|
|
|
$this->tracker(12);
|
|
|
|
|
|
|
|
|
|
$this->assertSame(12, Tracker::first()->count);
|
|
|
|
|
}
|
|
|
|
|
}
|