Skip to content

Commit

Permalink
Merge pull request #75 from RonasIT/49-add-api-resource-generator-tests
Browse files Browse the repository at this point in the history
Add API resource generator tests
  • Loading branch information
DenTray authored Jan 22, 2025
2 parents 4bbe059 + 762b192 commit c07dcdc
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
106 changes: 106 additions & 0 deletions tests/ResourceGeneratorTest.php
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 tests/fixtures/ResourceGeneratorTest/post_collection_resource.php
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;
}
20 changes: 20 additions & 0 deletions tests/fixtures/ResourceGeneratorTest/post_resource.php
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);
}
}

0 comments on commit c07dcdc

Please sign in to comment.