-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Testing): Add a unit test case for quick testing of resources
- use ResourceTestCase for non model resources
- Loading branch information
Showing
4 changed files
with
195 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,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
56
tests/Unit/Testing/PHPUnit/LaraStrictResourceTestCaseTest.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,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) | ||
); | ||
} | ||
} |
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,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
45
tests/Unit/Testing/PHPUnit/LaravelResourceTestCaseTest.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,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, | ||
]; | ||
} | ||
} |