From 5b99160c871f42e01333eabc8c69f0f06cd3c1ee Mon Sep 17 00:00:00 2001 From: Ahmed Bally Date: Mon, 8 May 2023 07:16:23 +0300 Subject: [PATCH] fix methods and tests --- composer.json | 2 +- src/FractalTransformFactory.php | 2 +- src/Resources/ResourceFactory.php | 2 +- src/Serializers/NoopSerializer.php | 4 ++-- src/Serializers/SuccessSerializer.php | 4 ++-- tests/Feature/TransformResponseDataTest.php | 2 +- tests/Unit/Exceptions/HandlerTest.php | 10 +++++++--- 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index c8422eb..c31b0c5 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ } ], "require": { - "php": "^7.0|^8.0", + "php": "^7.4|^8.0", "illuminate/contracts": "^5.1|^6.0|^7.0|^8.0|^9.0|^10.0", "illuminate/support": "^5.1|^6.0|^7.0|^8.0|^9.0|^10.0", "league/fractal": "^0.19.0|^0.20" diff --git a/src/FractalTransformFactory.php b/src/FractalTransformFactory.php index 30b8d47..b1e7761 100644 --- a/src/FractalTransformFactory.php +++ b/src/FractalTransformFactory.php @@ -70,7 +70,7 @@ protected function parseOptions(array $options, ResourceInterface $resource): ar ], $options); if (! empty($options['fieldsets'])) { - if (is_null($resourceKey = $resource->getResourceKey())) { + if (empty($resourceKey = $resource->getResourceKey())) { throw new LogicException('Filtering fields using sparse fieldsets require resource key to be set.'); } diff --git a/src/Resources/ResourceFactory.php b/src/Resources/ResourceFactory.php index 2dbb248..df1e54f 100644 --- a/src/Resources/ResourceFactory.php +++ b/src/Resources/ResourceFactory.php @@ -156,6 +156,6 @@ protected function resolveTransformer($data, $transformer) */ protected function resolveResourceKey($data, string $resourceKey = null) { - return isset($resourceKey) ? $resourceKey : $this->resourceKeyResolver->resolve($data); + return ! empty($resourceKey) ? $resourceKey : $this->resourceKeyResolver->resolve($data); } } \ No newline at end of file diff --git a/src/Serializers/NoopSerializer.php b/src/Serializers/NoopSerializer.php index 5097615..16bd4e9 100644 --- a/src/Serializers/NoopSerializer.php +++ b/src/Serializers/NoopSerializer.php @@ -44,9 +44,9 @@ public function item($resourceKey, array $data): array /** * Serialize null resources. * - * @return array + * @return null|array */ - public function null(): array + public function null(): ?array { return null; } diff --git a/src/Serializers/SuccessSerializer.php b/src/Serializers/SuccessSerializer.php index 64b57a8..95af6ae 100644 --- a/src/Serializers/SuccessSerializer.php +++ b/src/Serializers/SuccessSerializer.php @@ -42,9 +42,9 @@ public function item($resourceKey, array $data): array /** * Serialize null resources. * - * @return array + * @return null|array */ - public function null(): array + public function null(): ?array { return ['data' => null]; } diff --git a/tests/Feature/TransformResponseDataTest.php b/tests/Feature/TransformResponseDataTest.php index 0f0bef4..d1a3411 100644 --- a/tests/Feature/TransformResponseDataTest.php +++ b/tests/Feature/TransformResponseDataTest.php @@ -160,7 +160,7 @@ public function transform(Product $product) class ResourceKeySerializer extends SuccessSerializer { - public function item($resourceKey, array $data) + public function item($resourceKey, array $data): array { return [$resourceKey => $data]; } diff --git a/tests/Unit/Exceptions/HandlerTest.php b/tests/Unit/Exceptions/HandlerTest.php index 914af44..c403893 100644 --- a/tests/Unit/Exceptions/HandlerTest.php +++ b/tests/Unit/Exceptions/HandlerTest.php @@ -15,6 +15,7 @@ use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\AuthenticationException; use Illuminate\Contracts\Container\Container; +use Illuminate\Contracts\Translation\Translator; use Illuminate\Contracts\Validation\Validator; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\RelationNotFoundException as BaseRelationNotFoundException; @@ -126,7 +127,10 @@ public function testRenderMethodConvertsRelationNotFoundExceptions() public function testRenderMethodConvertsValidationExceptions() { $validator = Mockery::mock(Validator::class); - $validator->shouldReceive('errors')->andReturn(collect(['foo' => 'bar'])); + $validator->shouldReceive('errors')->andReturn(collect([['foo' => 'bar']])); + $translator = Mockery::mock(Translator::class); + $translator->shouldReceive('get')->andReturn('foo'); + $validator->shouldReceive('getTranslator')->andReturn($translator); $exception = new ValidationException($validator); $this->expectException(ValidationFailedException::class); @@ -161,9 +165,9 @@ public function testRenderMethodConvertsHttpExceptionsToResponses() */ public function testItShouldNotConvertNonHttpExceptions() { - $this->request->shouldReceive('expectsJson')->andReturn(false); + $request = Request::createFromGlobals(); - $result = $this->handler->render($this->request, $exception = new Exception); + $result = $this->handler->render($request, $exception = new Exception); $this->assertInstanceOf(Response::class, $result); }