Skip to content

Commit

Permalink
test: Added feature tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniputra committed May 25, 2024
1 parent 1efa371 commit 6ae017e
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 40 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</include>
</source>
<php>
<env name="APP_KEY" value="base64:asdf1RtFO57QGzbZX1kRYX9hIRB50+QzqFeg9zbFJlY="/>
<env name="DB_CONNECTION" value="testing"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
Expand Down
26 changes: 0 additions & 26 deletions src/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
// }
}
3 changes: 1 addition & 2 deletions testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ migrations:
- workbench/database/migrations

seeders:
- Workbench\Database\Seeders\DatabaseSeeder
- Workbench\Database\Seeders\DummyDataSeeder

workbench:
start: '/'
Expand All @@ -22,6 +22,5 @@ workbench:
- create-sqlite-db
- db:wipe
- migrate:refresh
- db:seed
assets: []
sync: []
5 changes: 0 additions & 5 deletions tests/Feature/ExampleTest.php

This file was deleted.

108 changes: 108 additions & 0 deletions tests/Feature/PostFeatureTest.php
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,
]);
}
111 changes: 111 additions & 0 deletions tests/Feature/TagFeatureTest.php
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,
]);
}
4 changes: 3 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
|
*/

// uses(Tests\TestCase::class)->in('Feature');
use AntoniPutra\Ngeblog\Tests\TestCase;

uses(TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
Expand Down
25 changes: 20 additions & 5 deletions tests/TestCase.php
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([]);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Workbench\Database\Seeders;

use AntoniPutra\Ngeblog\Models\Post;
use AntoniPutra\Ngeblog\Models\Postmeta;
use AntoniPutra\Ngeblog\Models\Tag;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;
use Workbench\App\Models\User;

class DatabaseSeeder extends Seeder
class DummyDataSeeder extends Seeder
{
/**
* Run the database seeds.
Expand Down

0 comments on commit 6ae017e

Please sign in to comment.