Skip to content

Commit f3684a5

Browse files
author
Nathan Esayeas
authored
Generate tests using Laravel 8 model factories (#367)
1 parent b2ada61 commit f3684a5

11 files changed

+1205
-7
lines changed

src/Generators/TestGenerator.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
use Blueprint\Models\Statements\SendStatement;
1818
use Blueprint\Models\Statements\SessionStatement;
1919
use Blueprint\Models\Statements\ValidateStatement;
20-
use Shift\Faker\Registry as FakerRegistry;
2120
use Blueprint\Tree;
21+
use Illuminate\Support\Facades\App;
2222
use Illuminate\Support\Str;
23+
use Shift\Faker\Registry as FakerRegistry;
2324

2425
class TestGenerator implements Generator
2526
{
@@ -120,7 +121,11 @@ protected function buildTestCases(Controller $controller)
120121
: config('blueprint.namespace');
121122

122123
if (in_array($name, ['edit', 'update', 'show', 'destroy'])) {
123-
$setup['data'][] = sprintf('$%s = factory(%s::class)->create();', $variable, $model);
124+
if ($this->isLaravel8OrHigher()) {
125+
$setup['data'][] = sprintf('$%s = %s::factory()->create();', $variable, $model);
126+
} else {
127+
$setup['data'][] = sprintf('$%s = factory(%s::class)->create();', $variable, $model);
128+
}
124129
}
125130

126131
foreach ($statements as $statement) {
@@ -429,10 +434,18 @@ protected function buildTestCases(Controller $controller)
429434
$assertions['generic'][] = '$this->assertDatabaseHas('.Str::camel(Str::plural($model)).', [ /* ... */ ]);';
430435
}
431436
} elseif ($statement->operation() === 'find') {
432-
$setup['data'][] = sprintf('$%s = factory(%s::class)->create();', $variable, $model);
437+
if ($this->isLaravel8OrHigher()) {
438+
$setup['data'][] = sprintf('$%s = %s::factory()->create();', $variable, $model);
439+
} else {
440+
$setup['data'][] = sprintf('$%s = factory(%s::class)->create();', $variable, $model);
441+
}
433442
} elseif ($statement->operation() === 'delete') {
434443
$tested_bits |= self::TESTS_DELETE;
435-
$setup['data'][] = sprintf('$%s = factory(%s::class)->create();', $variable, $model);
444+
if ($this->isLaravel8OrHigher()) {
445+
$setup['data'][] = sprintf('$%s = %s::factory()->create();', $variable, $model);
446+
} else {
447+
$setup['data'][] = sprintf('$%s = factory(%s::class)->create();', $variable, $model);
448+
}
436449
$assertions['generic'][] = sprintf('$this->assertDeleted($%s);', $variable);
437450
} elseif ($statement->operation() === 'update') {
438451
$assertions['sanity'][] = sprintf('$%s->refresh();', $variable);
@@ -445,8 +458,11 @@ protected function buildTestCases(Controller $controller)
445458
}
446459
} elseif ($statement instanceof QueryStatement) {
447460
$this->addRefreshDatabaseTrait($controller);
448-
449-
$setup['data'][] = sprintf('$%s = factory(%s::class, 3)->create();', Str::plural($variable), $model);
461+
if ($this->isLaravel8OrHigher()) {
462+
$setup['data'][] = sprintf('$%s = %s::factory()->times(3)->create();', Str::plural($variable), $model);
463+
} else {
464+
$setup['data'][] = sprintf('$%s = factory(%s::class, 3)->create();', Str::plural($variable), $model);
465+
}
450466

451467
$this->addImport($controller, $modelNamespace.'\\'.$this->determineModel($controller->prefix(), $statement->model()));
452468
}
@@ -674,10 +690,19 @@ private function generateReferenceFactory(Column $local_column, Controller $cont
674690
$reference = $local_column->attributes()[0];
675691
}
676692

677-
$faker = sprintf('$%s = factory(%s::class)->create();', Str::beforeLast($local_column->name(), '_id'), Str::studly($reference));
693+
if ($this->isLaravel8OrHigher()) {
694+
$faker = sprintf('$%s = %s::factory()->create();', Str::beforeLast($local_column->name(), '_id'), Str::studly($reference));
695+
} else {
696+
$faker = sprintf('$%s = factory(%s::class)->create();', Str::beforeLast($local_column->name(), '_id'), Str::studly($reference));
697+
}
678698

