Search⌘ K
AI Features

Order Item Tests

Explore how to implement and run order item tests within Laravel APIs. Understand setting up test environments, validating input, checking responses, and verifying database changes to ensure reliable API functionality.

Setup

We will need a few components for each of our tests: a logged-in user, a restaurant, a menu item, a table, etc. PHPUnit’s setup() method can be helpful for this. ...

PHP
<?php
namespace Tests\Feature;
use App\Models\MenuItem;
use App\Models\Order;
use App\Models\Restaurant;
use App\Models\Table;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Testing\Fluent\AssertableJson;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
final class OrderItemTest extends TestCase
{
use RefreshDatabase;
private $user;
private $restaurant;
private $table;
private $menuItem;
private $order;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
$this->restaurant = Restaurant::factory()->create([
'user_id' => $this->user->id,
]);
$this->menuItem = MenuItem::factory()->create([
'restaurant_id' => $this->restaurant->id,
]);
$this->table = Table::factory()->create([
'restaurant_id' => $this->restaurant->id,
]);
$this->order = Order::factory()->create([
'restaurant_id' => $this->restaurant->id,
'table_id' => $this->table->id,
]);
Sanctum::actingAs($this->user, ['*']);
}
}

Add

...