Skip to content

Commit 61280b6

Browse files
Fix test setup for validation shorthand (#280)
1 parent deafe71 commit 61280b6

File tree

6 files changed

+193
-19
lines changed

6 files changed

+193
-19
lines changed

src/Generators/TestGenerator.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,25 @@ protected function buildTestCases(Controller $controller)
240240
} else {
241241
$faker = sprintf('$%s = $this->faker->%s;', $data, FactoryGenerator::fakerData($local_column->name()) ?? FactoryGenerator::fakerDataType($local_model->column($column)->dataType()));
242242
}
243+
244+
$setup['data'][] = $faker;
245+
$request_data[$data] = '$'.$variable_name;
243246
} else {
244-
$faker = sprintf('$%s = $this->faker->word;', $data);
245-
}
247+
foreach ($local_model->columns() as $local_column) {
248+
if ($local_column->name() === 'id') {
249+
continue;
250+
}
251+
252+
if (in_array('nullable', $local_column->modifiers())) {
253+
continue;
254+
}
255+
256+
// TODO: support "reference"
246257

247-
$setup['data'][] = $faker;
248-
$request_data[$data] = '$'.$variable_name;
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();
260+
}
261+
}
249262
}
250263
}
251264
} elseif ($statement instanceof DispatchStatement) {

tests/Feature/Generator/TestGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ public function controllerTreeDataProvider()
180180
['drafts/readme-example-notification-model.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example-notification.php'],
181181
['drafts/respond-statements.yaml', 'tests/Feature/Http/Controllers/Api/PostControllerTest.php', 'tests/respond-statements.php'],
182182
['drafts/full-crud-example.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/full-crud-example.php'],
183+
['drafts/model-reference-validate.yaml', 'tests/Feature/Http/Controllers/CertificateControllerTest.php', 'tests/api-shorthand-validation.php'],
183184
];
184185
}
185186
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers;
4+
5+
use App\Certificate;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Foundation\Testing\WithFaker;
8+
use JMac\Testing\Traits\AdditionalAssertions;
9+
use Tests\TestCase;
10+
11+
/**
12+
* @see \App\Http\Controllers\CertificateController
13+
*/
14+
class CertificateControllerTest extends TestCase
15+
{
16+
use AdditionalAssertions, RefreshDatabase, WithFaker;
17+
18+
/**
19+
* @test
20+
*/
21+
public function index_behaves_as_expected()
22+
{
23+
$certificates = factory(Certificate::class, 3)->create();
24+
25+
$response = $this->get(route('certificate.index'));
26+
}
27+
28+
29+
/**
30+
* @test
31+
*/
32+
public function store_uses_form_request_validation()
33+
{
34+
$this->assertActionUsesFormRequest(
35+
\App\Http\Controllers\CertificateController::class,
36+
'store',
37+
\App\Http\Requests\CertificateStoreRequest::class
38+
);
39+
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function store_saves()
45+
{
46+
$name = $this->faker->name;
47+
$certificate_type_id = $this->faker->randomDigitNotNull;
48+
$reference = $this->faker->word;
49+
$document = $this->faker->word;
50+
$expiry_date = $this->faker->date();
51+
52+
$response = $this->post(route('certificate.store'), [
53+
'name' => $name,
54+
'certificate_type_id' => $certificate_type_id,
55+
'reference' => $reference,
56+
'document' => $document,
57+
'expiry_date' => $expiry_date,
58+
]);
59+
60+
$certificates = Certificate::query()
61+
->where('name', $name)
62+
->where('certificate_type_id', $certificate_type_id)
63+
->where('reference', $reference)
64+
->where('document', $document)
65+
->where('expiry_date', $expiry_date)
66+
->get();
67+
$this->assertCount(1, $certificates);
68+
$certificate = $certificates->first();
69+
}
70+
71+
72+
/**
73+
* @test
74+
*/
75+
public function show_behaves_as_expected()
76+
{
77+
$certificate = factory(Certificate::class)->create();
78+
79+
$response = $this->get(route('certificate.show', $certificate));
80+
}
81+
82+
83+
/**
84+
* @test
85+
*/
86+
public function update_uses_form_request_validation()
87+
{
88+
$this->assertActionUsesFormRequest(
89+
\App\Http\Controllers\CertificateController::class,
90+
'update',
91+
\App\Http\Requests\CertificateUpdateRequest::class
92+
);
93+
}
94+
95+
/**
96+
* @test
97+
*/
98+
public function update_behaves_as_expected()
99+
{
100+
$certificate = factory(Certificate::class)->create();
101+
$name = $this->faker->name;
102+
$certificate_type_id = $this->faker->randomDigitNotNull;
103+
$reference = $this->faker->word;
104+
$document = $this->faker->word;
105+
$expiry_date = $this->faker->date();
106+
107+
$response = $this->put(route('certificate.update', $certificate), [
108+
'name' => $name,
109+
'certificate_type_id' => $certificate_type_id,
110+
'reference' => $reference,
111+
'document' => $document,
112+
'expiry_date' => $expiry_date,
113+
]);
114+
}
115+
116+
117+
/**
118+
* @test
119+
*/
120+
public function destroy_deletes_and_responds_with()
121+
{
122+
$certificate = factory(Certificate::class)->create();
123+
124+
$response = $this->delete(route('certificate.destroy', $certificate));
125+
126+
$response->assertOk();
127+
128+
$this->assertDeleted($certificate);
129+
}
130+
}

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,26 @@ public function store_uses_form_request_validation()
4343
*/
4444
public function store_saves()
4545
{
46-
$certificate = $this->faker->word;
46+
$name = $this->faker->name;
47+
$certificate_type_id = $this->faker->randomDigitNotNull;
48+
$reference = $this->faker->word;
49+
$document = $this->faker->word;
50+
$expiry_date = $this->faker->date();
4751

4852
$response = $this->post(route('certificate.store'), [
49-
'certificate' => $certificate,
53+
'name' => $name,
54+
'certificate_type_id' => $certificate_type_id,
55+
'reference' => $reference,
56+
'document' => $document,
57+
'expiry_date' => $expiry_date,
5058
]);
5159

5260
$certificates = Certificate::query()
53-
->where('certificate', $certificate)
61+
->where('name', $name)
62+
->where('certificate_type_id', $certificate_type_id)
63+
->where('reference', $reference)
64+
->where('document', $document)
65+
->where('expiry_date', $expiry_date)
5466
->get();
5567
$this->assertCount(1, $certificates);
5668
$certificate = $certificates->first();
@@ -86,10 +98,18 @@ public function update_uses_form_request_validation()
8698
public function update_behaves_as_expected()
8799
{
88100
$certificate = factory(Certificate::class)->create();
89-
$certificate = $this->faker->word;
101+
$name = $this->faker->name;
102+
$certificate_type_id = $this->faker->randomDigitNotNull;
103+
$reference = $this->faker->word;
104+
$document = $this->faker->word;
105+
$expiry_date = $this->faker->date();
90106

91107
$response = $this->put(route('certificate.update', $certificate), [
92-
'certificate' => $certificate,
108+
'name' => $name,
109+
'certificate_type_id' => $certificate_type_id,
110+
'reference' => $reference,
111+
'document' => $document,
112+
'expiry_date' => $expiry_date,
93113
]);
94114
}
95115

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public function store_uses_form_request_validation()
4343
*/
4444
public function store_saves()
4545
{
46-
$certificateType = $this->faker->word;
46+
$name = $this->faker->name;
4747

4848
$response = $this->post(route('certificate-type.store'), [
49-
'certificateType' => $certificateType,
49+
'name' => $name,
5050
]);
5151

5252
$certificateTypes = CertificateType::query()
53-
->where('certificateType', $certificateType)
53+
->where('name', $name)
5454
->get();
5555
$this->assertCount(1, $certificateTypes);
5656
$certificateType = $certificateTypes->first();
@@ -86,10 +86,10 @@ public function update_uses_form_request_validation()
8686
public function update_behaves_as_expected()
8787
{
8888
$certificateType = factory(CertificateType::class)->create();
89-
$certificateType = $this->faker->word;
89+
$name = $this->faker->name;
9090

9191
$response = $this->put(route('certificate-type.update', $certificateType), [
92-
'certificateType' => $certificateType,
92+
'name' => $name,
9393
]);
9494
}
9595

tests/fixtures/tests/models-with-custom-namespace.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,20 @@ public function store_uses_form_request_validation()
4343
*/
4444
public function store_saves()
4545
{
46-
$category = $this->faker->word;
46+
$name = $this->faker->name;
47+
$image = $this->faker->word;
48+
$active = $this->faker->boolean;
4749

4850
$response = $this->post(route('category.store'), [
49-
'category' => $category,
51+
'name' => $name,
52+
'image' => $image,
53+
'active' => $active,
5054
]);
5155

5256
$categories = Category::query()
53-
->where('category', $category)
57+
->where('name', $name)
58+
->where('image', $image)
59+
->where('active', $active)
5460
->get();
5561
$this->assertCount(1, $categories);
5662
$category = $categories->first();
@@ -86,10 +92,14 @@ public function update_uses_form_request_validation()
8692
public function update_behaves_as_expected()
8793
{
8894
$category = factory(Category::class)->create();
89-
$category = $this->faker->word;
95+
$name = $this->faker->name;
96+
$image = $this->faker->word;
97+
$active = $this->faker->boolean;
9098

9199
$response = $this->put(route('category.update', $category), [
92-
'category' => $category,
100+
'name' => $name,
101+
'image' => $image,
102+
'active' => $active,
93103
]);
94104
}
95105

0 commit comments

Comments
 (0)