From bb4238c0d2fb3b3c447baac2fe1f7838bb446a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Wed, 6 Dec 2017 22:55:15 +0100 Subject: [PATCH] Fix tests because of undefined index --- src/JsonApi/Request/JsonApiRequestBuilder.php | 14 +++++++------- src/JsonApi/Schema/Relationship.php | 8 ++++---- .../JsonApi/Request/JsonApiRequestBuilderTest.php | 14 +++++++++++++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/JsonApi/Request/JsonApiRequestBuilder.php b/src/JsonApi/Request/JsonApiRequestBuilder.php index 996d4f8..8d11341 100644 --- a/src/JsonApi/Request/JsonApiRequestBuilder.php +++ b/src/JsonApi/Request/JsonApiRequestBuilder.php @@ -134,23 +134,23 @@ public function setUri(string $uri): JsonApiRequestBuilder return $this; } - if ($this->isBlank($parsedUrl["scheme"]) === false) { + if ($this->isBlankKey($parsedUrl, "scheme") === false) { $this->scheme = $parsedUrl["scheme"]; } - if (empty((int) $parsedUrl["port"]) === false) { + if ($this->isBlankKey($parsedUrl, "port") === false) { $this->port = (int) $parsedUrl["port"]; } - if ($this->isBlank($parsedUrl["host"]) === false) { + if ($this->isBlankKey($parsedUrl, "host") === false) { $this->host = $parsedUrl["host"]; } - if ($this->isBlank($parsedUrl["path"]) === false) { + if ($this->isBlankKey($parsedUrl, "path") === false) { $this->path = $parsedUrl["path"]; } - if ($this->isBlank($parsedUrl["query"]) === false) { + if ($this->isBlankKey($parsedUrl, "query") === false) { parse_str($parsedUrl["query"], $this->queryString); } @@ -309,8 +309,8 @@ private function setListQueryParam(string $name, $queryParam): void } } - private function isBlank($value): bool + private function isBlankKey(array $array, string $key): bool { - return empty($value) && !is_numeric($value); + return empty($array[$key]) && !is_numeric($array[$key]); } } diff --git a/src/JsonApi/Schema/Relationship.php b/src/JsonApi/Schema/Relationship.php index 4a77359..12397f8 100755 --- a/src/JsonApi/Schema/Relationship.php +++ b/src/JsonApi/Schema/Relationship.php @@ -79,7 +79,7 @@ private static function createToOneFromArray( $resourceMap = []; $isToOneRelationship = true; - if (self::isBlank($data["type"]) === false && self::isBlank($data["id"]) === false) { + if (self::isBlankKey($data, "type") === false && self::isBlankKey($data, "id") === false) { $resourceMap = [ [ "type" => $data["type"], @@ -105,7 +105,7 @@ private static function createToManyFromArray( $resourceMap = []; foreach ($data as $item) { - if (self::isBlank($item["type"]) === false && self::isBlank($item["id"]) === false) { + if (self::isBlankKey($item, "type") === false && self::isBlankKey($item, "id") === false) { $resource = [ "type" => $item["type"], "id" => $item["id"], @@ -293,8 +293,8 @@ private static function isArrayKey(array $array, string $key): bool return isset($array[$key]) && is_array($array[$key]); } - private static function isBlank($value): bool + private static function isBlankKey(array $array, string $key): bool { - return empty($value) && !is_numeric($value); + return empty($array[$key]) && !is_numeric($array[$key]); } } diff --git a/tests/JsonApi/Request/JsonApiRequestBuilderTest.php b/tests/JsonApi/Request/JsonApiRequestBuilderTest.php index 38190d1..0fc0e8d 100644 --- a/tests/JsonApi/Request/JsonApiRequestBuilderTest.php +++ b/tests/JsonApi/Request/JsonApiRequestBuilderTest.php @@ -108,7 +108,19 @@ public function setUri() /** * @test */ - public function setUriWithZero() + public function setUriWithoutPath() + { + $requestBuilder = $this->createRequestBuilder(); + + $requestBuilder->setUri("http://example.com"); + + $this->assertSame("http://example.com", $requestBuilder->getRequest()->getUri()->__toString()); + } + + /** + * @test + */ + public function setUriWithQueryParamAsZero() { $requestBuilder = $this->createRequestBuilder();