Files
eatvio-infra/tests/Feature/Plan/CreateTest.php

199 lines
7.1 KiB
PHP

<?php
namespace Tests\Feature\Plan;
use App\Livewire\Plan\Create;
use App\Models\Plan;
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 Plan $plan;
protected User $user;
public function setUp(): void
{
$this->refreshApplicationWithLocale('en');
Storage::fake('tmp-for-tests');
$this->image = UploadedFile::fake()->image('plan.png');
$this->user = User::factory()->create(['is_consultant' => true]);
$this->plan = Plan::factory()->make();
}
public function test_show_form_create_plan(): void
{
$response = $this
->actingAs($this->user)
->get(route('plan.create'));
$response->assertSuccessful();
}
public function test_publish_new_plan_success(): void
{
$this->actingAs($this->user);
$planCountAtStart = Plan::count();
$testCase = Livewire::test(Create::class)
->set('name', $this->plan->name)
->assertSet('name', $this->plan->name)
->set('image', $this->image)
->set('duration', $this->plan->duration)
->assertSet('duration', $this->plan->duration)
->set('state', $this->plan->state)
->assertSet('state', $this->plan->state)
->set('type', $this->plan->type)
->assertSet('type', $this->plan->type)
->set('details', $this->plan->details)
->assertSet('details', $this->plan->details)
->set('packets_count', $this->plan->packets_count)
->assertSet('packets_count', $this->plan->packets_count)
->set('packet1_options', $this->plan->packet1_options)
->assertSet('packet1_options', $this->plan->packet1_options)
->set('packet2_options', $this->plan->packet2_options)
->assertSet('packet2_options', $this->plan->packet2_options)
->set('packet3_options', $this->plan->packet3_options)
->assertSet('packet3_options', $this->plan->packet3_options)
->call('publish')
->assertSuccessful()
->assertHasNoErrors(['name', 'image', 'duration', 'details', 'packets_count', 'packet1_options', 'packet2_options', 'packet3_options']);
$lastCreatedPlan = Plan::orderBy('id', 'desc')->first();
$newPlanUrl = route('plan.show', $lastCreatedPlan, false);
$testCase->assertRedirect($newPlanUrl);
Storage::disk('tmp-for-tests')->assertExists($lastCreatedPlan->image);
$this->assertEquals($planCountAtStart + 1, Plan::count());
$response = $this->actingAs($this->user)->get(route('plan.index'));
$response->assertSuccessful();
}
public function test_to_draft_new_plan_success(): void
{
$this->actingAs($this->user);
$planCountAtStart = Plan::count();
$testCase = Livewire::test(Create::class)
->set('name', $this->plan->name)
->assertSet('name', $this->plan->name)
->set('image', $this->image)
->set('duration', $this->plan->duration)
->assertSet('duration', $this->plan->duration)
->set('state', $this->plan->state)
->assertSet('state', $this->plan->state)
->set('type', $this->plan->type)
->assertSet('type', $this->plan->type)
->set('details', $this->plan->details)
->assertSet('details', $this->plan->details)
->set('packets_count', $this->plan->packets_count)
->assertSet('packets_count', $this->plan->packets_count)
->set('packet1_options', $this->plan->packet1_options)
->assertSet('packet1_options', $this->plan->packet1_options)
->set('packet2_options', $this->plan->packet2_options)
->assertSet('packet2_options', $this->plan->packet2_options)
->set('packet3_options', $this->plan->packet3_options)
->assertSet('packet3_options', $this->plan->packet3_options)
->call('addToDrafts')
->assertSuccessful()
->assertHasNoErrors(['name', 'image', 'duration', 'details']);
$lastCreatedPlan = Plan::orderBy('id', 'desc')->first();
$newPlanUrl = route('plan.show', $lastCreatedPlan, false);
Storage::disk('tmp-for-tests')->assertExists($lastCreatedPlan->image);
$testCase->assertRedirect($newPlanUrl);
$this->assertEquals($planCountAtStart + 1, Plan::count());
}
public function test_to_preview_new_plan_success(): void
{
$this->actingAs($this->user);
$planCountAtStart = Plan::count();
$testCase = Livewire::test(Create::class)
->set('name', $this->plan->name)
->assertSet('name', $this->plan->name)
->set('image', $this->image)
->set('duration', $this->plan->duration)
->assertSet('duration', $this->plan->duration)
->set('state', $this->plan->state)
->assertSet('state', $this->plan->state)
->set('type', $this->plan->type)
->assertSet('type', $this->plan->type)
->set('details', $this->plan->details)
->assertSet('details', $this->plan->details)
->set('packets_count', $this->plan->packets_count)
->assertSet('packets_count', $this->plan->packets_count)
->set('packet1_options', $this->plan->packet1_options)
->assertSet('packet1_options', $this->plan->packet1_options)
->set('packet2_options', $this->plan->packet2_options)
->assertSet('packet2_options', $this->plan->packet2_options)
->set('packet3_options', $this->plan->packet3_options)
->assertSet('packet3_options', $this->plan->packet3_options)
->call('showPreview')
->assertSuccessful()
->assertHasNoErrors(['name', 'image', 'duration', 'details']);
$lastCreatedPlan = Plan::orderBy('id', 'desc')->first();
$newPlanUrl = route('plan.show', $lastCreatedPlan, false);
Storage::disk('tmp-for-tests')->assertExists($lastCreatedPlan->image);
$testCase->assertRedirect($newPlanUrl);
$this->assertEquals($planCountAtStart + 1, Plan::count());
}
public function test_set_empty_data(): void
{
$this->actingAs($this->user);
$planCountAtStart = Plan::count();
Livewire::test(Create::class)
->set('name', null)
->set('image', null)
->set('duration', null)
->set('state', null)
->set('type', null)
->set('details', null)
->set('packets_count', null)
->set('packet1_options', null)
->set('packet2_options', null)
->set('packet3_options', null)
->call('publish')
->assertHasErrors(['name', 'image', 'details']);
$this->assertEquals($planCountAtStart, Plan::count());
}
}