Skip to content

Commit aefd9d7

Browse files
author
Brian Faust
authored
Respect plural route config in tests (#615)
1 parent 1b20a79 commit aefd9d7

File tree

3 files changed

+183
-2
lines changed

3 files changed

+183
-2
lines changed

src/Generators/TestGenerator.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,10 @@ protected function buildTestCases(Controller $controller)
353353
} elseif ($statement instanceof RedirectStatement) {
354354
$tested_bits |= self::TESTS_REDIRECT;
355355

356-
$assertion = sprintf('$response->assertRedirect(route(\'%s\'', $statement->route());
356+
$assertion = sprintf(
357+
'$response->assertRedirect(route(\'%s\'',
358+
config('blueprint.plural_routes') ? Str::plural(Str::kebab($statement->route())) : Str::kebab($statement->route())
359+
);
357360

358361
if ($statement->data()) {
359362
$parameters = array_map(fn ($parameter) => '$' . $parameter, $statement->data());
@@ -455,7 +458,12 @@ protected function buildTestCases(Controller $controller)
455458
}
456459
}
457460

458-
$call = sprintf('$response = $this->%s(route(\'%s.%s\'', $this->httpMethodForAction($name), Str::kebab($context), $name);
461+
$call = sprintf(
462+
'$response = $this->%s(route(\'%s.%s\'',
463+
$this->httpMethodForAction($name),
464+
config('blueprint.plural_routes') ? Str::plural(Str::kebab($context)) : Str::kebab($context),
465+
$name,
466+
);
459467

460468
if (in_array($name, ['edit', 'update', 'show', 'destroy'])) {
461469
$call .= ', $' . Str::camel($context);

tests/Feature/Generators/TestGeneratorTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,41 @@ public function output_generates_tests_with_models_with_custom_namespace_correct
178178
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
179179
}
180180

181+
#[Test]
182+
public function output_generates_tests_with_pluralized_route_names(): void
183+
{
184+
$definition = 'drafts/models-with-custom-namespace.yaml';
185+
$path = 'tests/Feature/Http/Controllers/CategoryControllerTest.php';
186+
$test = 'tests/routes-with-pluralized-names.php';
187+
188+
$this->app['config']->set('blueprint.models_namespace', 'Models');
189+
$this->app['config']->set('blueprint.plural_routes', true);
190+
191+
$this->filesystem->expects('stub')
192+
->with('test.class.stub')
193+
->andReturn($this->stub('test.class.stub'));
194+
195+
$this->filesystem->expects('stub')
196+
->with('test.case.stub')
197+
->andReturn($this->stub('test.case.stub'));
198+
199+
$dirname = dirname($path);
200+
$this->filesystem->expects('exists')
201+
->with($dirname)
202+
->andReturnFalse();
203+
204+
$this->filesystem->expects('makeDirectory')
205+
->with($dirname, 0755, true);
206+
207+
$this->filesystem->expects('put')
208+
->with($path, $this->fixture($test));
209+
210+
$tokens = $this->blueprint->parse($this->fixture($definition));
211+
$tree = $this->blueprint->analyze($tokens);
212+
213+
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
214+
}
215+
181216
public static function controllerTreeDataProvider(): array
182217
{
183218
return [
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers;
4+
5+
use App\Models\Category;
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\CategoryController
13+
*/
14+
class CategoryControllerTest extends TestCase
15+
{
16+
use AdditionalAssertions, RefreshDatabase, WithFaker;
17+
18+
/**
19+
* @test
20+
*/
21+
public function index_behaves_as_expected(): void
22+
{
23+
$categories = Category::factory()->count(3)->create();
24+
25+
$response = $this->get(route('categories.index'));
26+
27+
$response->assertOk();
28+
$response->assertJsonStructure([]);
29+
}
30+
31+
32+
/**
33+
* @test
34+
*/
35+
public function store_uses_form_request_validation(): void
36+
{
37+
$this->assertActionUsesFormRequest(
38+
\App\Http\Controllers\CategoryController::class,
39+
'store',
40+
\App\Http\Requests\CategoryStoreRequest::class
41+
);
42+
}
43+
44+
/**
45+
* @test
46+
*/
47+
public function store_saves(): void
48+
{
49+
$name = $this->faker->name;
50+
$image = $this->faker->word;
51+
$active = $this->faker->boolean;
52+
53+
$response = $this->post(route('categories.store'), [
54+
'name' => $name,
55+
'image' => $image,
56+
'active' => $active,
57+
]);
58+
59+
$categories = Category::query()
60+
->where('name', $name)
61+
->where('image', $image)
62+
->where('active', $active)
63+
->get();
64+
$this->assertCount(1, $categories);
65+
$category = $categories->first();
66+
67+
$response->assertCreated();
68+
$response->assertJsonStructure([]);
69+
}
70+
71+
72+
/**
73+
* @test
74+
*/
75+
public function show_behaves_as_expected(): void
76+
{
77+
$category = Category::factory()->create();
78+
79+
$response = $this->get(route('categories.show', $category));
80+
81+
$response->assertOk();
82+
$response->assertJsonStructure([]);
83+
}
84+
85+
86+
/**
87+
* @test
88+
*/
89+
public function update_uses_form_request_validation(): void
90+
{
91+
$this->assertActionUsesFormRequest(
92+
\App\Http\Controllers\CategoryController::class,
93+
'update',
94+
\App\Http\Requests\CategoryUpdateRequest::class
95+
);
96+
}
97+
98+
/**
99+
* @test
100+
*/
101+
public function update_behaves_as_expected(): void
102+
{
103+
$category = Category::factory()->create();
104+
$name = $this->faker->name;
105+
$image = $this->faker->word;
106+
$active = $this->faker->boolean;
107+
108+
$response = $this->put(route('categories.update', $category), [
109+
'name' => $name,
110+
'image' => $image,
111+
'active' => $active,
112+
]);
113+
114+
$category->refresh();
115+
116+
$response->assertOk();
117+
$response->assertJsonStructure([]);
118+
119+
$this->assertEquals($name, $category->name);
120+
$this->assertEquals($image, $category->image);
121+
$this->assertEquals($active, $category->active);
122+
}
123+
124+
125+
/**
126+
* @test
127+
*/
128+
public function destroy_deletes_and_responds_with(): void
129+
{
130+
$category = Category::factory()->create();
131+
132+
$response = $this->delete(route('categories.destroy', $category));
133+
134+
$response->assertNoContent();
135+
136+
$this->assertSoftDeleted($category);
137+
}
138+
}

0 commit comments

Comments
 (0)