679699
$this->addImport($controller, $modelNamespace.'\\'.Str::studly($reference));
680700

681701
return [$faker, $variable_name];
682702
}
703+
704+
protected function isLaravel8OrHigher()
705+
{
706+
return version_compare(App::version(), '8.0.0', '>=');
707+
}
683708
}

tests/Feature/Generators/TestGeneratorTest.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function output_writes_nothing_for_empty_tree()
4949

5050
/**
5151
* @test
52+
* @environment-setup useLaravel7
5253
* @dataProvider controllerTreeDataProvider
5354
*/
5455
public function output_generates_test_for_controller_tree($definition, $path, $test)
@@ -75,8 +76,38 @@ public function output_generates_test_for_controller_tree($definition, $path, $t
7576
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
7677
}
7778

79+
/**
80+
* @test
81+
* @environment-setup useLaravel8
82+
* @dataProvider laravel8ControllerTreeDataProvider
83+
*/
84+
public function output_generates_test_for_controller_tree_l8($definition, $path, $test)
85+
{
86+
$this->files->expects('stub')
87+
->with('test.class.stub')
88+
->andReturn($this->stub('test.class.stub'));
89+
90+
$this->files->expects('stub')
91+
->with('test.case.stub')
92+
->andReturn($this->stub('test.case.stub'));
93+
$dirname = dirname($path);
94+
$this->files->expects('exists')
95+
->with($dirname)
96+
->andReturnFalse();
97+
$this->files->expects('makeDirectory')
98+
->with($dirname, 0755, true);
99+
$this->files->expects('put')
100+
->with($path, $this->fixture($test));
101+
102+
$tokens = $this->blueprint->parse($this->fixture($definition));
103+
$tree = $this->blueprint->analyze($tokens);
104+
105+
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
106+
}
107+
78108
/**
79109
* @test
110+
* @environment-setup useLaravel7
80111
*/
81112
public function output_works_for_pascal_case_definition()
82113
{
@@ -108,8 +139,43 @@ public function output_works_for_pascal_case_definition()
108139
$this->assertEquals(['created' => [$certificateControllerTest, $certificateTypeControllerTest]], $this->subject->output($tree));
109140
}
110141

