Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
refs: #49
  • Loading branch information
pirs1337 committed Jan 10, 2025
1 parent 270f93d commit 040af6a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 40 deletions.
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
51 changes: 41 additions & 10 deletions tests/RequestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

namespace RonasIT\Support\Tests;

use Illuminate\Support\Facades\Event;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Generators\RequestsGenerator;
use RonasIT\Support\Tests\Support\Request\RequestMockTrait;

class RequestGeneratorTest extends TestCase
{
use RequestMockTrait;

public function testCreateRequests()
public function setUp(): void
{
$this->mockConfigurations();
$this->mockViewsNamespace();
$this->mockFilesystem();
parent::setUp();

Event::fake();
}

public function testCreateRequests()
{
app(RequestsGenerator::class)
->setModel('Post')
->setRelations([
Expand All @@ -31,12 +37,37 @@ public function testCreateRequests()
->setCrudOptions(['C', 'R', 'U', 'D'])
->generate();

$this->rollbackToDefaultBasePath();
$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->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->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.',
);
}
}
11 changes: 0 additions & 11 deletions tests/Support/Request/RequestMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@ trait RequestMockTrait
{
use GeneratorMockTrait, MockTrait;

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 = [
Expand Down
10 changes: 0 additions & 10 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,4 @@ protected function assertExceptionThrew(string $className, string $message): voi
$this->expectException($className);
$this->expectExceptionMessage($message);
}

public function mockViewsNamespace(): void
{
View::addNamespace('entity-generator', getcwd() . '/stubs');
}

public function rollbackToDefaultBasePath(): void
{
$this->app->setBasePath(getcwd());
}
}
2 changes: 1 addition & 1 deletion tests/fixtures/RequestGeneratorTest/create_request.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Requests\Posts;
namespace App\Http\Requests\Post;

use App\Http\Requests\Request;

Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/RequestGeneratorTest/delete_request.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace App\Http\Requests\Posts;
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()
public function validateResolved(): void
{
parent::validateResolved();

Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/RequestGeneratorTest/get_request.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Requests\Posts;
namespace App\Http\Requests\Post;

use App\Http\Requests\Request;
use App\Services\PostService;
Expand All @@ -16,7 +16,7 @@ public function rules(): array
];
}

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

Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/RequestGeneratorTest/search_request.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Requests\Posts;
namespace App\Http\Requests\Post;

use App\Http\Requests\Request;

Expand All @@ -12,9 +12,9 @@ public function rules(): array
'user_id' => 'integer|exists:users,id|required',
'page' => 'integer',
'per_page' => 'integer',
'all' => 'integer',
'is_published' => 'boolean',
'desc' => 'boolean',
'all' => 'boolean',
'with' => 'array',
'order_by' => 'string',
'query' => 'string|nullable',
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/RequestGeneratorTest/update_request.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Requests\Posts;
namespace App\Http\Requests\Post;

use App\Http\Requests\Request;
use App\Services\PostService;
Expand All @@ -17,7 +17,7 @@ public function rules(): array
];
}

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

Expand Down

0 comments on commit 040af6a

Please sign in to comment.