From 6ae017e9f6c757e12668d43bdacdd44d8326a320 Mon Sep 17 00:00:00 2001 From: antoniputra Date: Sun, 26 May 2024 01:06:15 +0700 Subject: [PATCH] test: Added feature tests --- phpunit.xml.dist | 1 + src/Models/Post.php | 26 ---- testbench.yaml | 3 +- tests/Feature/ExampleTest.php | 5 - tests/Feature/PostFeatureTest.php | 108 +++++++++++++++++ tests/Feature/TagFeatureTest.php | 111 ++++++++++++++++++ tests/Pest.php | 4 +- tests/TestCase.php | 25 +++- ...DatabaseSeeder.php => DummyDataSeeder.php} | 4 +- 9 files changed, 247 insertions(+), 40 deletions(-) delete mode 100644 tests/Feature/ExampleTest.php create mode 100644 tests/Feature/PostFeatureTest.php create mode 100644 tests/Feature/TagFeatureTest.php rename workbench/database/seeders/{DatabaseSeeder.php => DummyDataSeeder.php} (96%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 929aaf8..da94637 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -15,6 +15,7 @@ + diff --git a/src/Models/Post.php b/src/Models/Post.php index 9574e50..7a6b8e5 100644 --- a/src/Models/Post.php +++ b/src/Models/Post.php @@ -9,7 +9,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Support\Str; class Post extends Model { @@ -95,29 +94,4 @@ public function toggleVisibility() $this->is_visible = ! $this->is_visible; return $this->save(); } - - // protected function parsedContent(): Attribute - // { - // return Attribute::get(function () { - // if (! $this->exists) { - // return null; - // } - - // return match ($this->content_type) { - // null => null, - // self::CONTENT_TYPE_MARKDOWN => $this->parseMarkdown(), - // self::CONTENT_TYPE_RICHTEXT => $this->parseRichtext(), - // }; - // }); - // } - - // protected function parseMarkdown() - // { - // return Str::markdown($this->content); - // } - - // protected function parseRichtext() - // { - // return $this->content; - // } } diff --git a/testbench.yaml b/testbench.yaml index 50a2568..62b7ae4 100644 --- a/testbench.yaml +++ b/testbench.yaml @@ -6,7 +6,7 @@ migrations: - workbench/database/migrations seeders: - - Workbench\Database\Seeders\DatabaseSeeder + - Workbench\Database\Seeders\DummyDataSeeder workbench: start: '/' @@ -22,6 +22,5 @@ workbench: - create-sqlite-db - db:wipe - migrate:refresh - - db:seed assets: [] sync: [] diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php deleted file mode 100644 index 61cd84c..0000000 --- a/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,5 +0,0 @@ -toBeTrue(); -}); diff --git a/tests/Feature/PostFeatureTest.php b/tests/Feature/PostFeatureTest.php new file mode 100644 index 0000000..6f41e65 --- /dev/null +++ b/tests/Feature/PostFeatureTest.php @@ -0,0 +1,108 @@ +user = User::first(); +}); + +it('can read posts list', function () { + $this->actingAs($this->user) + ->json('GET', route('api.posts.index')) + ->assertStatus(200) + ->assertJson(fn (AssertableJson $json) => + $json->hasAll('data', 'meta', 'links') + ); +}); + +it('can read posts stats', function () { + $this->actingAs($this->user) + ->json('GET', route('api.posts.stats')) + ->assertStatus(200) + ->assertJson(fn (AssertableJson $json) => + $json->hasAll('total_all_time', 'total_last_month') + ); +}); + +it('can read a single post', function () { + $post = createPost(); + + $this->actingAs($this->user) + ->json('GET', route('api.posts.show', $post->id)) + ->assertStatus(200) + ->assertJson([ + 'title' => $post->title, + ]); +}); + +it('can create a post', function () { + $postData = [ + 'title' => 'Test Post', + 'excerpt' => 'This is a test excerpt.', + 'content' => 'This is a test content.', + ]; + + $this->actingAs($this->user) + ->json('POST', route('api.posts.store'), $postData) + ->assertStatus(200) + ->assertJson([ + 'title' => $postData['title'], + ]); + + $this->assertDatabaseHas((new Post)->getTable(), $postData); +}); + +it('can update a post', function () { + $post = createPost(); + + $updatedData = [ + 'title' => 'Updated Title', + 'excerpt' => 'Updated excerpt.', + 'content' => 'Updated content.' + ]; + + $this->actingAs($this->user) + ->json('PUT', route('api.posts.update', $post->id), $updatedData) + ->assertStatus(200) + ->assertJson([ + 'title' => $updatedData['title'], + ]); + + $this->assertDatabaseHas((new Post)->getTable(), $updatedData); +}); + +it('can toggle visibility a post', function () { + $post = createPost(); + + $this->actingAs($this->user) + ->json('PATCH', route('api.posts.toggleVisibility', $post->id)) + ->assertStatus(200) + ->assertJson([ + 'is_visible' => false, + ]); +}); + +it('can delete a post', function () { + $post = createPost(); + + $this->actingAs($this->user) + ->json('DELETE', route('api.posts.destroy', $post->id)) + ->assertStatus(200); + + $this->assertDatabaseMissing((new Post)->getTable(), ['id' => $post->id]); +}); + +function createPost() { + return Post::create([ + 'title' => 'Test Post', + 'slug' => str('Test Post')->slug(), + 'excerpt' => 'This is a test.', + 'content' => 'This is a test.', + 'is_visible' => true, + ]); +} \ No newline at end of file diff --git a/tests/Feature/TagFeatureTest.php b/tests/Feature/TagFeatureTest.php new file mode 100644 index 0000000..7225113 --- /dev/null +++ b/tests/Feature/TagFeatureTest.php @@ -0,0 +1,111 @@ +user = User::first(); +}); + +it('can read tags list', function () { + $this->actingAs($this->user) + ->json('GET', route('api.tags.index')) + ->assertStatus(200) + ->assertJson(fn (AssertableJson $json) => + $json->hasAll('data', 'meta', 'links') + ); +}); + +it('can read tags stats', function () { + $this->actingAs($this->user) + ->json('GET', route('api.tags.stats')) + ->assertStatus(200) + ->assertJson(fn (AssertableJson $json) => + $json->hasAll('total_all_time', 'total_last_month') + ); +}); + +it('can read tags list as dropdown', function () { + $this->actingAs($this->user) + ->json('GET', route('api.tags.dropdown')) + ->assertStatus(200); +}); + +it('can read a single tag', function () { + $tag = createTag(); + + $this->actingAs($this->user) + ->json('GET', route('api.tags.show', $tag->id)) + ->assertStatus(200) + ->assertJson([ + 'title' => $tag->title, + ]); +}); + +it('can create a tag', function () { + $tagData = [ + 'title' => 'Test Tag', + 'description' => 'This is a test tag.' + ]; + + $this->actingAs($this->user) + ->json('POST', route('api.tags.store'), $tagData) + ->assertStatus(200) + ->assertJson([ + 'title' => $tagData['title'], + ]); + + $this->assertDatabaseHas((new Tag)->getTable(), $tagData); +}); + +it('can update a tag', function () { + $tag = createTag(); + + $updatedData = [ + 'title' => 'Updated Title', + 'description' => 'Updated content.' + ]; + + $this->actingAs($this->user) + ->json('PUT', route('api.tags.update', $tag->id), $updatedData) + ->assertStatus(200) + ->assertJson([ + 'title' => $updatedData['title'], + ]); + + $this->assertDatabaseHas((new Tag)->getTable(), $updatedData); +}); + +it('can toggle visibility a tag', function () { + $tag = createTag(); + + $this->actingAs($this->user) + ->json('PATCH', route('api.tags.toggleVisibility', $tag->id)) + ->assertStatus(200) + ->assertJson([ + 'is_visible' => false, + ]); +}); + +it('can delete a tag', function () { + $tag = createTag(); + + $this->actingAs($this->user) + ->json('DELETE', route('api.tags.destroy', $tag->id)) + ->assertStatus(200); + + $this->assertDatabaseMissing((new Tag)->getTable(), ['id' => $tag->id]); +}); + +function createTag() { + return Tag::create([ + 'title' => 'Test Tag', + 'slug' => str('Test Tag')->slug(), + 'description' => 'This is a test tag.', + 'is_visible' => true, + ]); +} \ No newline at end of file diff --git a/tests/Pest.php b/tests/Pest.php index 5949c61..b4c2958 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -11,7 +11,9 @@ | */ -// uses(Tests\TestCase::class)->in('Feature'); +use AntoniPutra\Ngeblog\Tests\TestCase; + +uses(TestCase::class)->in('Feature'); /* |-------------------------------------------------------------------------- diff --git a/tests/TestCase.php b/tests/TestCase.php index cfb05b6..a042926 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,10 +1,25 @@ usesTestingFeature(new WithMigration('laravel', 'queue')); + + parent::setUp(); + + AliasLoader::getInstance()->setAliases([]); + } +} \ No newline at end of file diff --git a/workbench/database/seeders/DatabaseSeeder.php b/workbench/database/seeders/DummyDataSeeder.php similarity index 96% rename from workbench/database/seeders/DatabaseSeeder.php rename to workbench/database/seeders/DummyDataSeeder.php index 5748319..8842975 100644 --- a/workbench/database/seeders/DatabaseSeeder.php +++ b/workbench/database/seeders/DummyDataSeeder.php @@ -1,5 +1,7 @@