Skip to content

Commit

Permalink
Merge pull request #95 from kbond/json-cache
Browse files Browse the repository at this point in the history
CS fixes, update docs, cache decoded json value
  • Loading branch information
kbond authored Jul 12, 2022
2 parents cd3cce3 + 157ce84 commit f35ce6d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 **/
Expand Down Expand Up @@ -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.*
Expand Down
10 changes: 5 additions & 5 deletions src/Browser/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Zenstruck\Assert;
use Zenstruck\Assert\Expectation;

use function JmesPath\search;

/**
Expand All @@ -15,6 +16,9 @@ final class Json
{
private string $source;

/** @var mixed */
private $decoded;

public function __construct(string $source)
{
$this->source = $source;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/KernelBrowserAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Zenstruck\Browser\Tests\Fixture\Kernel;
use Zenstruck\Foundry\Configuration;
use Zenstruck\Foundry\Factory;

use function Zenstruck\Foundry\factory;

/**
Expand Down

0 comments on commit f35ce6d

Please sign in to comment.