Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Commands/MakeEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class MakeEntityCommand extends Command
{--only-seeder : Set this flag if you want to create only seeder.}
{--only-nova-resource : Set this flag if you want to create only nova resource.}
{--only-nova-tests : Set this flag if you want to create only nova resource tests.}

{--resource-name= : Override the default Nova resource name. Used only with --only-nova-tests.}

{--methods=CRUD : Set types of methods to create. Affect on routes, requests classes, controller\'s methods and tests methods.}

{--i|integer=* : Add integer field to entity.}
Expand Down Expand Up @@ -213,7 +214,19 @@ protected function generate()

protected function runGeneration($generator)
{
app($generator)
$resourceName = "{$this->argument('name')}Resource";

if (!empty($this->option('resource-name'))){
$resourceName = $this->option('resource-name');
}

$generatorInstance = app($generator);

if (method_exists($generatorInstance, 'setResource')) {
$generatorInstance->setResource($resourceName);
}

$generatorInstance
->setModel($this->argument('name'))
->setFields($this->getFields())
->setRelations($this->getRelations())
Expand Down
74 changes: 54 additions & 20 deletions src/Generators/NovaTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,39 @@
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;

class NovaTestGenerator extends AbstractTestsGenerator
{
protected $novaModelName;
protected string $novaResourceName;

protected ?string $fullNovaResourcePath = null;

public function generate(): void
{
if (class_exists(NovaServiceProvider::class)) {
if (!$this->doesNovaResourceExists()) {
$this->throwFailureException(
ClassNotExistsException::class,
"Cannot create Nova{$this->model}Test cause {$this->model} Nova resource does not exist.",
"Create {$this->model} Nova resource."
"Cannot create Nova{$this->novaResourceName}Test cause {$this->novaResourceName} Nova resource does not exist.",
"Create {$this->novaResourceName} Nova resource."
);
}

if ($this->classExists('nova', "Nova{$this->model}Test")) {
if ($this->classExists('nova', "Nova{$this->novaResourceName}Test")) {
$this->throwFailureException(
ClassAlreadyExistsException::class,
"Cannot create Nova{$this->model}Test cause it's already exist.",
"Remove Nova{$this->model}Test."
"Cannot create Nova{$this->novaResourceName}Test cause it's already exist.",
"Remove Nova{$this->novaResourceName}Test."
);
}

if (!$this->classExists('models', $this->model)) {
$this->throwFailureException(
ClassNotExistsException::class,
"Cannot create Nova{$this->novaResourceName}Test cause {$this->model} does not exist.",
"Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'."
);
}

Expand All @@ -38,6 +50,13 @@ public function generate(): void
}
}

public function setResource($novaResourceName)
{
$this->novaResourceName = Str::studly($novaResourceName);

return $this;
}

public function generateTests(): void
{
if (!$this->isStubExists('nova_test')) {
Expand All @@ -58,9 +77,9 @@ public function generateTests(): void
'filters' => $filters,
]);

$this->saveClass('tests', "Nova{$this->model}Test", $fileContent);
$this->saveClass('tests', "Nova{$this->novaResourceName}Test", $fileContent);

event(new SuccessCreateMessage("Created a new Nova test: Nova{$this->model}Test"));
event(new SuccessCreateMessage("Created a new Nova test: Nova{$this->novaResourceName}Test"));
}

protected function getActions(): array
Expand All @@ -83,22 +102,22 @@ protected function getActions(): array

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return app("{$this->fullNovaResourcePath}")->actions(new NovaRequest());
return app($this->fullNovaResourcePath)->actions(new NovaRequest());

}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return app("{$this->fullNovaResourcePath}")->fields(new NovaRequest());
return app($this->fullNovaResourcePath)->fields(new NovaRequest());

}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return app("{$this->fullNovaResourcePath}")->filters(new NovaRequest());
return app($this->fullNovaResourcePath)->filters(new NovaRequest());

}

public function getTestClassName(): string
{
return "Nova{$this->model}Test";
return "Nova{$this->novaResourceName}Test";
}

protected function isFixtureNeeded($type): bool
Expand All @@ -108,15 +127,11 @@ protected function isFixtureNeeded($type): bool

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

foreach ($possibleNovaModelNames as $modelName) {
if ($this->classExists('nova', $modelName)) {
$this->novaModelName = $modelName;
foreach ($allNovaClasses as $class) {
if (Str::contains($class, $this->novaResourceName)) {
$this->fullNovaResourcePath = $class;

return true;
}
Expand All @@ -125,6 +140,25 @@ protected function doesNovaResourceExists(): bool
return false;
}

protected function getAllNovaClasses(): array
{
$classes = [];

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(app_path('Nova')));

foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$relativePath = str_replace(app_path() . DIRECTORY_SEPARATOR, '', $file->getPathname());

$class = 'App\\' . str_replace(['/', '.php'], ['\\', ''], $relativePath);

$classes[] = $class;
}
}

