Skip to content

Commit 7fc2d0e

Browse files
Wip
1 parent 9609fab commit 7fc2d0e

8 files changed

+139
-104
lines changed

tests/ExampleTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,26 @@ class ExampleTest extends \PHPUnit_Framework_TestCase
77
use MatchesSnapshots;
88

99
/** @test */
10-
public function true_is_true()
10+
public function can_match_a_string_snapshot()
1111
{
1212
$data = 'Foo';
1313

1414
$this->assertMatchesSnapshot($data);
1515
}
16+
17+
/** @test */
18+
public function can_match_an_xml_screenshot()
19+
{
20+
$data = '<foo><bar>Baz</bar></foo>';
21+
22+
$this->assertMatchesXmlSnapshot($data);
23+
}
24+
25+
/** @test */
26+
public function can_match_a_json_screenshot()
27+
{
28+
$data = '{"foo":"foo","bar":"bar","baz":"baz"}';
29+
30+
$this->assertMatchesJsonSnapshot($data);
31+
}
1632
}

tests/MatchesSnapshots.php

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,37 @@
44

55
trait MatchesSnapshots
66
{
7-
private $snapshotCount = 0;
8-
9-
public function assertMatchesSnapshot($serializable)
7+
public function assertMatchesSnapshot($actual, $type = 'var', $methodTrace = null)
108
{
11-
$this->snapshotCount++;
12-
13-
$snapshotHandler = SnapshotHandler::forTestMethod(
14-
debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)[1],
15-
$this->snapshotCount
9+
$snapshot = Snapshot::forTestMethod(
10+
$methodTrace ?? debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)[1],
11+
$type
1612
);
1713

18-
if (! $snapshotHandler->exists()) {
19-
$snapshotHandler->create($this->serializeForSnapshot($serializable));
14+
if (! $snapshot->exists()) {
15+
$snapshot->create($actual);
2016

21-
return $this->markTestIncomplete("Snapshot created for {$snapshotHandler->id()}");
17+
return $this->markTestIncomplete("Snapshot created for {$snapshot->id()}");
2218
}
2319

24-
if ($this->shouldUpdateSnapshots()) {
25-
$snapshotHandler->update($this->serializeForSnapshot($serializable));
26-
27-
return $this->markTestIncomplete("Snapshot updated for {$snapshotHandler->id()}");
20+
if ($type === 'xml') {
21+
return $this->assertXmlStringEqualsXmlString($snapshot->get(), $actual);
2822
}
2923

30-
return $this->assertEquals($snapshotHandler->get(), $serializable);
31-
}
24+
if ($type === 'json') {
25+
return $this->assertJsonStringEqualsJsonString($snapshot->get(), $actual);
26+
}
3227

33-
/** @after **/
34-
public function resetSnapshotCounter()
35-
{
36-
$this->snapshotCount = 0;
28+
$this->assertEquals($snapshot->get(), $actual);
3729
}
3830

39-
protected function shouldUpdateSnapshots(): bool
31+
public function assertMatchesXmlSnapshot($actual)
4032
{
41-
return getenv('UPDATE_SNAPSHOTS');
33+
$this->assertMatchesSnapshot($actual, 'xml', debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)[1]);
4234
}
4335

44-
protected function serializeForSnapshot($serializable)
36+
public function assertMatchesJsonSnapshot($actual)
4537
{
46-
return $serializable;
38+
$this->assertMatchesSnapshot($actual, 'json', debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)[1]);
4739
}
4840
}

tests/Snapshot.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Test;
4+
5+
use DOMDocument;
6+
use ReflectionClass;
7+
use SimpleXMLElement;
8+
9+
class Snapshot
10+
{
11+
/** @var string */
12+
protected $directory, $id, $type;
13+
14+
private function __construct(string $directory, string $id, string $type)
15+
{
16+
$this->directory = $directory;
17+
$this->id = $id;
18+
$this->type = $type;
19+
}
20+
21+
public static function forTestMethod($backtrace, $type): self
22+
{
23+
$class = new ReflectionClass($backtrace['class']);
24+
$method = $backtrace['function'];
25+
26+
$directory = dirname($class->getFileName()).'/__snapshots__';
27+
$id = "{$class->getShortName()}__{$method}";
28+
29+
return new self($directory, $id, $type);
30+
}
31+
32+
public function id(): string
33+
{
34+
return $this->id;
35+
}
36+
37+
public function path(): string
38+
{
39+
$extension = $this->type === 'var' ? 'php' : $this->type;
40+
41+
return "{$this->directory}/{$this->id}.{$extension}";
42+
}
43+
44+
public function exists(): bool
45+
{
46+
return file_exists($this->path());
47+
}
48+
49+
public function get()
50+
{
51+
if ($this->type === 'var') {
52+
return include $this->path();
53+
}
54+
55+
return file_get_contents($this->path());
56+
}
57+
58+
public function create($actual)
59+
{
60+
if (! file_exists($this->directory)) {
61+
mkdir($this->directory);
62+
}
63+
64+
file_put_contents($this->path(), $this->serializeForSnapshot($actual));
65+
}
66+
67+
protected function serializeForSnapshot($data): string
68+
{
69+
if ($this->type === 'xml') {
70+
return $this->formatXml($data);
71+
}
72+
73+
if ($this->type === 'json') {
74+
return $this->formatJson($data);
75+
}
76+
77+
return '<?php return '.var_export($data, true).';';
78+
}
79+
80+
protected function formatXml(string $xml): string
81+
{
82+
$domDocument = new DOMDocument('1.0');
83+
$domDocument->preserveWhiteSpace = false;
84+
$domDocument->formatOutput = true;
85+
86+
$domDocument->loadXML($xml);
87+
88+
return $domDocument->saveXML();
89+
}
90+
91+
protected function formatJson(string $json): string
92+
{
93+
return json_encode(json_decode($json), JSON_PRETTY_PRINT);
94+
}
95+
}

tests/SnapshotHandler.php

Lines changed: 0 additions & 73 deletions
This file was deleted.

tests/__snapshots__/ExampleTest.php

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"foo": "foo",
3+
"bar": "bar",
4+
"baz": "baz"
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php return 'Foo';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<foo>
3+
<bar>Baz</bar>
4+
</foo>

0 commit comments

Comments
 (0)