Skip to content

Commit

Permalink
Merge pull request #66 from RonasIT/add-nova-tests-generation
Browse files Browse the repository at this point in the history
Correct nova test generator.
  • Loading branch information
DenTray committed Dec 20, 2023
2 parents a793bcb + b65f091 commit 5df22a3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/Generators/AbstractTestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ protected function getModelFields($model): array

protected function getMockModel($model): array
{
if (!$this->isFactoryExists($model)) {
return [];
}

$modelClass = $this->getModelClass($model);
$hasFactory = method_exists($modelClass, 'factory') && $this->classExists('factory', "{$model}Factory");
$factory = ($hasFactory) ? $modelClass::factory() : factory($modelClass);
Expand Down
31 changes: 26 additions & 5 deletions src/Generators/NovaTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

class NovaTestGenerator extends AbstractTestsGenerator
{
protected $novaModelName;

public function generate(): void
{
if (class_exists(NovaServiceProvider::class)) {
if (!$this->classExists('nova', $this->model)) {
if (!$this->doesNovaResourceExists()) {
$this->throwFailureException(
ClassNotExistsException::class,
"Cannot create Nova{$this->model}Test cause {$this->model} Nova resource does not exist.",
Expand Down Expand Up @@ -80,17 +82,17 @@ protected function getActions(): array

protected function loadNovaActions()
{
return app("\\App\\Nova\\{$this->model}")->actions(new NovaRequest());
return app("\\App\\Nova\\{$this->novaModelName}")->actions(new NovaRequest());
}

protected function loadNovaFields()
{
return app("\\App\\Nova\\{$this->model}")->fields(new NovaRequest());
return app("\\App\\Nova\\{$this->novaModelName}")->fields(new NovaRequest());
}

protected function loadNovaFilters()
{
return app("\\App\\Nova\\{$this->model}")->filters(new NovaRequest());
return app("\\App\\Nova\\{$this->novaModelName}")->filters(new NovaRequest());
}

public function getTestClassName(): string
Expand All @@ -103,7 +105,26 @@ protected function isFixtureNeeded($type): bool
return true;
}

protected function collectFilters()
protected function doesNovaResourceExists(): bool
{
$possibleNovaModelNames = [
"{$this->model}NovaResource",
"{$this->model}Resource",
$this->model
];

foreach ($possibleNovaModelNames as $modelName) {
if ($this->classExists('nova', $modelName)) {
$this->novaModelName = $modelName;

return true;
}
}

return false;
}

protected function collectFilters(): array
{
$filtersFromFields = $this->getFiltersFromFields();
$filters = $this->getFilters();
Expand Down
3 changes: 1 addition & 2 deletions stubs/nova_test.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
namespace App\Tests;

use App\Models\{{$entity}};
use Illuminate\Support\Collection;
use RonasIT\Support\Tests\ModelTestState;
use RonasIT\Support\Traits\AuthTestTrait;
@if($shouldUseStatus)
use Symfony\Component\HttpFoundation\Response;
@endif

class {{$entity}}Test extends TestCase
class Nova{{$entity}}Test extends TestCase
{
use AuthTestTrait;

Expand Down
2 changes: 2 additions & 0 deletions tests/NovaTestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function testCreateNovaTestsResourceNotExists()

public function testCreateNovaTestAlreadyExists()
{
$this->setupConfigurations();

$mock = $this->mockClassExistsFunction();

$this->expectException(ClassAlreadyExistsException::class);
Expand Down
13 changes: 11 additions & 2 deletions tests/Support/NovaTestMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getGeneratorMockForNonExistingNovaResource(): MockInterface
$mock
->shouldAllowMockingProtectedMethods()
->shouldReceive('classExists')
->once()
->times(3)
->andReturn(false);

return $mock;
Expand All @@ -87,10 +87,19 @@ public function getGeneratorMockForExistingNovaResourceTest(): MockInterface
$mock
->shouldAllowMockingProtectedMethods()
->shouldReceive('classExists')
->once()
->with('nova', 'Post')
->andReturn(true);

$mock
->shouldReceive('classExists')
->with('nova', 'PostResource')
->andReturn(false);

$mock
->shouldReceive('classExists')
->with('nova', 'PostNovaResource')
->andReturn(false);

$mock
->shouldReceive('classExists')
->once()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace App\Tests;

use App\Models\Post;
use Illuminate\Support\Collection;
use RonasIT\Support\Tests\ModelTestState;
use RonasIT\Support\Traits\AuthTestTrait;

class PostTest extends TestCase
class NovaPostTest extends TestCase
{
use AuthTestTrait;

Expand Down

0 comments on commit 5df22a3

Please sign in to comment.