Skip to content

Commit 20c0df0

Browse files
Add Yaml driver & default to yaml for associative arrays
1 parent 15d9bef commit 20c0df0

File tree

8 files changed

+115
-12
lines changed

8 files changed

+115
-12
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
],
2222
"require": {
2323
"php": "^7.0",
24-
"phpunit/phpunit": "^6.0.5|^7.0"
24+
"phpunit/phpunit": "^6.0.5|^7.0",
25+
"symfony/yaml": "^4.2"
2526
},
2627
"autoload": {
2728
"psr-4": {

src/Drivers/JsonDriver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function extension(): string
2828

2929
public function match($expected, $actual)
3030
{
31+
if (is_array($actual)) {
32+
$actual = json_encode($data, JSON_PRETTY_PRINT).PHP_EOL;
33+
}
34+
3135
Assert::assertJsonStringEqualsJsonString($expected, $actual);
3236
}
3337
}

src/Drivers/YamlDriver.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Drivers;
4+
5+
use PHPUnit\Framework\Assert;
6+
use Spatie\Snapshots\Driver;
7+
use Spatie\Snapshots\Exceptions\CantBeSerialized;
8+
use Symfony\Component\Yaml\Yaml;
9+
10+
class YamlDriver implements Driver
11+
{
12+
public function serialize($data): string
13+
{
14+
if (is_string($data)) {
15+
$data = Yaml::parse($data);
16+
}
17+
18+
if (! is_array($data)) {
19+
throw new CantBeSerialized('Only strings can be serialized to json');
20+
}
21+
22+
return Yaml::dump($data);
23+
}
24+
25+
public function extension(): string
26+
{
27+
return 'yml';
28+
}
29+
30+
public function match($expected, $actual)
31+
{
32+
if (is_array($actual)) {
33+
$actual = Yaml::dump($actual);
34+
}
35+
36+
Assert::assertEquals($expected, $actual);
37+
}
38+
}

src/MatchesSnapshots.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Spatie\Snapshots\Drivers\JsonDriver;
99
use Spatie\Snapshots\Drivers\VarDriver;
1010
use Spatie\Snapshots\Drivers\XmlDriver;
11+
use Spatie\Snapshots\Drivers\YamlDriver;
1112

1213
trait MatchesSnapshots
1314
{
@@ -45,6 +46,12 @@ public function markTestIncompleteIfSnapshotsHaveChanged()
4546

4647
public function assertMatchesSnapshot($actual, Driver $driver = null)
4748
{
49+
if (is_null($driver) && is_array($actual)) {
50+
$this->assertMatchesYamlSnapshot($actual);
51+
52+
return;
53+
}
54+
4855
$this->doSnapshotAssertion($actual, $driver ?? new VarDriver());
4956
}
5057

@@ -58,6 +65,11 @@ public function assertMatchesJsonSnapshot($actual)
5865
$this->assertMatchesSnapshot($actual, new JsonDriver());
5966
}
6067

68+
public function assertMatchesYamlSnapshot($actual)
69+
{
70+
$this->assertMatchesSnapshot($actual, new YamlDriver());
71+
}
72+
6173
public function assertMatchesFileHashSnapshot($filePath)
6274
{
6375
if (! file_exists($filePath)) {

tests/Integration/MatchesSnapshotTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,26 @@ public function it_can_create_a_snapshot_from_a_string()
3434
});
3535

3636
$this->assertSnapshotMatchesExample(
37-
'MatchesSnapshotTest__it_can_match_an_existing_string_snapshot__1.php',
37+
'MatchesSnapshotTest__it_can_create_a_snapshot_from_a_string__1.php',
3838
'snapshot.php'
3939
);
4040
}
4141

42+
/** @test */
43+
public function it_can_create_a_snapshot_from_an_array()
44+
{
45+
$mockTrait = $this->getMatchesSnapshotMock();
46+
47+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
48+
$mockTrait->assertMatchesSnapshot(['foo' => 'bar']);
49+
});
50+
51+
$this->assertSnapshotMatchesExample(
52+
'MatchesSnapshotTest__it_can_create_a_snapshot_from_an_array__1.yml',
53+
'snapshot.yml'
54+
);
55+
}
56+
4257
/** @test */
4358
public function it_can_create_a_snapshot_from_xml()
4459
{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo: bar

tests/Unit/Drivers/JsonDriverTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,4 @@ public function it_can_serialize_a_json_array_to_pretty_json()
8686

8787
$this->assertEquals($expected, $driver->serialize(['foo', 'bar', 'baz']));
8888
}
89-
90-
/** @test */
91-
public function it_can_only_serialize_strings_and_arrays()
92-
{
93-
$this->expectException(CantBeSerialized::class);
94-
95-
$driver = new JsonDriver();
96-
97-
$driver->serialize(new stdClass);
98-
}
9989
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Test\Unit\Drivers;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Spatie\Snapshots\Drivers\YamlDriver;
7+
use Spatie\Snapshots\Exceptions\CantBeSerialized;
8+
use stdClass;
9+
10+
class YamlDriverTest extends TestCase
11+
{
12+
/** @test */
13+
public function it_can_serialize_a_yaml_string()
14+
{
15+
$driver = new YamlDriver();
16+
17+
$yamlString = implode(PHP_EOL, [
18+
'foo: bar',
19+
'baz: qux',
20+
'',
21+
]);
22+
23+
$this->assertEquals($yamlString, $driver->serialize($yamlString));
24+
}
25+
26+
/** @test */
27+
public function it_can_serialize_a_yaml_array()
28+
{
29+
$driver = new YamlDriver();
30+
31+
$expected = implode(PHP_EOL, [
32+
'foo: bar',
33+
'baz: qux',
34+
'',
35+
]);
36+
37+
$this->assertEquals($expected, $driver->serialize([
38+
'foo' => 'bar',
39+
'baz' => 'qux',
40+
]));
41+
}
42+
}

0 commit comments

Comments
 (0)