2026-08-15 15:10:26 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Tests\Feature;
|
|
|
|
|
|
|
|
|
|
use App\Models\Tracker;
|
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
class TrackerTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
|
|
|
|
public function test_show_reports_no_tracker_on_a_fresh_install(): void
|
|
|
|
|
{
|
|
|
|
|
$this->getJson('/tracker')
|
|
|
|
|
->assertOk()
|
|
|
|
|
->assertJson(['exists' => false]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_show_returns_the_tracker_once_created(): void
|
|
|
|
|
{
|
|
|
|
|
Tracker::factory()->create(['count' => 9]);
|
|
|
|
|
|
|
|
|
|
$this->getJson('/tracker')
|
|
|
|
|
->assertOk()
|
|
|
|
|
->assertJson([
|
|
|
|
|
'exists' => true,
|
|
|
|
|
'tracker' => ['count' => 9],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_a_tracker_can_be_created_without_a_label_or_unit(): void
|
|
|
|
|
{
|
|
|
|
|
$this->postJson('/tracker', [])->assertCreated();
|
|
|
|
|
|
|
|
|
|
$tracker = Tracker::first();
|
|
|
|
|
|
|
|
|
|
$this->assertSame('Counter', $tracker->label);
|
|
|
|
|
$this->assertSame('units', $tracker->unit);
|
|
|
|
|
$this->assertSame(0, $tracker->count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_a_new_counter_starts_at_zero(): void
|
|
|
|
|
{
|
|
|
|
|
$this->postJson('/tracker', []);
|
|
|
|
|
|
|
|
|
|
$this->getJson('/tracker')
|
|
|
|
|
->assertOk()
|
|
|
|
|
->assertJson(['tracker' => ['count' => 0]]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_creating_a_second_tracker_conflicts(): void
|
|
|
|
|
{
|
|
|
|
|
$this->postJson('/tracker', [])->assertCreated();
|
|
|
|
|
$this->postJson('/tracker', [])->assertStatus(409);
|
|
|
|
|
|
|
|
|
|
$this->assertSame(1, Tracker::count());
|
|
|
|
|
}
|
2026-08-15 21:34:19 +02:00
|
|
|
|
|
|
|
|
public function test_label_and_unit_can_be_updated(): void
|
|
|
|
|
{
|
|
|
|
|
$tracker = Tracker::factory()->create(['count' => 4]);
|
|
|
|
|
|
|
|
|
|
$this->patchJson('/tracker', ['label' => 'Books', 'unit' => 'books'])
|
|
|
|
|
->assertOk()
|
|
|
|
|
->assertJson(['label' => 'Books', 'unit' => 'books']);
|
|
|
|
|
|
|
|
|
|
$tracker->refresh();
|
|
|
|
|
|
|
|
|
|
$this->assertSame('Books', $tracker->label);
|
|
|
|
|
$this->assertSame('books', $tracker->unit);
|
|
|
|
|
$this->assertSame(4, $tracker->count, 'updating the label must not disturb the count');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_update_returns_404_without_a_tracker(): void
|
|
|
|
|
{
|
|
|
|
|
$this->patchJson('/tracker', ['label' => 'Books'])->assertNotFound();
|
|
|
|
|
}
|
2026-08-15 15:10:26 +02:00
|
|
|
}
|