Skip to content

Commit

Permalink
tests: add tests for json hash casting
Browse files Browse the repository at this point in the history
  • Loading branch information
lindyhopchris committed Mar 12, 2023
1 parent 8578500 commit 54e0d84
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/Core/Json/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
49 changes: 49 additions & 0 deletions tests/Unit/Json/HashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 54e0d84

Please sign in to comment.