125 lines
4.5 KiB
PHP
Executable File
125 lines
4.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Modules\Blog\Models\Category;
|
|
use Modules\Blog\Models\Post;
|
|
use Modules\Blog\Models\Comment;
|
|
|
|
class BlogTest extends TestCase
|
|
{
|
|
public function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$capsule = new Capsule;
|
|
|
|
$capsule->addConnection([
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
'prefix' => '',
|
|
]);
|
|
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
|
|
// Create tables with correct primary keys for Eloquent models
|
|
Capsule::schema()->create('blog_categories', function ($table) {
|
|
$table->id('categories_id'); // Match $primaryKey = 'categories_id'
|
|
$table->string('categories_name');
|
|
$table->string('categories_slug');
|
|
$table->text('categories_description')->nullable();
|
|
$table->timestamps();
|
|
});
|
|
|
|
Capsule::schema()->create('blog_posts', function ($table) {
|
|
$table->id('posts_id'); // Match $primaryKey = 'posts_id'
|
|
$table->foreignId('posts_category_id')->nullable(); // Match $fillable 'posts_category_id'
|
|
$table->foreignId('posts_author_id')->nullable();
|
|
$table->integer('posts_bus_id')->default(1);
|
|
$table->string('posts_title');
|
|
$table->string('posts_slug');
|
|
$table->text('posts_content');
|
|
$table->enum('posts_status', ['draft', 'published'])->default('draft');
|
|
$table->string('posts_visibility')->default('public');
|
|
$table->timestamps();
|
|
});
|
|
|
|
Capsule::schema()->create('blog_comments', function ($table) {
|
|
$table->id('comments_id'); // Match $primaryKey = 'comments_id'
|
|
$table->foreignId('comments_post_id'); // Match $fillable 'comments_post_id'
|
|
$table->foreignId('comments_user_id')->nullable();
|
|
$table->string('comments_author_name');
|
|
$table->string('comments_author_email');
|
|
$table->text('comments_content');
|
|
$table->enum('comments_status', ['pending', 'approved', 'spam'])->default('pending');
|
|
$table->timestamps();
|
|
$table->softDeletes();
|
|
});
|
|
}
|
|
|
|
public function test_can_create_category()
|
|
{
|
|
$category = Category::create([
|
|
'categories_name' => 'Tech',
|
|
'categories_slug' => 'tech',
|
|
'categories_description' => 'Technology news'
|
|
]);
|
|
|
|
$this->assertNotNull($category->categories_id);
|
|
$this->assertEquals('Tech', $category->categories_name);
|
|
}
|
|
|
|
public function test_can_create_post_with_category()
|
|
{
|
|
$category = Category::create([
|
|
'categories_name' => 'Tech',
|
|
'categories_slug' => 'tech'
|
|
]);
|
|
|
|
$post = Post::create([
|
|
'posts_category_id' => $category->categories_id,
|
|
'posts_author_id' => 1,
|
|
'posts_title' => 'New PHP Version',
|
|
'posts_slug' => 'new-php-version',
|
|
'posts_content' => 'PHP 8.2 is out!',
|
|
'posts_status' => 'published'
|
|
]);
|
|
|
|
$this->assertNotNull($post->posts_id);
|
|
$this->assertEquals($category->categories_id, $post->posts_category_id);
|
|
$this->assertEquals('New PHP Version', $post->posts_title);
|
|
}
|
|
|
|
public function test_can_create_comment_on_post()
|
|
{
|
|
$category = Category::create([
|
|
'categories_name' => 'Tech',
|
|
'categories_slug' => 'tech'
|
|
]);
|
|
|
|
$post = Post::create([
|
|
'posts_category_id' => $category->categories_id,
|
|
'posts_author_id' => 1,
|
|
'posts_title' => 'New PHP Version',
|
|
'posts_slug' => 'new-php-version',
|
|
'posts_content' => 'PHP 8.2 is out!',
|
|
'posts_status' => 'published'
|
|
]);
|
|
|
|
$comment = Comment::create([
|
|
'comments_post_id' => $post->posts_id,
|
|
'comments_author_name' => 'John Doe',
|
|
'comments_author_email' => 'john@example.com',
|
|
'comments_content' => 'Great post!',
|
|
'comments_status' => 'approved'
|
|
]);
|
|
|
|
$this->assertNotNull($comment->comments_id);
|
|
$this->assertEquals($post->posts_id, $comment->comments_post_id);
|
|
$this->assertEquals('John Doe', $comment->comments_author_name);
|
|
}
|
|
}
|