2025-10-13 14:57:11 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Feature\Dish;
|
|
|
|
|
|
|
|
|
|
use App\Models\Dish;
|
|
|
|
|
use App\Models\Planner;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Models\UserDish;
|
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
use Illuminate\Testing\Fluent\AssertableJson;
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
use Tests\Traits\HasPlanner;
|
|
|
|
|
|
|
|
|
|
class DeleteDishTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use HasPlanner;
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
2025-12-29 23:36:15 +01:00
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
$this->setUpHasPlanner();
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 14:57:11 +02:00
|
|
|
public function test_planner_can_delete_dish(): void
|
|
|
|
|
{
|
|
|
|
|
$planner = $this->planner;
|
|
|
|
|
$dish = Dish::factory()->planner($planner)->create();
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseCount(Dish::class, 1);
|
|
|
|
|
|
|
|
|
|
$this
|
|
|
|
|
->actingAs($planner)
|
|
|
|
|
->delete(route('api.dishes.destroy', $dish))
|
|
|
|
|
->assertStatus(200)
|
|
|
|
|
->assertJson(fn (AssertableJson $json) => $json
|
|
|
|
|
->where('success', true)
|
|
|
|
|
->where('payload', null)
|
|
|
|
|
->where('errors', null)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseEmpty(Dish::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_it_deletes_user_dishes_when_deleting_a_dish(): void
|
|
|
|
|
{
|
|
|
|
|
$planner = $this->planner;
|
|
|
|
|
$dish = Dish::factory()->planner($planner)->create();
|
|
|
|
|
$user = User::factory()->planner($planner)->create();
|
|
|
|
|
$user->dishes()->attach($dish);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseCount(UserDish::class, 1);
|
|
|
|
|
|
|
|
|
|
$this
|
|
|
|
|
->actingAs($planner)
|
|
|
|
|
->delete(route('api.dishes.destroy', $dish))
|
|
|
|
|
->assertStatus(200)
|
|
|
|
|
->assertJson(fn (AssertableJson $json) => $json
|
|
|
|
|
->where('success', true)
|
|
|
|
|
->where('payload', null)
|
|
|
|
|
->where('errors', null)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseEmpty(UserDish::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function test_planner_cannot_delete_dish_from_other_planner(): void
|
|
|
|
|
{
|
|
|
|
|
$planner = $this->planner;
|
|
|
|
|
$otherPlanner = Planner::factory()->create();
|
|
|
|
|
$dish = Dish::factory()->planner($otherPlanner)->create();
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseCount(Dish::class, 1);
|
|
|
|
|
|
|
|
|
|
$this
|
|
|
|
|
->actingAs($planner)
|
|
|
|
|
->delete(route('api.dishes.destroy', $dish))
|
|
|
|
|
->assertStatus(404)
|
|
|
|
|
->assertJson(fn (AssertableJson $json) => $json
|
|
|
|
|
->where('success', false)
|
|
|
|
|
->where('payload', null)
|
|
|
|
|
->where('errors', ['MODEL_NOT_FOUND'])
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseCount(Dish::class, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|