Skip to content

Commit 61f5d4b

Browse files
Use YAML with ObjectDriver
1 parent 578907b commit 61f5d4b

21 files changed

+57
-58
lines changed

src/Drivers/ObjectDriver.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace Spatie\Snapshots\Drivers;
44

5-
use PHPUnit\Framework\Assert;
65
use Spatie\Snapshots\Driver;
7-
use Symfony\Component\Serializer\Encoder\JsonEncoder;
8-
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
9-
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
6+
use PHPUnit\Framework\Assert;
7+
use Symfony\Component\Yaml\Yaml;
108
use Symfony\Component\Serializer\Serializer;
9+
use Symfony\Component\Serializer\Encoder\YamlEncoder;
10+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
11+
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
1112

1213
class ObjectDriver implements Driver
1314
{
@@ -19,7 +20,7 @@ public function serialize($data): string
1920
];
2021

2122
$encoders = [
22-
new JsonEncoder(),
23+
new YamlEncoder(),
2324
];
2425

2526
$serializer = new Serializer($normalizers, $encoders);
@@ -30,22 +31,27 @@ public function serialize($data): string
3031
$data = (array) $data;
3132
}
3233

33-
return $serializer->serialize(
34-
$data,
35-
'json',
36-
[
37-
'json_encode_options' => JSON_PRETTY_PRINT,
38-
]
34+
return $this->dedent(
35+
$serializer->serialize($data, 'yaml', [
36+
'yaml_inline' => 2,
37+
'yaml_indent' => 4,
38+
'yaml_flags' => Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK,
39+
])
3940
);
4041
}
4142

4243
public function extension(): string
4344
{
44-
return 'json';
45+
return 'yml';
4546
}
4647

4748
public function match($expected, $actual)
4849
{
49-
Assert::assertJsonStringEqualsJsonString($expected, $this->serialize($actual));
50+
Assert::assertEquals($expected, $this->serialize($actual));
51+
}
52+
53+
protected function dedent(string $string): string
54+
{
55+
return preg_replace('/^ {4}/m', '', $string);
5056
}
5157
}

src/MatchesSnapshots.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ public function markTestIncompleteIfSnapshotsHaveChanged()
4343

4444
public function assertMatchesSnapshot($actual, Driver $driver = null)
4545
{
46-
if (is_null($driver) && is_array($actual)) {
47-
$this->assertMatchesYamlSnapshot($actual);
48-
49-
return;
50-
}
51-
5246
$this->doSnapshotAssertion($actual, $driver ?? new ObjectDriver());
5347
}
5448

@@ -166,6 +160,8 @@ protected function doSnapshotAssertion($actual, Driver $driver)
166160
} catch (ExpectationFailedException $exception) {
167161
$this->updateSnapshotAndMarkTestIncomplete($snapshot, $actual);
168162
}
163+
164+
return;
169165
}
170166

171167
try {

src/Snapshot.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ public function id(): string
4141
public function filename(): string
4242
{
4343
$file = $this->id.'.'.$this->driver->extension();
44+
4445
// Remove anything which isn't a word, whitespace, number
4546
// or any of the following caracters -_~,;[]().
4647
$file = preg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $file);
48+
4749
// Remove any runs of periods
4850
$file = preg_replace("([\.]{2,})", '', $file);
4951

tests/Integration/MatchesSnapshotTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public function it_can_create_a_snapshot_from_a_string()
3434
});
3535

3636
$this->assertSnapshotMatchesExample(
37-
'MatchesSnapshotTest__it_can_create_a_snapshot_from_a_string__1.json',
38-
'string_snapshot.json'
37+
'MatchesSnapshotTest__it_can_create_a_snapshot_from_a_string__1.yml',
38+
'string_snapshot.yml'
3939
);
4040
}
4141

@@ -285,8 +285,8 @@ public function it_can_update_a_string_snapshot()
285285
});
286286

287287
$this->assertSnapshotMatchesExample(
288-
'MatchesSnapshotTest__it_can_update_a_string_snapshot__1.json',
289-
'string_snapshot.json'
288+
'MatchesSnapshotTest__it_can_update_a_string_snapshot__1.yml',
289+
'string_snapshot.yml'
290290
);
291291
}
292292

tests/Integration/stubs/__snapshots__/AssertionTest__can_do_multiple_snapshot_assertions__1.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Foo

tests/Integration/stubs/__snapshots__/AssertionTest__can_do_multiple_snapshot_assertions__2.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Bar

tests/Integration/stubs/__snapshots__/AssertionTest__can_match_a_file_hash_snapshot__1.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f558f8149e33fba2916877b2d3de4a42ebd544b4

0 commit comments

Comments
 (0)