Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion config/entity-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'tests' => 'tests',
'routes' => 'routes/api.php',
'factories' => 'database/factories',
'translations' => 'resources/lang/en/validation.php',
'translations' => 'lang/en/validation.php',
'resources' => 'app/Http/Resources',
'nova' => 'app/Nova',
],
Expand Down
13 changes: 0 additions & 13 deletions src/Commands/MakeEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,6 @@ protected function outputNewConfig($packageConfigs, $projectConfigs)

$newConfig = array_merge($flattenedPackageConfigs, $flattenedProjectConfigs);

$translations = 'lang/en/validation.php';
$translations = (version_compare(app()->version(), '9', '>=')) ? $translations : "resources/{$translations}";

if ($newConfig['paths.translations'] !== $translations) {
$newConfig['paths.translations'] = $translations;
}

$factories = 'database/factories';

if ($newConfig['paths.factories'] !== $factories) {
$newConfig['paths.factories'] = $factories;
}

$differences = array_diff_key($newConfig, $flattenedProjectConfigs);

foreach ($differences as $differenceKey => $differenceValue) {
Expand Down
3 changes: 2 additions & 1 deletion src/Generators/NovaResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ protected function getFieldsFromCommandLineArguments(): array

protected function getFieldsFromDatabase(): array
{
$modelClass = "App\\Models\\{$this->model}";
$modelClass = $this->getModelClass($this->model);
$model = app($modelClass);

$columns = DB::connection($model->getConnectionName())
->getDoctrineSchemaManager()
->listTableColumns($model->getTable());
Expand Down
116 changes: 116 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace RonasIT\Support\Tests;

use Carbon\Carbon;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Tests\Support\Command\CommandMockTrait;
use UnexpectedValueException;

class CommandTest extends TestCase
{
use CommandMockTrait;

public function testCallWithInvalidCrudOption()
{
$this->assertExceptionThrew(
className: UnexpectedValueException::class,
message: 'Invalid method T',
);

$this->artisan('make:entity Post --methods=T');
}

public function testCallWithMissingModelService()
{
$this->assertExceptionThrew(
className: ClassNotExistsException::class,
message: 'Cannot create API without entity.',
);

$this->artisan('make:entity Post --only-api');
}

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

Carbon::setTestNow('2016-10-20 11:05:00');

$this->mockFilesystem();
$this->mockGenerator();
$this->mockGettingModelInstance();

$this
->artisan('make:entity Post --methods=CRUD')
->assertSuccessful();

$this->assertGeneratedFileEquals('migration.php', 'database/migrations/2016_10_20_110500_posts_create_table.php');
$this->assertGeneratedFileEquals('factory.php', 'RonasIT/Support/Tests/Support/Command/Factories/PostFactory.php');
$this->assertGeneratedFileEquals('seeder.php', 'database/seeders/PostSeeder.php');
$this->assertGeneratedFileEquals('model.php', 'RonasIT/Support/Tests/Support/Command/Models/Post.php');
$this->assertGeneratedFileEquals('repository.php', 'app/Repositories/PostRepository.php');
$this->assertGeneratedFileEquals('service.php', 'app/Services/PostService.php');
$this->assertGeneratedFileEquals('create_request.php', 'app/Http/Requests/Post/CreatePostRequest.php');
$this->assertGeneratedFileEquals('get_request.php', 'app/Http/Requests/Post/GetPostRequest.php');
$this->assertGeneratedFileEquals('search_request.php', 'app/Http/Requests/Post/SearchPostsRequest.php');
$this->assertGeneratedFileEquals('update_request.php', 'app/Http/Requests/Post/UpdatePostRequest.php');
$this->assertGeneratedFileEquals('delete_request.php', 'app/Http/Requests/Post/DeletePostRequest.php');
$this->assertGeneratedFileEquals('controller.php', 'app/Http/Controllers/PostController.php');
$this->assertGeneratedFileEquals('resource.php', 'app/Http/Resources/Post/PostResource.php');
$this->assertGeneratedFileEquals('resource_collection.php', 'app/Http/Resources/Post/PostsCollectionResource.php');
$this->assertGeneratedFileEquals('routes.php', 'routes/api.php');
$this->assertGeneratedFileEquals('test.php', 'tests/PostTest.php');
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/PostTest/dump.sql');
$this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/PostTest/create_post_request.json');
$this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/PostTest/create_post_response.json');
$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');
}

public function testMakeOnly()
{
$this->mockFilesystemPostModelExists();

$this
->artisan('make:entity Post --methods=CRUD --only-repository')
->assertSuccessful();

$this->assertGeneratedFileEquals('make_only_repository.php', '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->assertFileDoesNotExist('tests/NovaPostTest.php');
$this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/nova_post_dump.sql');
$this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/create_post_request.json');
$this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/create_post_response.json');
$this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/update_post_request.json');
}
}
81 changes: 81 additions & 0 deletions tests/Support/Command/CommandMockTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace RonasIT\Support\Tests\Support\Command;

use Illuminate\Database\Connection;
use Illuminate\Support\Facades\DB;
use RonasIT\Support\Generators\NovaTestGenerator;
use RonasIT\Support\Tests\Support\Command\Models\Post;
use RonasIT\Support\Tests\Support\FileSystemMock;
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\SchemaManager;
use Mockery;

trait CommandMockTrait
{
use GeneratorMockTrait;

public function mockFilesystemPostModelExists(): void
{
$fileSystemMock = new FileSystemMock;

$fileSystemMock->models = ['Post.php' => $this->mockPhpFileContent()];
$fileSystemMock->config = ['entity-generator.php' => ''];

$fileSystemMock->setStructure();
}

public function mockFilesystem(): void
{
$fileSystemMock = new FileSystemMock;

$fileSystemMock->routes = [ 'api.php' => $this->mockPhpFileContent()];
$fileSystemMock->config = ['entity-generator.php' => ''];
$fileSystemMock->translations = [];

$fileSystemMock->setStructure();
}

public function mockGenerator(): void
{
$this->mockClass(NovaTestGenerator::class, [
$this->functionCall(
name: 'loadNovaActions',
result: [],
),
$this->functionCall(
name: 'loadNovaFields',
result: [],
),
$this->functionCall(
name: 'loadNovaFilters',
result: [],
),
]);

$this->mockNativeGeneratorFunctions(
$this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Command\Models\Post', true]),
$this->nativeClassExistsMethodCall(['Laravel\Nova\NovaServiceProvider', true]),
$this->nativeClassExistsMethodCall(['Laravel\Nova\NovaServiceProvider', true]),
$this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Command\Models\Post', true]),
);
}

public function mockGettingModelInstance(): void
{
$connectionMock = Mockery::mock(Connection::class)->makePartial();
$connectionMock
->expects('getDoctrineSchemaManager')
->andReturn(new SchemaManager);

$mock = Mockery::mock('alias:' . DB::class);
$mock
->expects('connection')
->with('pgsql')
->andReturn($connectionMock);

$mock->shouldReceive('beginTransaction', 'rollBack');

$this->app->instance('App\\Models\\Post', new Post());
}
}
26 changes: 26 additions & 0 deletions tests/Support/Command/Factories/PostFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace RonasIT\Support\Tests\Support\Command\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
use RonasIT\Support\Tests\Support\Command\Models\Post;

class PostFactory extends Factory
{
protected $model = Post::class;

public function definition(): array
{
return [
'title' => 'some title',
'body' => 'some body',
'posted_at' => Carbon::now(),
'drafted' => false,
'data' => [
'title' => '1',
'body' => '2',
],
];
}
}
27 changes: 27 additions & 0 deletions tests/Support/Command/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace RonasIT\Support\Tests\Support\Command\Models;

use Illuminate\Database\Eloquent\Model;
use RonasIT\Support\Traits\ModelTrait;

class Post extends Model
{
use ModelTrait;

protected $fillable = [
'title',
'body',
'data',
'drafted',
'user_id',
'posted_at',
'created_at',
'updated_at'
];

public function getConnectionName(): string
{
return 'pgsql';
}
}
13 changes: 11 additions & 2 deletions tests/Support/FileSystemMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FileSystemMock
public ?array $routes = null;
public ?array $factories = null;
public ?array $translations = null;
public ?array $config = null;

public function setStructure(): void
{
Expand Down Expand Up @@ -108,10 +109,18 @@ public function setStructure(): void
}

if (!is_null($this->translations)) {
$structure['resources']['lang']['en'] = [];
$structure['lang']['en'] = [];

foreach ($this->translations as $translation => $content) {
$structure['resources']['lang']['en'][$translation] = $content;
$structure['lang']['en'][$translation] = $content;
}
}

if (!is_null($this->config)) {
$structure['config'] = [];

foreach ($this->config as $config => $content) {
$structure['config'][$config] = $content;
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Support/Test/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Post extends Model
'updated_at'
];

public function getConnectionName(): string
{
return 'pgsql';
}

public function user()
{
return $this->belongsTo(User::class);
Expand Down
6 changes: 3 additions & 3 deletions tests/TranslationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public function testCreate()
->setModel('Post')
->generate();

$this->assertGeneratedFileEquals('validation.php', 'resources/lang/en/validation.php');
$this->assertGeneratedFileEquals('validation.php', 'lang/en/validation.php');

$this->assertEventPushed(
className: SuccessCreateMessage::class,
message: 'Created a new Translations dump on path: vfs://root/resources/lang/en/validation.php',
message: 'Created a new Translations dump on path: vfs://root/lang/en/validation.php',
);
}

Expand Down Expand Up @@ -55,7 +55,7 @@ public function testAppendNotFoundException()
->setModel('Post')
->generate();

$this->assertGeneratedFileEquals('validation_append_not_found_exception.php', 'resources/lang/en/validation.php');
$this->assertGeneratedFileEquals('validation_append_not_found_exception.php', 'lang/en/validation.php');

Event::assertNothingDispatched();
}
Expand Down
Loading