Commit d16f91bd authored by bvipul's avatar bvipul

Blog Unit test

parent 4b3a1cd8
......@@ -30,8 +30,8 @@ trait BlogRelationship
/**
* Blogs belongsTo with User.
*/
public function createdBy()
public function owner()
{
return $this->belongsTo(User::class, 'created_by', 'id');
return $this->belongsTo(User::class, 'created_by');
}
}
<?php
use App\Models\Blogs\Blog;
use Faker\Generator as Faker;
use App\Models\Access\User\User;
$factory->define(Blog::class, function (Faker $faker) {
$status = [
'Published',
'Draft',
'InActive',
'Scheduled'
];
return [
'name' => $faker->sentence,
'publish_datetime' => $faker->dateTime(),
'featured_image' => 'logo.png',
'content' => $faker->paragraph(3),
'status' => $status[$faker->numberBetween(0,3)],
'created_by' => function () {
return factory(User::class)->create()->id;
},
];
});
......@@ -2,6 +2,7 @@
use App\Models\Page\Page;
use Faker\Generator as Faker;
use App\Models\Access\User\User;
$factory->define(Page::class, function (Faker $faker) {
$title = $faker->sentence;
......@@ -10,6 +11,8 @@ $factory->define(Page::class, function (Faker $faker) {
'title' => $title,
'page_slug' => str_slug($title),
'description' => $faker->paragraph,
'created_by' => 1,
'created_by' => function () {
return factory(User::class)->create()->id;
},
];
});
<?php
namespace Tests\Feature\Backend;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ManageBlogsTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}
<?php
namespace Tests\Unit\Models;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\Blogs\Blog;
use App\Models\BlogTags\BlogTag;
use App\Models\Access\User\User;
use App\Models\BlogCategories\BlogCategory;
use Illuminate\Foundation\Testing\WithFaker;
class BlogTest extends TestCase
{
/** @test */
public function it_has_categories()
{
$this->actingAs($this->admin);
$blog = create(Blog::class, ['created_by' => access()->id()]);
$category = create(BlogCategory::class);
$blog->categories()->sync(array($category->id));
$this->assertInstanceOf(BlogCategory::class, $blog->categories->first());
$this->assertEquals($category->id, $blog->categories->first()->id);
}
/** @test */
public function it_has_tags()
{
$this->actingAs($this->admin);
$blog = create(Blog::class, ['created_by' => access()->id()]);
$tag = create(BlogTag::class);
$blog->tags()->sync(array($tag->id));
$this->assertInstanceOf(BlogTag::class, $blog->tags->first());
$this->assertEquals($tag->id, $blog->tags->first()->id);
}
/** @test */
public function it_has_an_owner()
{
$this->actingAs($this->admin);
$blog = create(Blog::class);
$this->assertInstanceOf(User::class, $blog->owner);
}
/** @test */
public function it_has_a_date_field_for_publish_datetime()
{
$this->actingAs($this->admin);
$blog = create(Blog::class);
$this->assertInstanceOf(Carbon::class, $blog->publish_datetime);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment