87 lines
2.7 KiB
PHP
87 lines
2.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Unit\Actions;
|
||
|
|
|
||
|
|
use App\Actions\User\CreateUserAction;
|
||
|
|
use App\Actions\User\DeleteUserAction;
|
||
|
|
use App\Models\Planner;
|
||
|
|
use App\Models\User;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class UserActionIntegrationTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
private Planner $planner;
|
||
|
|
private CreateUserAction $createAction;
|
||
|
|
private DeleteUserAction $deleteAction;
|
||
|
|
|
||
|
|
protected function setUp(): void
|
||
|
|
{
|
||
|
|
parent::setUp();
|
||
|
|
|
||
|
|
$this->planner = Planner::factory()->create();
|
||
|
|
$this->createAction = new CreateUserAction();
|
||
|
|
$this->deleteAction = new DeleteUserAction();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_complete_user_lifecycle_with_actions(): void
|
||
|
|
{
|
||
|
|
// Test creation
|
||
|
|
$userData = [
|
||
|
|
'name' => 'Integration Test User',
|
||
|
|
'planner_id' => $this->planner->id,
|
||
|
|
];
|
||
|
|
|
||
|
|
$user = $this->createAction->execute($userData);
|
||
|
|
|
||
|
|
$this->assertInstanceOf(User::class, $user);
|
||
|
|
$this->assertEquals('Integration Test User', $user->name);
|
||
|
|
$this->assertEquals($this->planner->id, $user->planner_id);
|
||
|
|
$this->assertDatabaseHas('users', [
|
||
|
|
'id' => $user->id,
|
||
|
|
'name' => 'Integration Test User',
|
||
|
|
'planner_id' => $this->planner->id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
// Test deletion
|
||
|
|
$userId = $user->id;
|
||
|
|
$result = $this->deleteAction->execute($user);
|
||
|
|
|
||
|
|
$this->assertTrue($result);
|
||
|
|
$this->assertDatabaseMissing('users', ['id' => $userId]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_creating_and_deleting_user_with_relationships(): void
|
||
|
|
{
|
||
|
|
// Create user
|
||
|
|
$user = $this->createAction->execute([
|
||
|
|
'name' => 'User With Relationships',
|
||
|
|
'planner_id' => $this->planner->id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
// Create a dish and associate it with the user
|
||
|
|
$dish = \App\Models\Dish::factory()->create(['planner_id' => $this->planner->id]);
|
||
|
|
$user->dishes()->attach($dish->id);
|
||
|
|
|
||
|
|
// Verify the relationship exists
|
||
|
|
$this->assertEquals(1, $user->dishes()->count());
|
||
|
|
$this->assertDatabaseHas('user_dishes', [
|
||
|
|
'user_id' => $user->id,
|
||
|
|
'dish_id' => $dish->id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
// Delete the user
|
||
|
|
$userId = $user->id;
|
||
|
|
$result = $this->deleteAction->execute($user);
|
||
|
|
|
||
|
|
// Verify deletion and cascade
|
||
|
|
$this->assertTrue($result);
|
||
|
|
$this->assertDatabaseMissing('users', ['id' => $userId]);
|
||
|
|
$this->assertDatabaseMissing('user_dishes', ['user_id' => $userId]);
|
||
|
|
|
||
|
|
// Dish should still exist
|
||
|
|
$this->assertDatabaseHas('dishes', ['id' => $dish->id]);
|
||
|
|
}
|
||
|
|
}
|