mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
Method json() to parse JSON strings
- Parse a JSON string into array - Test
This commit is contained in:
@ -60,6 +60,17 @@ final class Expectation
|
|||||||
return new self($value);
|
return new self($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses Json String to Array.
|
||||||
|
*/
|
||||||
|
public function json(): Expectation
|
||||||
|
{
|
||||||
|
Assert::assertIsString($this->value);
|
||||||
|
Assert::assertJson($this->value);
|
||||||
|
|
||||||
|
return new self(json_decode($this->value, true));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dump the expectation value and end the script.
|
* Dump the expectation value and end the script.
|
||||||
*
|
*
|
||||||
|
|||||||
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