Method json() to parse JSON strings

- Parse a JSON string into array
- Test
This commit is contained in:
Daniel Ang
2021-06-18 12:43:17 +02:00
parent 5116b4341e
commit c1b27579ca
2 changed files with 25 additions and 0 deletions

View File

@ -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.
* *

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);