Last active
February 15, 2024 02:37
-
-
Save jhm-ciberman/b6063c31a680b75fca55f7dcb37f66ae to your computer and use it in GitHub Desktop.
Simple Automatic Test for Laravel Nova Resources (Updated for Nova 4)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature; | |
use Tests\TestCase; | |
use Laravel\Nova\Http\Requests\NovaRequest; | |
use Laravel\Nova\Nova; | |
class NovaResourcesTest extends TestCase | |
{ | |
public function testAllNovaResources() | |
{ | |
// This test only instanciates one resource of each type | |
// It is meant for testing only basic syntax errors | |
// Nova resource behaviours should be manually tested | |
Nova::resourcesIn(app_path('Nova')); | |
$resources = collect(Nova::$resources); | |
$this->assertTrue($resources->count() > 0); | |
$resources->each(function($resourceClass) { | |
$this->executeTestFor($resourceClass); | |
}); | |
} | |
private function executeTestFor($resourceClass) | |
{ | |
// Asserts the model asociated to the nova resource can be instantiated | |
$model = app($resourceClass::$model); | |
$this->assertNotNull($model); | |
// Instantiates the nova resource | |
$resource = new $resourceClass($model); | |
$this->assertResourceMethod($resource, 'fields'); | |
$this->assertResourceMethod($resource, 'cards'); | |
$this->assertResourceMethod($resource, 'filters'); | |
$this->assertResourceMethod($resource, 'lenses'); | |
$this->assertResourceMethod($resource, 'actions'); | |
} | |
public function assertResourceMethod($resource, $methodName) | |
{ | |
$request = new NovaRequest(); | |
$array = $resource->{$methodName}($request); | |
$this->assertIsArray($array); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
assertInternalType
has been removed as of PHPUnit 9, so line 51 will have to be changed to$this->assertIsArray($array);