Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,23 @@ Assert::that(['foo', 'bar'])
;
```

### Json Expectations

```php
use Zenstruck\Assert;

Assert::that('[4, 5, 6]')
->isJson() // json_decode's the current value and starts a new expectation with this
->contains(5)
;

Assert::that('5')
->isJson()
->is(5)
->isGreaterThan(4)
;
```

## `AssertionFailed` Exception

When triggering a failed assertion, it is important to provide a useful failure
Expand Down
24 changes: 24 additions & 0 deletions src/Assert/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,28 @@ public function throws($expectedException, ?string $expectedMessage = null): sel

return $this;
}

/**
* Json decodes the current value and returns a new instance
* wrapping the decoded value.
*
* Fails if not a valid json string.
*/
public function isJson(): self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure of the name: it sounds like it's just an expectation, but it does more... decodeJson() ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this tripped me up as well. Technically it is also an expectation but is not clear that it changes the underlying data.

I chose this purely because the api reads better: "assert that value is json" vs "assert that value decode json". I definitely probably get too hung up on these semantics 😄.

{
if (!\is_string($this->value)) {
Assert::fail('"{value}" is not a string.', ['value' => $this->value]);
}

// TODO: use \JSON_THROW_ON_ERROR once min PHP >= 7.3
$decoded = \json_decode($this->value, true);

if (\JSON_ERROR_NONE !== \json_last_error()) {
Assert::fail('"{value}" is not a json string.', ['value' => $this->value]);
}

Assert::pass();

return new self($decoded);
}
}
32 changes: 32 additions & 0 deletions tests/ExpectationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Zenstruck\Assert;
use Zenstruck\Assert\AssertionFailed;
use Zenstruck\Assert\Tests\Fixture\CountableObject;
use Zenstruck\Assert\Tests\Fixture\IterableObject;
use Zenstruck\Assert\Type;
Expand Down Expand Up @@ -736,6 +737,37 @@ public static function isTypeFailProvider(): iterable
yield ['foo', Type::json()];
}

/**
* @test
*/
public function is_json(): void
{
$this->assertSuccess(6, function() {
Assert::that('6')->isJson()->is(Type::int())->isGreaterThan(5);
Assert::that('[6, 7]')->isJson()->contains(7)->doesNotContain(5);
});
}

/**
* @test
*/
public function is_json_failure(): void
{
$this->assertFails('"stdClass" is not a string.', function() {
try {
Assert::that(new \stdClass())->isJson();
} catch (AssertionFailed $e) {
}
});

$this->assertFails('"foo" is not a json string.', function() {
try {
Assert::that('foo')->isJson();
} catch (AssertionFailed $e) {
}
});
}

private function assertSuccess(int $expectedCount, callable $what): self
{
$this->handler->reset();
Expand Down