Skip to content

Commit cb7bbb0

Browse files
committed
Add factory and test into generator
1 parent 4b8ce73 commit cb7bbb0

File tree

5 files changed

+163
-1
lines changed

5 files changed

+163
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ This will create:
5959

6060
- **model** `app/Models/{ModuleName}.php`
6161

62+
- **factory** `database/factories/{ModuleName}Factory.php`
63+
64+
- **tests** `tests/Feature/{ModuleName}Test.php`
65+
6266
- **backend module** `app/Modules/{ModuleName}/`
6367
```
6468
{ModuleName}/

app/Console/Commands/MakeBackEndModule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ private function createController()
5858

5959
$this->info('Controller created successfully.');
6060
}
61-
6261
}
6362

6463

app/Console/Commands/MakeModuleCommand.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public function handle(Filesystem $files, MakeFrontEndModule $frontEndModule, Ma
7878

7979
$frontEndModule->create($this->module);
8080

81+
$this->createFactory();
82+
83+
$this->createTest();
84+
8185
}
8286

8387

@@ -112,6 +116,40 @@ protected function createMigration()
112116
}
113117
}
114118

119+
/**
120+
* Create a factory file for the module.
121+
*
122+
* @return void
123+
*/
124+
protected function createFactory()
125+
{
126+
$this->call("make:factory", [
127+
'name' => $this->module . 'Factory',
128+
'--model' => "Models\\{$this->module}"
129+
]);
130+
}
131+
132+
/**
133+
* Create a test file for the module.
134+
*
135+
* @return void
136+
* @throws FileNotFoundException
137+
*/
138+
protected function createTest()
139+
{
140+
$path = base_path('tests/Feature/' . $this->module . 'Test.php');
141+
142+
if ($this->alreadyExists($path)) {
143+
$this->error('Test file already exists!');
144+
} else {
145+
$stub = (new Filesystem)->get(base_path('stubs/test.stub'));
146+
147+
$this->createFileWithStub($stub, $path);
148+
149+
$this->info('Tests created successfully.');
150+
}
151+
}
152+
115153
/**
116154
* Determine if the class already exists.
117155
*

stubs/factory.stub

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
4+
5+
use Faker\Generator as Faker;
6+
use {{ namespacedModel }};
7+
8+
$factory->define({{ model }}::class, function (Faker $faker) {
9+
return [
10+
'name' => $faker->word,
11+
];
12+
});

stubs/test.stub

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\User;
7+
use App\Models\DummySingular;
8+
use Tests\TestCase;
9+
10+
class DummySingularTest extends TestCase
11+
{
12+
/** @var User */
13+
protected $user;
14+
15+
public function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->user = factory(User::class)->create();
20+
}
21+
22+
/** @test */
23+
public function create_dummyVariableSingular()
24+
{
25+
$this->actingAs($this->user)
26+
->postJson(route('dummyVariablePlural.store'), [
27+
'name' => 'Lorem'
28+
])
29+
->assertSuccessful()
30+
->assertJson(['type' => Controller::RESPONSE_TYPE_SUCCESS]);
31+
32+
$this->assertDatabaseHas('dummyVariablePlural', [
33+
'name' => 'Lorem',
34+
]);
35+
}
36+
37+
/** @test */
38+
public function update_dummyVariableSingular()
39+
{
40+
$dummyVariableSingular = factory(DummySingular::class)->create();
41+
42+
$this->actingAs($this->user)
43+
->putJson(route('dummyVariablePlural.update', $dummyVariableSingular->id), [
44+
'name' => 'Updated dummyVariableSingular',
45+
])
46+
->assertSuccessful()
47+
->assertJson(['type' => Controller::RESPONSE_TYPE_SUCCESS]);
48+
49+
$this->assertDatabaseHas('dummyVariablePlural', [
50+
'id' => $dummyVariableSingular->id,
51+
'name' => 'Updated dummyVariableSingular',
52+
]);
53+
}
54+
55+
/** @test */
56+
public function show_dummyVariableSingular()
57+
{
58+
$dummyVariableSingular = factory(DummySingular::class)->create();
59+
60+
$this->actingAs($this->user)
61+
->getJson(route('dummyVariablePlural.show', $dummyVariableSingular->id))
62+
->assertSuccessful()
63+
->assertJson([
64+
'data' => [
65+
'name' => $dummyVariableSingular->name,
66+
]
67+
]);
68+
}
69+
70+
/** @test */
71+
public function list_dummyVariableSingular()
72+
{
73+
$dummyVariablePlural = factory(DummySingular::class, 2)->create()->map(function ($dummyVariableSingular) {
74+
return $dummyVariableSingular->only(['id', 'name']);
75+
});
76+
77+
$this->actingAs($this->user)
78+
->getJson(route('dummyVariablePlural.index'))
79+
->assertSuccessful()
80+
->assertJson([
81+
'data' => $dummyVariablePlural->toArray()
82+
])
83+
->assertJsonStructure([
84+
'data' => [
85+
'*' => ['id', 'name']
86+
],
87+
'links',
88+
'meta'
89+
]);
90+
}
91+
92+
/** @test */
93+
public function delete_dummyVariableSingular()
94+
{
95+
$dummyVariableSingular = factory(DummySingular::class)->create([
96+
'name' => 'DummySingular for delete',
97+
]);
98+
99+
$this->actingAs($this->user)
100+
->deleteJson(route('dummyVariablePlural.update', $dummyVariableSingular->id))
101+
->assertSuccessful()
102+
->assertJson(['type' => Controller::RESPONSE_TYPE_SUCCESS]);
103+
104+
$this->assertDatabaseMissing('dummyVariablePlural', [
105+
'id' => $dummyVariableSingular->id,
106+
'name' => 'DummySingular for delete',
107+
]);
108+
}
109+
}

0 commit comments

Comments
 (0)