Skip to content

Commit 43591ef

Browse files
authored
Merge pull request #4 from RonasIT/dpankratov/version-update
Dpankratov/version update
2 parents 954427e + c67299b commit 43591ef

8 files changed

+47
-35
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public function generate()
2222
]);
2323
$now = Carbon::now()->format('Y_m_d_His');
2424

25-
$this->saveClass('migrations', "{$now}_create_{$entities}_table", $content);
25+
$this->saveClass('migrations', "{$now}_{$entities}_create_table", $content);
2626

27-
event(new SuccessCreateMessage("Created a new Migration: create_{$entities}_table"));
27+
event(new SuccessCreateMessage("Created a new Migration: {$entities}_create_table"));
2828
}
2929

3030
protected function isJson($typeName)

src/Generators/RequestsGenerator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ protected function getRules($name, $type, $required)
144144

145145
if ($required) {
146146
$rules[] = 'required';
147-
} else {
148-
$rules[] = 'nullable';
149147
}
150148

151149
return [

src/Generators/TestsGenerator.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ class TestsGenerator extends EntityGenerator
1414
protected $getFields = [];
1515
protected $withAuth = false;
1616

17-
const FIXTURE_TYPES = ['create', 'update'];
17+
const FIXTURE_TYPES = [
18+
'create' => ['request', 'response'],
19+
'update' => ['request'],
20+
];
1821

1922
const EMPTY_GUARDED_FIELD = '*';
2023
const UPDATED_AT = 'updated_at';
@@ -175,11 +178,14 @@ protected function generateExistedEntityFixture()
175178
$object = $this->getFixtureValuesList($this->model);
176179
$entity = Str::snake($this->model);
177180

178-
foreach (self::FIXTURE_TYPES as $type) {
179-
$this->generateFixture(
180-
"{$type}_{$entity}.json",
181-
$object
182-
);
181+
foreach (self::FIXTURE_TYPES as $type => $modifications) {
182+
foreach ($modifications as $modification) {
183+
$excepts = [];
184+
if ($modification === 'request') {
185+
$excepts = ['id'];
186+
}
187+
$this->generateFixture("{$type}_{$entity}_{$modification}.json", Arr::except($object, $excepts));
188+
}
183189
}
184190
}
185191

@@ -199,7 +205,8 @@ protected function generateTest()
199205
{
200206
$content = $this->getStub('test', [
201207
'entity' => $this->model,
202-
'entities' => $this->getTableName($this->model),
208+
'databaseTableName' => $this->getTableName($this->model),
209+
'entities' => $this->getTableName($this->model, '-'),
203210
'withAuth' => $this->withAuth
204211
]);
205212

stubs/controller.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ public function search(Search{{\Illuminate\Support\Str::plural($entity)}}Request
4646
{
4747
$result = $service->search($request->onlyValidated());
4848

49-
return response($result);
49+
return response()->json($result);
5050
}
5151
}

stubs/migration.blade.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
use Illuminate\Support\Facades\Schema;
2-
use Illuminate\Database\Schema\Blueprint;
31
use Illuminate\Database\Migrations\Migration;
4-
use Illuminate\Support\Facades\DB;
2+
use Illuminate\Database\Schema\Blueprint;
3+
use Illuminate\Support\Facades\Schema;
54
use RonasIT\Support\Traits\MigrationTrait;
65

7-
class Create{{$class}}Table extends Migration
6+
class {{$class}}CreateTable extends Migration
87
{
98
use MigrationTrait;
109

1110
public function up()
1211
{
12+
@if(!empty($relations['belongsToMany']) || !empty($relations['belongsTo']) || !empty($relations['hasOne']) || !empty($relations['hasMany']))
1313
$this->createTable();
14+
@else
15+
Schema::create('{{\Illuminate\Support\Str::plural(\Illuminate\Support\Str::snake($entity))}}', function (Blueprint $table) {
16+
$table->increments('id');
17+
@foreach ($table as $row )
18+
{!!$row!!}
19+
@endforeach
20+
$table->timestamps();
21+
});
22+
@endif
1423
@foreach($relations['belongsToMany'] as $relation)
1524

1625
$this->createBridgeTable('{{$entity}}', '{{$relation}}');
@@ -43,17 +52,19 @@ public function down()
4352
$this->dropBridgeTable('{{$entity}}', '{{$relation}}');
4453

4554
@endforeach
46-
Schema::drop('{{\Illuminate\Support\Str::plural(\Illuminate\Support\Str::snake($entity))}}');
55+
Schema::dropIfExists('{{\Illuminate\Support\Str::plural(\Illuminate\Support\Str::snake($entity))}}');
4756
}
57+
@if(!empty($relations['belongsToMany']) || !empty($relations['belongsTo']) || !empty($relations['hasOne']) || !empty($relations['hasMany']))
4858

4959
public function createTable()
5060
{
5161
Schema::create('{{\Illuminate\Support\Str::plural(\Illuminate\Support\Str::snake($entity))}}', function (Blueprint $table) {
5262
$table->increments('id');
53-
$table->timestamps();
5463
@foreach ($table as $row )
5564
{!!$row!!}
5665
@endforeach
66+
$table->timestamps();
5767
});
5868
}
69+
@endif
5970
}

stubs/repository.blade.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
namespace App\Repositories;
22

3+
use App\Models\{{$entity}};
4+
@if(!empty($fields['json']))
35
use Illuminate\Support\Arr;
6+
@endif
47
use RonasIT\Support\Repositories\BaseRepository;
5-
use App\Models\{{$entity}};
68
{{--
79
Laravel inserts two spaces between @property and type, so we are forced
810
to use hack here to preserve one space

stubs/request.blade.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ public function rules()
1515
@foreach($parameters as $parameter)
1616
'{{$parameter['name']}}' => '{{implode('|', $parameter['rules'])}}',
1717
@endforeach
18-
@if($method === 'Search')
1918
];
20-
@else
21-
];
22-
@endif
2319
@else
2420
return [];
2521
@endif

stubs/test.blade.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function setUp() : void
2323

2424
public function testCreate()
2525
{
26-
$data = $this->getJsonFixture('create_{{\Illuminate\Support\Str::snake($entity)}}.json');
26+
$data = $this->getJsonFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_request.json');
2727

2828
@if (!$withAuth)
2929
$response = $this->json('post', '/{{$entities}}', $data);
@@ -33,17 +33,15 @@ public function testCreate()
3333

3434
$response->assertStatus(Response::HTTP_OK);
3535

36-
$expect = Arr::except($data, ['id', 'updated_at', 'created_at']);
37-
$actual = Arr::except($response->json(), ['id', 'updated_at', 'created_at']);
36+
$this->assertEqualsFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_response.json', $response->json());
3837

39-
$this->assertEquals($expect, $actual);
40-
$this->assertDatabaseHas('{{$entities}}', $expect);
38+
$this->assertDatabaseHas('{{$databaseTableName}}', $this->getJsonFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_response.json'));
4139
}
4240

4341
@if ($withAuth)
4442
public function testCreateNoAuth()
4543
{
46-
$data = $this->getJsonFixture('create_{{\Illuminate\Support\Str::snake($entity)}}.json');
44+
$data = $this->getJsonFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_request.json');
4745

4846
$response = $this->json('post', '/{{$entities}}', $data);
4947

@@ -53,7 +51,7 @@ public function testCreateNoAuth()
5351
@endif
5452
public function testUpdate()
5553
{
56-
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}.json');
54+
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_request.json');
5755

5856
@if (!$withAuth)
5957
$response = $this->json('put', '/{{$entities}}/1', $data);
@@ -63,12 +61,12 @@ public function testUpdate()
6361

6462
$response->assertStatus(Response::HTTP_NO_CONTENT);
6563

66-
$this->assertDatabaseHas('{{$entities}}', $data);
64+
$this->assertDatabaseHas('{{$databaseTableName}}', $data);
6765
}
6866

6967
public function testUpdateNotExists()
7068
{
71-
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}.json');
69+
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_request.json');
7270

7371
@if (!$withAuth)
7472
$response = $this->json('put', '/{{$entities}}/0', $data);
@@ -82,7 +80,7 @@ public function testUpdateNotExists()
8280
@if ($withAuth)
8381
public function testUpdateNoAuth()
8482
{
85-
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}.json');
83+
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_request.json');
8684

8785
$response = $this->json('put', '/{{$entities}}/1', $data);
8886

@@ -100,7 +98,7 @@ public function testDelete()
10098

10199
$response->assertStatus(Response::HTTP_NO_CONTENT);
102100

103-
$this->assertDatabaseMissing('{{$entities}}', [
101+
$this->assertDatabaseMissing('{{$databaseTableName}}', [
104102
'id' => 1
105103
]);
106104
}
@@ -115,7 +113,7 @@ public function testDeleteNotExists()
115113

116114
$response->assertStatus(Response::HTTP_NOT_FOUND);
117115

118-
$this->assertDatabaseMissing('{{$entities}}', [
116+
$this->assertDatabaseMissing('{{$databaseTableName}}', [
119117
'id' => 0
120118
]);
121119
}

0 commit comments

Comments
 (0)