mirror of
https://github.com/pestphp/pest.git
synced 2026-03-12 18:57:22 +01:00
Merge pull request #325 from dansysanalyst/feat/json
Method json() to parse JSON strings
This commit is contained in:
@ -60,6 +60,14 @@ final class Expectation
|
|||||||
return new self($value);
|
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.
|
* Dump the expectation value and end the script.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -140,6 +140,10 @@
|
|||||||
✓ it macros true is true with argument
|
✓ it macros true is true with argument
|
||||||
✓ it macros false is not 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
|
PASS Tests\Features\Expect\not
|
||||||
✓ not property calls
|
✓ not property calls
|
||||||
|
|
||||||
@ -569,5 +573,5 @@
|
|||||||
✓ it is a test
|
✓ it is a test
|
||||||
✓ it uses correct parent class
|
✓ it uses correct parent class
|
||||||
|
|
||||||
Tests: 4 incompleted, 7 skipped, 355 passed
|
Tests: 4 incompleted, 7 skipped, 357 passed
|
||||||
|
|
||||||
14
tests/Features/Expect/json.php
Normal file
14
tests/Features/Expect/json.php
Normal 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);
|
||||||
Reference in New Issue
Block a user