From 54e0d849473b732ba00d29e4e32d53fa67c1a3e4 Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sun, 12 Mar 2023 18:20:02 +0000 Subject: [PATCH] tests: add tests for json hash casting --- CHANGELOG.md | 7 ++++++ src/Core/Json/Hash.php | 8 +++--- tests/Unit/Json/HashTest.php | 49 ++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7376b90..70f1d9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/). +## Unreleased + +### Added + +- [#9](https://github.com/laravel-json-api/core/pull/9) Can now cast a `stdClass` object to a `Hash` via + the `Hash::cast()` method. + ## [3.0.0] - 2023-02-14 ### Changed diff --git a/src/Core/Json/Hash.php b/src/Core/Json/Hash.php index 404c881..6929973 100644 --- a/src/Core/Json/Hash.php +++ b/src/Core/Json/Hash.php @@ -24,17 +24,17 @@ use Illuminate\Contracts\Support\Arrayable; use IteratorAggregate; use JsonSerializable; +use stdClass; use function collect; class Hash implements ArrayAccess, Arrayable, Countable, IteratorAggregate, JsonSerializable { - use Concerns\Hashable; /** * Cast a value to a JSON hash. * - * @param Hash|JsonSerializable|array|null $value + * @param Hash|JsonSerializable|array|stdClass|null $value * @return static */ public static function cast($value): self @@ -47,11 +47,11 @@ public static function cast($value): self $value = $value->jsonSerialize(); } - if ($value instanceof \stdClass) { + if ($value instanceof stdClass) { $value = (array) $value; } - if (is_array($value) || is_null($value)) { + if (is_array($value) || null === $value) { return new self($value); } diff --git a/tests/Unit/Json/HashTest.php b/tests/Unit/Json/HashTest.php index 6e79dd8..93ed85e 100644 --- a/tests/Unit/Json/HashTest.php +++ b/tests/Unit/Json/HashTest.php @@ -24,6 +24,55 @@ class HashTest extends TestCase { + public function testCastHash(): void + { + $hash = new Hash(['foo' => 'bar']); + + $this->assertSame($hash, Hash::cast($hash)); + } + + public function testCastJsonSerializable(): void + { + $object = new class() implements \JsonSerializable { + public function jsonSerialize(): array { + return ['foo' => 'bar']; + } + }; + + $expected = new Hash(['foo' => 'bar']); + $actual = Hash::cast($object); + + $this->assertEquals($expected, $actual); + } + + public function testCastArray(): void + { + $expected = new Hash(['foo' => 'bar']); + $actual = Hash::cast(['foo' => 'bar']); + + $this->assertEquals($expected, $actual); + } + + public function testCastStdClass(): void + { + $expected = new Hash(['foo' => 'bar']); + $actual = Hash::cast((object) ['foo' => 'bar']); + + $this->assertEquals($expected, $actual); + } + + public function testCastNull(): void + { + $actual = Hash::cast(null); + + $this->assertEquals(new Hash(), $actual); + } + + public function testCastInvalidValue(): void + { + $this->expectException(\LogicException::class); + Hash::cast(true); + } public function testCamelize(): array {