Skip to content

Commit 5622dab

Browse files
Use factory for referenced relationships in validation statements (#282)
1 parent 61280b6 commit 5622dab

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

src/Generators/TestGenerator.php

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Blueprint\Blueprint;
66
use Blueprint\Contracts\Generator;
7+
use Blueprint\Models\Column;
78
use Blueprint\Models\Controller;
89
use Blueprint\Models\Statements\DispatchStatement;
910
use Blueprint\Models\Statements\EloquentStatement;
@@ -226,23 +227,17 @@ protected function buildTestCases(Controller $controller)
226227

227228
if (! is_null($local_model) && $local_model->hasColumn($column)) {
228229
$local_column = $local_model->column($column);
229-
if (($local_column->dataType() === 'id' || $local_column->dataType() === 'uuid') && ($local_column->attributes() && Str::endsWith($local_column->name(), '_id'))) {
230-
$variable_name = Str::beforeLast($local_column->name(), '_id');
231-
$reference = $variable_name;
232230

233-
if ($local_column->attributes()) {
234-
$reference = $local_column->attributes()[0];
235-
$variable_name .= '->id';
236-
}
237-
$faker = sprintf('$%s = factory(%s::class)->create();', Str::beforeLast($local_column->name(), '_id'), Str::studly($reference));
231+
$factory = $this->generateReferenceFactory($local_column, $controller, $modelNamespace);
238232

239-
$this->addImport($controller, $modelNamespace.'\\'.Str::studly($reference));
233+
if ($factory) {
234+
[$faker, $variable_name] = $factory;
240235
} else {
241236
$faker = sprintf('$%s = $this->faker->%s;', $data, FactoryGenerator::fakerData($local_column->name()) ?? FactoryGenerator::fakerDataType($local_model->column($column)->dataType()));
242237
}
243238

244239
$setup['data'][] = $faker;
245-
$request_data[$data] = '$'.$variable_name;
240+
$request_data[$data] = '$' . $variable_name;
246241
} else {
247242
foreach ($local_model->columns() as $local_column) {
248243
if ($local_column->name() === 'id') {
@@ -253,10 +248,16 @@ protected function buildTestCases(Controller $controller)
253248
continue;
254249
}
255250

256-
// TODO: support "reference"
251+
$factory = $this->generateReferenceFactory($local_column, $controller, $modelNamespace);
252+
if ($factory) {
253+
[$faker, $variable_name] = $factory;
254+
} else {
255+
$faker = sprintf('$%s = $this->faker->%s;', $local_column->name(), FactoryGenerator::fakerData($local_column->name()) ?? FactoryGenerator::fakerDataType($local_column->dataType()));
256+
$variable_name = $local_column->name();
257+
}
257258

258-
$setup['data'][] = sprintf('$%s = $this->faker->%s;', $local_column->name(), FactoryGenerator::fakerData($local_column->name()) ?? FactoryGenerator::fakerDataType($local_column->dataType()));
259-
$request_data[$local_column->name()] = '$'.$local_column->name();
259+
$setup['data'][] = $faker;
260+
$request_data[$local_column->name()] = '$'.$variable_name;
260261
}
261262
}
262263
}
@@ -659,4 +660,24 @@ private function uniqueSetupLines(array $setup)
659660
})
660661
->toArray();
661662
}
663+
664+
private function generateReferenceFactory(Column $local_column, Controller $controller, string $modelNamespace)
665+
{
666+
if (!in_array($local_column->dataType(), ['id', 'uuid']) && !($local_column->attributes() && Str::endsWith($local_column->name(), '_id'))) {
667+
return null;
668+
}
669+
670+
$reference = Str::beforeLast($local_column->name(), '_id');
671+
$variable_name = $reference . '->id';
672+
673+
if ($local_column->attributes()) {
674+
$reference = $local_column->attributes()[0];
675+
}
676+
677+
$faker = sprintf('$%s = factory(%s::class)->create();', Str::beforeLast($local_column->name(), '_id'), Str::studly($reference));
678+
679+
$this->addImport($controller, $modelNamespace . '\\' . Str::studly($reference));
680+
681+
return [$faker, $variable_name];
682+
}
662683
}

tests/fixtures/drafts/model-reference-validate.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ models:
55
reference: string
66
document: string
77
expiry_date: date
8-
remarks: text nullable
8+
remarks: nullable text
9+
CertificateType:
10+
name: string
11+
relationships:
12+
hasMany: Certificate
913

