Skip to content

Commit ab7a2dc

Browse files
committed
Merge remote-tracking branch 'origin/master' into 49-add-migration-generator-tests
2 parents 82131a2 + 9e1b452 commit ab7a2dc

File tree

11 files changed

+67
-43
lines changed

11 files changed

+67
-43
lines changed

composer.lock

Lines changed: 17 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Generators/AbstractTestsGenerator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ protected function buildRelationsTree($models): array
238238
protected function canGenerateUserData(): bool
239239
{
240240
return $this->classExists('models', 'User')
241-
&& $this->isFactoryExists('User')
242241
&& $this->isMethodExists('User', 'getFields');
243242
}
244243

src/Generators/ControllerGenerator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public function generate(): void
1717
"Cannot create {$this->model}Controller cause {$this->model}Controller already exists.",
1818
"Remove {$this->model}Controller.",
1919
);
20-
2120
}
2221

2322
if (!$this->classExists('services', "{$this->model}Service")) {
@@ -45,7 +44,7 @@ protected function getControllerContent($model): string
4544
{
4645
return $this->getStub('controller', [
4746
'entity' => $model,
48-
'requestsFolder' => $this->getPluralName($model),
47+
'requestsFolder' => $model,
4948
'namespace' => $this->getOrCreateNamespace('controllers'),
5049
'requestsNamespace' => $this->getOrCreateNamespace('requests'),
5150
'resourcesNamespace' => $this->getOrCreateNamespace('resources'),

src/Generators/RequestsGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function generate(): void
6767

6868
protected function createRequest($method, $needToValidate = true, $parameters = []): void
6969
{
70-
$requestsFolder = $this->getPluralName($this->model);
70+
$requestsFolder = $this->model;
7171
$modelName = $this->getEntityName($method);
7272

7373
$content = $this->getStub('request', [

src/Generators/ResourceGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function generateCollectionResource(): void
3636
'namespace' => $this->getOrCreateNamespace('resources')
3737
]);
3838

39-
$this->saveClass('resources', "{$pluralName}CollectionResource", $collectionResourceContent);
39+
$this->saveClass('resources', "{$pluralName}CollectionResource", $collectionResourceContent, $this->model);
4040

4141
event(new SuccessCreateMessage("Created a new CollectionResource: {$pluralName}CollectionResource"));
4242
}
@@ -57,8 +57,8 @@ public function generateResource(): void
5757
'model_namespace' => $this->getOrCreateNamespace('models'),
5858
]);
5959

60-
$this->saveClass('resources', "{$this->model}Resource", $resourceContent);
60+
$this->saveClass('resources', "{$this->model}Resource", $resourceContent, $this->model);
6161

6262
event(new SuccessCreateMessage("Created a new Resource: {$this->model}Resource"));
6363
}
64-
}
64+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
namespace {{$namespace}};
1+
namespace {{$namespace}}\{{$singular_name}};
22

33
use Illuminate\Http\Resources\Json\ResourceCollection;
44

55
class {{$plural_name}}CollectionResource extends ResourceCollection
66
{
77
public $collects = {{$singular_name}}Resource::class;
8-
}
8+
}

stubs/controller.blade.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace {{$namespace}};
22

33
@inject('str', 'Illuminate\Support\Str')
4-
use {{$resourcesNamespace}}\{{$str::plural($entity)}}CollectionResource;
4+
use {{$resourcesNamespace}}\{{$entity}}\{{$str::plural($entity)}}CollectionResource;
55
@if (in_array('C', $options))
66
use {{$requestsNamespace}}\{{$requestsFolder}}\Create{{$entity}}Request;
77
@endif
@@ -15,12 +15,12 @@
1515
@if (in_array('U', $options))
1616
use {{$requestsNamespace}}\{{$requestsFolder}}\Update{{$entity}}Request;
1717
@endif
18-
use {{$resourcesNamespace}}\{{$entity}}Resource;
18+
use {{$resourcesNamespace}}\{{$entity}}\{{$entity}}Resource;
1919
use {{$servicesNamespace}}\{{$entity}}Service;
2020
@if (in_array('D', $options) || in_array('U', $options))
2121
use Symfony\Component\HttpFoundation\Response;
22-
2322
@endif
23+
2424
class {{$entity}}Controller extends Controller
2525
{
2626
@if (in_array('C', $options))
@@ -70,4 +70,4 @@ public function delete(Delete{{$entity}}Request $request, {{$entity}}Service $se
7070
return response('', Response::HTTP_NO_CONTENT);
7171
}
7272
@endif
73-
}
73+
}

