taktik - laravel
This commit is contained in:
106
tests/Feature/CategoryTest.php
Normal file
106
tests/Feature/CategoryTest.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Post;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CategoryTest extends TestCase
|
||||
{
|
||||
public function testApplicationWillReturnCategoryOnGet(): void
|
||||
{
|
||||
$category = Category::factory()->create();
|
||||
$response = $this->get("/api/v1/category/" . $category->id);
|
||||
$response->assertJsonFragment([
|
||||
'name' => $category->name,
|
||||
]);
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
public function testApplicationWillAcceptFilteringOnCategories(): void
|
||||
{
|
||||
Category::factory()->create();
|
||||
$category = Category::factory()->create(['name' => 'Specific Category' . microtime(true)]);
|
||||
$response = $this->get("/api/v1/categories/1?name=" . $category->name);
|
||||
$response->assertJsonFragment([
|
||||
'name' => $category->name,
|
||||
]);
|
||||
$this->assertEquals(1, count($response->decodeResponseJson()['items']));
|
||||
}
|
||||
|
||||
public function testApplicationWillNotReturnCategoryWithInvalidId(): void
|
||||
{
|
||||
$response = $this->get("/api/v1/category/-1");
|
||||
$response->assertStatus(422);
|
||||
|
||||
$response = $this->get("/api/v1/category/foo");
|
||||
$response->assertStatus(422);
|
||||
|
||||
$response = $this->get("/api/v1/category/0");
|
||||
$response->assertStatus(404);
|
||||
}
|
||||
|
||||
public function testApplicationWillCreateCategoryOnPost(): void
|
||||
{
|
||||
$data = [
|
||||
'name' => 'New Category for Testing',
|
||||
];
|
||||
|
||||
$response = $this->post("/api/v1/categories", $data);
|
||||
$response->assertJsonFragment([
|
||||
'name' => $data['name'],
|
||||
]);
|
||||
|
||||
$categoryDb = Category::find($response->getOriginalContent()->getAttributes()['id']);
|
||||
$this->assertEquals(['name' => $data['name']], ['name' => $categoryDb->name]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
}
|
||||
|
||||
public function testApplicationWillUpdateCategory(): void
|
||||
{
|
||||
$category = Category::factory()->create();
|
||||
$data = [
|
||||
'name' => 'Updated Category Name',
|
||||
];
|
||||
|
||||
$response = $this->put("/api/v1/category/" . $category->id, $data);
|
||||
$response->assertJsonFragment([
|
||||
'name' => $data['name'],
|
||||
]);
|
||||
|
||||
$categoryDb = Category::find($category->id);
|
||||
$this->assertEquals(['name' => $data['name']], ['name' => $categoryDb->name]);
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
public function testApplicationWillDeleteCategory(): void
|
||||
{
|
||||
$category = Category::factory()->create();
|
||||
$response = $this->delete("/api/v1/category/" . $category->id);
|
||||
|
||||
$response->assertStatus(204);
|
||||
|
||||
$categoryDb = Category::find($category->id);
|
||||
$this->assertNull($categoryDb);
|
||||
}
|
||||
|
||||
|
||||
public function testApplicationWillNotDeleteCategoryIfContainsPosts(): void
|
||||
{
|
||||
$category = Category::factory()->create();
|
||||
$post = Post::factory()->create(
|
||||
['category_id' => $category->id],
|
||||
);
|
||||
$response = $this->delete("/api/v1/category/" . $category->id);
|
||||
|
||||
$response->assertStatus(404);
|
||||
|
||||
$categoryDb = Category::find($category->id);
|
||||
$this->assertNotNull($categoryDb);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user