-
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.
Merge pull request #75 from RonasIT/49-add-api-resource-generator-tests
Add API resource generator tests
- Loading branch information
Showing
3 changed files
with
136 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,106 @@ | ||
<?php | ||
|
||
namespace RonasIT\Support\Tests; | ||
|
||
use RonasIT\Support\Events\SuccessCreateMessage; | ||
use RonasIT\Support\Events\WarningEvent; | ||
use RonasIT\Support\Exceptions\ClassAlreadyExistsException; | ||
use RonasIT\Support\Generators\ResourceGenerator; | ||
use RonasIT\Support\Tests\Support\GeneratorMockTrait; | ||
|
||
class ResourceGeneratorTest extends TestCase | ||
{ | ||
use GeneratorMockTrait; | ||
|
||
public function testResourceAlreadyExists() | ||
{ | ||
$this->mockClass(ResourceGenerator::class, [ | ||
$this->classExistsMethodCall(['resources', 'PostResource']), | ||
]); | ||
|
||
$this->assertExceptionThrew( | ||
className: ClassAlreadyExistsException::class, | ||
message: 'Cannot create PostResource cause PostResource already exists. Remove PostResource.', | ||
); | ||
|
||
app(ResourceGenerator::class) | ||
->setModel('Post') | ||
->generate(); | ||
} | ||
|
||
public function testCollectionResourceAlreadyExists() | ||
{ | ||
$this->mockClass(ResourceGenerator::class, [ | ||
$this->classExistsMethodCall(['resources', 'PostResource'], false), | ||
$this->classExistsMethodCall(['resources', 'PostsCollectionResource']), | ||
]); | ||
|
||
$this->assertExceptionThrew( | ||
className: ClassAlreadyExistsException::class, | ||
message: 'Cannot create PostsCollectionResource cause PostsCollectionResource already exists. ' | ||
. 'Remove PostsCollectionResource.', | ||
); | ||
|
||
app(ResourceGenerator::class) | ||
->setModel('Post') | ||
->generate(); | ||
|
||
$this->assertGeneratedFileEquals('post_resource.php', 'app/Http/Resources/Post/PostResource.php'); | ||
|
||
$this->assertEventPushed( | ||
className: SuccessCreateMessage::class, | ||
message: 'Created a new Resource: PostResource', | ||
); | ||
} | ||
|
||
public function testCreateResources() | ||
{ | ||
app(ResourceGenerator::class) | ||
->setModel('Post') | ||
->generate(); | ||
|
||
$this->assertGeneratedFileEquals('post_resource.php', 'app/Http/Resources/Post/PostResource.php'); | ||
$this->assertGeneratedFileEquals('post_collection_resource.php', 'app/Http/Resources/Post/PostsCollectionResource.php'); | ||
|
||
$this->assertEventPushedChain([ | ||
SuccessCreateMessage::class => [ | ||
'Created a new Resource: PostResource', | ||
'Created a new CollectionResource: PostsCollectionResource', | ||
], | ||
]); | ||
} | ||
|
||
public function testCreateResourcesResourceStubNotExist() | ||
{ | ||
config(['entity-generator.stubs.resource' => 'incorrect_stub']); | ||
|
||
app(ResourceGenerator::class) | ||
->setModel('Post') | ||
->generate(); | ||
|
||
$this->assertFileDoesNotExist('app/Http/Resources/Post/PostResource.php'); | ||
$this->assertFileDoesNotExist('app/Http/Resources/Post/PostsCollectionResource.php'); | ||
|
||
$this->assertEventPushed( | ||
className: WarningEvent::class, | ||
message: 'Generation of resource has been skipped cause the view incorrect_stub from the config entity-generator.stubs.resource is not exists. Please check that config has the correct view name value.', | ||
); | ||
} | ||
|
||
public function testCreateResourcesCollectionResourceStubNotExist() | ||
{ | ||
config(['entity-generator.stubs.collection_resource' => 'incorrect_stub']); | ||
|
||
app(ResourceGenerator::class) | ||
->setModel('Post') | ||
->generate(); | ||
|
||
$this->assertGeneratedFileEquals('post_resource.php', 'app/Http/Resources/Post/PostResource.php'); | ||
$this->assertFileDoesNotExist('app/Http/Resources/Post/PostsCollectionResource.php'); | ||
|
||
$this->assertEventPushedChain([ | ||
SuccessCreateMessage::class => ['Created a new Resource: PostResource'], | ||
WarningEvent::class => ['Generation of collection resource has been skipped cause the view incorrect_stub from the config entity-generator.stubs.collection_resource is not exists. Please check that config has the correct view name value.'], | ||
]); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/fixtures/ResourceGeneratorTest/post_collection_resource.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,10 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources\Post; | ||
|
||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
|
||
class PostsCollectionResource extends ResourceCollection | ||
{ | ||
public $collects = PostResource::class; | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources\Post; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
use App\Models\Post; | ||
|
||
/** | ||
* @property Post $resource | ||
*/ | ||
class PostResource extends JsonResource | ||
{ | ||
public static $wrap = null; | ||
|
||
//TODO implement custom serialization logic or remove method redefining | ||
public function toArray($request): array | ||
{ | ||
return parent::toArray($request); | ||
} | ||
} |