Skip to content

Commit affad14

Browse files
committed
tests: tests for api_scaffold with fieldsFile as input
1 parent fa8ca87 commit affad14

File tree

5 files changed

+145
-21
lines changed

5 files changed

+145
-21
lines changed

src/Facades/FileUtils.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ protected static function getFacadeAccessor()
1313
return FileSystem::class;
1414
}
1515

16-
public static function fake()
16+
public static function fake($allowedMethods = [])
1717
{
18-
$fake = Mockery::mock()->allows([
19-
'getFile' => '',
20-
'createFile' => true,
21-
'createDirectoryIfNotExist' => true,
22-
'deleteFile' => true,
23-
]);
18+
if (empty($allowedMethods)) {
19+
$allowedMethods = [
20+
'getFile' => '',
21+
'createFile' => true,
22+
'createDirectoryIfNotExist' => true,
23+
'deleteFile' => true,
24+
];
25+
}
2426

25-
static::swap($fake);
27+
static::swap($fake = Mockery::mock()->allows($allowedMethods));
2628

2729
return $fake;
2830
}

src/Generators/FactoryGenerator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct()
3232
$rel = $relation;
3333
$this->relations[$field] = [
3434
'relation' => $rel,
35-
'model_class' => $this->config->config->nsModel.'\\'.$relation,
35+
'model_class' => $this->config->namespaces->model.'\\'.$relation,
3636
];
3737
}
3838
}
@@ -83,7 +83,10 @@ private function generateFields(): array
8383

8484
//get model validation rules
8585
$class = $this->config->namespaces->model.'\\'.$this->config->modelNames->name;
86-
$rules = $class::$rules;
86+
$rules = [];
87+
if (class_exists($class)) {
88+
$rules = $class::$rules;
89+
}
8790
$relations = array_keys($this->relations);
8891

