-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f37808
commit 39ac954
Showing
6 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Spatie\Snapshots\Drivers; | ||
|
||
use Exception; | ||
use Flow\JSONPath\JSONPath; | ||
use PHPUnit\Framework\Assert; | ||
|
||
class JsonPathDriver extends JsonDriver | ||
{ | ||
public function __construct(private array $placeholders = []) | ||
{ | ||
} | ||
|
||
public function match($expected, $actual): void | ||
{ | ||
if (! class_exists(JSONPath::class)) { | ||
throw new Exception('The softcreatr/jsonpath package is not installed. Please install it to enable JSONPath driver.'); | ||
} | ||
|
||
if (is_string($actual)) { | ||
$actual = json_decode($actual, false, 512, JSON_THROW_ON_ERROR); | ||
} | ||
|
||
$expected = json_decode($expected, false, 512, JSON_THROW_ON_ERROR); | ||
|
||
$jpActual = new JSONPath($actual); | ||
$jpExpected = new JSONPath($expected); | ||
foreach ($this->placeholders as $path => $pattern) { | ||
$expectedData = $jpExpected->find($path)->getData(); | ||
$actualData = $jpActual->find($path)->getData(); | ||
foreach ($actualData as $i => $data) { | ||
Assert::assertMatchesRegularExpression($pattern, $data, 'Failed asserting that JSON path "'.$path.'" matches pattern "'.$pattern.'".'); | ||
$jpActual->offsetSet(str_replace('$.', '', $path), $expectedData[$i]); | ||
} | ||
} | ||
|
||
Assert::assertJsonStringEqualsJsonString(json_encode($expected), json_encode($jpActual)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spatie\Snapshots\Test\Unit\Drivers; | ||
|
||
use Generator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\ExpectationFailedException; | ||
use PHPUnit\Framework\TestCase; | ||
use Spatie\Snapshots\Drivers\JsonPathDriver; | ||
|
||
class JsonPathDriverTest extends TestCase | ||
{ | ||
#[Test] | ||
#[DataProvider('provideJsonData')] | ||
public function it_can_replace_placeholders_in_json(string $pathExpected, string $pathActual, array $replacements): void | ||
{ | ||
$expected = file_get_contents($pathExpected); | ||
$actual = file_get_contents($pathActual); | ||
$driver = new JsonPathDriver($replacements); | ||
|
||
try { | ||
$driver->match($expected, $actual); | ||
$status = true; | ||
} catch (ExpectationFailedException) { | ||
$status = false; | ||
} | ||
|
||
$this->assertTrue($status); | ||
} | ||
|
||
public static function provideJsonData(): Generator | ||
{ | ||
yield 'simple' => [ | ||
dirname(__DIR__).'/test_files/json_path_simpleA.json', | ||
dirname(__DIR__).'/test_files/json_path_simpleB.json', | ||
[ | ||
'$.id' => '@\d+@', | ||
'$.cover' => '@https://bucket.foo/bar/\d+.[webp|jpg]@', | ||
'$.createdAt' => '@\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[\+\-]\d{2}:\d{2}@', | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"id": 123456, | ||
"title": "Some Title", | ||
"author": "Jane Doe", | ||
"ISBN": "978-0-545-01022-1", | ||
"cover": "https://bucket.foo/bar/123456.webp", | ||
"published": 2017, | ||
"createdAt": "2024-03-08T17:37:09+00:00", | ||
"more": "values" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"id": 987654, | ||
"title": "Some Title", | ||
"author": "Jane Doe", | ||
"ISBN": "978-0-545-01022-1", | ||
"cover": "https://bucket.foo/bar/987654.webp", | ||
"published": 2017, | ||
"createdAt": "2024-04-02T22:33:44+55:11", | ||
"more": "values" | ||
} |