composer require romegasoftware/nova-test-suite --dev
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'])
.
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);
$this->assertHasManyRelationships([
'product',
]);
$this->assertBelongsToRelationships([
'user',
]);
assertHasOneRelationships(array)
assertHasManyRelationships(array)
assertBelongsToRelationships(array)
assertBelongsToManyRelationships(array)
assertMorphToRelationships(array)
assertMorphOneRelationships(array)
assertMorphManyRelationships(array)
Use assertHasActions()
use assertHasLenses()
use assertHasFilters()
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();
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']);
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;
}
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();
Run the tests with:
vendor/bin/phpunit
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.