Merge pull request #325 from dansysanalyst/feat/json

Method json() to parse JSON strings
This commit is contained in:
Nuno Maduro
2021-06-18 12:28:45 +01:00
committed by GitHub
3 changed files with 27 additions and 1 deletions

View File

@ -60,6 +60,14 @@ final class Expectation
return new self($value);
}
/**
* Creates a new expectation with the decoded JSON value.
*/
public function json(): Expectation
{
return $this->toBeJson()->and(json_decode($this->value, true));
}
/**
* Dump the expectation value and end the script.
*

View File

@ -140,6 +140,10 @@
✓ it macros true is true with argument
✓ it macros false is not true with argument
PASS Tests\Features\Expect\json
✓ it properly parses json string
✓ fails with broken json string
PASS Tests\Features\Expect\not
✓ not property calls
@ -569,5 +573,5 @@
✓ it is a test
✓ it uses correct parent class
Tests: 4 incompleted, 7 skipped, 355 passed
Tests: 4 incompleted, 7 skipped, 357 passed

View File

@ -0,0 +1,14 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('it properly parses json string', function () {
expect('{"name":"Nuno"}')
->json()
->name
->toBe('Nuno');
});
test('fails with broken json string', function () {
expect('{":"Nuno"}')->json();
})->throws(ExpectationFailedException::class);