romegasoftware / nova-test-suite
Library to help testing laravel nova resources faster
Installs: 3 832
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 0
Open Issues: 0
Type:package
Requires
README
Install
composer require romegasoftware/nova-test-suite --dev
Usage
Generate Resource Test Cases
To get you started run php artisan nova:test resource_name
. This will generate a Resource test and publish the NovaResourceTestCase
if it was not already published.
First thing you will need to do after creating a resource tests is filling the remapResource()
method. This method must map the nova fields names to
the resource properties. It's also not needed to map each property of the resource, just the ones that change or are required to be diferent.
protected function remapResource($resource): array { return [ 'location' => $resource->location_id, 'theme' => $resource->theme_id, ]; }
The $resource
parameter is a fresh generated model instance via factory()
and therefore should hold any necessary values you need. The $data
parameter is only filled if you call a nova request method with any data like $this->storeResource(['name' => 'test'])
.
requests
get resources
// retrieve all available resources = viewing the index page $this->getResources(); // retrieve a single resource = viewing a single resource in detail view $resource = Resource::factory()->create(); $this->getResources($resource);
store resources
// resource data is generated behind the scenes with factory()->make() $this->storeResource(); // also accepts model classes or arrays $resource = Resource::factory()->make(); $this->storeResource($resource); $this->storeResource(['name' => 'Vader']);
If a resource is stored successfully the returned status code of the response is 201
.
update resources
// resource data is generated behind the scenes with factory()->create() $this->updateResource(['name' => 'Vader']); // accepts model classes $resource = Resource::factory()->create(); $resource->name = 'Vader'; $this->updateResource($resource);
delete resources
// resource data is generated behind the scenes with factory()->create() $this->deleteResource(); // also accepts model classes, arrays or integers (ids) $resource = Resource::factory()->create(); $this->deleteResource($resource); $this->deleteResource(['id' => 12]); $this->deleteResource(12);
relationships
$this->assertHasManyRelationships([ 'product', ]); $this->assertBelongsToRelationships([ 'user', ]);
assertHasOneRelationships(array)
assertHasManyRelationships(array)
assertBelongsToRelationships(array)
assertBelongsToManyRelationships(array)
assertMorphToRelationships(array)
assertMorphOneRelationships(array)
assertMorphManyRelationships(array)
actions
Use assertHasActions()
lenses
use assertHasLenses()
filters
use assertHasFilters()
Asserting a request has failed
Since failed nova request return a redirect with status code 301
we introduced a new method assertNovaFailed()
which checks for this without having to think about what status code a failed nova response returns.
$this->storeResource() ->assertNovaFailed();
Testing required fields
To test if your Nova resource is setup correctly and check if all required fields are set as expected you can use the setNullValuesOn([..])
method, which assignes every key you enter a null
value for the next request.
$this->setNullValuesOn(['customer', 'number_of_participants']) ->storeResource() ->assertRequiredFields(['customer', 'number_of_participants']);
Using the default user
By default each nova request method checks whether a request was already authenticated through actingAs($user, 'api')
. If no user was provided to authenticate the request we will use the getDefaultUser()
method to authenticate your request. If you want to be explicit about using the default user for a request you can use $this->beDefaultUser()
which will return the current class, therefore it will also work with chaining e.g. $this->beDefaultUser()->storeResource()
.
If you want to use your own user for every request you can override the getDefaultUser()
method.
protected function getDefaultUser() { return $this->yourOwnUser; }
Debugging failing requests
To debug more easily why your nova request is failing you can chain assertSessionDoesntHaveErrors()
before you make any assertions about the status. This method will dump all session errors and the json response of the request.
$this->storeResource() ->assertSessionDoesntHaveErrors();
If you still can't figure out why you are receiving the status code of the response, try to get behind the reason of the response with withoutNovaExceptionHandling()
. This method call will create a callback which calls the underlying withoutExceptionHandling()
as soon as the Nova middleware runs. You can also chain the calls:
$this->withoutNovaExceptionHandling() ->storeResource();
Testing
Run the tests with:
vendor/bin/phpunit
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
Security
If you discover any security-related issues, please email bkeith@romegasoftware.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.