Skip to content

Commit cfdab87

Browse files
Merge pull request #63 from spatie/v2
Use YAML by default for associative arrays
2 parents e23e5a3 + 2087393 commit cfdab87

File tree

7 files changed

+109
-14
lines changed

7 files changed

+109
-14
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.1",
24-
"phpunit/phpunit": "^6.0.5|^7.0"
24+
"phpunit/phpunit": "^7.0",
25+
"symfony/yaml": "^4.2"
2526
},
2627
"autoload": {
2728
"psr-4": {

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, PHP_INT_MAX);
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, PHP_INT_MAX);
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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use PHPUnit\Framework\TestCase;
66
use Spatie\Snapshots\Drivers\JsonDriver;
7-
use Spatie\Snapshots\Exceptions\CantBeSerialized;
8-
use stdClass;
97

108
class JsonDriverTest extends TestCase
119
{
@@ -86,14 +84,4 @@ public function it_can_serialize_a_json_array_to_pretty_json()
8684

8785
$this->assertEquals($expected, $driver->serialize(['foo', 'bar', 'baz']));
8886
}
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-
}
9987
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Test\Unit\Drivers;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Spatie\Snapshots\Drivers\YamlDriver;
7+
8+
class YamlDriverTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_can_serialize_a_yaml_string()
12+
{
13+
$driver = new YamlDriver();
14+
15+
$yamlString = implode(PHP_EOL, [
16+
'foo: bar',
17+
'baz: qux',
18+
'',
19+
]);
20+
21+
$this->assertEquals($yamlString, $driver->serialize($yamlString));
22+
}
23+
24+
/** @test */
25+
public function it_can_serialize_a_yaml_array()
26+
{
27+
$driver = new YamlDriver();
28+
29+
$expected = implode(PHP_EOL, [
30+
'foo: bar',
31+
'baz: qux',
32+
'',
33+
]);
34+
35+
$this->assertEquals($expected, $driver->serialize([
36+
'foo' => 'bar',
37+
'baz' => 'qux',
38+
]));
39+
}
40+
}

0 commit comments

Comments
 (0)