146 lines
4.0 KiB
PHP
Raw Normal View History

2025-01-23 00:19:07 +01:00
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Category;
use App\Models\Post;
use App\Services\Post\PostService;
use App\Services\Post\PostServiceInterface;
use App\Services\QueryRequestModifiers\Post\PostFilter;
use App\Services\QueryRequestModifiers\Post\PostOrder;
use Tests\TestCase;
class PostTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
// replace cached service
$this->app->instance(
PostServiceInterface::class,
new PostService($this->app->get(PostFilter::class), $this->app->get(PostOrder::class))
);
}
public function testApplicationWillReturnPostOnGet(): void
{
$post = Post::factory()->create();
$response = $this->get("/api/v1/post/" . $post->id);
$response->assertJsonFragment(
[
'content' => $post->content,
'title' => $post->title,
]
);
$response->assertStatus(200);
}
public function testApplicationWillAcceptFiltering(): void
{
Post::factory()->create();
$post = Post::factory()->create();
$response = $this->get("/api/v1/posts/1?title=" . $post->title);
$response->assertJsonFragment(
[
'content' => $post->content,
'title' => $post->title,
]
);
$this->assertEquals(1, count($response->decodeResponseJson()['items']));
}
public function testApplicationWillNotReturnPostWithInvalidId(): void
{
$response = $this->get("/api/v1/post/-1");
$response->assertStatus(422);
$response = $this->get("/api/v1/post/foo");
$response->assertStatus(422);
$response = $this->get("/api/v1/post/0");
$response->assertStatus(404);
}
public function testApplicationWillCreatePostOnPost(): void
{
$category = Category::factory()->create();
$data = [
'content' => 'This is a test post',
'title' => 'This is a test post',
'category_id' => $category->id,
];
$response = $this->post("/api/v1/posts", $data);
$response->assertJsonFragment(
[
'content' => $data['content'],
'title' => $data['title'],
]
);
$postDb = Post::find($response->getOriginalContent()->getAttributes()['id']);
$this->assertEquals(
[
'content' => $data['content'],
'title' => $data['title'],
'category_id' => $data['category_id'],
],
[
'content' => $postDb->content,
'title' => $postDb->title,
'category_id' => $postDb->category_id,
]
);
$this->assertEquals($category->id, $postDb->category_id);
$response->assertStatus(201);
}
public function testApplicationWillUpdatePost(): void
{
$post = Post::factory()->create();
$data = [
'content' => 'This is a test post',
'title' => 'This is a test post',
];
$response = $this->put("/api/v1/post/" . $post->id, $data);
$response->assertJsonFragment(
[
'content' => $data['content'],
'title' => $data['title'],
]
);
$postDb = Post::find($response->getOriginalContent()->getAttributes()['id']);
$this->assertEquals(
[
'content' => $data['content'],
'title' => $data['title'],
],
[
'content' => $postDb->content,
'title' => $postDb->title,
]
);
$response->assertStatus(200);
}
public function testApplicationWillDeletePost(): void
{
$post = Post::factory()->create();
$response = $this->delete("/api/v1/post/" . $post->id);
$response->assertStatus(204);
$postDb = Post::find($post->id);
$this->assertNull($postDb);
}
}