90 lines
2.7 KiB
PHP
90 lines
2.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Feature\Dish;
|
||
|
|
|
||
|
|
use App\Models\Dish;
|
||
|
|
use App\Models\Planner;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
use Illuminate\Testing\Fluent\AssertableJson;
|
||
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||
|
|
use Tests\TestCase;
|
||
|
|
use Tests\Traits\HasPlanner;
|
||
|
|
|
||
|
|
class CreateDishTest extends TestCase
|
||
|
|
{
|
||
|
|
use HasPlanner;
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
public function test_user_can_create_dish(): void
|
||
|
|
{
|
||
|
|
$planner = $this->planner;
|
||
|
|
|
||
|
|
$this->assertDatabaseEmpty(Dish::class);
|
||
|
|
$this->assertDatabaseEmpty('user_dishes');
|
||
|
|
|
||
|
|
$this
|
||
|
|
->actingAs($planner)
|
||
|
|
->post(route('api.dishes.store'), [
|
||
|
|
'name' => 'Pizza',
|
||
|
|
])
|
||
|
|
->assertStatus(200)
|
||
|
|
->assertJson(fn (AssertableJson $json) => $json
|
||
|
|
->where('success', true)
|
||
|
|
->has('payload', fn ($json) => $json
|
||
|
|
->has('dish', fn ($json) => $json
|
||
|
|
->has('id')
|
||
|
|
->where('planner_id', $planner->id)
|
||
|
|
->where('name', 'Pizza')
|
||
|
|
->has('users')
|
||
|
|
)
|
||
|
|
)
|
||
|
|
->where('errors', null)
|
||
|
|
);
|
||
|
|
|
||
|
|
$this->assertDatabaseCount(Dish::class, 1);
|
||
|
|
|
||
|
|
$dish = Dish::all()->first();
|
||
|
|
$this->assertEquals($planner->id, $dish->planner_id);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[DataProvider('invalidNameValues')]
|
||
|
|
public function test_it_throws_exception_for_invalid_name_values(?string $name, string $expectedError): void
|
||
|
|
{
|
||
|
|
$planner = $this->planner;
|
||
|
|
|
||
|
|
$this
|
||
|
|
->actingAs($planner)
|
||
|
|
->post(route('api.dishes.store'), $name ? ['name' => $name] : [])
|
||
|
|
// ->assertStatus(422)
|
||
|
|
->assertJson(fn (AssertableJson $json) => $json
|
||
|
|
->where('success', false)
|
||
|
|
->where('payload', null)
|
||
|
|
->where('errors', [$expectedError])
|
||
|
|
);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function invalidNameValues(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'empty name' => [
|
||
|
|
'name' => '',
|
||
|
|
'expectedError' => 'The name field is required.',
|
||
|
|
],
|
||
|
|
'null name' => [
|
||
|
|
'name' => null,
|
||
|
|
'expectedError' => 'The name field is required.',
|
||
|
|
],
|
||
|
|
'too long name' => [
|
||
|
|
'name' => Str::random(130),
|
||
|
|
'expectedError' => 'The name field must not be greater than 128 characters.',
|
||
|
|
],
|
||
|
|
'too short name' => [
|
||
|
|
'name' => 'a',
|
||
|
|
'expectedError' => 'The name field must be at least 3 characters.',
|
||
|
|
],
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|