142+
/**
143+
* @test
144+
* @environment-setup useLaravel8
145+
*/
146+
public function output_works_for_pascal_case_definition_l8()
147+
{
148+
$this->files->expects('stub')
149+
->with('test.class.stub')
150+
->andReturn($this->stub('test.class.stub'));
151+
152+
$this->files->expects('stub')
153+
->with('test.case.stub')
154+
->andReturn($this->stub('test.case.stub'));
155+
156+
$certificateControllerTest = 'tests/Feature/Http/Controllers/CertificateControllerTest.php';
157+
$certificateTypeControllerTest = 'tests/Feature/Http/Controllers/CertificateTypeControllerTest.php';
158+
159+
$this->files->expects('exists')
160+
->with(dirname($certificateControllerTest))
161+
->andReturnTrue();
162+
$this->files->expects('put')
163+
->with($certificateControllerTest, $this->fixture('tests/certificate-pascal-case-example-laravel8.php'));
164+
165+
$this->files->expects('exists')
166+
->with(dirname($certificateTypeControllerTest))
167+
->andReturnTrue();
168+
$this->files->expects('put')
169+
->with($certificateTypeControllerTest, $this->fixture('tests/certificate-type-pascal-case-example-laravel8.php'));
170+
171+
$tokens = $this->blueprint->parse($this->fixture('drafts/pascal-case.yaml'));
172+
$tree = $this->blueprint->analyze($tokens);
173+
$this->assertEquals(['created' => [$certificateControllerTest, $certificateTypeControllerTest]], $this->subject->output($tree));
174+
}
175+
111176
/**
112177
* @test
178+
* @environment-setup useLaravel7
113179
*/
114180
public function output_generates_test_for_controller_tree_using_cached_model()
115181
{
@@ -142,6 +208,40 @@ public function output_generates_test_for_controller_tree_using_cached_model()
142208

143209
/**
144210
* @test
211+
* @environment-setup useLaravel8
212+
*/
213+
public function output_generates_test_for_controller_tree_using_cached_model_l8()
214+
{
215+
$this->files->expects('stub')
216+
->with('test.class.stub')
217+
->andReturn($this->stub('test.class.stub'));
218+
219+
$this->files->expects('stub')
220+
->with('test.case.stub')
221+
->andReturn($this->stub('test.case.stub'));
222+
$this->files->expects('exists')
223+
->with('tests/Feature/Http/Controllers')
224+
->andReturnFalse();
225+
$this->files->expects('makeDirectory')
226+
->with('tests/Feature/Http/Controllers', 0755, true);
227+
$this->files->expects('put')
228+
->with('tests/Feature/Http/Controllers/UserControllerTest.php', $this->fixture('tests/reference-cache-laravel8.php'));
229+
230+
$tokens = $this->blueprint->parse($this->fixture('drafts/reference-cache.yaml'));
231+
$tokens['cache'] = [
232+
'User' => [
233+
'email' => 'string',
234+
'password' => 'string',
235+
]
236+
];
237+
$tree = $this->blueprint->analyze($tokens);
238+
239+
$this->assertEquals(['created' => ['tests/Feature/Http/Controllers/UserControllerTest.php']], $this->subject->output($tree));
240+
}
241+
242+
/**
243+
* @test
244+
* @environment-setup useLaravel7
145245
*/
146246
public function output_generates_tests_with_models_with_custom_namespace_correctly()
147247
{
@@ -173,6 +273,40 @@ public function output_generates_tests_with_models_with_custom_namespace_correct
173273
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
174274
}
175275

276+
/**
277+
* @test
278+
* @environment-setup useLaravel8
279+
*/
280+
public function output_generates_tests_with_models_with_custom_namespace_correctly_l8()
281+
{
282+
$definition = 'drafts/models-with-custom-namespace.yaml';
283+
$path = 'tests/Feature/Http/Controllers/CategoryControllerTest.php';
284+
$test = 'tests/models-with-custom-namespace-laravel8.php';
285+
286+
$this->app['config']->set('blueprint.models_namespace', 'Models');
287+
288+
$this->files->expects('stub')
289+
->with('test.class.stub')
290+
->andReturn($this->stub('test.class.stub'));
291+
292+
$this->files->expects('stub')
293+
->with('test.case.stub')
294+
->andReturn($this->stub('test.case.stub'));
295+
$dirname = dirname($path);
296+
$this->files->expects('exists')
297+
->with($dirname)
298+
->andReturnFalse();
299+
$this->files->expects('makeDirectory')
300+
->with($dirname, 0755, true);
301+
$this->files->expects('put')
302+
->with($path, $this->fixture($test));
303+
304+
$tokens = $this->blueprint->parse($this->fixture($definition));
305+
$tree = $this->blueprint->analyze($tokens);
306+
307+
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
308+
}
309+
176310
public function controllerTreeDataProvider()
177311
{
178312
return [
@@ -184,4 +318,16 @@ public function controllerTreeDataProvider()
184318
['drafts/model-reference-validate.yaml', 'tests/Feature/Http/Controllers/CertificateControllerTest.php', 'tests/api-shorthand-validation.php'],
185319
];
186320
}
321+
322+
public function laravel8ControllerTreeDataProvider()
323+
{
324+
return [
325+
['drafts/readme-example.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example-laravel8.php'],
326+
['drafts/readme-example-notification-facade.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example-notification-laravel8.php'],
327+
['drafts/readme-example-notification-model.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example-notification-laravel8.php'],
328+
['drafts/respond-statements.yaml', 'tests/Feature/Http/Controllers/Api/PostControllerTest.php', 'tests/respond-statements-laravel8.php'],
329+
['drafts/full-crud-example.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/full-crud-example-laravel8.php'],
330+
['drafts/model-reference-validate.yaml', 'tests/Feature/Http/Controllers/CertificateControllerTest.php', 'tests/api-shorthand-validation-laravel8.php'],
331+
];
332+
}
187333
}

0 commit comments

Comments
 (0)