-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add requests generator tests.
- Loading branch information
Konstantin Lapkovsky
committed
Dec 21, 2023
1 parent
f7f1b13
commit 6dbec55
Showing
7 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'])); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'])); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'])); | ||
} | ||
} | ||
} |