return $classes;
}

protected function collectFilters(): array
{
$filtersFromFields = $this->getFiltersFromFields();
Expand Down
56 changes: 51 additions & 5 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public function testCallCommand()
$this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/PostTest/update_post_request.json');
$this->assertGeneratedFileEquals('validation.php', 'lang/en/validation.php');
$this->assertGeneratedFileEquals('nova_resource.php', 'app/Nova/PostResource.php');
$this->assertGeneratedFileEquals('nova_test.php', 'tests/NovaPostTest.php');
$this->assertGeneratedFileEquals('nova_dump.php', 'tests/fixtures/NovaPostTest/nova_post_dump.sql');
$this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/NovaPostTest/create_post_request.json');
$this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/NovaPostTest/create_post_response.json');
$this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/NovaPostTest/update_post_request.json');
$this->assertGeneratedFileEquals('nova_test.php', 'tests/NovaPostResourceTest.php');
$this->assertGeneratedFileEquals('nova_dump.php', 'tests/fixtures/NovaPostResourceTest/nova_post_dump.sql');
$this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/NovaPostResourceTest/create_post_request.json');
$this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/NovaPostResourceTest/create_post_response.json');
$this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/NovaPostResourceTest/update_post_request.json');
}

public function testMakeOnly()
Expand Down Expand Up @@ -121,6 +121,52 @@ public function testMakeOnly()
$this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/update_post_request.json');
}

public function testMakeOnlyNovaTest(): void
{
Carbon::setTestNow('2016-10-20 11:05:00');

$this->mockFilesystemWithPostModelAndResource();

config([
'entity-generator.paths.models' => 'RonasIT\Support\Tests\Support\Command\Models',
'entity-generator.paths.factories' => 'RonasIT\Support\Tests\Support\Command\Factories',
]);

$this->mockGeneratorOnlyNovaTests();

$this
->artisan('make:entity Post --only-nova-tests --resource-name=PostResource')
->assertSuccessful();

$this->assertFileDoesNotExist('app/Repositories/PostRepository.php');
$this->assertFileDoesNotExist('database/migrations/2016_10_20_110500_posts_create_table.php');
$this->assertFileDoesNotExist('database/factories/PostFactory.php');
$this->assertFileDoesNotExist('database/seeders/PostSeeder.php');
$this->assertFileDoesNotExist('app/Models/Post.php');
$this->assertFileDoesNotExist('app/Services/PostService.php');
$this->assertFileDoesNotExist('app/Http/Requests/Post/CreatePostRequest.php');
$this->assertFileDoesNotExist('app/Http/Requests/Post/GetPostRequest.php');
$this->assertFileDoesNotExist('app/Http/Requests/Post/SearchPostsRequest.php');
$this->assertFileDoesNotExist('app/Http/Requests/Post/UpdatePostRequest.php');
$this->assertFileDoesNotExist('app/Http/Requests/Post/DeletePostRequest.php');
$this->assertFileDoesNotExist('app/Http/Controllers/PostController.php');
$this->assertFileDoesNotExist('app/Http/Resources/Post/PostResource.php');
$this->assertFileDoesNotExist('app/Http/Resources/Post/PostsCollectionResource.php');
$this->assertFileDoesNotExist('routes/api.php');
$this->assertFileDoesNotExist('tests/PostTest.php');
$this->assertFileDoesNotExist('tests/fixtures/PostTest/dump.sql');
$this->assertFileDoesNotExist('tests/fixtures/PostTest/create_post_request.json');
$this->assertFileDoesNotExist('tests/fixtures/PostTest/create_post_response.json');
$this->assertFileDoesNotExist('tests/fixtures/PostTest/update_post_request.json');
$this->assertFileDoesNotExist('lang/en/validation.php');
$this->assertFileDoesNotExist('app/Nova/PostResource.php');
$this->assertGeneratedFileEquals('nova_test.php', 'tests/NovaPostResourceTest.php');
$this->assertGeneratedFileEquals('nova_dump.php', 'tests/fixtures/NovaPostResourceTest/nova_post_dump.sql');
$this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/NovaPostResourceTest/create_post_request.json');
$this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/NovaPostResourceTest/create_post_response.json');
$this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/NovaPostResourceTest/update_post_request.json');
}

