-
Notifications
You must be signed in to change notification settings - Fork 3
Add command tests #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
964438f
test: add tests for console command.
88031e1
Merge remote-tracking branch 'origin/49-add-service-provider-tests' i…
pirs1337 f594a48
feat: add tests
pirs1337 c2c4f57
Merge remote-tracking branch 'origin/49-add-service-provider-tests' i…
pirs1337 d326fe5
refactor: code
pirs1337 685d4c2
refactor: code
pirs1337 306e27b
feat: remove test
pirs1337 59b2c17
Merge remote-tracking branch 'origin/49-add-service-provider-tests' i…
pirs1337 f29abfb
Merge remote-tracking branch 'origin/master' into 49-add-command-tests
pirs1337 0843ed0
fix: test
pirs1337 dd8b074
Update tests/Support/Command/CommandMockTrait.php
DenTray c393d3c
Update tests/Support/Command/CommandMockTrait.php
DenTray 8e92bfb
Update tests/Support/Command/CommandMockTrait.php
DenTray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| 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'); | ||
| } | ||
| } |
This file contains hidden or 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
| 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; | ||
DenTray marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| $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); | ||
DenTray marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| $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()); | ||
| } | ||
| } | ||
This file contains hidden or 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
| 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', | ||
| ], | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or 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
| 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'; | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.