-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1efa371
commit 6ae017e
Showing
9 changed files
with
247 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
use AntoniPutra\Ngeblog\Models\Post; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Workbench\App\Models\User; | ||
use Illuminate\Testing\Fluent\AssertableJson; | ||
|
||
uses(RefreshDatabase::class); | ||
|
||
beforeEach(function () { | ||
$this->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, | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
use AntoniPutra\Ngeblog\Models\Tag; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Workbench\App\Models\User; | ||
use Illuminate\Testing\Fluent\AssertableJson; | ||
|
||
uses(RefreshDatabase::class); | ||
|
||
beforeEach(function () { | ||
$this->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, | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
namespace AntoniPutra\Ngeblog\Tests; | ||
|
||
use PHPUnit\Framework\TestCase as BaseTestCase; | ||
use Illuminate\Foundation\AliasLoader; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Orchestra\Testbench\Attributes\WithMigration; | ||
use Orchestra\Testbench\Concerns\WithWorkbench; | ||
use Orchestra\Testbench\TestCase as OrchestraTestCase; | ||
|
||
abstract class TestCase extends BaseTestCase | ||
abstract class TestCase extends OrchestraTestCase | ||
{ | ||
// | ||
} | ||
use RefreshDatabase, WithWorkbench; | ||
|
||
protected $enablesPackageDiscoveries = true; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->usesTestingFeature(new WithMigration('laravel', 'queue')); | ||
|
||
parent::setUp(); | ||
|
||
AliasLoader::getInstance()->setAliases([]); | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
...bench/database/seeders/DatabaseSeeder.php → ...ench/database/seeders/DummyDataSeeder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters