Skip to content

Commit b61770d

Browse files
authored
Fix wrong model imports in tests (#256)
1 parent 588eaa3 commit b61770d

File tree

4 files changed

+166
-2
lines changed

4 files changed

+166
-2
lines changed

src/Generators/TestGenerator.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,12 @@ protected function buildTestCases(Controller $controller)
357357
} elseif ($statement instanceof EloquentStatement) {
358358
$this->addRefreshDatabaseTrait($controller);
359359

360+
$modelNamespace = config('blueprint.models_namespace')
361+
? config('blueprint.namespace') . '\\' . config('blueprint.models_namespace')
362+
: config('blueprint.namespace');
363+
360364
$model = $this->determineModel($controller->prefix(), $statement->reference());
361-
$this->addImport($controller, config('blueprint.namespace') . '\\' . $model);
365+
$this->addImport($controller, $modelNamespace . '\\' . $model);
362366

363367
if ($statement->operation() === 'save') {
364368
$tested_bits |= self::TESTS_SAVE;
@@ -390,7 +394,11 @@ protected function buildTestCases(Controller $controller)
390394

391395
$setup['data'][] = sprintf('$%s = factory(%s::class, 3)->create();', Str::plural($variable), $model);
392396

393-
$this->addImport($controller, config('blueprint.namespace') . '\\' . $this->determineModel($controller->prefix(), $statement->model()));
397+
$modelNamespace = config('blueprint.models_namespace')
398+
? config('blueprint.namespace') . '\\' . config('blueprint.models_namespace')
399+
: config('blueprint.namespace');
400+
401+
$this->addImport($controller, $modelNamespace . '\\' . $this->determineModel($controller->prefix(), $statement->model()));
394402
}
395403
}
396404

tests/Feature/Generator/TestGeneratorTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,39 @@ public function output_generates_test_for_controller_tree_using_cached_model()
139139
$this->assertEquals(['created' => ['tests/Feature/Http/Controllers/UserControllerTest.php']], $this->subject->output($tree));
140140
}
141141

142+
/**
143+
* @test
144+
*/
145+
public function output_generates_tests_with_models_with_custom_namespace_correctly()
146+
{
147+
$definition = 'drafts/models-with-custom-namespace.yaml';
148+
$path = 'tests/Feature/Http/Controllers/CategoryControllerTest.php';
149+
$test = 'tests/models-with-custom-namespace.php';
150+
151+
$this->app['config']->set('blueprint.models_namespace', 'Models');
152+
153+
$this->files->expects('get')
154+
->with('stubs/test/class.stub')
155+
->andReturn(file_get_contents('stubs/test/class.stub'));
156+
157+
$this->files->expects('get')
158+
->with('stubs/test/case.stub')
159+
->andReturn(file_get_contents('stubs/test/case.stub'));
160+
$dirname = dirname($path);
161+
$this->files->expects('exists')
162+
->with($dirname)
163+
->andReturnFalse();
164+
$this->files->expects('makeDirectory')
165+
->with($dirname, 0755, true);
166+
$this->files->expects('put')
167+
->with($path, $this->fixture($test));
168+
169+
$tokens = $this->blueprint->parse($this->fixture($definition));
170+
$tree = $this->blueprint->analyze($tokens);
171+
172+
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
173+
}
174+
142175
public function controllerTreeDataProvider()
143176
{
144177
return [
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
models:
2+
Category:
3+
name: string:30
4+
image: string
5+
parent_id: id:Category nullable
6+
active: boolean default:true
7+
relationships:
8+
hasMany: Category
9+
softDeletes
10+
11+
controllers:
12+
Category:
13+
resource: api
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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()
22+
{
23+
$categories = factory(Category::class, 3)->create();
24+
25+
$response = $this->get(route('category.index'));
26+
}
27+
28+
29+
/**
30+
* @test
31+
*/
32+
public function store_uses_form_request_validation()
33+
{
34+
$this->assertActionUsesFormRequest(
35+
\App\Http\Controllers\CategoryController::class,
36+
'store',
37+
\App\Http\Requests\CategoryStoreRequest::class
38+
);
39+
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function store_saves()
45+
{
46+
$category = $this->faker->word;
47+
48+
$response = $this->post(route('category.store'), [
49+
'category' => $category,
50+
]);
51+
52+
$categories = Category::query()
53+
->where('category', $category)
54+
->get();
55+
$this->assertCount(1, $categories);
56+
$category = $categories->first();
57+
}
58+
59+
60+
/**
61+
* @test
62+
*/
63+
public function show_behaves_as_expected()
64+
{
65+
$category = factory(Category::class)->create();
66+
67+
$response = $this->get(route('category.show', $category));
68+
}
69+
70+
71+
/**
72+
* @test
73+
*/
74+
public function update_uses_form_request_validation()
75+
{
76+
$this->assertActionUsesFormRequest(
77+
\App\Http\Controllers\CategoryController::class,
78+
'update',
79+
\App\Http\Requests\CategoryUpdateRequest::class
80+
);
81+
}
82+
83+
/**
84+
* @test
85+
*/
86+
public function update_behaves_as_expected()
87+
{
88+
$category = factory(Category::class)->create();
89+
$category = $this->faker->word;
90+
91+
$response = $this->put(route('category.update', $category), [
92+
'category' => $category,
93+
]);
94+
}
95+
96+
97+
/**
98+
* @test
99+
*/
100+
public function destroy_deletes_and_responds_with()
101+
{
102+
$category = factory(Category::class)->create();
103+
104+
$response = $this->delete(route('category.destroy', $category));
105+
106+
$response->assertOk();
107+
108+
$this->assertDeleted($category);
109+
}
110+
}

0 commit comments

Comments
 (0)