Files
eatvio-infra/tests/Feature/Plan/CreateTest.php
Anatoliy Poloz ec216293ef plan styling fix
2023-12-20 11:23:12 +03:00

186 lines
6.8 KiB
PHP

<?php
namespace Tests\Feature\Plan;
use App\Livewire\Plan\Create;
use App\Models\Plan;
// use Livewire\WithFileUploads;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
class CreateTest extends TestCase
{
use RefreshDatabase;
private $image;
private $plan;
protected User $user;
public function setUp(): void
{
parent::setUp();
Storage::fake('plans');
$this->image = UploadedFile::fake()->image('plan.png');
$this->user = User::factory()->create(['is_consultant' => true]);
$this->plan = Plan::factory()->make();
$this->refreshApplicationWithLocale('en');
}
public function test_show_form_create_plan()
{
$response = $this
->actingAs($this->user)
->get(route('plan.create'));
$response->assertSuccessful();
// ->assertRedirect(route('home'));
}
public function test_publish_new_plan_success(): void
{
$this->actingAs($this->user);
// $this->assertEquals($this->plan->count(), $count = Plan::count());
$planCountAtStart = Plan::count();
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']);
Storage::disk('public')->assertExists($this->plan->image);
$this->assertEquals($planCountAtStart + 1, Plan::count());
$response = $this->actingAs($this->user)->get(route('plan.index'));
$response->assertSuccessful();
// $this->assertEquals($count + 1, Plan::count());
}
public function test_to_draft_new_plan_success(): void
{
$this->actingAs($this->user);
$planCountAtStart = Plan::count();
// $planLastId = $this->plan->latest()->first()->id;
// $expectedRedirectRoute = route('plan.show', ['plan' => $planLastId + 1]);
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')
->assertRedirect()
->assertHasNoErrors(['name', 'image', 'duration', 'details']);
Storage::disk('public')->assertExists($this->plan->image);
$this->assertEquals($planCountAtStart + 1, Plan::count());
}
public function test_to_preview_new_plan_success(): void
{
$this->actingAs($this->user);
$planCountAtStart = Plan::count();
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']);
Storage::disk('public')->assertExists($this->plan->image);
$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', 'duration', 'details']);
$this->assertEquals($planCountAtStart, Plan::count());
}
}