Back to Course |
Laravel Web to Mobile API: Reuse Old Code with Services

Testing Admin Restaurant Web Routes

As we have tests for the admin restaurant API routes, let's write ones for web routes using Inertia components.


Web Tests

Create a new test file in the Web folder:

php artisan make:test Web/RestaurantTest

tests/Feature/Web/RestaurantTest.php

namespace Tests\Feature\Web;
 
use App\Models\City;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;
use Tests\Traits\WithTestingSeeder;
 
class RestaurantTest extends TestCase
{
use RefreshDatabase;
use WithTestingSeeder;
 
public function test_admin_can_view_restaurants_index(): void
{
$admin = User::factory()->admin()->create();
 
$response = $this
->actingAs($admin)
->get(route('admin.restaurants.index'));
 
$response->assertInertia(function (Assert $page) {
return $page->component('Admin/Restaurants/Index')
->has('restaurants');
});
}
 
public function test_admin_can_view_restaurants_create(): void
{
$admin = User::factory()->admin()->create();
 
$response = $this
->actingAs($admin)
->get(route('admin.restaurants.create'));
 
$response->assertInertia(function (Assert $page) {
return $page->component('Admin/Restaurants/Create')
->has('cities');
});
}
 
public function test_admin_can_store_restaurant()
{
$admin = User::factory()->admin()->create();
 
$response = $this
->actingAs($admin)
->postJson(route('admin.restaurants.store'), [
'restaurant_name' => 'Test Restaurant',
'email' => 'test.vendor@example.org',
'owner_name' => 'Test Owner Name',
'city_id' => City::where('name', 'Vilnius')->first()->id,
'address' => 'Dummy address 101',
]);
 
$response->assertRedirectToRoute('admin.restaurants.index');
}
 
public function test_admin_can_edit_restaurant()
{
$admin = User::factory()->admin()->create();
$vendor = $this->getVendorUser();
 
$response = $this
->actingAs($admin)
->get(route('admin.restaurants.edit', $vendor->restaurant));
 
$response->assertInertia(function (Assert $page) {
return $page->component('Admin/Restaurants/Edit')
->has('restaurant')
->has('cities');
});
}
 
public function test_admin_can_update_restaurant()
{
$admin = User::factory()->admin()->create();
$vendor = $this->getVendorUser();
$restaurant = $vendor->restaurant;
 
$response = $this->actingAs($admin)
->putJson(route('admin.restaurants.update', $restaurant), [
'restaurant_name' => 'Updated Restaurant Name',
'city_id' => City::where('name', 'Other')->first()->id,
'address' => 'Updated Address',
]);
 
$response->assertRedirectToRoute('admin.restaurants.index');
}
}

Testing Inertia endpoints are different than APIs. We can use the assertInertia(Closure $closure) assertion method to test the data provided to the Inertia response. We can also use the has method. You can think of this method as being similar to the PHP's isset function.

See the example:

use Inertia\Testing\AssertableInertia as Assert;
 
// ...
 
$response->assertInertia(function (Assert $page) {
return $page->component('Admin/Restaurants/Index')
->has('restaurants');
});

The closure function passed to assertInertia expects the AssertableInertia class as an argument. Here we check if the endpoint returns the Admin/Restaurants/Index component and has the property restaurants.

Since all data submitted to endpoints are XHR requests and pass JSON, we use the same postJson and putJson methods as we do testing APIs.

When submitting data, Inertia always expects a redirect. We can test if the endpoint redirects to the right route using the assertRedirectToRoute method. This implementation is seen in the test_admin_can_update_restaurant method:

$response->assertRedirectToRoute('admin.restaurants.index');

More information can be found on the official Inertia Testing page.


Run Tests

Now we can run both Web and API tests for restaurant endpoints with this command:

php artisan --filter RestaurantTest

Output:

 
PASS Tests\Feature\Api\RestaurantTest
admin can view restaurants 1.12s
admin can view restaurant 0.24s
admin can update restaurant 0.23s
 
PASS Tests\Feature\Web\RestaurantTest
admin can view restaurants index 0.26s
admin can view restaurants create 0.26s
admin can store restaurant 0.26s
admin can edit restaurant 0.25s
admin can update restaurant 0.31s
 
Tests: 8 passed (32 assertions)
Duration: 3.00s

Let's implement more services and tests in the next lesson.