stubs/model.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class {{$entity}} extends Model
88
{
9-
use ModelTrait, HasFactory;
9+
use HasFactory, ModelTrait;
1010

1111
protected $fillable = [
1212
@foreach($fields as $field)

stubs/resource.blade.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace {{$namespace}};
1+
namespace {{$namespace}}\{{$entity}};
22

33
use Illuminate\Http\Resources\Json\JsonResource;
44
use {{$model_namespace}}\{{$entity}};
@@ -8,9 +8,11 @@
88
*/
99
class {{$entity}}Resource extends JsonResource
1010
{
11+
public static $wrap = null;
12+
1113
//TODO implement custom serialization logic or remove method redefining
1214
public function toArray($request): array
1315
{
1416
return parent::toArray($request);
1517
}
16-
}
18+
}

stubs/test.blade.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class {{$entity}}Test extends TestCase
2121
protected static ModelTestState ${{\Illuminate\Support\Str::camel($entity)}}State;
2222

2323
@endif
24-
public function setUp() : void
24+
public function setUp(): void
2525
{
2626
parent::setUp();
2727
@if ($withAuth)
@@ -47,7 +47,8 @@ public function testCreate()
4747

4848
$response->assertCreated();
4949

50-
$this->assertEqualsFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_response.json', $response->json());
50+
// TODO: Need to remove last argument after first successful start
51+
$this->assertEqualsFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_response.json', $response->json(), true);
5152

5253
// TODO: Need to remove last argument after first successful start
5354
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertChangesEqualsFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_state.json', true);
@@ -93,6 +94,8 @@ public function testUpdateNotExists()
9394
@endif
9495

9596
$response->assertNotFound();
97+
98+
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertNotChanged();
9699
}
97100

98101
@if ($withAuth)
@@ -103,6 +106,8 @@ public function testUpdateNoAuth()
103106
$response = $this->json('put', '/{{$entities}}/1', $data);
104107

105108
$response->assertUnauthorized();
109+
110+
self::${{\Illuminate\Support\Str::camel($entity)}}State->assertNotChanged();
106111
}
107112

108113
@endif
@@ -172,6 +177,15 @@ public function testGetNotExists()
172177

173178
$response->assertNotFound();
174179
}
180+
@if ($withAuth)
181+
182+
public function testGetNoAuth()
183+
{
184+
$response = $this->json('get', '/{{$entities}}/1');
185+
186+
$response->assertUnauthorized();
187+
}
188+
@endif
175189

176190
public static function getSearchFilters(): array
177191
{
@@ -193,7 +207,7 @@ public static function getSearchFilters(): array
193207
#[DataProvider('getSearchFilters')]
194208
public function testSearch(array $filter, string $fixture)
195209
{
196-
$response = $this->json('get', '/{{$entities}}', $filter);
210+
$response = $this->actingAs(self::$user)->json('get', '/{{$entities}}', $filter);
197211

198212
$response->assertOk();
199213

@@ -202,5 +216,14 @@ public function testSearch(array $filter, string $fixture)
202216

203217
$this->assertEqualsFixture($fixture, $response->json());
204218
}
219+
220+
@if ($withAuth)
221+
public function testSearchNoAuth()
222+
{
223+
$response = $this->json('get', '/{{$entities}}');
224+
225+
$response->assertUnauthorized();
226+
}
227+
@endif
205228
@endif
206229
}

0 commit comments

Comments
 (0)