Skip to content
3 changes: 3 additions & 0 deletions stubs/request.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function rules(): array
return [];
@endif
}
@endif
@if($method !== $requestsGenerator::DELETE_METHOD && $needToValidate)

@endif
@if($needToValidate)
public function validateResolved(): void
Expand Down
62 changes: 62 additions & 0 deletions tests/RequestGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace RonasIT\Support\Tests;

use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Generators\RequestsGenerator;

class RequestGeneratorTest extends TestCase
{
public function testCreateRequests()
{
app(RequestsGenerator::class)
->setModel('Post')
->setRelations([
'belongsTo' => ['User'],
'hasMany' => ['Comments'],
'hasOne' => [],
'belongsToMany' => []
])
->setFields([
'boolean-required' => ['is_published'],
'integer' => ['user_id'],
'boolean' => ['is_draft'],
])
->setCrudOptions(['C', 'R', 'U', 'D'])
->generate();

$this->assertGeneratedFileEquals('get_request.php', 'app/Http/Requests/Post/GetPostRequest.php');
$this->assertGeneratedFileEquals('search_request.php', 'app/Http/Requests/Post/SearchPostsRequest.php');
$this->assertGeneratedFileEquals('delete_request.php', 'app/Http/Requests/Post/DeletePostRequest.php');
$this->assertGeneratedFileEquals('update_request.php', 'app/Http/Requests/Post/UpdatePostRequest.php');
$this->assertGeneratedFileEquals('create_request.php', 'app/Http/Requests/Post/CreatePostRequest.php');

$this->assertEventPushedChain([
SuccessCreateMessage::class => [
'Created a new Request: GetPostRequest',
'Created a new Request: SearchPostsRequest',
'Created a new Request: DeletePostRequest',
'Created a new Request: CreatePostRequest',
'Created a new Request: UpdatePostRequest',
],
]);
}

public function testCreateRequestStubNotExist()
{
config(['entity-generator.stubs.request' => 'incorrect_stub']);

app(RequestsGenerator::class)
->setModel('Post')
->setCrudOptions(['C'])
->generate();

$this->assertFileDoesNotExist('app/Http/Requests/Post/CreatePostRequest.php');

$this->assertEventPushed(
className: WarningEvent::class,
message: 'Generation of request has been skipped cause the view incorrect_stub from the config entity-generator.stubs.request is not exists. Please check that config has the correct view name value.',
);
}
}
17 changes: 17 additions & 0 deletions tests/fixtures/RequestGeneratorTest/create_request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Http\Requests\Post;

use App\Http\Requests\Request;

class CreatePostRequest extends Request
{
public function rules(): array
{
return [
'user_id' => 'integer|exists:users,id|required',
'is_draft' => 'boolean',
'is_published' => 'boolean|present',
];
}
}
21 changes: 21 additions & 0 deletions tests/fixtures/RequestGeneratorTest/delete_request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Requests\Post;

use App\Http\Requests\Request;
use App\Services\PostService;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class DeletePostRequest extends Request
{
public function validateResolved(): void
{
parent::validateResolved();

$service = app(PostService::class);

if (!$service->exists($this->route('id'))) {
throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post']));
}
}
}
29 changes: 29 additions & 0 deletions tests/fixtures/RequestGeneratorTest/get_request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Requests\Post;

use App\Http\Requests\Request;
use App\Services\PostService;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class GetPostRequest extends Request
{
public function rules(): array
{
return [
'with' => 'array',
'with.*' => 'string|required',
];
}

public function validateResolved(): void
{
parent::validateResolved();

$service = app(PostService::class);

if (!$service->exists($this->route('id'))) {
throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post']));
}
}
}
24 changes: 24 additions & 0 deletions tests/fixtures/RequestGeneratorTest/search_request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Requests\Post;

use App\Http\Requests\Request;

class SearchPostsRequest extends Request
{
public function rules(): array
{
return [
'user_id' => 'integer|exists:users,id|required',
'page' => 'integer',
'per_page' => 'integer',
'is_published' => 'boolean',
'desc' => 'boolean',
'all' => 'boolean',
'with' => 'array',
'order_by' => 'string',
'query' => 'string|nullable',
'with.*' => 'string',
];
}
}
30 changes: 30 additions & 0 deletions tests/fixtures/RequestGeneratorTest/update_request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests\Post;

use App\Http\Requests\Request;
use App\Services\PostService;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class UpdatePostRequest extends Request
{
public function rules(): array
{
return [
'user_id' => 'integer|exists:users,id|required',
'is_draft' => 'boolean',
'is_published' => 'boolean',
];
}

public function validateResolved(): void
{
parent::validateResolved();

$service = app(PostService::class);

if (!$service->exists($this->route('id'))) {
throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post']));
}
}
}
Loading