Skip to content

Commit

Permalink
Change assertJsonEquals, add assertJsonSame
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Jul 6, 2024
1 parent 4e26145 commit b5ec0b7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [5.2.0](https://github.com/userfrosting/framework/compare/5.0.1...5.2.0)
- [Testing] `assertJsonEquals`/`assertJsonNotEquals` (and `assertResponse` by extension) now use `assertEquals` under the hood instead of `assertSame` to better reflect their name. Added `assertJsonSame`/`assertJsonNotSame` for the old behavior.

## [5.1.3](https://github.com/userfrosting/framework/compare/5.1.2...5.1.3)
- [Config] Fix issue with `getBool`, `getString`, `getInt` and `getArray` where a null value could be returned even if a default parameter was provided when the data did in fact return `null`, making the return value not type safe as it should be.
Expand Down
36 changes: 35 additions & 1 deletion src/Testing/CustomAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,29 @@ protected function assertJsonEquals(mixed $expected, string|ResponseInterface $j
$json = (string) $json->getBody();
}

$this->assertJson($json);
$this->assertEquals($expected, $this->decodeJson($json, $key));
}

/**
* Asserts json is the same as something expected.
*
* @param mixed $expected Expected structure
* @param string|ResponseInterface $json The json string
* @param string|null $key Scope to the key if required. Support dot notation.
*/
protected function assertJsonSame(mixed $expected, string|ResponseInterface $json, ?string $key = null): void
{
if ($json instanceof ResponseInterface) {
$json = (string) $json->getBody();
}

$this->assertJson($json);
$this->assertSame($expected, $this->decodeJson($json, $key));
}

/**
* Asserts json is equals to something.
* Asserts json is not equals to something.
*
* @param mixed $expected Expected structure
* @param string|ResponseInterface $json The json string
Expand All @@ -100,6 +117,23 @@ protected function assertJsonNotEquals(mixed $expected, string|ResponseInterface
$json = (string) $json->getBody();
}

$this->assertJson($json);
$this->assertNotEquals($expected, $this->decodeJson($json, $key));
}

/**
* Asserts json is not the same as something else.
*
* @param mixed $expected Expected structure
* @param string|ResponseInterface $json The json string
* @param string|null $key Scope to the key if required. Support dot notation.
*/
protected function assertJsonNotSame(mixed $expected, string|ResponseInterface $json, ?string $key = null): void
{
if ($json instanceof ResponseInterface) {
$json = (string) $json->getBody();
}

$this->assertJson($json);
$this->assertNotSame($expected, $this->decodeJson($json, $key));
}
Expand Down
38 changes: 33 additions & 5 deletions tests/Testing/CustomAssertionsTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,54 @@ public function testAssertNotJsonResponse(): void

/** @var ResponseInterface $response */
$response = Mockery::mock(ResponseInterface::class)
->shouldReceive('getBody')->times(3)->andReturn($stream)
->shouldReceive('getBody')->times(4)->andReturn($stream)
->getMock();

$this->assertNotJsonResponse(['foo'], $response);
$this->assertNotJsonResponse(['foo'], $response, 'result.list');
$this->assertJsonNotEquals(['foo'], $response);
$this->assertJsonNotSame(['foo'], $response);
}

public function testAssertJsonEquals(): void
{
$array = ['result' => ['foo' => true, 'bar' => false, 'list' => ['foo', 'bar']]];
// N.B.: Array is in the reverse order, which is equals
$array = ['result' => ['list' => ['foo', 'bar'], 'bar' => false, 'foo' => true]];

$this->assertJsonEquals($array, $this->json);
$this->assertJsonEquals(['foo', 'bar'], $this->json, 'result.list');
$this->assertJsonEquals(true, $this->json, 'result.foo');
}

public function testAssertJsonSame(): void
{
// N.B.: Array order is important with same
$array = ['result' => ['foo' => true, 'bar' => false, 'list' => ['foo', 'bar']]];

$this->assertJsonSame($array, $this->json);
$this->assertJsonSame(['foo', 'bar'], $this->json, 'result.list');
$this->assertJsonSame(true, $this->json, 'result.foo');
}

public function testAssertJsonNotEquals(): void
{
$this->assertJsonNotEquals(['foo'], $this->json);
$this->assertJsonNotEquals(['foo'], $this->json, 'result.list');
$this->assertJsonNotEquals(false, $this->json, 'result.foo');
}

public function testAssertJsonEqualsWithResponse(): void
public function testAssertJsonNotSame(): void
{
// Use reversed array
$array = ['result' => ['list' => ['foo', 'bar'], 'bar' => false, 'foo' => true]];

$this->assertJsonNotSame($array, $this->json);
$this->assertJsonNotSame(['foo'], $this->json);
$this->assertJsonNotSame(['foo'], $this->json, 'result.list');
$this->assertJsonNotSame(false, $this->json, 'result.foo');
}

public function testAssertJsonEqualsAndSameWithResponse(): void
{
/** @var StreamInterface $stream */
$stream = Mockery::mock(StreamInterface::class)
Expand All @@ -119,14 +142,19 @@ public function testAssertJsonEqualsWithResponse(): void

/** @var ResponseInterface $response */
$response = Mockery::mock(ResponseInterface::class)
->shouldReceive('getBody')->times(3)->andReturn($stream)
->shouldReceive('getBody')->times(4)->andReturn($stream)
->getMock();

$array = ['result' => ['foo' => true, 'bar' => false, 'list' => ['foo', 'bar']]];
// N.B.: Array is in the reverse order, which is equals
$array = ['result' => ['list' => ['foo', 'bar'], 'bar' => false, 'foo' => true]];

$this->assertJsonEquals($array, $response);
$this->assertJsonEquals(['foo', 'bar'], $response, 'result.list');
$this->assertJsonEquals(true, $response, 'result.foo');

// N.B.: Array order is important with same. Change and retest same.
$array = ['result' => ['foo' => true, 'bar' => false, 'list' => ['foo', 'bar']]];
$this->assertJsonSame($array, $response);
}

public function testAssertJsonStructure(): void
Expand Down

0 comments on commit b5ec0b7

Please sign in to comment.