taktik - laravel

This commit is contained in:
2025-01-23 00:19:07 +01:00
commit 43b6cff880
127 changed files with 15025 additions and 0 deletions

View 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);
}
}

View File

@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Comment;
use App\Models\Post;
use Tests\TestCase;
class CommentTest extends TestCase
{
public function testApplicationWillCreateCommentOnPost(): void
{
$post = Post::factory()->create();
$data = [
'content' => 'This is test comment',
];
$response = $this->post("/api/v1/post/" . $post->id . "/comments", $data);
$response->assertJsonFragment(
[
'content' => $data['content'],
]
);
$postDb = Comment::find($response->getOriginalContent()->getAttributes()['id']);
$this->assertEquals(
[
'content' => $data['content'],
],
[
'content' => $postDb->content,
]
);
$response->assertStatus(201);
}
public function testApplicationWillListComment(): void
{
$post = Post::factory()->create();
$comment = $post->comments()->create([ // @phpstan-ignore method.notFound
'content' => 'This is test comment',
]);
$comment2 = $post->comments()->create([ // @phpstan-ignore method.notFound
'content' => 'This is test comment #2',
]);
$response = $this->get("/api/v1/post/" . $post->id . "/comments/1");
$response->assertJsonFragment(
[
'content' => $comment->content,
]
);
$response->assertJsonFragment(
[
'content' => $comment2->content,
]
);
$this->assertEquals(2, count($response->decodeResponseJson()));
}
public function testApplicationWillDeleteComment(): void
{
$post = Post::factory()->create();
$comment = $post->comments()->create([ // @phpstan-ignore method.notFound
'content' => 'This is test comment',
]);
$response = $this->delete("/api/v1/post/" . $post->id . "/comment/" . $comment->id);
$response->assertStatus(204);
$comment = Comment::find($comment->id);
$this->assertNull($comment);
}
public function testApplicationWillUpdateComment(): void
{
$post = Post::factory()->create();
$comment = $post->comments()->create([ // @phpstan-ignore method.notFound
'content' => 'This is test comment',
]);
$patchData = [
'content' => 'This is test comment #2',
];
$response = $this->put("/api/v1/post/" . $post->id . "/comment/" . $comment->id, $patchData);
$response->assertJsonFragment(
$patchData
);
$commentDb = Comment::find($comment->id);
$this->assertEquals(
$patchData,
[
'content' => $commentDb->content,
]
);
}
}

145
tests/Feature/PostTest.php Normal file
View File

@@ -0,0 +1,145 @@
<?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);
}
}

12
tests/TestCase.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
//
}