first commit
This commit is contained in:
122
laravel/tests/Feature/TaskControllerTest.php
Normal file
122
laravel/tests/Feature/TaskControllerTest.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Tag;
|
||||
use App\Models\Task;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TaskControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_index_returns_tasks(): void
|
||||
{
|
||||
Task::factory(3)->create();
|
||||
|
||||
$response = $this->getJson(route('tasks.index'));
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonCount(3, 'data');
|
||||
}
|
||||
|
||||
public function test_store_creates_task(): void
|
||||
{
|
||||
$payload = [
|
||||
'title' => 'Новая задача',
|
||||
'is_done' => false,
|
||||
'due_at' => now()->addDay()->toIso8601ZuluString(),
|
||||
'tags' => ['home', 'shopping'],
|
||||
];
|
||||
|
||||
$response = $this->postJson(route('tasks.store'), $payload);
|
||||
|
||||
$response->assertCreated()
|
||||
->assertJsonFragment([
|
||||
'title' => 'Новая задача',
|
||||
'is_done' => false,
|
||||
])
|
||||
->assertJsonCount(2, 'data.tags');
|
||||
|
||||
$this->assertDatabaseHas('tasks', [
|
||||
'title' => 'Новая задача',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('tags', ['name' => 'home']);
|
||||
$this->assertDatabaseHas('tags', ['name' => 'shopping']);
|
||||
}
|
||||
|
||||
public function test_show_returns_task(): void
|
||||
{
|
||||
$task = Task::factory()->create();
|
||||
|
||||
$response = $this->getJson(route('tasks.show', $task));
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('data.id', $task->id);
|
||||
}
|
||||
|
||||
public function test_update_modifies_task(): void
|
||||
{
|
||||
$task = Task::factory()->create();
|
||||
|
||||
$payload = ['title' => 'Обновлённая задача', 'is_done' => true];
|
||||
|
||||
$response = $this->putJson(route('tasks.update', $task), $payload);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonFragment([
|
||||
'title' => 'Обновлённая задача',
|
||||
'is_done' => true,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('tasks', [
|
||||
'id' => $task->id,
|
||||
'title' => 'Обновлённая задача',
|
||||
'is_done' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_destroy_deletes_task(): void
|
||||
{
|
||||
$task = Task::factory()->create();
|
||||
|
||||
$response = $this->deleteJson(route('tasks.destroy', $task));
|
||||
|
||||
$response->assertNoContent();
|
||||
|
||||
$this->assertDatabaseMissing('tasks', [
|
||||
'id' => $task->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_can_filter_tasks_by_tag_and_is_done(): void
|
||||
{
|
||||
$tagWork = Tag::create(['name' => 'work']);
|
||||
$tagHome = Tag::create(['name' => 'home']);
|
||||
|
||||
$task1 = Task::factory()->create(['is_done' => false]);
|
||||
$task2 = Task::factory()->create(['is_done' => true]);
|
||||
$task3 = Task::factory()->create(['is_done' => false]);
|
||||
|
||||
$task1->tags()->attach($tagWork->id);
|
||||
$task2->tags()->attach($tagWork->id);
|
||||
$task3->tags()->attach($tagHome->id);
|
||||
|
||||
$response = $this->getJson(route('tasks.index', ['tag' => 'work']));
|
||||
$response->assertOk()
|
||||
->assertJsonCount(2, 'data');
|
||||
|
||||
$response = $this->getJson(route('tasks.index', ['is_done' => 0]));
|
||||
$response->assertOk()
|
||||
->assertJsonCount(2, 'data');
|
||||
|
||||
$response = $this->getJson(route('tasks.index', ['is_done' => 1, 'tag' => 'work']));
|
||||
$response->assertOk()
|
||||
->assertJsonCount(1, 'data')
|
||||
->assertJsonPath('data.0.id', $task2->id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user