diff --git a/.phpstan.neon b/.phpstan.neon index 8136274..5567202 100644 --- a/.phpstan.neon +++ b/.phpstan.neon @@ -1,32 +1,78 @@ # SPDX-FileCopyrightText: 2015-2023 Artur Weigandt https://wlabs.de/kontakt # SPDX-License-Identifier: GPL-3.0-or-later +includes: + - vendor/phpstan/phpstan-phpunit/extension.neon + parameters: - level: 8 + level: 9 paths: - src/ + - tests/ scanDirectories: - vendor + treatPhpDocTypesAsCertain: false + ignoreErrors: - - message: "#^Class Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey extends generic class SplStack but does not specify its types\\: TValue$#" + message: "#^Constructor of class Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull has an unused parameter \\$data\\.$#" count: 1 - path: src/Helper/AccessKey.php + path: src/V1/ResourceNull.php + # parameter is required by Art4\JsonApiClient\Element - - message: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$data is never read, only written\\.$#" + message: "#^Constructor of class Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull has an unused parameter \\$manager\\.$#" count: 1 path: src/V1/ResourceNull.php + # parameter is required by Art4\JsonApiClient\Element - - message: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$manager is never read, only written\\.$#" + message: "#^Constructor of class Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull has an unused parameter \\$parent\\.$#" count: 1 path: src/V1/ResourceNull.php + # parameter is required by Art4\JsonApiClient\Element - - message: "#^Property Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:\\$parent is never read, only written\\.$#" + message: "#^Constructor of an anonymous class has an unused parameter \\$data\\.$#" count: 1 - path: src/V1/ResourceNull.php + path: tests/BC/ElementTest.php + # parameter is required by Art4\JsonApiClient\Element + + - + message: "#^Constructor of an anonymous class has an unused parameter \\$manager\\.$#" + count: 1 + path: tests/BC/ElementTest.php + # parameter is required by Art4\JsonApiClient\Element + + - + message: "#^Constructor of an anonymous class has an unused parameter \\$parent\\.$#" + count: 1 + path: tests/BC/ElementTest.php + # parameter is required by Art4\JsonApiClient\Element + + - + message: "#^Parameter \\#1 \\$string of class Art4\\\\JsonApiClient\\\\Input\\\\RequestStringInput constructor expects string, mixed given\\.$#" + count: 1 + path: tests/Unit/Input/RequestStringInputTest.php + # We are providing an invalid parameter to test the exception message + + - + message: "#^Parameter \\#1 \\$string of class Art4\\\\JsonApiClient\\\\Input\\\\ResponseStringInput constructor expects string, mixed given\\.$#" + count: 1 + path: tests/Unit/Input/ResponseStringInputTest.php + # We are providing an invalid parameter to test the exception message + + - + message: "#^Parameter \\#1 \\$key of method Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:has\\(\\) expects Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey\\|int\\|string, array given\\.$#" + count: 1 + path: tests/Unit/V1/ResourceNullTest.php + # We are providing an invalid parameter to test the deprecation message + + - + message: "#^Parameter \\#1 \\$key of method Art4\\\\JsonApiClient\\\\V1\\\\ResourceNull\\:\\:has\\(\\) expects Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey\\|int\\|string, stdClass given\\.$#" + count: 1 + path: tests/Unit/V1/ResourceNullTest.php + # We are providing an invalid parameter to test the deprecation message diff --git a/composer.json b/composer.json index 82738e1..48f9de4 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "require-dev": { "friendsofphp/php-cs-fixer": "^3.35", "phpstan/phpstan": "^1.10", + "phpstan/phpstan-phpunit": "^1.3", "phpunit/phpunit": "^9 || ^10" }, "autoload": { diff --git a/src/Accessable.php b/src/Accessable.php index ec46ab1..cf63145 100644 --- a/src/Accessable.php +++ b/src/Accessable.php @@ -8,6 +8,8 @@ namespace Art4\JsonApiClient; +use Art4\JsonApiClient\Helper\AccessKey; + /** * Accessable Interface */ @@ -39,7 +41,7 @@ public function has($key); * * @deprecated `\Art4\JsonApiClient\Accessable::getKeys()` will add `array` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. * - * @return array Keys of all setted values + * @return array Keys of all setted values */ public function getKeys(); // public function getKeys(): array; diff --git a/src/Helper/AccessKey.php b/src/Helper/AccessKey.php index a57558f..bed0120 100644 --- a/src/Helper/AccessKey.php +++ b/src/Helper/AccessKey.php @@ -13,6 +13,8 @@ /** * AccessKey * + * @extends SplStack + * * @internal */ final class AccessKey extends SplStack @@ -20,7 +22,7 @@ final class AccessKey extends SplStack /** * Transforms the Key to a string * - * @param mixed $key + * @param int|string $key * * @return AccessKey */ diff --git a/src/Helper/AccessableTrait.php b/src/Helper/AccessableTrait.php index 81eb8bf..6cee963 100644 --- a/src/Helper/AccessableTrait.php +++ b/src/Helper/AccessableTrait.php @@ -41,7 +41,7 @@ final protected function set(string $key, $value): void /** * Returns the keys of all setted values * - * @return array Keys of all setted values + * @return array Keys of all setted values */ final public function getKeys(): array { @@ -51,10 +51,19 @@ final public function getKeys(): array /** * Check if a value exists * - * @param mixed $key The key + * @param int|string|AccessKey $key The key */ final public function has($key): bool { + if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) { + trigger_error(sprintf( + '%s::has(): Providing Argument #1 ($key) as %s is deprecated since 1.2.0, please provide as int|string|%s instead.', + get_class($this), + gettype($key), + AccessKey::class + ), \E_USER_DEPRECATED); + } + $key = $this->parseKey($key); $string = $key->shift(); @@ -82,7 +91,7 @@ final public function has($key): bool /** * Get a value by a key * - * @param mixed $key The key + * @param int|string|AccessKey $key The key * * @return mixed */ @@ -126,7 +135,7 @@ private function getValue(string $key) /** * Parse a dot.notated.key to an object * - * @param string|AccessKey $key The key + * @param int|string|AccessKey $key The key * * @return AccessKey The parsed key */ diff --git a/src/Serializer/ArraySerializer.php b/src/Serializer/ArraySerializer.php index 42d1491..422037f 100644 --- a/src/Serializer/ArraySerializer.php +++ b/src/Serializer/ArraySerializer.php @@ -35,7 +35,7 @@ public function __construct(array $params = []) /** * Convert data in an array * - * @return array|null + * @return array|null */ public function serialize(Accessable $data): ?array { diff --git a/src/V1/Attributes.php b/src/V1/Attributes.php index 918dcf3..2f4a660 100644 --- a/src/V1/Attributes.php +++ b/src/V1/Attributes.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Attributes Object @@ -50,7 +51,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/Document.php b/src/V1/Document.php index ff3194d..4e971cb 100644 --- a/src/V1/Document.php +++ b/src/V1/Document.php @@ -9,9 +9,10 @@ namespace Art4\JsonApiClient\V1; use Art4\JsonApiClient\Accessable; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Document Top Level Object @@ -73,7 +74,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/DocumentLink.php b/src/V1/DocumentLink.php index 30ab3c3..4d0dc0b 100644 --- a/src/V1/DocumentLink.php +++ b/src/V1/DocumentLink.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Document Link Object @@ -103,7 +104,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/Error.php b/src/V1/Error.php index 69c2f7b..346cd55 100644 --- a/src/V1/Error.php +++ b/src/V1/Error.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Error Object @@ -105,7 +106,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ErrorCollection.php b/src/V1/ErrorCollection.php index 42def2f..b61e0f2 100644 --- a/src/V1/ErrorCollection.php +++ b/src/V1/ErrorCollection.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Error Collection Object @@ -44,7 +45,7 @@ protected function parse($object): void /** * Get a value by the key of this document * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ErrorLink.php b/src/V1/ErrorLink.php index 112d09b..6c08772 100644 --- a/src/V1/ErrorLink.php +++ b/src/V1/ErrorLink.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Error Link Object @@ -63,7 +64,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ErrorSource.php b/src/V1/ErrorSource.php index 3c70b99..e8136e2 100644 --- a/src/V1/ErrorSource.php +++ b/src/V1/ErrorSource.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Error Source Object @@ -52,7 +53,7 @@ protected function parse($object): void /** * Get a value by the key of this document * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/Jsonapi.php b/src/V1/Jsonapi.php index db56bef..6a5acbb 100644 --- a/src/V1/Jsonapi.php +++ b/src/V1/Jsonapi.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * JSON API Object @@ -48,7 +49,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/Link.php b/src/V1/Link.php index 1c07bae..5e585b1 100644 --- a/src/V1/Link.php +++ b/src/V1/Link.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Link Object @@ -44,7 +45,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ @@ -60,8 +61,7 @@ public function get($key) /** * Set a link * - * @param string $name The Name - * @param string|object $link The Link + * @param mixed $link The Link */ private function setAsLink(string $name, $link): void { diff --git a/src/V1/Meta.php b/src/V1/Meta.php index 5836bab..db1dcc1 100644 --- a/src/V1/Meta.php +++ b/src/V1/Meta.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Meta Object @@ -46,7 +47,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/Relationship.php b/src/V1/Relationship.php index 5a842cf..ec6b0a4 100644 --- a/src/V1/Relationship.php +++ b/src/V1/Relationship.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Resource Identifier Object @@ -59,7 +60,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/RelationshipCollection.php b/src/V1/RelationshipCollection.php index 49680f0..fd9c9dc 100644 --- a/src/V1/RelationshipCollection.php +++ b/src/V1/RelationshipCollection.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Relationship Collection Object @@ -54,7 +55,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/RelationshipLink.php b/src/V1/RelationshipLink.php index 84b1c59..3cb303f 100644 --- a/src/V1/RelationshipLink.php +++ b/src/V1/RelationshipLink.php @@ -11,6 +11,7 @@ use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Relationship Link Object @@ -105,7 +106,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ResourceCollection.php b/src/V1/ResourceCollection.php index 4577e2b..2b515e0 100644 --- a/src/V1/ResourceCollection.php +++ b/src/V1/ResourceCollection.php @@ -9,9 +9,10 @@ namespace Art4\JsonApiClient\V1; use Art4\JsonApiClient\Accessable; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Resource Object @@ -47,7 +48,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ResourceIdentifier.php b/src/V1/ResourceIdentifier.php index 26e6b2c..df39116 100644 --- a/src/V1/ResourceIdentifier.php +++ b/src/V1/ResourceIdentifier.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Resource Identifier Object @@ -59,7 +60,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ResourceIdentifierCollection.php b/src/V1/ResourceIdentifierCollection.php index a887070..e820d37 100644 --- a/src/V1/ResourceIdentifierCollection.php +++ b/src/V1/ResourceIdentifierCollection.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Resource Object @@ -42,7 +43,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ResourceItem.php b/src/V1/ResourceItem.php index caae85a..35c43b0 100644 --- a/src/V1/ResourceItem.php +++ b/src/V1/ResourceItem.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * Resource Identifier Object @@ -77,7 +78,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ResourceItemLink.php b/src/V1/ResourceItemLink.php index 69ce850..5315593 100644 --- a/src/V1/ResourceItemLink.php +++ b/src/V1/ResourceItemLink.php @@ -8,9 +8,10 @@ namespace Art4\JsonApiClient\V1; -use Art4\JsonApiClient\Helper\AbstractElement; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AbstractElement; +use Art4\JsonApiClient\Helper\AccessKey; /** * ItemLink Object @@ -40,7 +41,7 @@ protected function parse($object): void /** * Get a value by the key of this object * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return mixed The value */ diff --git a/src/V1/ResourceNull.php b/src/V1/ResourceNull.php index c7df660..3d48c84 100644 --- a/src/V1/ResourceNull.php +++ b/src/V1/ResourceNull.php @@ -12,17 +12,13 @@ use Art4\JsonApiClient\Element; use Art4\JsonApiClient\Manager; use Art4\JsonApiClient\Exception\AccessException; +use Art4\JsonApiClient\Helper\AccessKey; /** * Null Resource */ final class ResourceNull implements Accessable, Element { - /** @var mixed */ - private $data; - private Manager $manager; - private Accessable $parent; - /** * Constructor * @@ -30,22 +26,26 @@ final class ResourceNull implements Accessable, Element * @param \Art4\JsonApiClient\Manager $manager The manager * @param \Art4\JsonApiClient\Accessable $parent The parent */ - public function __construct($data, Manager $manager, Accessable $parent) - { - $this->data = $data; - $this->manager = $manager; - $this->parent = $parent; - } + public function __construct($data, Manager $manager, Accessable $parent) {} /** * Check if a value exists in this resource * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value * * @return bool false */ public function has($key) { + if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) { + trigger_error(sprintf( + '%s::has(): Providing Argument #1 ($key) as %s is deprecated since 1.2.0, please provide as int|string|%s instead.', + get_class($this), + gettype($key), + AccessKey::class + ), \E_USER_DEPRECATED); + } + return false; } @@ -62,7 +62,7 @@ public function getKeys() /** * Get a value by the key of this identifier * - * @param string $key The key of the value + * @param int|string|AccessKey $key The key of the value */ public function get($key): void { diff --git a/tests/Fixtures/Factory.php b/tests/Fixtures/Factory.php deleted file mode 100644 index b6d916c..0000000 --- a/tests/Fixtures/Factory.php +++ /dev/null @@ -1,35 +0,0 @@ -testcase - ->getMockBuilder('Art4\JsonApiClient\\' . $name . 'Interface') // Mock only the interfaces - ->disableOriginalConstructor() - ->disableOriginalClone() - ->disableArgumentCloning() - ->disallowMockingUnknownTypes() - ->getMock(); - } -} diff --git a/tests/Fixtures/HelperTrait.php b/tests/Fixtures/HelperTrait.php index 94cc0d1..aa0b604 100644 --- a/tests/Fixtures/HelperTrait.php +++ b/tests/Fixtures/HelperTrait.php @@ -11,23 +11,26 @@ use Art4\JsonApiClient\Accessable; use Art4\JsonApiClient\Factory; use Art4\JsonApiClient\Manager; -use Art4\JsonApiClient\Tests\Fixtures\Factory as FixtureFactory; +use PHPUnit\Framework\MockObject\MockObject; /** * Helper Trait */ trait HelperTrait { + /** @var Manager&MockObject */ protected Manager $manager; + /** @var Factory&MockObject */ protected Factory $factory; + /** @var Accessable&MockObject */ protected Accessable $parent; /** * Json Values Provider * - * @see http://json.org/ + * @return array> */ public static function jsonValuesProvider(): array { @@ -47,7 +50,7 @@ public static function jsonValuesProvider(): array /** * Json Values Provider but without the object * - * @see http://json.org/ + * @return array> */ public static function jsonValuesProviderWithoutObject(): array { @@ -61,7 +64,7 @@ public static function jsonValuesProviderWithoutObject(): array /** * Json Values Provider but without the array * - * @see http://json.org/ + * @return array> */ public static function jsonValuesProviderWithoutArray(): array { @@ -75,7 +78,7 @@ public static function jsonValuesProviderWithoutArray(): array /** * Json Values Provider but without the string * - * @see http://json.org/ + * @return array> */ public static function jsonValuesProviderWithoutString(): array { @@ -89,7 +92,7 @@ public static function jsonValuesProviderWithoutString(): array /** * Json Values Provider but without the object and string * - * @see http://json.org/ + * @return array> */ public static function jsonValuesProviderWithoutObjectAndString(): array { @@ -104,7 +107,7 @@ public static function jsonValuesProviderWithoutObjectAndString(): array /** * Json Values as string Provider * - * @see http://json.org/ + * @return array> */ public static function jsonValuesAsStringProvider(): array { @@ -124,7 +127,7 @@ public static function jsonValuesAsStringProvider(): array /** * Json Values as string Provider but without the object * - * @see http://json.org/ + * @return array> */ public static function jsonValuesAsStringProviderWithoutObject(): array { @@ -135,30 +138,6 @@ public static function jsonValuesAsStringProviderWithoutObject(): array return array_values($data); } - /** - * Builds a Manager Mock - */ - public function buildManagerMock() - { - // Mock factory - $factory = new FixtureFactory(); - $factory->testcase = $this; - - // Mock Manager - $manager = $this->createMock('Art4\JsonApiClient\Utils\FactoryManagerInterface'); - - $manager->expects($this->any()) - ->method('getFactory') - ->will($this->returnValue($factory)); - - $manager->expects($this->any()) - ->method('getConfig') - ->with('optional_item_id') - ->willReturn(false); - - return $manager; - } - /** * Builds a Manager Mock and set it into the TestCase */ @@ -185,8 +164,8 @@ public function setUpManagerMock(): void * * @param mixed $filename */ - protected function getJsonString($filename) + protected function getJsonString($filename): string { - return file_get_contents(__DIR__ . '/../files/' . $filename); + return strval(file_get_contents(__DIR__ . '/../files/' . $filename)); } } diff --git a/tests/Fixtures/V1Factory.php b/tests/Fixtures/V1Factory.php index 282ea05..835c047 100644 --- a/tests/Fixtures/V1Factory.php +++ b/tests/Fixtures/V1Factory.php @@ -8,31 +8,26 @@ namespace Art4\JsonApiClient\Tests\Fixtures; -use Art4\JsonApiClient\Element; use Art4\JsonApiClient\Factory as FactoryInterface; +use PHPUnit\Framework\TestCase; final class V1Factory implements FactoryInterface { - public $testcase; + public TestCase $testcase; /** * Create a factory - * - * @param object $testcase - * @param array $args - * - * @return object */ - public function __construct($testcase) + public function __construct(TestCase $testcase) { - return $this->testcase = $testcase; + $this->testcase = $testcase; } /** * Create a new instance of a class * * @param string $name - * @param array $args + * @param array $args * * @return object */ diff --git a/tests/Functional/ParsingTest.php b/tests/Functional/ParsingTest.php index 40565e1..7e4957c 100644 --- a/tests/Functional/ParsingTest.php +++ b/tests/Functional/ParsingTest.php @@ -22,6 +22,8 @@ class ParsingTest extends TestCase /** * Provide Parser + * + * @return array> */ public static function createParserProvider(): array { @@ -37,12 +39,9 @@ public static function createParserProvider(): array } /** - * @test * @dataProvider createParserProvider - * - * @param mixed $parser */ - public function testParseSimpleResourceWithDifferentParser($parser): void + public function testParseSimpleResourceWithDifferentParser(callable $parser): void { $string = $this->getJsonString('01_simple_resource.json'); $document = $parser($string); @@ -176,6 +175,7 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void $this->assertTrue($document->has('included')); $this->assertTrue($document->has('data')); + /** @var Accessable */ $resources = $document->get('data'); $this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceCollection', $resources); @@ -184,6 +184,8 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void $this->assertSame($resources->getKeys(), [0]); $this->assertTrue($resources->has(0)); + + /** @var Accessable */ $resource = $resources->get(0); $this->assertFalse($resource->has('meta')); @@ -193,24 +195,28 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void $this->assertTrue($resource->has('relationships')); $this->assertTrue($resource->has('links')); + /** @var Accessable */ $attributes = $resource->get('attributes'); $this->assertInstanceOf('Art4\JsonApiClient\V1\Attributes', $attributes); $this->assertTrue($attributes->has('title')); $this->assertSame($attributes->get('title'), 'JSON API paints my bikeshed!'); + /** @var Accessable */ $collection = $resource->get('relationships'); $this->assertInstanceOf('Art4\JsonApiClient\V1\RelationshipCollection', $collection); $this->assertTrue($collection->has('author')); $this->assertTrue($collection->has('comments')); + /** @var Accessable */ $author = $collection->get('author'); $this->assertInstanceOf('Art4\JsonApiClient\V1\Relationship', $author); $this->assertTrue($author->has('links')); $this->assertTrue($author->has('data')); + /** @var Accessable */ $links = $author->get('links'); $this->assertInstanceOf('Art4\JsonApiClient\V1\RelationshipLink', $links); @@ -219,18 +225,21 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void $this->assertTrue($links->has('related')); $this->assertSame($links->get('related'), 'http://example.com/articles/1/author'); + /** @var Accessable */ $data = $author->get('data'); $this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceIdentifier', $data); $this->assertSame($data->get('type'), 'people'); $this->assertSame($data->get('id'), '9'); + /** @var Accessable */ $comments = $collection->get('comments'); $this->assertInstanceOf('Art4\JsonApiClient\V1\Relationship', $comments); $this->assertTrue($comments->has('links')); $this->assertTrue($comments->has('data')); + /** @var Accessable */ $links = $comments->get('links'); $this->assertInstanceOf('Art4\JsonApiClient\V1\RelationshipLink', $links); @@ -239,35 +248,42 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void $this->assertTrue($links->has('related')); $this->assertSame($links->get('related'), 'http://example.com/articles/1/comments'); + /** @var Accessable */ $data_array = $comments->get('data'); $this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceIdentifierCollection', $data_array); $this->assertCount(2, $data_array->getKeys()); + /** @var Accessable */ $identifier = $data_array->get(0); $this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceIdentifier', $identifier); $this->assertSame($identifier->get('type'), 'comments'); $this->assertSame($identifier->get('id'), '5'); + /** @var Accessable */ $identifier = $data_array->get(1); $this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceIdentifier', $identifier); $this->assertSame($identifier->get('type'), 'comments'); $this->assertSame($identifier->get('id'), '12'); + /** @var Accessable */ $links = $resource->get('links'); $this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceItemLink', $links); $this->assertTrue($links->has('self')); $this->assertSame($links->get('self'), 'http://example.com/articles/1'); + /** @var Accessable */ $includes = $document->get('included'); $this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceCollection', $includes); $this->assertSame($includes->getKeys(), [0, 1, 2]); $this->assertTrue($includes->has(0)); + + /** @var Accessable */ $include = $includes->get(0); $this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceItem', $include); @@ -276,6 +292,7 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void $this->assertTrue($include->has('attributes')); $this->assertTrue($include->has('links')); + /** @var Accessable */ $attributes = $include->get('attributes'); $this->assertInstanceOf('Art4\JsonApiClient\V1\Attributes', $attributes); @@ -286,6 +303,7 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void $this->assertTrue($attributes->has('twitter')); $this->assertSame($attributes->get('twitter'), 'dgeb'); + /** @var Accessable */ $links = $include->get('links'); $this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceItemLink', $links); @@ -293,6 +311,8 @@ public function testParseCompleteResourceObjectWithMultipleRelationships(): void $this->assertSame($links->get('self'), 'http://example.com/people/9'); $this->assertTrue($includes->has(1)); + + /** @var Accessable */ $include = $includes->get(1); $this->assertInstanceOf('Art4\JsonApiClient\V1\ResourceItem', $include); diff --git a/tests/Functional/SerializerTest.php b/tests/Functional/SerializerTest.php index f96c043..c835969 100644 --- a/tests/Functional/SerializerTest.php +++ b/tests/Functional/SerializerTest.php @@ -14,6 +14,7 @@ use Art4\JsonApiClient\Serializer\ArraySerializer; use Art4\JsonApiClient\Tests\Fixtures\HelperTrait; use Art4\JsonApiClient\V1\Factory; +use Exception; use PHPUnit\Framework\TestCase; class SerializerTest extends TestCase @@ -22,6 +23,8 @@ class SerializerTest extends TestCase /** * Provide JSON API data + * + * @return array> */ public static function jsonapiDataProvider(): array { @@ -33,7 +36,13 @@ public static function jsonapiDataProvider(): array '15_create_resource_without_id.json', ]; - foreach (glob($path . '*.json') as $file) { + $filenames = glob($path . '*.json'); + + if ($filenames === false) { + throw new Exception('Error while getting all json files.'); + } + + foreach ($filenames as $file) { $filename = str_replace($path, '', $file); // Ignore files with errors @@ -45,9 +54,7 @@ public static function jsonapiDataProvider(): array $files[] = [ $filename, - [ - 'is_request' => in_array($filename, $requestFiles), - ], + in_array($filename, $requestFiles), ]; } @@ -55,18 +62,15 @@ public static function jsonapiDataProvider(): array } /** - * @test * @dataProvider jsonapiDataProvider - * - * @param mixed $filename */ - public function parseJsonapiDataWithErrorAbortManager($filename, array $meta): void + public function testParseJsonapiDataWithErrorAbortManager(string $filename, bool $isRequest): void { $manager = new ErrorAbortManager(new Factory()); $string = $this->getJsonString($filename); - if ($meta['is_request']) { + if ($isRequest) { $input = new RequestStringInput($string); } else { $input = new ResponseStringInput($string); diff --git a/tests/Unit/Helper/AccessableTraitTest.php b/tests/Unit/Helper/AccessableTraitTest.php new file mode 100644 index 0000000..cda7d40 --- /dev/null +++ b/tests/Unit/Helper/AccessableTraitTest.php @@ -0,0 +1,60 @@ +getMockForTrait(AccessableTrait::class); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertStringEndsWith( + '::has(): Providing Argument #1 ($key) as object is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', + $errstr + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED + ); + + $resource->has(new \stdClass()); + } + + public function testHasWithArrayAsKeyTriggersException(): void + { + /** @var Accessable */ + $resource = $this->getMockForTrait(AccessableTrait::class); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertStringEndsWith( + '::has(): Providing Argument #1 ($key) as array is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', + $errstr + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED + ); + + $resource->has([]); + } +} diff --git a/tests/Unit/Input/RequestStringInputTest.php b/tests/Unit/Input/RequestStringInputTest.php index 5256afa..f9d49e7 100644 --- a/tests/Unit/Input/RequestStringInputTest.php +++ b/tests/Unit/Input/RequestStringInputTest.php @@ -17,9 +17,6 @@ class RequestStringInputTest extends TestCase { use HelperTrait; - /** - * @test - */ public function testGetAsObjectFromStringReturnsObject(): void { $input = new RequestStringInput('{}'); @@ -29,7 +26,6 @@ public function testGetAsObjectFromStringReturnsObject(): void /** * @dataProvider jsonValuesProviderWithoutString - * @test * * @param mixed $input */ @@ -44,9 +40,6 @@ public function testCreateWithoutStringThrowsException($input): void /** * @dataProvider jsonValuesAsStringProviderWithoutObject - * @test - * - * @param string $input */ public function testGetAsObjectWithInvalidStringsThrowsException(string $input): void { diff --git a/tests/Unit/Input/ResponseStringInputTest.php b/tests/Unit/Input/ResponseStringInputTest.php index c554f1b..8b6f24c 100644 --- a/tests/Unit/Input/ResponseStringInputTest.php +++ b/tests/Unit/Input/ResponseStringInputTest.php @@ -17,9 +17,6 @@ class ResponseStringInputTest extends TestCase { use HelperTrait; - /** - * @test - */ public function testGetAsObjectFromStringReturnsObject(): void { $input = new ResponseStringInput('{}'); @@ -29,7 +26,6 @@ public function testGetAsObjectFromStringReturnsObject(): void /** * @dataProvider jsonValuesProviderWithoutString - * @test * * @param mixed $input */ @@ -44,9 +40,6 @@ public function testCreateWithoutStringThrowsException($input): void /** * @dataProvider jsonValuesAsStringProviderWithoutObject - * @test - * - * @param string $input */ public function testGetAsObjectWithInvalidStringsThrowsException(string $input): void { diff --git a/tests/Unit/V1/ErrorCollectionTest.php b/tests/Unit/V1/ErrorCollectionTest.php index 31b2731..eadff7d 100644 --- a/tests/Unit/V1/ErrorCollectionTest.php +++ b/tests/Unit/V1/ErrorCollectionTest.php @@ -11,6 +11,7 @@ use Art4\JsonApiClient\Accessable; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AccessKey; use Art4\JsonApiClient\Tests\Fixtures\HelperTrait; use Art4\JsonApiClient\V1\ErrorCollection; use PHPUnit\Framework\TestCase; @@ -47,10 +48,10 @@ public function testCreate(): void $this->assertSame($collection->getKeys(), [0, 1]); - $this->assertFalse($collection->has(new \stdClass())); - $this->assertFalse($collection->has([])); $this->assertFalse($collection->has('string')); + $this->assertTrue($collection->has(AccessKey::create(0))); + $this->assertTrue($collection->has(0)); $error = $collection->get(0); diff --git a/tests/Unit/V1/ResourceCollectionTest.php b/tests/Unit/V1/ResourceCollectionTest.php index 67d00e1..aed1f05 100644 --- a/tests/Unit/V1/ResourceCollectionTest.php +++ b/tests/Unit/V1/ResourceCollectionTest.php @@ -11,6 +11,7 @@ use Art4\JsonApiClient\Accessable; use Art4\JsonApiClient\Exception\AccessException; use Art4\JsonApiClient\Exception\ValidationException; +use Art4\JsonApiClient\Helper\AccessKey; use Art4\JsonApiClient\Tests\Fixtures\HelperTrait; use Art4\JsonApiClient\V1\ResourceCollection; use PHPUnit\Framework\TestCase; @@ -41,11 +42,10 @@ public function testCreateWithEmptyArray(): void $this->assertInstanceOf(Accessable::class, $collection); $this->assertSame($collection->getKeys(), []); - $this->assertFalse($collection->has(0)); // Test get() with various key types - $this->assertFalse($collection->has(new \stdClass())); - $this->assertFalse($collection->has([])); + $this->assertFalse($collection->has(0)); + $this->assertFalse($collection->has(AccessKey::create(0))); $this->assertFalse($collection->has('string')); } diff --git a/tests/Unit/V1/ResourceNullTest.php b/tests/Unit/V1/ResourceNullTest.php index cbaf377..b59c69e 100644 --- a/tests/Unit/V1/ResourceNullTest.php +++ b/tests/Unit/V1/ResourceNullTest.php @@ -64,4 +64,54 @@ public function testGetThrowsException(): void $resource->get('something'); } + + public function testHasWithObjectAsKeyTriggersException(): void + { + $resource = new ResourceNull( + null, + $this->manager, + $this->parent + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + 'Art4\JsonApiClient\V1\ResourceNull::has(): Providing Argument #1 ($key) as object is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', + $errstr + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED + ); + + $resource->has(new \stdClass()); + } + + public function testHasWithArrayAsKeyTriggersException(): void + { + $resource = new ResourceNull( + null, + $this->manager, + $this->parent + ); + + // PHPUnit 10 compatible way to test trigger_error(). + set_error_handler( + function ($errno, $errstr): bool { + $this->assertSame( + 'Art4\JsonApiClient\V1\ResourceNull::has(): Providing Argument #1 ($key) as array is deprecated since 1.2.0, please provide as int|string|Art4\JsonApiClient\Helper\AccessKey instead.', + $errstr + ); + + restore_error_handler(); + return true; + }, + E_USER_DEPRECATED + ); + + $resource->has([]); + } }