If you find this website useful, your sponsorship would greatly support its ongoing development and maintenance. Learn more

Tests

Laravel AdminTW comes with a suite of tests using PestPHP.

There are 74 tests out the box that ensured the application works as expected.

/tests/Pest.php contains:


    
    uses(
        TestCase::class,
        RefreshDatabase::class,
        WithFaker::class
    )->in(__DIR__);
        

This uses the test case from /tests/TestCase.php This file is where custom methods have been added. More on this below.

Before each tests the database in memory is cleared, all migrations and seeds are re-run this ensures each test is in a fresh state and does not depend on other tests.

Inside TestCase.php there is a custom method called authenticate this is used to create a user, give the user the role speciied and logged in.


    
    authenticate(string $role = 'admin', string $permissionName = '')
        

Permissions when passed will be created and assigned to the user, Admin roles have all permissions by default so permissions work best with none admin roles.

For example to run a test that requires a user to be authenticated:


    
    test('can see users page')
    ->authenticate()
    ->get(fn() => route('admin.users.index'))
    ->assertOk();
        

Writing Tests

Writing test is a simple as creating a file ending with Test.php inside the Tests/Unit or Tests/Feature folders. Ie PaymentTest.php

Since AdminTW uses PestPHP there are no namespaces to import, the Pest.php takes care of loading the required classes. To create new tests simple write the tests inside php:


    
    test('can see profile')
    ->authenticate()
    ->get(fn() => route('admin.users.show', User::factory()->create()))
    ->assertOk();
        

© 2024 Laravel AdminTW. All rights reserved.

Built by David Carr