1014
controllers:
1115
Certificate:

tests/fixtures/tests/api-shorthand-validation.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Feature\Http\Controllers;
44

55
use App\Certificate;
6+
use App\CertificateType;
67
use Illuminate\Foundation\Testing\RefreshDatabase;
78
use Illuminate\Foundation\Testing\WithFaker;
89
use JMac\Testing\Traits\AdditionalAssertions;
@@ -44,22 +45,22 @@ public function store_uses_form_request_validation()
4445
public function store_saves()
4546
{
4647
$name = $this->faker->name;
47-
$certificate_type_id = $this->faker->randomDigitNotNull;
48+
$certificate_type = factory(CertificateType::class)->create();
4849
$reference = $this->faker->word;
4950
$document = $this->faker->word;
5051
$expiry_date = $this->faker->date();
5152

5253
$response = $this->post(route('certificate.store'), [
5354
'name' => $name,
54-
'certificate_type_id' => $certificate_type_id,
55+
'certificate_type_id' => $certificate_type->id,
5556
'reference' => $reference,
5657
'document' => $document,
5758
'expiry_date' => $expiry_date,
5859
]);
5960

6061
$certificates = Certificate::query()
6162
->where('name', $name)
62-
->where('certificate_type_id', $certificate_type_id)
63+
->where('certificate_type_id', $certificate_type->id)
6364
->where('reference', $reference)
6465
->where('document', $document)
6566
->where('expiry_date', $expiry_date)
@@ -99,14 +100,14 @@ public function update_behaves_as_expected()
99100
{
100101
$certificate = factory(Certificate::class)->create();
101102
$name = $this->faker->name;
102-
$certificate_type_id = $this->faker->randomDigitNotNull;
103+
$certificate_type = factory(CertificateType::class)->create();
103104
$reference = $this->faker->word;
104105
$document = $this->faker->word;
105106
$expiry_date = $this->faker->date();
106107

107108
$response = $this->put(route('certificate.update', $certificate), [
108109
'name' => $name,
109-
'certificate_type_id' => $certificate_type_id,
110+
'certificate_type_id' => $certificate_type->id,
110111
'reference' => $reference,
111112
'document' => $document,
112113
'expiry_date' => $expiry_date,

tests/fixtures/tests/certificate-pascal-case-example.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Feature\Http\Controllers;
44

55
use App\Certificate;
6+
use App\CertificateType;
67
use Illuminate\Foundation\Testing\RefreshDatabase;
78
use Illuminate\Foundation\Testing\WithFaker;
89
use JMac\Testing\Traits\AdditionalAssertions;
@@ -44,22 +45,22 @@ public function store_uses_form_request_validation()
4445
public function store_saves()
4546
{
4647
$name = $this->faker->name;
47-
$certificate_type_id = $this->faker->randomDigitNotNull;
48+
$certificate_type = factory(CertificateType::class)->create();
4849
$reference = $this->faker->word;
4950
$document = $this->faker->word;
5051
$expiry_date = $this->faker->date();
5152

5253
$response = $this->post(route('certificate.store'), [
5354
'name' => $name,
54-
'certificate_type_id' => $certificate_type_id,
55+
'certificate_type_id' => $certificate_type->id,
5556
'reference' => $reference,
5657
'document' => $document,
5758
'expiry_date' => $expiry_date,
5859
]);
5960

6061
$certificates = Certificate::query()
6162
->where('name', $name)
62-
->where('certificate_type_id', $certificate_type_id)
63+
->where('certificate_type_id', $certificate_type->id)
6364
->where('reference', $reference)
6465
->where('document', $document)
6566
->where('expiry_date', $expiry_date)
@@ -99,14 +100,14 @@ public function update_behaves_as_expected()
99100
{
100101
$certificate = factory(Certificate::class)->create();
101102
$name = $this->faker->name;
102-
$certificate_type_id = $this->faker->randomDigitNotNull;
103+
$certificate_type = factory(CertificateType::class)->create();
103104
$reference = $this->faker->word;
104105
$document = $this->faker->word;
105106
$expiry_date = $this->faker->date();
106107

107108
$response = $this->put(route('certificate.update', $certificate), [
108109
'name' => $name,
109-
'certificate_type_id' => $certificate_type_id,
110+
'certificate_type_id' => $certificate_type->id,
110111
'reference' => $reference,
111112
'document' => $document,
112113
'expiry_date' => $expiry_date,

0 commit comments

Comments
 (0)