Skip to content

Commit

Permalink
test(spa, graphql): Fixes.
Browse files Browse the repository at this point in the history
(cherry picked from commit 15e8f52)
  • Loading branch information
LastDragon-ru committed Feb 16, 2024
1 parent cf8cb0b commit 65e71a0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait Concerns {
/**
* @param array<string, mixed> $attributes
*/
public function __construct(string $table, string $id = null, array $attributes = []) {
public function __construct(string $table = null, string $id = null, array $attributes = []) {
parent::__construct();

$this->table = $table;
Expand Down
99 changes: 57 additions & 42 deletions packages/spa/src/Http/Resources/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ public function testConstruct(bool|Exception $expected, mixed $value): void {
* @param class-string $expected
*/
public function testCollection(string $expected, mixed $value): void {
$class = get_class(new class(null) extends Resource {
// empty
});
$class = get_class(
new class(null) extends Resource {
// empty
},
);
$actual = $class::collection($value);

self::assertInstanceOf($expected, $actual);
Expand Down Expand Up @@ -92,10 +94,13 @@ public function toArray($request): mixed {
self::expectExceptionObject($expected);
}

self::assertEquals($expected, json_decode(
$resource->response()->content(),
true,
));
self::assertEquals(
$expected,
json_decode(
$resource->response()->content(),
true,
),
);
}

public function testMapResourceDataImplicitModel(): void {
Expand All @@ -106,9 +111,11 @@ public function testMapResourceDataImplicitModel(): void {
// empty
};

self::expectExceptionObject(new LogicException(
'Implicit conversions of Models is not supported, please redefine this method to make it explicit.',
));
self::expectExceptionObject(
new LogicException(
'Implicit conversions of Models is not supported, please redefine this method to make it explicit.',
),
);

self::assertIsArray($resource->toArray(new Request()));
}
Expand Down Expand Up @@ -304,38 +311,7 @@ public function jsonSerialize(): array {
'collection_date_no_cast' => $date->format(Package::DateTimeFormat),
],
],
new
/**
* @property DateTimeInterface $date
* @property DateTimeInterface $datetime
* @property DateTimeInterface $date_no_cast
* @property array<array-key, DateTimeInterface> $nested
* @property Collection<array-key, DateTimeInterface> $collection
*/
class($date, $format) extends Model {
public function __construct(DateTimeInterface $date, string $format) {
parent::__construct([]);

$this->dateFormat = $format;
$this->casts = [
'date' => 'date',
'datetime' => 'datetime',
];
$this->date = $date;
$this->datetime = $date;
$this->date_no_cast = $date;
$this->nested = [
'nested_date' => $date,
'nested_datetime' => $date,
'nested_date_no_cast' => $date,
];
$this->collection = new Collection([
'collection_date' => $date,
'collection_datetime' => $date,
'collection_date_no_cast' => $date,
]);
}
},
ResourceTest_Model::create($date, $format),
],
'JsonResource inside properties' => [
new LogicException('Please do not return JsonResource directly, use our Resources instead.'),
Expand All @@ -360,3 +336,42 @@ public function toJson(mixed $options = 0): mixed {
}
// </editor-fold>
}

// @phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
// @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps

/**
* @internal
* @noinspection PhpMultipleClassesDeclarationsInOneFile
*
* @property DateTimeInterface $date
* @property DateTimeInterface $datetime
* @property DateTimeInterface $date_no_cast
* @property array<array-key, DateTimeInterface> $nested
* @property Collection<array-key, DateTimeInterface> $collection
*/
class ResourceTest_Model extends Model {
public static function create(DateTimeInterface $date, string $format): self {
$model = new self();
$model->dateFormat = $format;
$model->casts = [
'date' => 'date',
'datetime' => 'datetime',
];
$model->date = $date;
$model->datetime = $date;
$model->date_no_cast = $date;
$model->nested = [
'nested_date' => $date,
'nested_datetime' => $date,
'nested_date_no_cast' => $date,
];
$model->collection = new Collection([
'collection_date' => $date,
'collection_datetime' => $date,
'collection_date_no_cast' => $date,
]);

return $model;
}
}

0 comments on commit 65e71a0

Please sign in to comment.