Skip to content

Commit

Permalink
fix: phpstan & init phpunit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ark4ne committed Dec 20, 2024
1 parent d3b2f8a commit 3d0c977
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 14 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ jobs:
- name: Install dependencies
run: composer require laravel/framework ${{ matrix.laravel_version }} --prefer-dist --no-progress

- name: PHPUnit
run: vendor/bin/phpunit --coverage-clover coverage.xml --configuration phpunit.php${{ matrix.php_version }}.xml.dist

- name: PHPStan
run: vendor/bin/phpstan analyze

- name: PHPUnit
run: vendor/bin/phpunit --coverage-clover coverage.xml --configuration phpunit.php${{ matrix.php_version }}.xml.dist

- name: Coverage
run: bash <(curl -s https://codecov.io/bash)
26 changes: 26 additions & 0 deletions phpunit.php8.4.xml.dist
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>
55 changes: 55 additions & 0 deletions src/Asserts/AssertJsonApiResource.php
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);
}
}
}
}
13 changes: 13 additions & 0 deletions src/Asserts/EagerSetAttribute.php
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);
}
}
13 changes: 13 additions & 0 deletions src/Asserts/FailGenerateSchema.php
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);
}
}
6 changes: 3 additions & 3 deletions src/Descriptors/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function meta(Closure $meta): static
* @param bool|null $whenIncluded
* @return $this
*/
public function whenIncluded(bool $whenIncluded = null): static
public function whenIncluded(null|bool $whenIncluded = null): static
{
if ($whenIncluded === null) {
$this->whenIncluded ??= true;
Expand All @@ -83,7 +83,7 @@ public function whenIncluded(bool $whenIncluded = null): static
*
* @return static
*/
public function whenLoaded(string $relation = null): self
public function whenLoaded(null|string $relation = null): self
{
return $this->when(fn(
Request $request,
Expand All @@ -100,7 +100,7 @@ public function whenLoaded(string $relation = null): self
*
* @return static
*/
public function whenPivotLoaded(string $table, string $accessor = null): self
public function whenPivotLoaded(string $table, null|string $accessor = null): self
{
return $this->when(fn(
Request $request,
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Concerns/Relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private function requestedRelationshipsLoadFromSchema(Request $request, Skeleton
if ($load && Includes::include($request, $name)) {
$include = Includes::through($name, fn() => $this->requestedRelationshipsLoadFromSchema($request, $schema->relationships[$name]));

$apply = static function ($load, string $pre = null) use (&$loads, &$apply) {
$apply = static function ($load, null|string $pre = null) use (&$loads, &$apply) {
foreach ((array)$load as $key => $value) {
if (is_string($value)) {
$loads["$pre.$value"] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Concerns/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait Schema
*/
private static array $schemas = [];

public static function schema(Request $request = null): Skeleton
public static function schema(null|Request $request = null): Skeleton
{
if (isset(self::$schemas[static::class])) {
return self::$schemas[static::class];
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Concerns/SchemaCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

trait SchemaCollection
{
public static function schema(Request $request = null): Skeleton
public static function schema(null|Request $request = null): Skeleton
{
return self::new()->collects::schema($request);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function asCollection(): self
* @param bool|null $whenIncluded
* @return $this
*/
public function whenIncluded(bool $whenIncluded = null): static
public function whenIncluded(null|bool $whenIncluded = null): static
{
if ($whenIncluded === null) {
$this->whenIncluded ??= true;
Expand Down
4 changes: 2 additions & 2 deletions src/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private static function flattenDot(array $array, string $prepend, string $saveKe
* @param string|null $saveKey
* @return array<mixed>
*/
public static function undot(array $array, string $saveKey = null): array
public static function undot(array $array, null|string $saveKey = null): array
{
$results = [];

Expand All @@ -129,7 +129,7 @@ public static function undot(array $array, string $saveKey = null): array
* @param string|null $saveKey
* @return mixed
*/
public static function apply(array &$array, string $path, mixed $value, string $saveKey = null): mixed
public static function apply(array &$array, string $path, mixed $value, null|string $saveKey = null): mixed
{
$keys = explode('.', $path);

Expand Down
4 changes: 2 additions & 2 deletions src/Support/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function through(string $type, callable $callable): mixed
*
* @return string[]|null
*/
public static function get(Request $request, string $type = null): ?array
public static function get(Request $request, null|string $type = null): ?array
{
$type ??= self::$current;

Expand All @@ -57,7 +57,7 @@ public static function get(Request $request, string $type = null): ?array
*
* @return bool
*/
public static function has(Request $request, string $field, string $type =null): bool {
public static function has(Request $request, string $field, null|string $type =null): bool {
$type ??= self::$current;

if ($type === null) {
Expand Down

0 comments on commit 3d0c977

Please sign in to comment.