diff --git a/README.md b/README.md index 753ac3f..ef72488 100644 --- a/README.md +++ b/README.md @@ -394,7 +394,8 @@ $browser Make assertions about json responses using [JMESPath expressions](https://jmespath.org/) See the [JMESPath Tutorials](https://jmespath.org/tutorial.html) to learn more. -**NOTE:** `mtdowling/jmespath.php` is required: `composer require --dev mtdowling/jmespath.php`. +> **Note** +> `mtdowling/jmespath.php` is required: `composer require --dev mtdowling/jmespath.php`. ```php /** @var \Zenstruck\Browser\KernelBrowser $browser **/ @@ -437,6 +438,10 @@ $json = $browser ; ``` +> **Note** +> See the [full `zenstruck/assert` expectation API documentation](https://github.com/zenstruck/assert#expectation-api) +> to see all the methods available on `Zenstruck\Browser\Json`. + ### PantherBrowser *The `PantherBrowser` is experimental in 1.0 and may be subject to BC Breaks.* diff --git a/src/Browser/Json.php b/src/Browser/Json.php index e8fd3bb..6b888cc 100644 --- a/src/Browser/Json.php +++ b/src/Browser/Json.php @@ -4,6 +4,7 @@ use Zenstruck\Assert; use Zenstruck\Assert\Expectation; + use function JmesPath\search; /** @@ -15,6 +16,9 @@ final class Json { private string $source; + /** @var mixed */ + private $decoded; + public function __construct(string $source) { $this->source = $source; @@ -125,11 +129,7 @@ public function search(string $selector) */ public function decoded() { - if (empty($this->source)) { - return null; - } - - return \json_decode($this->source, true, 512, \JSON_THROW_ON_ERROR); + return $this->decoded ??= empty($this->source) ? null : \json_decode($this->source, true, 512, \JSON_THROW_ON_ERROR); } /** diff --git a/tests/KernelBrowserAuthenticationTest.php b/tests/KernelBrowserAuthenticationTest.php index cbab613..665fc14 100644 --- a/tests/KernelBrowserAuthenticationTest.php +++ b/tests/KernelBrowserAuthenticationTest.php @@ -11,6 +11,7 @@ use Zenstruck\Browser\Tests\Fixture\Kernel; use Zenstruck\Foundry\Configuration; use Zenstruck\Foundry\Factory; + use function Zenstruck\Foundry\factory; /**