Skip to content

Commit

Permalink
tests: add requests generator tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Lapkovsky committed Dec 21, 2023
1 parent f7f1b13 commit 6dbec55
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/RequestGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace RonasIT\Support\Tests;

use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Generators\RepositoryGenerator;
use RonasIT\Support\Generators\RequestsGenerator;
use RonasIT\Support\Tests\Support\Request\RequestMockTrait;

class RequestGeneratorTest extends TestCase
{
use RequestMockTrait;

public function testCreateRequests()
{
$this->mockConfigurations();
$this->mockViewsNamespace();
$this->mockFilesystem();

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->rollbackToDefaultBasePath();

$this->assertGeneratedFileEquals('get_request.php', 'app/Http/Requests/Posts/GetPostRequest.php');
$this->assertGeneratedFileEquals('search_request.php', 'app/Http/Requests/Posts/SearchPostsRequest.php');
$this->assertGeneratedFileEquals('delete_request.php', 'app/Http/Requests/Posts/DeletePostRequest.php');
$this->assertGeneratedFileEquals('update_request.php', 'app/Http/Requests/Posts/UpdatePostRequest.php');
$this->assertGeneratedFileEquals('create_request.php', 'app/Http/Requests/Posts/CreatePostRequest.php');
}
}
52 changes: 52 additions & 0 deletions tests/Support/Request/RequestMockTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace RonasIT\Support\Tests\Support\Request;

use org\bovigo\vfs\vfsStream;
use RonasIT\Support\Generators\RepositoryGenerator;
use RonasIT\Support\Tests\Support\Shared\GeneratorMockTrait;

trait RequestMockTrait
{
use GeneratorMockTrait;

public function mockGeneratorForCreation(): void
{
$this->mockClass(RepositoryGenerator::class, [
[
'method' => 'classExists',
'arguments' => ['models', 'Post'],
'result' => false
],
]);
}

public function mockConfigurations(): void
{
config([
'entity-generator.stubs.request' => 'entity-generator::request',
'entity-generator.paths' => [
'requests' => 'app/Http/Requests',
'services' => 'app/Services',
]
]);
}

public function mockFilesystem(): void
{
$structure = [
'app' => [
'Http' => [
'Requests' => [
'Posts' => []
],
],
'Services' => [
'PostService.php' => '<?php'
]
],
];

vfsStream::create($structure);
}
}
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\Posts;

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\Posts;

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

class DeletePostRequest extends Request
{
public function validateResolved()
{
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\Posts;

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()
{
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\Posts;

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',
'all' => 'integer',
'is_published' => 'boolean',
'desc' => '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\Posts;

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()
{
parent::validateResolved();

$service = app(PostService::class);

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

0 comments on commit 6dbec55

Please sign in to comment.