Initial commit
This commit is contained in:
65
tests/Feature/Catalog/CategoryControllerTest.php
Normal file
65
tests/Feature/Catalog/CategoryControllerTest.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Catalog;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CategoryControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_index_returns_categories(): void
|
||||
{
|
||||
$category = $this->createCategory();
|
||||
|
||||
$response = $this->getJson('/api/categories');
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJsonFragment(['id' => $category->id]);
|
||||
}
|
||||
|
||||
public function test_show_returns_category(): void
|
||||
{
|
||||
$category = $this->createCategory();
|
||||
|
||||
$response = $this->getJson("/api/categories/{$category->id}");
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJsonFragment(['id' => $category->id]);
|
||||
}
|
||||
|
||||
public function test_admin_can_create_update_and_delete_category(): void
|
||||
{
|
||||
$this->actingAsAdmin();
|
||||
|
||||
$createResponse = $this->postJson('/api/categories', [
|
||||
'name' => 'CAT-A',
|
||||
'order' => 1,
|
||||
]);
|
||||
|
||||
$createResponse->assertStatus(201);
|
||||
$categoryId = $createResponse->json('id');
|
||||
|
||||
$updateResponse = $this->putJson("/api/categories/{$categoryId}", [
|
||||
'name' => 'CAT-B',
|
||||
'order' => 2,
|
||||
]);
|
||||
|
||||
$updateResponse->assertStatus(200)
|
||||
->assertJsonFragment(['id' => $categoryId]);
|
||||
|
||||
$this->deleteJson("/api/categories/{$categoryId}")
|
||||
->assertStatus(204);
|
||||
}
|
||||
|
||||
public function test_non_admin_cannot_create_category(): void
|
||||
{
|
||||
$this->actingAsUser();
|
||||
|
||||
$this->postJson('/api/categories', [
|
||||
'name' => 'CAT-A',
|
||||
'order' => 1,
|
||||
])->assertStatus(403);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user