Skip to content

Commit 9cdafc1

Browse files
Use text snapshots by default for scalars
1 parent 61f5d4b commit 9cdafc1

11 files changed

+55
-8
lines changed

src/Drivers/TextDriver.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Drivers;
4+
5+
use Spatie\Snapshots\Driver;
6+
use PHPUnit\Framework\Assert;
7+
8+
class TextDriver implements Driver
9+
{
10+
public function serialize($data): string
11+
{
12+
return $data;
13+
}
14+
15+
public function extension(): string
16+
{
17+
return 'txt';
18+
}
19+
20+
public function match($expected, $actual)
21+
{
22+
Assert::assertEquals($expected, $this->serialize($actual));
23+
}
24+
}

src/MatchesSnapshots.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace Spatie\Snapshots;
44

5-
use PHPUnit\Framework\ExpectationFailedException;
65
use ReflectionClass;
76
use ReflectionObject;
7+
use Spatie\Snapshots\Drivers\XmlDriver;
88
use Spatie\Snapshots\Drivers\HtmlDriver;
99
use Spatie\Snapshots\Drivers\JsonDriver;
10-
use Spatie\Snapshots\Drivers\ObjectDriver;
11-
use Spatie\Snapshots\Drivers\XmlDriver;
10+
use Spatie\Snapshots\Drivers\TextDriver;
1211
use Spatie\Snapshots\Drivers\YamlDriver;
12+
use Spatie\Snapshots\Drivers\ObjectDriver;
13+
use PHPUnit\Framework\ExpectationFailedException;
1314

1415
trait MatchesSnapshots
1516
{
@@ -43,7 +44,19 @@ public function markTestIncompleteIfSnapshotsHaveChanged()
4344

4445
public function assertMatchesSnapshot($actual, Driver $driver = null)
4546
{
46-
$this->doSnapshotAssertion($actual, $driver ?? new ObjectDriver());
47+
if (! is_null($driver)) {
48+
$this->doSnapshotAssertion($actual, $driver);
49+
50+
return;
51+
}
52+
53+
if (is_string($actual) || is_int($actual) || is_float($actual)) {
54+
$this->doSnapshotAssertion($actual, new TextDriver());
55+
56+
return;
57+
}
58+
59+
$this->doSnapshotAssertion($actual, new ObjectDriver());
4760
}
4861

4962
public function assertMatchesHtmlSnapshot($actual)
@@ -82,6 +95,16 @@ public function assertMatchesFileSnapshot($file)
8295
$this->doFileSnapshotAssertion($file);
8396
}
8497

98+
public function assertMatchesObjectSnapshot($actual)
99+
{
100+
$this->assertMatchesSnapshot($actual, new ObjectDriver());
101+
}
102+
103+
public function assertMatchesTextSnapshot($actual)
104+
{
105+
$this->assertMatchesSnapshot($actual, new TextDriver());
106+
}
107+
85108
/**
86109
* Determines the snapshot's id. By default, the test case's class and
87110
* method names are used.

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.yml',
38-
'string_snapshot.yml'
37+
'MatchesSnapshotTest__it_can_create_a_snapshot_from_a_string__1.txt',
38+
'string_snapshot.txt'
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.yml',
289-
'string_snapshot.yml'
288+
'MatchesSnapshotTest__it_can_update_a_string_snapshot__1.txt',
289+
'string_snapshot.txt'
290290
);
291291
}
292292

0 commit comments

Comments
 (0)