-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
121 additions
and
14 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
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 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
executionOrder="depends,defects" | ||
failOnRisky="true" | ||
failOnWarning="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Unit"> | ||
<directory>tests/Unit</directory> | ||
</testsuite> | ||
<testsuite name="Feature"> | ||
<directory>tests/Feature</directory> | ||
</testsuite> | ||
</testsuites> | ||
<php> | ||
<env name="DB_CONNECTION" value="sqlite"/> | ||
<env name="DB_DATABASE" value=":memory:"/> | ||
</php> | ||
<source> | ||
<include> | ||
<directory suffix=".php">src</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
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,55 @@ | ||
<?php | ||
|
||
namespace Ark4ne\JsonApi\Asserts; | ||
|
||
use Ark4ne\JsonApi\Descriptors\Values\Value; | ||
use Ark4ne\JsonApi\Resources\JsonApiResource; | ||
use Ark4ne\JsonApi\Support\FakeModel; | ||
use Ark4ne\JsonApi\Support\Values; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\MissingValue; | ||
use ReflectionClass; | ||
|
||
class AssertJsonApiResource | ||
{ | ||
/** | ||
* @param class-string<JsonApiResource> $class | ||
*/ | ||
public function __construct(protected string $class) | ||
{ | ||
} | ||
|
||
public function assert(): void | ||
{ | ||
$this->itCanGenerateSchema(); | ||
$this->allAttributesAreLazySet(); | ||
} | ||
|
||
private function itCanGenerateSchema(): void | ||
{ | ||
try { | ||
$this->class::schema(); | ||
} catch (\Throwable $throwable) { | ||
throw new FailGenerateSchema($this->class, $throwable); | ||
} | ||
} | ||
|
||
private function allAttributesAreLazySet(): void | ||
{ | ||
$reflection = new ReflectionClass($this->class); | ||
$instance = $reflection->newInstanceWithoutConstructor(); | ||
$instance->resource = new FakeModel; | ||
|
||
$method = $reflection->getMethod('toAttributes'); | ||
$method->setAccessible(true); | ||
/** @var iterable<array-key, mixed> $attributes */ | ||
$attributes = $method->invoke($instance, new Request()); | ||
$attributes = Values::mergeValues($attributes); | ||
|
||
foreach ($attributes as $key => $attribute) { | ||
if (!($attribute instanceof \Closure || $attribute instanceof MissingValue || $attribute instanceof Value)) { | ||
throw new EagerSetAttribute($this->class, $key); | ||
} | ||
} | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Ark4ne\JsonApi\Asserts; | ||
|
||
use Throwable; | ||
|
||
class EagerSetAttribute extends \Exception | ||
{ | ||
public function __construct(string $class, string $key, ?Throwable $previous = null) | ||
{ | ||
parent::__construct("Attribute [$key] on resource [$class] is eager set.", 0, $previous); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Ark4ne\JsonApi\Asserts; | ||
|
||
use Throwable; | ||
|
||
class FailGenerateSchema extends \Exception | ||
{ | ||
public function __construct(string $class, ?Throwable $previous = null) | ||
{ | ||
parent::__construct("Can't generate schema for resource [$class].", 0, $previous); | ||
} | ||
} |
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
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
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
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
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
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
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