Skip to content

Commit f594a48

Browse files
committed
feat: add tests
refs: #49
1 parent 88031e1 commit f594a48

29 files changed

+1145
-38
lines changed

src/Generators/NovaResourceGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ protected function getFieldsFromCommandLineArguments(): array
140140

141141
protected function getFieldsFromDatabase(): array
142142
{
143-
$modelClass = "App\\Models\\{$this->model}";
143+
$modelClass = $this->getModelClass($this->model);
144144
$model = app($modelClass);
145+
145146
$columns = DB::connection($model->getConnectionName())
146147
->getDoctrineSchemaManager()
147148
->listTableColumns($model->getTable());

tests/CommandTest.php

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace RonasIT\Support\Tests;
44

5-
use RonasIT\Support\EntityGeneratorServiceProvider;
5+
use Carbon\Carbon;
66
use RonasIT\Support\Exceptions\ClassNotExistsException;
77
use RonasIT\Support\Tests\Support\Command\CommandMockTrait;
88
use UnexpectedValueException;
@@ -11,47 +11,101 @@ class CommandTest extends TestCase
1111
{
1212
use CommandMockTrait;
1313

14-
public function setUp(): void
15-
{
16-
parent::setUp();
17-
18-
$provider = new EntityGeneratorServiceProvider($this->app);
19-
$provider->boot();
20-
}
21-
2214
public function testCallWithInvalidCrudOption()
2315
{
24-
$this->expectException(UnexpectedValueException::class);
25-
$this->expectErrorMessage('Invalid method T');
16+
$this->assertExceptionThrew(
17+
className: UnexpectedValueException::class,
18+
message: 'Invalid method T',
19+
);
2620

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

3024
public function testCallWithMissingModelService()
3125
{
32-
$this->mockConfigurations();;
33-
34-
$this->expectException(ClassNotExistsException::class);
35-
$this->expectErrorMessage('Cannot create API without entity.');
26+
$this->assertExceptionThrew(
27+
className: ClassNotExistsException::class,
28+
message: 'Cannot create API without entity.',
29+
);
3630

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

4034
public function testCallCommand()
4135
{
42-
$this->mockConfigurations();
36+
Carbon::setTestNow('2016-10-20 11:05:00');
37+
4338
$this->mockFilesystem();
39+
$this->mockGenerator();
40+
$this->mockGettingModelInstance();
4441

45-
$this->artisan('make:entity Post --methods=CRUD')
42+
$this
43+
->artisan('make:entity Post --methods=CRUD')
4644
->assertSuccessful();
45+
46+
$this->assertGeneratedFileEquals('migration.php', 'database/migrations/2016_10_20_110500_posts_create_table.php');
47+
$this->assertGeneratedFileEquals('factory.php', 'database/factories/PostFactory.php');
48+
$this->assertGeneratedFileEquals('seeder.php', 'database/seeders/PostSeeder.php');
49+
$this->assertGeneratedFileEquals('model.php', 'app/Models/Post.php');
50+
$this->assertGeneratedFileEquals('repository.php', 'app/Repositories/PostRepository.php');
51+
$this->assertGeneratedFileEquals('service.php', 'app/Services/PostService.php');
52+
$this->assertGeneratedFileEquals('create_request.php', 'app/Http/Requests/Post/CreatePostRequest.php');
53+
$this->assertGeneratedFileEquals('get_request.php', 'app/Http/Requests/Post/GetPostRequest.php');
54+
$this->assertGeneratedFileEquals('search_request.php', 'app/Http/Requests/Post/SearchPostsRequest.php');
55+
$this->assertGeneratedFileEquals('update_request.php', 'app/Http/Requests/Post/UpdatePostRequest.php');
56+
$this->assertGeneratedFileEquals('delete_request.php', 'app/Http/Requests/Post/DeletePostRequest.php');
57+
$this->assertGeneratedFileEquals('controller.php', 'app/Http/Controllers/PostController.php');
58+
$this->assertGeneratedFileEquals('resource.php', 'app/Http/Resources/Post/PostResource.php');
59+
$this->assertGeneratedFileEquals('resource_collection.php', 'app/Http/Resources/Post/PostsCollectionResource.php');
60+
$this->assertGeneratedFileEquals('routes.php', 'routes/api.php');
61+
$this->assertGeneratedFileEquals('test.php', 'tests/PostTest.php');
62+
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/PostTest/dump.sql');
63+
$this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/PostTest/create_post_request.json');
64+
$this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/PostTest/create_post_response.json');
65+
$this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/PostTest/update_post_request.json');
66+
$this->assertGeneratedFileEquals('validation.php', 'lang/en/validation.php');
67+
$this->assertGeneratedFileEquals('nova_resource.php', 'app/Nova/PostResource.php');
68+
$this->assertGeneratedFileEquals('nova_test.php', 'tests/NovaPostTest.php');
69+
$this->assertGeneratedFileEquals('nova_dump.php', 'tests/fixtures/NovaPostTest/nova_post_dump.sql');
70+
$this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/NovaPostTest/create_post_request.json');
71+
$this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/NovaPostTest/create_post_response.json');
72+
$this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/NovaPostTest/update_post_request.json');
4773
}
4874

4975
public function testMakeOnly()
5076
{
51-
$this->mockConfigurations();
52-
$this->mockFilesystem();
77+
$this->mockFilesystemPostModelExists();
5378

54-
$this->artisan('make:entity Post --methods=CRUD --only-repository')
79+
$this
80+
->artisan('make:entity Post --methods=CRUD --only-repository')
5581
->assertSuccessful();
82+
83+
$this->assertGeneratedFileEquals('repository.php', 'app/Repositories/PostRepository.php');
84+
$this->assertFileDoesNotExist('database/migrations/2016_10_20_110500_posts_create_table.php');
85+
$this->assertFileDoesNotExist('database/factories/PostFactory.php');
86+
$this->assertFileDoesNotExist('database/seeders/PostSeeder.php');
87+
$this->assertFileDoesNotExist('app/Models/Post.php');
88+
$this->assertFileDoesNotExist('app/Services/PostService.php');
89+
$this->assertFileDoesNotExist('app/Http/Requests/Post/CreatePostRequest.php');
90+
$this->assertFileDoesNotExist('app/Http/Requests/Post/GetPostRequest.php');
91+
$this->assertFileDoesNotExist('app/Http/Requests/Post/SearchPostsRequest.php');
92+
$this->assertFileDoesNotExist('app/Http/Requests/Post/UpdatePostRequest.php');
93+
$this->assertFileDoesNotExist('app/Http/Requests/Post/DeletePostRequest.php');
94+
$this->assertFileDoesNotExist('app/Http/Controllers/PostController.php');
95+
$this->assertFileDoesNotExist('app/Http/Resources/Post/PostResource.php');
96+
$this->assertFileDoesNotExist('app/Http/Resources/Post/PostsCollectionResource.php');
97+
$this->assertFileDoesNotExist('routes/api.php');
98+
$this->assertFileDoesNotExist('tests/PostTest.php');
99+
$this->assertFileDoesNotExist('tests/fixtures/PostTest/dump.sql');
100+
$this->assertFileDoesNotExist('tests/fixtures/PostTest/create_post_request.json');
101+
$this->assertFileDoesNotExist('tests/fixtures/PostTest/create_post_response.json');
102+
$this->assertFileDoesNotExist('tests/fixtures/PostTest/update_post_request.json');
103+
$this->assertFileDoesNotExist('lang/en/validation.php');
104+
$this->assertFileDoesNotExist('app/Nova/PostResource.php');
105+
$this->assertFileDoesNotExist('tests/NovaPostTest.php');
106+
$this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/nova_post_dump.sql');
107+
$this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/create_post_request.json');
108+
$this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/create_post_response.json');
109+
$this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/update_post_request.json');
56110
}
57111
}

tests/Support/Command/CommandMockTrait.php

Lines changed: 147 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,171 @@
22

33
namespace RonasIT\Support\Tests\Support\Command;
44

5+
use Illuminate\Database\Connection;
6+
use Illuminate\Support\Facades\DB;
57
use org\bovigo\vfs\vfsStream;
6-
use RonasIT\Support\Tests\Support\Shared\GeneratorMockTrait;
8+
use RonasIT\Support\Generators\NovaResourceGenerator;
9+
use RonasIT\Support\Generators\NovaTestGenerator;
10+
use RonasIT\Support\Generators\TestsGenerator;
11+
use RonasIT\Support\Tests\Support\FileSystemMock;
12+
use RonasIT\Support\Tests\Support\GeneratorMockTrait;
13+
use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\SchemaManager;
14+
use Mockery;
15+
use RonasIT\Support\Tests\Support\Test\Post;
716

817
trait CommandMockTrait
918
{
1019
use GeneratorMockTrait;
1120

12-
public function mockConfigurations(): void
13-
{
14-
config([
15-
'entity-generator.stubs.service' => 'entity-generator::service',
16-
'entity-generator.stubs.service_with_trait' => 'entity-generator::service_with_trait',
17-
'entity-generator.paths' => [
18-
'repositories' => 'app/Repositories',
19-
'services' => 'app/Services',
20-
'models' => 'app/Models',
21-
'translations' => 'lang/en/validation.php'
22-
]
23-
]);
24-
}
25-
26-
public function mockFilesystem(): void
21+
public function mockFilesystemPostModelExists(): void
2722
{
2823
$structure = [
2924
'app' => [
25+
'Http' => [
26+
'Controllers' => [],
27+
],
3028
'Models' => [
3129
'Post.php' => '<?php'
3230
],
3331
'Repositories' => []
3432
],
3533
'config' => [
3634
'entity-generator.php' => ''
37-
]
35+
],
36+
];
37+
38+
vfsStream::create($structure);
39+
}
40+
41+
public function mockFilesystem(): void
42+
{
43+
$structure = [
44+
'database' => [
45+
'migrations' => [],
46+
'factories' => [],
47+
'seeders' => [],
48+
],
49+
'lang' => [
50+
'en' => [],
51+
],
52+
'app' => [
53+
'Http' => [
54+
'Controllers' => [],
55+
'Resources' => [],
56+
'Requests' => [],
57+
],
58+
'Nova' => [],
59+
'Models' => [],
60+
'Repositories' => [],
61+
'Services' => [],
62+
],
63+
'tests' => [
64+
'fixtures' => [
65+
'PostTest' => [],
66+
],
67+
],
68+
'routes' => [
69+
'api.php' => $this->mockPhpFileContent(),
70+
],
71+
'config' => [
72+
'entity-generator.php' => '',
73+
],
3874
];
3975

4076
vfsStream::create($structure);
4177
}
78+
79+
public function mockGenerator(): void
80+
{
81+
$this->mockClass(TestsGenerator::class, [
82+
$this->functionCall(
83+
name: 'getModelClass',
84+
arguments: ['Post'],
85+
result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post',
86+
),
87+
$this->functionCall(
88+
name: 'getModelClass',
89+
arguments: ['Post'],
90+
result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post',
91+
),
92+
$this->functionCall(
93+
name: 'getModelClass',
94+
arguments: ['Post'],
95+
result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post',
96+
),
97+
$this->functionCall(
98+
name: 'getModelClass',
99+
arguments: ['Post'],
100+
result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post',
101+
),
102+
]);
103+
104+
$this->mockClass(NovaResourceGenerator::class, [
105+
$this->functionCall(
106+
name: 'getModelClass',
107+
arguments: ['Post'],
108+
result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post',
109+
),
110+
]);
111+
112+
$this->mockClass(NovaTestGenerator::class, [
113+
$this->functionCall(
114+
name: 'getModelClass',
115+
arguments: ['Post'],
116+
result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post',
117+
),
118+
$this->functionCall(
119+
name: 'getModelClass',
120+
arguments: ['Post'],
121+
result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post',
122+
),
123+
$this->functionCall(
124+
name: 'getModelClass',
125+
arguments: ['Post'],
126+
result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post',
127+
),
128+
$this->functionCall(
129+
name: 'getModelClass',
130+
arguments: ['Post'],
131+
result: 'RonasIT\\Support\\Tests\\Support\\Test\\Post',
132+
),
133+
$this->functionCall(
134+
name: 'loadNovaActions',
135+
result: [],
136+
),
137+
$this->functionCall(
138+
name: 'loadNovaFields',
139+
result: [],
140+
),
141+
$this->functionCall(
142+
name: 'loadNovaFilters',
143+
result: [],
144+
),
145+
]);
146+
147+
$this->mockNativeGeneratorFunctions(
148+
$this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Test\Post', true]),
149+
$this->nativeClassExistsMethodCall(['Laravel\Nova\NovaServiceProvider', true]),
150+
$this->nativeClassExistsMethodCall(['Laravel\Nova\NovaServiceProvider', true]),
151+
$this->nativeClassExistsMethodCall(['RonasIT\Support\Tests\Support\Test\Post', true]),
152+
);
153+
}
154+
155+
public function mockGettingModelInstance(): void
156+
{
157+
$connectionMock = Mockery::mock(Connection::class)->makePartial();
158+
$connectionMock
159+
->expects('getDoctrineSchemaManager')
160+
->andReturn(new SchemaManager);
161+
162+
$mock = Mockery::mock('alias:' . DB::class);
163+
$mock
164+
->expects('connection')
165+
->with('pgsql')
166+
->andReturn($connectionMock);
167+
168+
$mock->shouldReceive('beginTransaction', 'rollBack');
169+
170+
$this->app->instance('App\\Models\\Post', new Post());
171+
}
42172
}

tests/Support/GeneratorMockTrait.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace RonasIT\Support\Tests\Support;
44

55
use Laravel\Nova\NovaServiceProvider;
6+
use RonasIT\Support\Traits\MockTrait;
67

78
trait GeneratorMockTrait
89
{
10+
use MockTrait;
11+
912
public function mockNativeGeneratorFunctions(...$functionCalls): void
1013
{
1114
$this->mockNativeFunction('\RonasIT\Support\Generators', $functionCalls);

tests/Support/Test/Post.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class Post extends Model
2121
'updated_at'
2222
];
2323

24+
public function getConnectionName(): string
25+
{
26+
return 'pgsql';
27+
}
28+
2429
public function user()
2530
{
2631
return $this->belongsTo(User::class);

0 commit comments

Comments
 (0)