From 0f88a450484df8924095a754c6a499f87477acbb Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Tue, 12 Jul 2022 08:52:47 -0400 Subject: [PATCH 1/3] [minor] cs fixes --- src/Browser/Json.php | 1 + tests/KernelBrowserAuthenticationTest.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Browser/Json.php b/src/Browser/Json.php index e8fd3bb..52b2378 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; /** 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; /** From c860ff9e8bcd5850fd5f0920aca5253f7ba8f138 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Tue, 12 Jul 2022 09:00:46 -0400 Subject: [PATCH 2/3] [doc] add note/link to full `zenstruck/assert` expectation API --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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.* From 157ce84ef4f925e780751963d475afc2fdfb7966 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Tue, 12 Jul 2022 09:04:06 -0400 Subject: [PATCH 3/3] [minor] locally cache decoded json --- src/Browser/Json.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Browser/Json.php b/src/Browser/Json.php index 52b2378..6b888cc 100644 --- a/src/Browser/Json.php +++ b/src/Browser/Json.php @@ -16,6 +16,9 @@ final class Json { private string $source; + /** @var mixed */ + private $decoded; + public function __construct(string $source) { $this->source = $source; @@ -126,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); } /**