-
-
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(Http): Add support for setting container to collection of resources
BREAKING CHANGE: If you pass container and do not use JsonResource exception will be thrown.
- Loading branch information
Showing
11 changed files
with
419 additions
and
23 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 |
---|---|---|
@@ -1,21 +1,36 @@ | ||
parameters: | ||
ignoreErrors: | ||
- | ||
message: "#^Cannot cast array\\|string to string\\.$#" | ||
count: 1 | ||
path: src/Testing/Actions/GetNamespaceForStubsAction.php | ||
ignoreErrors: | ||
- | ||
message: "#^Access to an undefined property LaraStrict\\\\Http\\\\Resources\\\\JsonResource\\:\\:\\$preserveKeys\\.$#" | ||
count: 1 | ||
path: src/Http/Resources/JsonResource.php | ||
|
||
- | ||
message: "#^Calling PHPStan\\\\PhpDoc\\\\PhpDocStringResolver\\:\\:resolve\\(\\) is not covered by backward compatibility promise\\. The method might change in a minor PHPStan version\\.$#" | ||
count: 1 | ||
path: src/Testing/Actions/ParsePhpDocAction.php | ||
- | ||
message: "#^Unsafe usage of new static\\(\\)\\.$#" | ||
count: 1 | ||
path: src/Http/Resources/JsonResource.php | ||
|
||
- | ||
message: "#^Call to method PHPUnit\\\\Framework\\\\Assert\\:\\:assertTrue\\(\\) with false and 'Hook should be…' will always evaluate to false\\.$#" | ||
count: 1 | ||
path: tests/Feature/Testing/Commands/MakeExpectationCommandRealTest.php | ||
- | ||
message: "#^Method LaraStrict\\\\Http\\\\Resources\\\\JsonResourceCollection\\:\\:toArray\\(\\) return type with generic interface Illuminate\\\\Contracts\\\\Support\\\\Arrayable does not specify its types\\: TKey, TValue$#" | ||
count: 1 | ||
path: src/Http/Resources/JsonResourceCollection.php | ||
|
||
- | ||
message: "#^Method Tests\\\\LaraStrict\\\\Feature\\\\Translations\\\\InvalidServiceProviderTranslations\\:\\:getProviderClass\\(\\) should return class\\-string\\<LaraStrict\\\\Providers\\\\AbstractServiceProvider\\> but returns string\\.$#" | ||
count: 1 | ||
path: tests/Feature/Translations/InvalidServiceProviderTranslations.php | ||
- | ||
message: "#^Cannot cast array\\|string to string\\.$#" | ||
count: 1 | ||
path: src/Testing/Actions/GetNamespaceForStubsAction.php | ||
|
||
- | ||
message: "#^Calling PHPStan\\\\PhpDoc\\\\PhpDocStringResolver\\:\\:resolve\\(\\) is not covered by backward compatibility promise\\. The method might change in a minor PHPStan version\\.$#" | ||
count: 1 | ||
path: src/Testing/Actions/ParsePhpDocAction.php | ||
|
||
- | ||
message: "#^Call to method PHPUnit\\\\Framework\\\\Assert\\:\\:assertTrue\\(\\) with false and 'Hook should be…' will always evaluate to false\\.$#" | ||
count: 1 | ||
path: tests/Feature/Testing/Commands/MakeExpectationCommandRealTest.php | ||
|
||
- | ||
message: "#^Method Tests\\\\LaraStrict\\\\Feature\\\\Translations\\\\InvalidServiceProviderTranslations\\:\\:getProviderClass\\(\\) should return class\\-string\\<LaraStrict\\\\Providers\\\\AbstractServiceProvider\\> but returns string\\.$#" | ||
count: 1 | ||
path: tests/Feature/Translations/InvalidServiceProviderTranslations.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
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Http\Resources; | ||
|
||
use Illuminate\Contracts\Container\Container; | ||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection; | ||
|
||
class JsonResourceCollection extends AnonymousResourceCollection | ||
{ | ||
private ?Container $container = null; | ||
|
||
public function setContainer(?Container $container): self | ||
{ | ||
$this->container = $container; | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray($request) | ||
{ | ||
return $this->collection->map(function (JsonResource $resource) use ($request) { | ||
if ($this->container !== null) { | ||
$resource->setContainer($this->container); | ||
} | ||
|
||
return $resource->toArray($request); | ||
})->all(); | ||
} | ||
} |
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
130 changes: 130 additions & 0 deletions
130
tests/Feature/Http/Resources/JsonResourceCollectionTest.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,130 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Http\Resources; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection; | ||
use LaraStrict\Http\Resources\JsonResourceCollection; | ||
use LaraStrict\Http\Resources\MessageResource; | ||
use LaraStrict\Testing\Laravel\TestingContainer; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class JsonResourceCollectionTest extends TestCase | ||
{ | ||
private Request $request; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->request = new Request(); | ||
} | ||
|
||
/** | ||
* @return array<string|int, array{0: Closure(static):void}> | ||
*/ | ||
public function data(): array | ||
{ | ||
// Preserve keys changes keys using mergeData, not input/output array | ||
$input = [ | ||
1 => '1', | ||
2 => '2', | ||
]; | ||
$output = [ | ||
1 => [ | ||
'message' => '1', | ||
], | ||
2 => [ | ||
'message' => '2', | ||
], | ||
]; | ||
|
||
return [ | ||
'preserve keys false by default' => [ | ||
static fn (self $self) => $self->assert( | ||
laravelCollection: MessageJsonResource::collection($input), | ||
laraStrictCollection: MessageResource::collection($input), | ||
expectedPreserveKeys: false, | ||
expectedOutput: $output | ||
), | ||
], | ||
'preserve keys true' => [ | ||
static fn (self $self) => $self->assert( | ||
laravelCollection: PreserveKeysJsonResource::collection($input), | ||
laraStrictCollection: PreserveKeysLaraStrictResource::collection($input), | ||
expectedPreserveKeys: true, | ||
expectedOutput: $output | ||
), | ||
], | ||
'preserve keys false by default - no container' => [ | ||
static fn (self $self) => $self->assert( | ||
laravelCollection: MessageJsonResource::collection($input), | ||
laraStrictCollection: MessageResource::collection($input), | ||
expectedPreserveKeys: false, | ||
expectedOutput: $output, | ||
setContainer: false | ||
), | ||
], | ||
'preserve keys true - no container' => [ | ||
static fn (self $self) => $self->assert( | ||
laravelCollection: PreserveKeysJsonResource::collection($input), | ||
laraStrictCollection: PreserveKeysLaraStrictResource::collection($input), | ||
expectedPreserveKeys: true, | ||
expectedOutput: $output, | ||
setContainer: false | ||
), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param Closure(static):void $assert | ||
* | ||
* @dataProvider data | ||
*/ | ||
public function test(Closure $assert): void | ||
{ | ||
$assert($this); | ||
} | ||
|
||
public function assert( | ||
AnonymousResourceCollection $laravelCollection, | ||
JsonResourceCollection $laraStrictCollection, | ||
bool $expectedPreserveKeys, | ||
array $expectedOutput, | ||
bool $setContainer = true | ||
): void { | ||
// preserveKeys was added Laravel v9.45.0 | ||
if (property_exists($laravelCollection, 'preserveKeys')) { | ||
$this->assertEquals( | ||
expected: $expectedPreserveKeys, | ||
actual: $laravelCollection->preserveKeys, | ||
message: 'Laravel preserve keys' | ||
); | ||
} | ||
|
||
if (property_exists($laraStrictCollection, 'preserveKeys')) { | ||
$this->assertEquals( | ||
expected: $expectedPreserveKeys, | ||
actual: $laraStrictCollection->preserveKeys, | ||
message: 'LaraStrict preserve keys' | ||
); | ||
} | ||
|
||
$this->assertEquals( | ||
expected: $expectedOutput, | ||
actual: $laravelCollection->toArray($this->request), | ||
message: 'Laravel toArray' | ||
); | ||
|
||
$this->assertEquals( | ||
expected: $expectedOutput, | ||
actual: $laraStrictCollection | ||
->setContainer($setContainer ? new TestingContainer() : null) | ||
->toArray($this->request), | ||
message: 'LaraStrict toArray' | ||
); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
/** | ||
* @property string $resource | ||
*/ | ||
class MessageJsonResource extends JsonResource | ||
{ | ||
public static $wrap = null; | ||
|
||
public function __construct(string|null $resource) | ||
{ | ||
parent::__construct($resource); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'message' => $this->resource, | ||
]; | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Http\Resources; | ||
|
||
class PreserveKeysJsonResource extends MessageJsonResource | ||
{ | ||
public bool $preserveKeys = true; | ||
|
||
// TODO: Remove array when Laravel fixes its own implementation. | ||
public function __construct(string|null|array $resource) | ||
{ | ||
parent::__construct(is_array($resource) ? null : $resource); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Feature/Http/Resources/PreserveKeysLaraStrictResource.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Http\Resources; | ||
|
||
use LaraStrict\Http\Resources\MessageResource; | ||
|
||
class PreserveKeysLaraStrictResource extends MessageResource | ||
{ | ||
public bool $preserveKeys = true; | ||
} |
Oops, something went wrong.