Skip to content

Commit 4bbe059

Browse files
authored
Merge pull request #74 from RonasIT/49-add-requests-generator-tests
Add requests generator tests
2 parents 7230314 + 7622502 commit 4bbe059

File tree

7 files changed

+186
-0
lines changed

7 files changed

+186
-0
lines changed

stubs/request.blade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public function rules(): array
2222
return [];
2323
@endif
2424
}
25+
@endif
26+
@if($method !== $requestsGenerator::DELETE_METHOD && $needToValidate)
27+
2528
@endif
2629
@if($needToValidate)
2730
public function validateResolved(): void

tests/RequestGeneratorTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests;
4+
5+
use RonasIT\Support\Events\SuccessCreateMessage;
6+
use RonasIT\Support\Events\WarningEvent;
7+
use RonasIT\Support\Generators\RequestsGenerator;
8+
9+
class RequestGeneratorTest extends TestCase
10+
{
11+
public function testCreateRequests()
12+
{
13+
app(RequestsGenerator::class)
14+
->setModel('Post')
15+
->setRelations([
16+
'belongsTo' => ['User'],
17+
'hasMany' => ['Comments'],
18+
'hasOne' => [],
19+
'belongsToMany' => []
20+
])
21+
->setFields([
22+
'boolean-required' => ['is_published'],
23+
'integer' => ['user_id'],
24+
'boolean' => ['is_draft'],
25+
])
26+
->setCrudOptions(['C', 'R', 'U', 'D'])
27+
->generate();
28+
29+
$this->assertGeneratedFileEquals('get_request.php', 'app/Http/Requests/Post/GetPostRequest.php');
30+
$this->assertGeneratedFileEquals('search_request.php', 'app/Http/Requests/Post/SearchPostsRequest.php');
31+
$this->assertGeneratedFileEquals('delete_request.php', 'app/Http/Requests/Post/DeletePostRequest.php');
32+
$this->assertGeneratedFileEquals('update_request.php', 'app/Http/Requests/Post/UpdatePostRequest.php');
33+
$this->assertGeneratedFileEquals('create_request.php', 'app/Http/Requests/Post/CreatePostRequest.php');
34+
35+
$this->assertEventPushedChain([
36+
SuccessCreateMessage::class => [
37+
'Created a new Request: GetPostRequest',
38+
'Created a new Request: SearchPostsRequest',
39+
'Created a new Request: DeletePostRequest',
40+
'Created a new Request: CreatePostRequest',
41+
'Created a new Request: UpdatePostRequest',
42+
],
43+
]);
44+
}
45+
46+
public function testCreateRequestStubNotExist()
47+
{
48+
config(['entity-generator.stubs.request' => 'incorrect_stub']);
49+
50+
app(RequestsGenerator::class)
51+
->setModel('Post')
52+
->setCrudOptions(['C'])
53+
->generate();
54+
55+
$this->assertFileDoesNotExist('app/Http/Requests/Post/CreatePostRequest.php');
56+
57+
$this->assertEventPushed(
58+
className: WarningEvent::class,
59+
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.',
60+
);
61+
}
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Post;
4+
5+
use App\Http\Requests\Request;
6+
7+
class CreatePostRequest extends Request
8+
{
9+
public function rules(): array
10+
{
11+
return [
12+
'user_id' => 'integer|exists:users,id|required',
13+
'is_draft' => 'boolean',
14+
'is_published' => 'boolean|present',
15+
];
16+
}
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Post;
4+
5+
use App\Http\Requests\Request;
6+
use App\Services\PostService;
7+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8+
9+
class DeletePostRequest extends Request
10+
{
11+
public function validateResolved(): void
12+
{
13+
parent::validateResolved();
14+
15+
$service = app(PostService::class);
16+
17+
if (!$service->exists($this->route('id'))) {
18+
throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post']));
19+
}
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Post;
4+
5+
use App\Http\Requests\Request;
6+
use App\Services\PostService;
7+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8+
9+
class GetPostRequest extends Request
10+
{
11+
public function rules(): array
12+
{
13+
return [
14+
'with' => 'array',
15+
'with.*' => 'string|required',
16+
];
17+
}
18+
19+
public function validateResolved(): void
20+
{
21+
parent::validateResolved();
22+
23+
$service = app(PostService::class);
24+
25+
if (!$service->exists($this->route('id'))) {
26+
throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post']));
27+
}
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Post;
4+
5+
use App\Http\Requests\Request;
6+
7+
class SearchPostsRequest extends Request
8+
{
9+
public function rules(): array
10+
{
11+
return [
12+
'user_id' => 'integer|exists:users,id|required',
13+
'page' => 'integer',
14+
'per_page' => 'integer',
15+
'is_published' => 'boolean',
16+
'desc' => 'boolean',
17+
'all' => 'boolean',
18+
'with' => 'array',
19+
'order_by' => 'string',
20+
'query' => 'string|nullable',
21+
'with.*' => 'string',
22+
];
23+
}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Post;
4+
5+
use App\Http\Requests\Request;
6+
use App\Services\PostService;
7+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8+
9+
class UpdatePostRequest extends Request
10+
{
11+
public function rules(): array
12+
{
13+
return [
14+
'user_id' => 'integer|exists:users,id|required',
15+
'is_draft' => 'boolean',
16+
'is_published' => 'boolean',
17+
];
18+
}
19+
20+
public function validateResolved(): void
21+
{
22+
parent::validateResolved();
23+
24+
$service = app(PostService::class);
25+
26+
if (!$service->exists($this->route('id'))) {
27+
throw new NotFoundHttpException(__('validation.exceptions.not_found', ['entity' => 'Post']));
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)