8992
foreach ($this->config->fields as $field) {

tests/Commands/APIScaffoldGeneratorCommandTest.php

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use InfyOm\Generator\Generators\API\APIRequestGenerator;
77
use InfyOm\Generator\Generators\API\APIRoutesGenerator;
88
use InfyOm\Generator\Generators\API\APITestGenerator;
9+
use InfyOm\Generator\Generators\FactoryGenerator;
910
use InfyOm\Generator\Generators\MigrationGenerator;
1011
use InfyOm\Generator\Generators\ModelGenerator;
1112
use InfyOm\Generator\Generators\RepositoryGenerator;
@@ -19,10 +20,6 @@
1920
use Mockery as m;
2021
use function Pest\Laravel\artisan;
2122

22-
beforeEach(function () {
23-
FileUtils::fake();
24-
});
25-
2623
afterEach(function () {
2724
m::close();
2825
});
@@ -67,7 +64,9 @@ function mockShouldNotHaveCalledAPIScaffoldGenerators(array $shouldNotHaveCalled
6764
return $mockedObjects;
6865
}
6966

70-
it('generates all files for api_scaffold', function () {
67+
it('generates all files for api_scaffold from console', function () {
68+
FileUtils::fake();
69+
7170
$shouldHaveCalledGenerators = [
7271
MigrationGenerator::class,
7372
ModelGenerator::class,
@@ -80,6 +79,7 @@ function mockShouldNotHaveCalledAPIScaffoldGenerators(array $shouldNotHaveCalled
8079
ViewGenerator::class,
8180
RoutesGenerator::class,
8281
MenuGenerator::class,
82+
SeederGenerator::class,
8383
];
8484

8585
mockShouldHaveCalledAPIScaffoldGenerator($shouldHaveCalledGenerators);
@@ -88,15 +88,64 @@ function mockShouldNotHaveCalledAPIScaffoldGenerators(array $shouldNotHaveCalled
8888
RepositoryTestGenerator::class,
8989
APITestGenerator::class,
9090
FactoryGenerator::class,
91-
SeederGenerator::class,
9291
];
9392

9493
mockShouldNotHaveCalledAPIScaffoldGenerators($shouldNotHaveCalledGenerator);
9594

95+
config()->set('laravel_generator.options.seeder', true);
96+
9697
artisan(APIScaffoldGeneratorCommand::class, ['model' => 'Post'])
9798
->expectsQuestion('Field: (name db_type html_type options)', 'title body text')
9899
->expectsQuestion('Enter validations: ', 'required')
99100
->expectsQuestion('Field: (name db_type html_type options)', 'exit')
100101
->expectsQuestion(PHP_EOL.'Do you want to migrate database? [y|N]', false)
101102
->assertSuccessful();
102103
});
104+
105+
it('generates all files for api_scaffold from fields file', function () {
106+
107+
$fileUtils = FileUtils::fake([
108+
'createFile' => true,
109+
'createDirectoryIfNotExist' => true,
110+
'deleteFile' => true,
111+
]);
112+
113+
$shouldHaveCalledGenerators = [
114+
MigrationGenerator::class,
115+
ModelGenerator::class,
116+
RepositoryGenerator::class,
117+
APIRequestGenerator::class,
118+
APIControllerGenerator::class,
119+
APIRoutesGenerator::class,
120+
RequestGenerator::class,
121+
ControllerGenerator::class,
122+
ViewGenerator::class,
123+
RoutesGenerator::class,
124+
MenuGenerator::class,
125+
RepositoryTestGenerator::class,
126+
APITestGenerator::class,
127+
FactoryGenerator::class,
128+
];
129+
130+
mockShouldHaveCalledAPIScaffoldGenerator($shouldHaveCalledGenerators);
131+
132+
$shouldNotHaveCalledGenerator = [
133+
SeederGenerator::class,
134+
];
135+
136+
mockShouldNotHaveCalledAPIScaffoldGenerators($shouldNotHaveCalledGenerator);
137+
138+
config()->set('laravel_generator.add_ons.tests', true);
139+
140+
$modelSchemaFile = __DIR__.'/../fixtures/model_schema/Post.json';
141+
142+
$fileUtils->shouldReceive('getFile')
143+
->withArgs([$modelSchemaFile])
144+
->andReturn(file_get_contents($modelSchemaFile));
145+
$fileUtils->shouldReceive('getFile')
146+
->andReturn('');
147+
148+
artisan(APIScaffoldGeneratorCommand::class, ['model' => 'Post', '--fieldsFile' => $modelSchemaFile])
149+
->expectsQuestion(PHP_EOL.'Do you want to migrate database? [y|N]', false)
150+
->assertSuccessful();
151+
});

tests/Pest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,3 @@
3838
| global functions to help you to reduce the number of lines of code in your test files.
3939
|
4040
*/
41-
42-
function orchestra_testbench_laravel()
43-
{
44-
return __DIR__.'/../vendor/orchestra/testbench-core/laravel/';
45-
}

tests/fixtures/model_schema/Post.json

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[
2+
{
3+
"name": "id",
4+
"dbType": "id",
5+
"htmlType": null,
6+
"validations": null,
7+
"searchable": false,
8+
"fillable": false,
9+
"primary": true,
10+
"inForm": false,
11+
"inIndex": false,
12+
"inView": false
13+
},
14+
{
15+
"name": "user_id",
16+
"dbType": "unsignedBigInteger:foreign,users,id",
17+
"htmlType": "text",
18+
"relation": "mt1,User,user_id,id",
19+
"validations": "required",
20+
"searchable": false,
21+
"fillable": true,
22+
"primary": false,
23+
"inForm": true,
24+
"inIndex": true,
25+
"inView": true
26+
},
27+
{
28+
"name": "title",
29+
"dbType": "string",
30+
"htmlType": "text",
31+
"validations": "required",
32+
"searchable": true,
33+
"fillable": true,
34+
"primary": false,
35+
"inForm": true,
36+
"inIndex": true,
37+
"inView": true
38+
},
39+
{
40+
"name": "body",
41+
"dbType": "text",
42+
"htmlType": "textarea",
43+
"validations": "",
44+
"searchable": true,
45+
"fillable": true,
46+
"primary": false,
47+
"inForm": true,
48+
"inIndex": true,
49+
"inView": true
50+
},
51+
{
52+
"name": "created_at",
53+
"dbType": "timestamp",
54+
"htmlType": null,
55+
"validations": null,
56+
"searchable": false,
57+
"fillable": false,
58+
"primary": false,
59+
"inForm": false,
60+
"inIndex": false,
61+
"inView": true
62+
},
63+
{
64+
"name": "updated_at",
65+
"dbType": "timestamp",
66+
"htmlType": null,
67+
"validations": null,
68+
"searchable": false,
69+
"fillable": false,
70+
"primary": false,
71+
"inForm": false,
72+
"inIndex": false,
73+
"inView": true
74+
}
75+
]

0 commit comments

Comments
 (0)