public function testCallWithNotDefaultConfig()
{
$this->app->instance('path.base', $this->generatedFileBasePath);
Expand Down
47 changes: 36 additions & 11 deletions tests/NovaTestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace RonasIT\Support\Tests;

use RonasIT\Support\Tests\Support\Models\WelcomeBonus;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Generators\NovaTestGenerator;
use RonasIT\Support\Tests\Support\NovaTestGeneratorTest\NovaTestGeneratorMockTrait;
use Laravel\Nova\NovaServiceProvider;
use RonasIT\Support\Tests\Support\Models\WelcomeBonus;
use Mockery;

class NovaTestGeneratorTest extends TestCase
Expand All @@ -28,9 +28,7 @@ public function testGenerateResourceNotExists()
$this->mockNovaServiceProviderExists();

$this->mockClass(NovaTestGenerator::class, [
$this->classExistsMethodCall(['nova', 'PostNovaResource'], false),
$this->classExistsMethodCall(['nova', 'PostResource'], false),
$this->classExistsMethodCall(['nova', 'Post'], false),
$this->doesNovaResourceExistsCall(false),
]);

$this->assertExceptionThrew(
Expand All @@ -40,6 +38,7 @@ className: ClassNotExistsException::class,

app(NovaTestGenerator::class)
->setModel('Post')
->setResource('Post')
->generate();
}

Expand All @@ -48,8 +47,7 @@ public function testGenerateNovaTestAlreadyExists()
$this->mockNovaServiceProviderExists();

$this->mockClass(NovaTestGenerator::class, [
$this->classExistsMethodCall(['nova', 'PostNovaResource']),
$this->classExistsMethodCall(['nova', 'NovaPostTest'])
$this->classExistsMethodCall(['nova', 'NovaPostTest']),
]);

$this->assertExceptionThrew(
Expand All @@ -59,30 +57,40 @@ className: ClassAlreadyExistsException::class,

app(NovaTestGenerator::class)
->setModel('Post')
->setResource('Post')
->generate();
}

public function testNovaTestStubNotExist()
{
config([
'entity-generator.paths.models' => 'RonasIT/Support/Tests/Support/Models',
'entity-generator.stubs.nova_test' => 'incorrect_stub',
]);

$this->mockNativeGeneratorFunctions(
$this->nativeClassExistsMethodCall([NovaServiceProvider::class, true]),
$this->nativeClassExistsMethodCall([WelcomeBonus::class, true]),
);

$this->mockNovaRequestClassCall();

config([
'entity-generator.paths.models' => 'RonasIT/Support/Tests/Support/Models',
'entity-generator.stubs.nova_test' => 'incorrect_stub',
$this->mockClass(NovaTestGenerator::class, [
$this->classExistsMethodCall(['nova', 'NovaWelcomeBonusTest'], false),
$this->classExistsMethodCall(['models', 'WelcomeBonus']),
$this->classExistsMethodCall(['models', 'User'], false),
$this->classExistsMethodCall(['factories', 'WelcomeBonusFactory'], false),
$this->classExistsMethodCall(['factories', 'WelcomeBonusFactory'], false),
]);

$this->mockNovaRequestClassCall();

$mock = Mockery::mock('alias:Illuminate\Support\Facades\DB');
$mock
->shouldReceive('beginTransaction', 'rollBack')
->once();

app(NovaTestGenerator::class)
->setModel('WelcomeBonus')
->setResource('WelcomeBonus')
->generate();

$this->assertFileDoesNotExist('tests/NovaWelcomeBonusTest.php');
Expand All @@ -108,8 +116,16 @@ public function testDumpStubNotExist()
'entity-generator.stubs.dump' => 'incorrect_stub',
]);

$this->mockClass(NovaTestGenerator::class, [
$this->classExistsMethodCall(['nova', 'NovaWelcomeBonusTest'], false),
$this->classExistsMethodCall(['models', 'WelcomeBonus']),
$this->classExistsMethodCall(['models', 'User'], false),
$this->classExistsMethodCall(['factories', 'WelcomeBonusFactory'], false),
]);

app(NovaTestGenerator::class)
->setModel('WelcomeBonus')
->setResource('WelcomeBonus')
->generate();

$this->assertGeneratedFileEquals('created_resource_test.php', 'tests/NovaWelcomeBonusTest.php');
Expand All @@ -130,6 +146,14 @@ public function testSuccess()
'entity-generator.paths.models' => 'RonasIT/Support/Tests/Support/Models',
]);

$this->mockClass(NovaTestGenerator::class, [
$this->classExistsMethodCall(['nova', 'NovaWelcomeBonusTest'], false),
$this->classExistsMethodCall(['models', 'WelcomeBonus']),
$this->classExistsMethodCall(['models', 'User'], false),
$this->classExistsMethodCall(['factories', 'WelcomeBonusFactory'], false),
$this->classExistsMethodCall(['factories', 'WelcomeBonusFactory'], false),
]);

$mock = Mockery::mock('alias:Illuminate\Support\Facades\DB');
$mock
->shouldReceive('beginTransaction', 'rollBack')
Expand All @@ -144,6 +168,7 @@ public function testSuccess()

app(NovaTestGenerator::class)
->setModel('WelcomeBonus')
->setResource('WelcomeBonus')
->generate();

$this->assertGeneratedFileEquals('created_resource_test.php', 'tests/NovaWelcomeBonusTest.php');
Expand Down
Loading