Skip to content

Commit 514837b

Browse files
authored
Merge pull request #145 from cschindl/main
Add type safe json matching
2 parents 4ee06e3 + f01428d commit 514837b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Drivers/JsonDriver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public function match($expected, $actual)
3232
$actual = json_decode($actual, true, 512, JSON_THROW_ON_ERROR);
3333
}
3434
$expected = json_decode($expected, true, 512, JSON_THROW_ON_ERROR);
35-
Assert::assertEquals($expected, $actual);
35+
Assert::assertJsonStringEqualsJsonString(json_encode($expected), json_encode($actual));
3636
}
3737
}

tests/Unit/Drivers/JsonDriverTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Spatie\Snapshots\Test\Unit\Drivers;
44

5+
use PHPUnit\Framework\ExpectationFailedException;
56
use PHPUnit\Framework\TestCase;
67
use Spatie\Snapshots\Drivers\JsonDriver;
78
use Spatie\Snapshots\Exceptions\CantBeSerialized;
@@ -120,4 +121,25 @@ public function it_can_not_serialize_resources()
120121

121122
$driver->serialize($resource);
122123
}
124+
125+
/**
126+
* @test
127+
* @testWith ["{}", "{}", true]
128+
* ["{}", "{\"data\":1}", false]
129+
* ["{\"data\":1}", "{\"data\":1}", true]
130+
* ["{\"data\":1}", "{\"data\":\"1\"}", false]
131+
*/
132+
public function it_can_match_json_strings(string $expected, string $actual, bool $assertion)
133+
{
134+
$driver = new JsonDriver();
135+
136+
try {
137+
$driver->match($expected, $actual);
138+
$status = true;
139+
} catch (ExpectationFailedException $th) {
140+
$status = false;
141+
}
142+
143+
$this->assertSame($assertion, $status);
144+
}
123145
}

0 commit comments

Comments
 (0)