diff --git a/.phpstan.neon b/.phpstan.neon index 5567202..5199950 100644 --- a/.phpstan.neon +++ b/.phpstan.neon @@ -76,3 +76,15 @@ parameters: 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\\\\Tests\\\\Fixtures\\\\AccessableTraitMock\\:\\:get\\(\\) expects Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey\\|int\\|string, mixed given\\.$#" + count: 1 + path: tests/Unit/Helper/AccessableTraitTest.php + # We are providing an invalid parameter to test the deprecation message + + - + message: "#^Parameter \\#1 \\$key of method Art4\\\\JsonApiClient\\\\Tests\\\\Fixtures\\\\AccessableTraitMock\\:\\:has\\(\\) expects Art4\\\\JsonApiClient\\\\Helper\\\\AccessKey\\|int\\|string, mixed given\\.$#" + count: 1 + path: tests/Unit/Helper/AccessableTraitTest.php + # We are providing an invalid parameter to test the deprecation message diff --git a/CHANGELOG.md b/CHANGELOG.md index fae9c7d..30cab1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Add support for PHP 8.2 +### Deprecated + +- Providing Argument #1 ($key) in `\Art4\JsonApiClient\Accessable::has()` not as `int|string` is deprecated and will throw an Exception in 2.0.0. +- Providing Argument #1 ($key) in `\Art4\JsonApiClient\Accessable::get()` not as `int|string` is deprecated and will throw an Exception in 2.0.0. + ## [1.1.0 - 2021-10-05](https://github.com/Art4/json-api-client/compare/1.0.0...1.1.0) ### Added @@ -25,16 +30,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Deprecated -- `\Art4\JsonApiClient\Accessable::has()` will add `bool` as a native return type declaration in v2.0, do the same in your implementation now to avoid errors. -- `\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. -- `\Art4\JsonApiClient\Exception\Exception` will extend `\Throwable` in v2.0, do the same in your implementation now to avoid errors. -- `\Art4\JsonApiClient\Factory::make()` methods first parameter signature will be `string` in v2.0. -- `\Art4\JsonApiClient\Factory::make()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0, do the same in your implementation now to avoid errors. -- `\Art4\JsonApiClient\Input\Input::getAsObject()` will add `\stdClass` as a native return type declaration in v2.0, do the same in your implementation now to avoid errors. -- `\Art4\JsonApiClient\Manager::parse()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0, do the same in your implementation now to avoid errors. -- `\Art4\JsonApiClient\Manager::getFactory()` will add `\Art4\JsonApiClient\Factory` as a native return type declaration in v2.0, do the same in your implementation now to avoid errors. -- `\Art4\JsonApiClient\Manager::getParam()` methods first parameter signature will be `string` in v2.0. -- `\Art4\JsonApiClient\Serializer\Serializer::serialize()` will add `?array` as a native return type declaration in v2.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Accessable::has()` will add `bool` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Accessable::getKeys()` will add `array` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Exception\Exception` will extend `\Throwable` in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Factory::make()` methods first parameter signature will be `string` in 2.0.0. +- `\Art4\JsonApiClient\Factory::make()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Input\Input::getAsObject()` will add `\stdClass` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Manager::parse()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Manager::getFactory()` will add `\Art4\JsonApiClient\Factory` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. +- `\Art4\JsonApiClient\Manager::getParam()` methods first parameter signature will be `string` in 2.0.0. +- `\Art4\JsonApiClient\Serializer\Serializer::serialize()` will add `?array` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. ## [1.0.0 - 2021-03-05](https://github.com/Art4/json-api-client/compare/0.10.2...1.0.0) diff --git a/src/Accessable.php b/src/Accessable.php index cf63145..11f3c93 100644 --- a/src/Accessable.php +++ b/src/Accessable.php @@ -27,22 +27,20 @@ public function get($key); /** * Check if a value exists * - * @deprecated `\Art4\JsonApiClient\Accessable::has()` will add `bool` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change bool `\Art4\JsonApiClient\Accessable::has()` will add `bool` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * @param mixed $key The key * * @return bool */ - public function has($key); - // public function has($key): bool; + public function has($key)/*: bool */; /** * Returns the keys of all setted values * - * @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-type-will-change array `\Art4\JsonApiClient\Accessable::getKeys()` will add `array` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * @return array Keys of all setted values */ - public function getKeys(); - // public function getKeys(): array; + public function getKeys()/*: array */; } diff --git a/src/Exception/AccessException.php b/src/Exception/AccessException.php index 9edf22e..ee57d2e 100644 --- a/src/Exception/AccessException.php +++ b/src/Exception/AccessException.php @@ -8,4 +8,4 @@ namespace Art4\JsonApiClient\Exception; -class AccessException extends \RuntimeException implements Exception, \Throwable {} +class AccessException extends \RuntimeException implements Exception {} diff --git a/src/Exception/Exception.php b/src/Exception/Exception.php index 0e88f54..ffdeb7c 100644 --- a/src/Exception/Exception.php +++ b/src/Exception/Exception.php @@ -9,7 +9,7 @@ namespace Art4\JsonApiClient\Exception; /** - * @deprecated `\Art4\JsonApiClient\Exception\Exception` will extend `\Throwable` in v2.0. Do the same in your implementation now to avoid errors. + * Please note: `\Art4\JsonApiClient\Exception\Exception` will extend `\Throwable` in 2.0.0, do the same in your implementation now to avoid errors. */ -// interface Exception extends \Throwable -interface Exception {} +interface Exception /*extends \Throwable */ +{} diff --git a/src/Exception/FactoryException.php b/src/Exception/FactoryException.php index 3b8ffbb..be7cb9c 100644 --- a/src/Exception/FactoryException.php +++ b/src/Exception/FactoryException.php @@ -8,4 +8,4 @@ namespace Art4\JsonApiClient\Exception; -class FactoryException extends \Exception implements Exception, \Throwable {} +class FactoryException extends \Exception implements Exception {} diff --git a/src/Exception/InputException.php b/src/Exception/InputException.php index b59fe12..5ec8cc6 100644 --- a/src/Exception/InputException.php +++ b/src/Exception/InputException.php @@ -8,4 +8,4 @@ namespace Art4\JsonApiClient\Exception; -class InputException extends \Exception implements Exception, \Throwable {} +class InputException extends \Exception implements Exception {} diff --git a/src/Exception/ValidationException.php b/src/Exception/ValidationException.php index 771e20a..4273774 100644 --- a/src/Exception/ValidationException.php +++ b/src/Exception/ValidationException.php @@ -8,4 +8,4 @@ namespace Art4\JsonApiClient\Exception; -class ValidationException extends \InvalidArgumentException implements Exception, \Throwable {} +class ValidationException extends \InvalidArgumentException implements Exception {} diff --git a/src/Factory.php b/src/Factory.php index 7dd1ed9..d25a5c1 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -16,14 +16,11 @@ interface Factory /** * Create a new instance of a class * - * @deprecated The `\Art4\JsonApiClient\Factory::make()` methods first parameter signature will be `string` in v2.0. - * @deprecated `\Art4\JsonApiClient\Factory::make()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change Accessable `\Art4\JsonApiClient\Factory::make()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * - * @param string $name - * @param array $args + * @param array $args * * @return \Art4\JsonApiClient\Accessable */ - public function make($name, array $args = []); - // public function make(string $name, array $args = []): Accessable; + public function make(string $name, array $args = [])/*: Accessable */; } diff --git a/src/Helper/AccessKey.php b/src/Helper/AccessKey.php index bed0120..fbc7a31 100644 --- a/src/Helper/AccessKey.php +++ b/src/Helper/AccessKey.php @@ -22,31 +22,20 @@ final class AccessKey extends SplStack /** * Transforms the Key to a string * - * @param int|string $key - * * @return AccessKey */ - public static function create($key): AccessKey + public static function create(string $key): AccessKey { - // Ignore arrays and objects - if (is_object($key) or is_array($key)) { - $key = ''; - } - - $key_string = strval($key); - - $key = new self(); - $key->raw = $key_string; - - $keys = explode('.', $key_string); + $accessKey = new self(); + $accessKey->raw = $key; - foreach ($keys as $value) { - $key->push($value); + foreach (explode('.', $key) as $value) { + $accessKey->push($value); } - $key->rewind(); + $accessKey->rewind(); - return $key; + return $accessKey; } /** diff --git a/src/Helper/AccessableTrait.php b/src/Helper/AccessableTrait.php index 6cee963..4a6a6ce 100644 --- a/src/Helper/AccessableTrait.php +++ b/src/Helper/AccessableTrait.php @@ -57,11 +57,12 @@ 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.', + '%s::has(): Providing Argument #1 ($key) as `%s` is deprecated since 1.2.0, please provide as `int|string` instead.', get_class($this), - gettype($key), - AccessKey::class + gettype($key) ), \E_USER_DEPRECATED); + + $key = ''; } $key = $this->parseKey($key); @@ -97,6 +98,16 @@ final public function has($key): bool */ public function get($key) { + if (! is_int($key) && ! is_string($key) && (! is_object($key) || ! $key instanceof AccessKey)) { + trigger_error(sprintf( + '%s::get(): Providing Argument #1 ($key) as `%s` is deprecated since 1.2.0, please provide as `int|string` instead.', + get_class($this), + gettype($key) + ), \E_USER_DEPRECATED); + + $key = ''; + } + $key = $this->parseKey($key); $string = $key->shift(); @@ -136,8 +147,6 @@ private function getValue(string $key) * Parse a dot.notated.key to an object * * @param int|string|AccessKey $key The key - * - * @return AccessKey The parsed key */ private function parseKey($key): AccessKey { @@ -145,8 +154,6 @@ private function parseKey($key): AccessKey return $key; } - $key = AccessKey::create($key); - - return $key; + return AccessKey::create(strval($key)); } } diff --git a/src/Helper/RootAccessable.php b/src/Helper/RootAccessable.php index 78c46ba..2983ed9 100644 --- a/src/Helper/RootAccessable.php +++ b/src/Helper/RootAccessable.php @@ -9,8 +9,6 @@ namespace Art4\JsonApiClient\Helper; use Art4\JsonApiClient\Accessable; -use Art4\JsonApiClient\Element; -use Art4\JsonApiClient\Manager; /** * RootAccessable diff --git a/src/Input/Input.php b/src/Input/Input.php index 457a534..bb168c4 100644 --- a/src/Input/Input.php +++ b/src/Input/Input.php @@ -16,7 +16,7 @@ interface Input /** * Get the input as simple object * - * @deprecated `\Art4\JsonApiClient\Input\Input::getAsObject()` will add `\stdClass` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change \stdClass `\Art4\JsonApiClient\Input\Input::getAsObject()` will add `\stdClass` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * This should be a native PHP stdClass object, so Manager could * iterate over all public attributes @@ -25,6 +25,5 @@ interface Input * * @return \stdClass */ - public function getAsObject(); - // public function getAsObject(): \stdClass; + public function getAsObject()/*: \stdClass */; } diff --git a/src/Manager.php b/src/Manager.php index b22dde4..0610da0 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -18,36 +18,30 @@ interface Manager /** * Parse the input * - * @deprecated `\Art4\JsonApiClient\Manager::parse()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change Accessable `\Art4\JsonApiClient\Manager::parse()` will add `\Art4\JsonApiClient\Accessable` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * @throws \Art4\JsonApiClient\Exception\InputException If $input contains invalid JSON API * @throws \Art4\JsonApiClient\Exception\ValidationException If $input contains invalid JSON API * * @return \Art4\JsonApiClient\Accessable */ - public function parse(Input $input); - // public function parse(Input $input): Accessable; + public function parse(Input $input)/*: Accessable */; /** * Get a factory from the manager * - * @deprecated `\Art4\JsonApiClient\Manager::getFactory()` will add `\Art4\JsonApiClient\Factory` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change Factory `\Art4\JsonApiClient\Manager::getFactory()` will add `\Art4\JsonApiClient\Factory` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * @return \Art4\JsonApiClient\Factory */ - public function getFactory(); - // public function getFactory(): Factory; + public function getFactory()/*: Factory */; /** * Get a param by key * - * @deprecated The `\Art4\JsonApiClient\Manager::getParam()` methods first parameter signature will be `string` in v2.0. - * - * @param string $key - * @param mixed $default + * @param mixed $default * * @return mixed */ - public function getParam($key, $default); - // public function getParam(string $key, $default); + public function getParam(string $key, $default); } diff --git a/src/Manager/ErrorAbortManager.php b/src/Manager/ErrorAbortManager.php index 996d3c5..db8567c 100644 --- a/src/Manager/ErrorAbortManager.php +++ b/src/Manager/ErrorAbortManager.php @@ -79,12 +79,11 @@ public function getFactory(): Factory /** * Get a param by key * - * @param string $key - * @param mixed $default + * @param mixed $default * * @return mixed */ - public function getParam($key, $default) + public function getParam(string $key, $default) { if (array_key_exists($key, $this->config)) { return $this->config[$key]; diff --git a/src/Serializer/Serializer.php b/src/Serializer/Serializer.php index 823ebe1..3801cbd 100644 --- a/src/Serializer/Serializer.php +++ b/src/Serializer/Serializer.php @@ -15,9 +15,9 @@ interface Serializer /** * Serialize data * - * @deprecated `\Art4\JsonApiClient\Serializer\Serializer::serialize()` will add `?array` as a native return type declaration in v2.0. Do the same in your implementation now to avoid errors. + * @return-type-will-change ?array `\Art4\JsonApiClient\Serializer\Serializer::serialize()` will add `?array` as a native return type declaration in 2.0.0, do the same in your implementation now to avoid errors. * * @return array|null */ - public function serialize(Accessable $data); + public function serialize(Accessable $data)/*: ?array */; } diff --git a/src/V1/Factory.php b/src/V1/Factory.php index 78dc1dc..33efadf 100644 --- a/src/V1/Factory.php +++ b/src/V1/Factory.php @@ -55,10 +55,9 @@ public function __construct(array $overload = []) /** * Create a new instance of a class * - * @param string $name - * @param array $args + * @param array $args */ - public function make($name, array $args = []): Accessable + public function make(string $name, array $args = []): Accessable { if (! isset($this->classes[$name])) { throw new FactoryException('"' . $name . '" is not a registered class'); diff --git a/src/V1/ResourceItemLink.php b/src/V1/ResourceItemLink.php index 5315593..cf31ef6 100644 --- a/src/V1/ResourceItemLink.php +++ b/src/V1/ResourceItemLink.php @@ -57,7 +57,6 @@ public function get($key) /** * Set a link * - * @param string $name The Name * @param string|object|mixed $link The Link */ private function setLink(string $name, $link): void diff --git a/src/V1/ResourceNull.php b/src/V1/ResourceNull.php index 3d48c84..d687293 100644 --- a/src/V1/ResourceNull.php +++ b/src/V1/ResourceNull.php @@ -35,14 +35,13 @@ public function __construct($data, Manager $manager, Accessable $parent) {} * * @return bool false */ - public function has($key) + 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.', + '%s::has(): Providing Argument #1 ($key) as `%s` is deprecated since 1.2.0, please provide as `int|string` instead.', get_class($this), - gettype($key), - AccessKey::class + gettype($key) ), \E_USER_DEPRECATED); } @@ -54,7 +53,7 @@ public function has($key) * * @return array Keys of all setted values */ - public function getKeys() + public function getKeys(): array { return []; } diff --git a/tests/BC/AccessableTest.php b/tests/BC/AccessableTest.php index 7ee74b8..2a0d12c 100644 --- a/tests/BC/AccessableTest.php +++ b/tests/BC/AccessableTest.php @@ -15,11 +15,14 @@ class AccessableTest extends TestCase { /** * This test will test a custom implementation of the Accessable interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForAccessableInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements Accessable { /** * Get a value by a key diff --git a/tests/BC/ElementTest.php b/tests/BC/ElementTest.php index 73c28e3..c48e5ca 100644 --- a/tests/BC/ElementTest.php +++ b/tests/BC/ElementTest.php @@ -17,8 +17,6 @@ class ElementTest extends TestCase { /** * This test will test a custom implementation of the Element interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForElementInterface(): void { @@ -26,6 +24,11 @@ public function testBcForElementInterface(): void $manager = $this->createMock(Manager::class); $parent = $this->createMock(Accessable::class); + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class ($data, $manager, $parent) implements Element { /** * Sets the manager and parent diff --git a/tests/BC/Exception/ExceptionTest.php b/tests/BC/Exception/ExceptionTest.php index c84d828..495427c 100644 --- a/tests/BC/Exception/ExceptionTest.php +++ b/tests/BC/Exception/ExceptionTest.php @@ -15,11 +15,14 @@ class ExceptionTest extends TestCase { /** * This test will test a custom implementation of the Exception interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForExceptionInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements Exception {}; $this->assertInstanceOf(Exception::class, $class); diff --git a/tests/BC/FactoryTest.php b/tests/BC/FactoryTest.php index beb1ecd..253983f 100644 --- a/tests/BC/FactoryTest.php +++ b/tests/BC/FactoryTest.php @@ -15,11 +15,14 @@ class FactoryTest extends TestCase { /** * This test will test a custom implementation of the Factory interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForFactoryInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements Factory { /** * Create a new instance of a class diff --git a/tests/BC/Input/InputTest.php b/tests/BC/Input/InputTest.php index 1e0d0c7..16664cb 100644 --- a/tests/BC/Input/InputTest.php +++ b/tests/BC/Input/InputTest.php @@ -15,11 +15,14 @@ class InputTest extends TestCase { /** * This test will test a custom implementation of the Input interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForInputInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements Input { /** * Get the input as simple object diff --git a/tests/BC/Input/RequestInputTest.php b/tests/BC/Input/RequestInputTest.php index c52b939..ffc76b9 100644 --- a/tests/BC/Input/RequestInputTest.php +++ b/tests/BC/Input/RequestInputTest.php @@ -15,11 +15,14 @@ class RequestInputTest extends TestCase { /** * This test will test a custom implementation of the RequestInput interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForRequestInputInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements RequestInput {}; $this->assertInstanceOf(RequestInput::class, $class); diff --git a/tests/BC/ManagerTest.php b/tests/BC/ManagerTest.php index 34005a8..573dfd1 100644 --- a/tests/BC/ManagerTest.php +++ b/tests/BC/ManagerTest.php @@ -16,11 +16,14 @@ class ManagerTest extends TestCase { /** * This test will test a custom implementation of the Manager interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForManagerInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements Manager { /** * Parse the input diff --git a/tests/BC/Serializer/SerializerTest.php b/tests/BC/Serializer/SerializerTest.php index 6ce99fd..ee82bca 100644 --- a/tests/BC/Serializer/SerializerTest.php +++ b/tests/BC/Serializer/SerializerTest.php @@ -16,11 +16,14 @@ class SerializerTest extends TestCase { /** * This test will test a custom implementation of the Serializer interface. - * DO NOT CHANGE THIS! - * Changes are only allowed by increasing the major version number. */ public function testBcForSerializerInterface(): void { + /** + * DO NOT CHANGE THIS CLASS! + * + * This anonymous class represents an implementation in user code + */ $class = new class () implements Serializer { /** * Serialize data diff --git a/tests/Fixtures/AccessableTraitMock.php b/tests/Fixtures/AccessableTraitMock.php new file mode 100644 index 0000000..c789b86 --- /dev/null +++ b/tests/Fixtures/AccessableTraitMock.php @@ -0,0 +1,17 @@ +> + */ + public static function jsonValuesProviderWithoutStringAndInt(): array + { + $data = static::jsonValuesProvider(); + + unset($data[2]); + unset($data[3]); + + return array_values($data); + } + /** * Json Values as string Provider * diff --git a/tests/Unit/Helper/AccessableTraitTest.php b/tests/Unit/Helper/AccessableTraitTest.php index cda7d40..f52ef81 100644 --- a/tests/Unit/Helper/AccessableTraitTest.php +++ b/tests/Unit/Helper/AccessableTraitTest.php @@ -8,22 +8,29 @@ namespace Art4\JsonApiClient\Tests\Unit\Helper; -use Art4\JsonApiClient\Helper\AccessableTrait; -use Art4\JsonApiClient\Accessable; +use Art4\JsonApiClient\Exception\AccessException; +use Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock; +use Art4\JsonApiClient\Tests\Fixtures\HelperTrait; use PHPUnit\Framework\TestCase; class AccessableTraitTest extends TestCase { - public function testHasWithObjectAsKeyTriggersException(): void + use HelperTrait; + + /** + * @dataProvider jsonValuesProviderWithoutStringAndInt + * + * @param mixed $key + */ + public function testHasWithInvalidKeyTypeTriggersDeprecationError($key): void { - /** @var Accessable */ - $resource = $this->getMockForTrait(AccessableTrait::class); + $resource = new AccessableTraitMock(); // 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.', + function ($errno, $errstr) use ($key): bool { + $this->assertSame( + 'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::has(): Providing Argument #1 ($key) as `' . gettype($key) . '` is deprecated since 1.2.0, please provide as `int|string` instead.', $errstr ); @@ -33,19 +40,23 @@ function ($errno, $errstr): bool { E_USER_DEPRECATED ); - $resource->has(new \stdClass()); + $resource->has($key); } - public function testHasWithArrayAsKeyTriggersException(): void + /** + * @dataProvider jsonValuesProviderWithoutStringAndInt + * + * @param mixed $key + */ + public function testGetWithInvalidKeyTypeTriggersDeprecationError($key): void { - /** @var Accessable */ - $resource = $this->getMockForTrait(AccessableTrait::class); + $resource = new AccessableTraitMock(); // 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.', + function ($errno, $errstr) use ($key): bool { + $this->assertSame( + 'Art4\JsonApiClient\Tests\Fixtures\AccessableTraitMock::get(): Providing Argument #1 ($key) as `' . gettype($key) . '` is deprecated since 1.2.0, please provide as `int|string` instead.', $errstr ); @@ -55,6 +66,10 @@ function ($errno, $errstr): bool { E_USER_DEPRECATED ); - $resource->has([]); + try { + $resource->get($key); + } catch (AccessException $th) { + // ignore AccessException + } } } diff --git a/tests/Unit/V1/ErrorCollectionTest.php b/tests/Unit/V1/ErrorCollectionTest.php index eadff7d..e6fc4a1 100644 --- a/tests/Unit/V1/ErrorCollectionTest.php +++ b/tests/Unit/V1/ErrorCollectionTest.php @@ -49,15 +49,15 @@ public function testCreate(): void $this->assertSame($collection->getKeys(), [0, 1]); $this->assertFalse($collection->has('string')); - - $this->assertTrue($collection->has(AccessKey::create(0))); - + $this->assertTrue($collection->has(AccessKey::create('0'))); $this->assertTrue($collection->has(0)); + $error = $collection->get(0); $this->assertInstanceOf(Accessable::class, $error); $this->assertTrue($collection->has(1)); + $error = $collection->get(1); $this->assertInstanceOf(Accessable::class, $error); diff --git a/tests/Unit/V1/ResourceCollectionTest.php b/tests/Unit/V1/ResourceCollectionTest.php index aed1f05..9955a9d 100644 --- a/tests/Unit/V1/ResourceCollectionTest.php +++ b/tests/Unit/V1/ResourceCollectionTest.php @@ -45,7 +45,7 @@ public function testCreateWithEmptyArray(): void // Test get() with various key types $this->assertFalse($collection->has(0)); - $this->assertFalse($collection->has(AccessKey::create(0))); + $this->assertFalse($collection->has(AccessKey::create('0'))); $this->assertFalse($collection->has('string')); } @@ -95,6 +95,9 @@ public function testCreateWithIdentifierAndMeta(): void $this->assertInstanceOf(Accessable::class, $resource); + $this->assertSame($resource, $collection->get('0')); + $this->assertSame($resource, $collection->get(AccessKey::create('0'))); + $this->assertTrue($collection->has(1)); $resource = $collection->get(1); diff --git a/tests/Unit/V1/ResourceNullTest.php b/tests/Unit/V1/ResourceNullTest.php index b59c69e..b476e3d 100644 --- a/tests/Unit/V1/ResourceNullTest.php +++ b/tests/Unit/V1/ResourceNullTest.php @@ -77,7 +77,7 @@ public function testHasWithObjectAsKeyTriggersException(): void 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.', + 'Art4\JsonApiClient\V1\ResourceNull::has(): Providing Argument #1 ($key) as `object` is deprecated since 1.2.0, please provide as `int|string` instead.', $errstr ); @@ -102,7 +102,7 @@ public function testHasWithArrayAsKeyTriggersException(): void 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.', + 'Art4\JsonApiClient\V1\ResourceNull::has(): Providing Argument #1 ($key) as `array` is deprecated since 1.2.0, please provide as `int|string` instead.', $errstr );