-
-
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 ability to test models without Laravel framework
- Use MockModels trait and call mockModels() in setUp function - Use ModelResourceTestCase to test model resources
- Loading branch information
Showing
4 changed files
with
128 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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\PHPUnit; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use LaraStrict\Testing\PHPUnit\Traits\MockModels; | ||
|
||
/** | ||
* @template TModel of Model | ||
* @extends ResourceTestCase<TModel> | ||
*/ | ||
abstract class ModelResourceTestCase extends ResourceTestCase | ||
{ | ||
use MockModels; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->mockModels(); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Testing\PHPUnit\Traits; | ||
|
||
use Illuminate\Database\ConnectionResolverInterface; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Mockery; | ||
|
||
trait MockModels | ||
{ | ||
public function mockModels(): void | ||
{ | ||
// Resolve connection for all models | ||
$resolver = Mockery::mock(ConnectionResolverInterface::class); | ||
$resolver->shouldReceive('connection->getQueryGrammar->getDateFormat') | ||
->andReturn('Y-m-d H:i:s'); | ||
|
||
Model::setConnectionResolver($resolver); | ||
} | ||
} |
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\Database\Models\Test; | ||
|
||
/** | ||
* @property Test $resource | ||
*/ | ||
class ModelResource extends JsonResource | ||
{ | ||
public function __construct(?Test $resource) | ||
{ | ||
parent::__construct($resource); | ||
} | ||
|
||
public function toArray($request): array | ||
{ | ||
return [ | ||
'test' => $this->resource->test, | ||
]; | ||
} | ||
} |
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 Carbon\Carbon; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
use LaraStrict\Testing\PHPUnit\ModelResourceTestCase; | ||
use Tests\LaraStrict\Feature\Database\Models\Test; | ||
|
||
/** | ||
* @extends ModelResourceTestCase<Test> | ||
*/ | ||
class ModelResourceTestCaseTest extends ModelResourceTestCase | ||
{ | ||
public function data(): array | ||
{ | ||
return [ | ||
[ | ||
static fn (self $self) => $self->assert( | ||
object: self::create(value: 1), | ||
expected: self::expect(value: 1) | ||
), | ||
], | ||
[ | ||
static fn (self $self) => $self->assert( | ||
object: self::create(value: 2), | ||
expected: self::expect(value: 2) | ||
), | ||
], | ||
]; | ||
} | ||
|
||
protected function createResource(object $object): JsonResource | ||
{ | ||
return new ModelResource($object); | ||
} | ||
|
||
private static function expect(int $value): array | ||
{ | ||
return [ | ||
'test' => $value, | ||
]; | ||
} | ||
|
||
private static function create(int $value): Test | ||
{ | ||
$test = new Test(); | ||
$test->test = $value; | ||
// Should trigger mock models | ||
$test->deleted_at = Carbon::now(); | ||
|
||
return $test; | ||
} | ||
} |