forked from eatvio/eatvio-infra
added tests for blogPost
This commit is contained in:
180
tests/Feature/BlogPost/CreateTest.php
Normal file
180
tests/Feature/BlogPost/CreateTest.php
Normal file
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\BlogPost;
|
||||
|
||||
use App\Livewire\BlogPost\Create;
|
||||
use App\Models\BlogPost;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Http\Testing\File;
|
||||
|
||||
class CreateTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private File $image;
|
||||
|
||||
private BlogPost $blogPost;
|
||||
|
||||
protected User $user;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->refreshApplicationWithLocale('en');
|
||||
|
||||
Storage::fake('tmp-for-tests');
|
||||
$this->image = UploadedFile::fake()->image('blogPost.png');
|
||||
|
||||
$this->user = User::factory()->create(['is_consultant' => true]);
|
||||
$this->blogPost = BlogPost::factory()->make();
|
||||
|
||||
}
|
||||
|
||||
public function test_show_form_create_blogPost(): void
|
||||
{
|
||||
$response = $this
|
||||
->actingAs($this->user)
|
||||
->get(route('blog_post.create'));
|
||||
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
|
||||
public function test_publish_new_blogPost_success(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$blogPostCountAtStart = BlogPost::count();
|
||||
|
||||
$testCase = Livewire::test(Create::class)
|
||||
->set('title', $this->blogPost->title)
|
||||
->assertSet('title', $this->blogPost->title)
|
||||
->set('state', $this->blogPost->state)
|
||||
->assertSet('state', $this->blogPost->state)
|
||||
->set('image', $this->image)
|
||||
->set('description', $this->blogPost->description)
|
||||
->assertSet('description', $this->blogPost->description)
|
||||
->set('youtube_url', $this->blogPost->youtube_url)
|
||||
->assertSet('youtube_url', $this->blogPost->youtube_url)
|
||||
->call('publish')
|
||||
->assertSuccessful()
|
||||
->assertHasNoErrors(['title', 'description', 'youtube_url']);
|
||||
|
||||
|
||||
$lastCreatedBlogPost = BlogPost::orderBy('id', 'desc')->first();
|
||||
|
||||
$newBlogPostUrl = route('blog_post.show', $lastCreatedBlogPost);
|
||||
|
||||
$testCase->assertRedirect($newBlogPostUrl);
|
||||
|
||||
Storage::disk('tmp-for-tests')->assertExists($lastCreatedBlogPost->image);
|
||||
|
||||
$this->assertEquals($blogPostCountAtStart + 1, BlogPost::count());
|
||||
|
||||
$response = $this->actingAs($this->user)->get(route('blog_post.index'));
|
||||
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
|
||||
public function test_to_draft_new_blogPost_success(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$blogPostCountAtStart = BlogPost::count();
|
||||
|
||||
$testCase = Livewire::test(Create::class)
|
||||
->set('title', $this->blogPost->title)
|
||||
->assertSet('title', $this->blogPost->title)
|
||||
->set('state', $this->blogPost->state)
|
||||
->assertSet('state', $this->blogPost->state)
|
||||
->set('image', $this->image)
|
||||
->set('description', $this->blogPost->description)
|
||||
->assertSet('description', $this->blogPost->description)
|
||||
->set('youtube_url', $this->blogPost->youtube_url)
|
||||
->assertSet('youtube_url', $this->blogPost->youtube_url)
|
||||
->call('draft')
|
||||
->assertSuccessful()
|
||||
->assertHasNoErrors(['title', 'description', 'youtube_url']);
|
||||
|
||||
|
||||
self::assertEquals('archived', $testCase->state);
|
||||
|
||||
$lastCreatedBlogPost = BlogPost::orderBy('id', 'desc')->first();
|
||||
|
||||
$newBlogPostUrl = route('blog_post.show', $lastCreatedBlogPost);
|
||||
|
||||
$testCase->assertRedirect($newBlogPostUrl);
|
||||
|
||||
Storage::disk('tmp-for-tests')->assertExists($lastCreatedBlogPost->image);
|
||||
|
||||
$this->assertEquals($blogPostCountAtStart + 1, BlogPost::count());
|
||||
|
||||
$response = $this->actingAs($this->user)->get(route('blog_post.index'));
|
||||
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
|
||||
public function test_to_preview_new_blogPost_success(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$blogPostCountAtStart = BlogPost::count();
|
||||
|
||||
$testCase = Livewire::test(Create::class)
|
||||
->set('title', $this->blogPost->title)
|
||||
->assertSet('title', $this->blogPost->title)
|
||||
->set('state', $this->blogPost->state)
|
||||
->assertSet('state', $this->blogPost->state)
|
||||
->set('image', $this->image)
|
||||
->set('description', $this->blogPost->description)
|
||||
->assertSet('description', $this->blogPost->description)
|
||||
->set('youtube_url', $this->blogPost->youtube_url)
|
||||
->assertSet('youtube_url', $this->blogPost->youtube_url)
|
||||
->call('preview')
|
||||
->assertSuccessful()
|
||||
->assertHasNoErrors(['title', 'description', 'youtube_url']);
|
||||
|
||||
|
||||
self::assertEquals('archived', $testCase->state);
|
||||
|
||||
$lastCreatedBlogPost = BlogPost::orderBy('id', 'desc')->first();
|
||||
|
||||
$newBlogPostUrl = route('blog_post.show', $lastCreatedBlogPost);
|
||||
|
||||
$testCase->assertRedirect($newBlogPostUrl);
|
||||
|
||||
Storage::disk('tmp-for-tests')->assertExists($lastCreatedBlogPost->image);
|
||||
|
||||
$this->assertEquals($blogPostCountAtStart + 1, BlogPost::count());
|
||||
|
||||
$response = $this->actingAs($this->user)->get(route('blog_post.index'));
|
||||
|
||||
$response->assertSuccessful();
|
||||
}
|
||||
|
||||
public function test_set_empty_data(): void
|
||||
{
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$blogPostCountAtStart = BlogPost::count();
|
||||
|
||||
Livewire::test(Create::class)
|
||||
->set('title', null)
|
||||
->assertSet('title', null)
|
||||
->set('state', null)
|
||||
->assertSet('state', null)
|
||||
->set('image', null)
|
||||
->set('description', null)
|
||||
->assertSet('description', null)
|
||||
->set('youtube_url', null)
|
||||
->assertSet('youtube_url', null)
|
||||
->call('publish')
|
||||
->assertSuccessful()
|
||||
->assertHasErrors(['state', 'image', 'description']);
|
||||
|
||||
$this->assertEquals($blogPostCountAtStart, BlogPost::count());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user