Files
eatvio-infra/tests/Feature/Diet/CreateTest.php
2024-01-10 17:11:50 +03:00

188 lines
6.1 KiB
PHP

<?php
namespace Tests\Feature\Diet;
use App\Livewire\Diet\Create;
use App\Models\Diet;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Testing\File;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
class CreateTest extends TestCase
{
use RefreshDatabase;
private File $image;
private Diet $diet;
protected User $user;
public function setUp(): void
{
$this->refreshApplicationWithLocale('en');
Storage::fake('tmp-for-tests');
$this->image = UploadedFile::fake()->image('diet.png');
$this->user = User::factory()->create(['is_consultant' => true]);
$this->diet = Diet::factory()->make();
}
public function test_show_form_create_diet(): void
{
$response = $this
->actingAs($this->user)
->get(route('diet.create'));
$response->assertSuccessful();
}
public function test_publish_new_diet_success(): void
{
$this->actingAs($this->user);
$dietCountAtStart = Diet::count();
$testCase = Livewire::test(Create::class)
->set('name', $this->diet->name)
->assertSet('name', $this->diet->name)
->set('image', $this->image)
->set('duration', $this->diet->duration)
->assertSet('duration', $this->diet->duration)
->set('state', $this->diet->state)
->assertSet('state', $this->diet->state)
->set('type', $this->diet->type)
->assertSet('type', $this->diet->type)
->set('details', $this->diet->details)
->assertSet('details', $this->diet->details)
->set('daily_schedule', $this->diet->daily_schedule)
->assertSet('daily_schedule', $this->diet->daily_schedule)
->call('publish')
->assertSuccessful()
->assertHasNoErrors(['name', 'image', 'duration', 'state', 'details', 'daily_schedule', 'type']);
$lastCreatedDiet = Diet::orderBy('id', 'desc')->first();
$this->assertEquals($dietCountAtStart + 1, Diet::count());
$newDietUrl = route('diet.show', $lastCreatedDiet);
$testCase->assertRedirect($newDietUrl);
Storage::disk('tmp-for-tests')->assertExists($lastCreatedDiet->image);
$response = $this->actingAs($this->user)->get(route('diet.index'));
$response->assertSuccessful();
}
public function test_to_draft_new_diet_success(): void
{
$this->actingAs($this->user);
$dietCountAtStart = Diet::count();
$testCase = Livewire::test(Create::class)
->set('name', $this->diet->name)
->assertSet('name', $this->diet->name)
->set('image', $this->image)
->set('duration', $this->diet->duration)
->assertSet('duration', $this->diet->duration)
->set('state', $this->diet->state)
->assertSet('state', $this->diet->state)
->set('type', $this->diet->type)
->assertSet('type', $this->diet->type)
->set('details', $this->diet->details)
->assertSet('details', $this->diet->details)
->set('daily_schedule', $this->diet->daily_schedule)
->assertSet('daily_schedule', $this->diet->daily_schedule)
->call('addToDrafts')
->assertSuccessful()
->assertHasNoErrors(['name', 'image', 'duration', 'details', 'daily_schedule']);
self::assertEquals('draft', $testCase->state);
$lastCreatedDiet = Diet::orderBy('id', 'desc')->first();
$newDietUrl = route('diet.show', $lastCreatedDiet);
$testCase->assertRedirect($newDietUrl);
Storage::disk('tmp-for-tests')->assertExists($lastCreatedDiet->image);
$this->assertEquals($dietCountAtStart + 1, Diet::count());
$response = $this->actingAs($this->user)->get(route('diet.index'));
$response->assertSuccessful();
}
public function test_to_preview_new_diet_success(): void
{
$this->actingAs($this->user);
$dietCountAtStart = Diet::count();
$testCase = Livewire::test(Create::class)
->set('name', $this->diet->name)
->assertSet('name', $this->diet->name)
->set('image', $this->image)
->set('duration', $this->diet->duration)
->assertSet('duration', $this->diet->duration)
->set('state', $this->diet->state)
->assertSet('state', $this->diet->state)
->set('type', $this->diet->type)
->assertSet('type', $this->diet->type)
->set('details', $this->diet->details)
->assertSet('details', $this->diet->details)
->set('daily_schedule', $this->diet->daily_schedule)
->assertSet('daily_schedule', $this->diet->daily_schedule)
->call('showPreview')
->assertSuccessful()
->assertHasNoErrors(['name', 'image', 'duration', 'details', 'daily_schedule']);
self::assertEquals('draft', $testCase->state);
$lastCreatedDiet = Diet::orderBy('id', 'desc')->first();
$newDietUrl = route('diet.show', $lastCreatedDiet);
$testCase->assertRedirect($newDietUrl);
Storage::disk('tmp-for-tests')->assertExists($lastCreatedDiet->image);
$this->assertEquals($dietCountAtStart + 1, Diet::count());
$response = $this->actingAs($this->user)->get(route('diet.index'));
$response->assertSuccessful();
}
public function test_set_empty_data(): void
{
$this->actingAs($this->user);
$dietCountAtStart = Diet::count();
Livewire::test(Create::class)
->set('name', null)
->set('image', null)
->set('duration', null)
->set('state', null)
->set('type', null)
->set('details', null)
->set('daily_schedule', null)
->call('publish')
->assertHasErrors(['name', 'image', 'duration', 'details', 'state', 'type']);
$this->assertEquals($dietCountAtStart, Diet::count());
}
}