77 lines
2.3 KiB
PHP
77 lines
2.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace Tests\Feature;
|
||
|
|
|
||
|
|
use App\Models\BodyPart;
|
||
|
|
use App\Models\Exercise;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class ExerciseTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
public function test_index_lists_exercises(): void
|
||
|
|
{
|
||
|
|
Exercise::factory()->count(3)->create();
|
||
|
|
|
||
|
|
$response = $this->get(route('exercises.index'));
|
||
|
|
|
||
|
|
$response->assertOk();
|
||
|
|
$response->assertViewHas('exercises', function ($exercises) {
|
||
|
|
return $exercises->total() === 3;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_index_can_filter_by_search_query(): void
|
||
|
|
{
|
||
|
|
Exercise::factory()->create(['name' => 'Barbell Bench Press']);
|
||
|
|
Exercise::factory()->create(['name' => 'Dumbbell Curl']);
|
||
|
|
|
||
|
|
$response = $this->get(route('exercises.index', ['q' => 'Bench']));
|
||
|
|
|
||
|
|
$response->assertOk();
|
||
|
|
$response->assertViewHas('exercises', function ($exercises) {
|
||
|
|
return $exercises->total() === 1
|
||
|
|
&& $exercises->first()->name === 'Barbell Bench Press';
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_index_can_filter_by_body_part(): void
|
||
|
|
{
|
||
|
|
$chest = BodyPart::create(['name' => 'Chest', 'slug' => 'chest']);
|
||
|
|
$back = BodyPart::create(['name' => 'Back', 'slug' => 'back']);
|
||
|
|
|
||
|
|
$chestExercise = Exercise::factory()->create();
|
||
|
|
$chestExercise->bodyParts()->attach($chest->id);
|
||
|
|
|
||
|
|
$backExercise = Exercise::factory()->create();
|
||
|
|
$backExercise->bodyParts()->attach($back->id);
|
||
|
|
|
||
|
|
$response = $this->get(route('exercises.index', ['bodypart' => 'chest']));
|
||
|
|
|
||
|
|
$response->assertOk();
|
||
|
|
$response->assertViewHas('exercises', function ($exercises) use ($chestExercise) {
|
||
|
|
return $exercises->total() === 1
|
||
|
|
&& $exercises->first()->id === $chestExercise->id;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_show_displays_exercise(): void
|
||
|
|
{
|
||
|
|
$exercise = Exercise::factory()->create(['name' => 'Barbell Squat']);
|
||
|
|
|
||
|
|
$response = $this->get(route('exercises.show', $exercise->slug));
|
||
|
|
|
||
|
|
$response->assertOk();
|
||
|
|
$response->assertSee('Barbell Squat');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_show_returns_404_for_unknown_slug(): void
|
||
|
|
{
|
||
|
|
$response = $this->get('/exercises/does-not-exist');
|
||
|
|
|
||
|
|
$response->assertNotFound();
|
||
|
|
}
|
||
|
|
}
|