Skip to content

Commit

Permalink
feat(Testing): Add a unit test case for quick testing of resources
Browse files Browse the repository at this point in the history
- use ResourceTestCase for non model resources
  • Loading branch information
pionl committed Jun 28, 2023
1 parent 2573fce commit 882c3b5
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/Testing/PHPUnit/ResourceTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace LaraStrict\Testing\PHPUnit;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use LaraStrict\Http\Resources\JsonResource as LaraStrictJsonResource;
use LaraStrict\Testing\Assert\AssertExpectationTestCase;
use LaraStrict\Testing\Laravel\TestingContainer;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Mockery\Adapter\Phpunit\MockeryTestCaseSetUp;

/**
* @template TEntity of object
*/
abstract class ResourceTestCase extends AssertExpectationTestCase
{
use MockeryPHPUnitIntegration;
use MockeryTestCaseSetUp;

/**
* @return array<string|int, array{0: Closure(static):void}>
*/
abstract public function data(): array;

/**
* @param Closure(static):void $assert
* @dataProvider data
*/
public function test(Closure $assert): void
{
$assert($this);
}

protected function mockeryTestSetUp(): void
{
}

protected function mockeryTestTearDown(): void
{
}

/**
* @param TEntity $object
* @param array<string|int, mixed> $expected
* @param TestingContainer|null $container Set container to the resource.
*/
protected function assert(object $object, array $expected, ?TestingContainer $container = null): void
{
$request = new Request();

$resource = $this->createResource($object);

if ($resource instanceof LaraStrictJsonResource && $container !== null) {
$resource->setContainer($container);
}

$this->assertEquals(expected: $expected, actual: $resource->resolve($request));
}

/**
* @param TEntity $object
*/
abstract protected function createResource(object $object): JsonResource;
}
56 changes: 56 additions & 0 deletions tests/Unit/Testing/PHPUnit/LaraStrictResourceTestCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Tests\LaraStrict\Unit\Testing\PHPUnit;

use Illuminate\Http\Resources\Json\JsonResource;
use LaraStrict\Testing\Laravel\TestingContainer;
use LaraStrict\Testing\PHPUnit\ResourceTestCase;
use Tests\LaraStrict\Feature\Http\Resources\LaraStrictResource;
use Tests\LaraStrict\Feature\Http\Resources\TestAction;
use Tests\LaraStrict\Feature\Http\Resources\TestEntity;

/**
* @extends ResourceTestCase<TestEntity>
*/
class LaraStrictResourceTestCaseTest extends ResourceTestCase
{
public function data(): array
{
return [
[
static fn (self $testCase) => $testCase->myAssert(value: 'test', instance: '2'),
],
[
static fn (self $testCase) => $testCase->myAssert(value: 'test22', instance: '1'),
],
];
}

protected function myAssert(string $value, string $instance): void
{
$this->assert(
object: new TestEntity(value: $value),
expected: [
'test' => $value,
'instance' => $instance,
],
container: new TestingContainer(
makeAlwaysBinding: static fn () => new TestAction($instance)
),
);
}

protected function createResource(object $object): JsonResource
{
return new LaraStrictResource($object);
}

protected static function createContainer(string $value): TestingContainer
{
return new TestingContainer(
makeAlwaysBinding: static fn () => new TestAction($value)
);
}
}
26 changes: 26 additions & 0 deletions tests/Unit/Testing/PHPUnit/LaravelResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tests\LaraStrict\Unit\Testing\PHPUnit;

use Illuminate\Http\Resources\Json\JsonResource;
use Tests\LaraStrict\Feature\Http\Resources\TestEntity;

/**
* @property TestEntity $resource
*/
class LaravelResource extends JsonResource
{
public function __construct(?TestEntity $resource)
{
parent::__construct($resource);
}

public function toArray($request): array
{
return [
'test' => $this->resource->value,
];
}
}
45 changes: 45 additions & 0 deletions tests/Unit/Testing/PHPUnit/LaravelResourceTestCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Tests\LaraStrict\Unit\Testing\PHPUnit;

use Illuminate\Http\Resources\Json\JsonResource;
use LaraStrict\Testing\PHPUnit\ResourceTestCase;
use Tests\LaraStrict\Feature\Http\Resources\TestEntity;

/**
* @extends ResourceTestCase<TestEntity>
*/
class LaravelResourceTestCaseTest extends ResourceTestCase
{
public function data(): array
{
return [
[
static fn (self $testCase) => $testCase->assert(
object: new TestEntity(value: 'test'),
expected: self::expect(value: 'test')
),
],
[
static fn (self $testCase) => $testCase->assert(
object: new TestEntity(value: 'test22'),
expected: self::expect(value: 'test22')
),
],
];
}

protected function createResource(object $object): JsonResource
{
return new LaravelResource($object);
}

protected static function expect(string $value): array
{
return [
'test' => $value,
];
}
}

0 comments on commit 882c3b5

Please sign in to comment.