Integration Test: Determine all Nova Resources
Hello everyone,
I am trying to write a PHPUnit integration test that tests the API all Nova resources to catch the most obvious bugs.
The way I am thinking to go about this is:
- Determine all resource classes
- Determine the URI key
- Call the appropriate URLs with /app/resources/
<uriKey>and /nova-api/<uriKey>/<lenses/filters/actions>and assert a status code of 200
However, I am struggling with step 1. I can only get the comment resource, not any other resources defined in the project.
This is the test method, including step 1:
public function testGetAllResources()
{
dd(Nova::availableResources(NovaRequest::create('/app/dashboard')));
}
This code gives me the following output:
array:1 [
0 => "KirschbaumDevelopment\NovaComments\Nova\Comment"
]
Does someone have an idea as to why I am not getting all available resources via the availableResources method? Maybe there's a better solution for the issue?
The alternative would be using reflection on the default resource directory in order to get all classes, but I am trying to avoid that, as it is unreliable when defining resources outside the default path.
Edit: I have found a solution. You need to register the resources manually in the NovaServiceProvider, after which they appear in the $resources field of the Nova class. If you are using this method inside a data provider, you also need refreshApplication(), as the data provider gets executed even before the setUp method.
private function getAvailableResources() {
/* Load Laravel (Nova) classes */
$this->refreshApplication();
/* Register Nova resources */
$serviceProvider = new NovaServiceProvider(require_once base_path("bootstrap/app.php"));
$serviceProvider->resources();
return Nova::$resources;
}
Please or to participate in this conversation.