55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Movies;
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Movies\Models\{Movie, Genre};
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class AdminManageMovieTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function signIn(): User
|
|
{
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
return $user;
|
|
}
|
|
|
|
public function test_patch_updates_scalar_and_relations(): void
|
|
{
|
|
$this->signIn();
|
|
$movie = Movie::factory()->create(['title' => 'Old', 'rating' => 'PG']);
|
|
|
|
$payload = [
|
|
'title' => 'New Title',
|
|
'rating' => 'R',
|
|
'genres' => ['Adventure', 'Action'],
|
|
];
|
|
|
|
$res = $this->patchJson("/admin/movies/{$movie->id}", $payload);
|
|
$res->assertOk()->assertJsonPath('movie.title', 'New Title');
|
|
|
|
$this->assertDatabaseHas('movies', ['id' => $movie->id, 'title' => 'New Title', 'rating' => 'R']);
|
|
$movie->refresh();
|
|
$this->assertSame(2, $movie->genres()->count());
|
|
}
|
|
|
|
public function test_delete_removes_movie(): void
|
|
{
|
|
$this->signIn();
|
|
$movie = Movie::factory()->create();
|
|
// attach a relation to ensure cascade works
|
|
$g = Genre::factory()->create();
|
|
$movie->genres()->sync([$g->id]);
|
|
|
|
$this->deleteJson("/admin/movies/{$movie->id}")
|
|
->assertOk()
|
|
->assertJsonPath('deleted', true);
|
|
|
|
$this->assertDatabaseMissing('movies', ['id' => $movie->id]);
|
|
}
|
|
}
|