Skip to content

Commit 7da647e

Browse files
Merge pull request #76 from Gummibeer/ft-html-driver
html driver
2 parents 38d1b73 + 0384266 commit 7da647e

14 files changed

+171
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ To make snapshot assertions, use the `Spatie\Snapshots\MatchesSnapshots` trait i
8383
- `assertMatchesSnapshot($actual)`
8484
- `assertMatchesJsonSnapshot($actual)`
8585
- `assertMatchesXmlSnapshot($actual)`
86+
- `assertMatchesHtmlSnapshot($actual)`
8687
- `assertMatchesFileSnapshot($filePath)`
8788
- `assertMatchesFileHashSnapshot($filePath)`
8889

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
],
2222
"require": {
2323
"php": "^7.2",
24+
"ext-dom": "*",
2425
"phpunit/phpunit": "^8.0",
2526
"symfony/yaml": "^4.0"
2627
},

src/Drivers/HtmlDriver.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Drivers;
4+
5+
use DOMDocument;
6+
use PHPUnit\Framework\Assert;
7+
use Spatie\Snapshots\Driver;
8+
use Spatie\Snapshots\Exceptions\CantBeSerialized;
9+
10+
class HtmlDriver implements Driver
11+
{
12+
public function serialize($data): string
13+
{
14+
if (! is_string($data)) {
15+
throw new CantBeSerialized('Only strings can be serialized to html');
16+
}
17+
18+
$domDocument = new DOMDocument('1.0');
19+
$domDocument->preserveWhiteSpace = false;
20+
$domDocument->formatOutput = true;
21+
22+
@$domDocument->loadHTML($data); // to ignore HTML5 errors
23+
24+
return $domDocument->saveHTML();
25+
}
26+
27+
public function extension(): string
28+
{
29+
return 'html';
30+
}
31+
32+
public function match($expected, $actual)
33+
{
34+
Assert::assertEquals($expected, $this->serialize($actual));
35+
}
36+
}

src/MatchesSnapshots.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\ExpectationFailedException;
66
use ReflectionClass;
77
use ReflectionObject;
8+
use Spatie\Snapshots\Drivers\HtmlDriver;
89
use Spatie\Snapshots\Drivers\JsonDriver;
910
use Spatie\Snapshots\Drivers\VarDriver;
1011
use Spatie\Snapshots\Drivers\XmlDriver;
@@ -55,6 +56,11 @@ public function assertMatchesSnapshot($actual, Driver $driver = null)
5556
$this->doSnapshotAssertion($actual, $driver ?? new VarDriver());
5657
}
5758

59+
public function assertMatchesHtmlSnapshot($actual)
60+
{
61+
$this->assertMatchesSnapshot($actual, new HtmlDriver());
62+
}
63+
5864
public function assertMatchesXmlSnapshot($actual)
5965
{
6066
$this->assertMatchesSnapshot($actual, new XmlDriver());

tests/Integration/AssertionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public function can_match_a_string_snapshot()
2525
$this->assertMatchesSnapshot($data);
2626
}
2727

28+
/** @test */
29+
public function can_match_an_html_snapshot()
30+
{
31+
$data = '<!doctype html><html lang="en"><head></head><body><h1>Hello, world!</h1></body></html>';
32+
33+
$this->assertMatchesHtmlSnapshot($data);
34+
}
35+
2836
/** @test */
2937
public function can_match_an_xml_snapshot()
3038
{

tests/Integration/MatchesSnapshotTest.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ public function it_can_create_a_snapshot_from_an_array()
5454
);
5555
}
5656

57+
/** @test */
58+
public function it_can_create_a_snapshot_from_html()
59+
{
60+
$mockTrait = $this->getMatchesSnapshotMock();
61+
62+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
63+
$mockTrait->assertMatchesHtmlSnapshot('<!doctype html><html lang="en"><head></head><body><h1>Hello, world!</h1></body></html>');
64+
});
65+
66+
$this->assertSnapshotMatchesExample(
67+
'MatchesSnapshotTest__it_can_create_a_snapshot_from_html__1.html',
68+
'snapshot.html'
69+
);
70+
}
71+
5772
/** @test */
5873
public function it_can_create_a_snapshot_from_xml()
5974
{
@@ -107,6 +122,14 @@ public function it_can_match_an_existing_string_snapshot()
107122
$mockTrait->assertMatchesSnapshot('Foo');
108123
}
109124

125+
/** @test */
126+
public function it_can_match_an_existing_html_snapshot()
127+
{
128+
$mockTrait = $this->getMatchesSnapshotMock();
129+
130+
$mockTrait->assertMatchesHtmlSnapshot('<!doctype html><html lang="en"><head></head><body><h1>Hello, world!</h1></body></html>');
131+
}
132+
110133
/** @test */
111134
public function it_can_match_an_existing_xml_snapshot()
112135
{
@@ -141,6 +164,16 @@ public function it_can_mismatch_a_string_snapshot()
141164
$mockTrait->assertMatchesSnapshot('Bar');
142165
}
143166

167+
/** @test */
168+
public function it_can_mismatch_a_html_snapshot()
169+
{
170+
$mockTrait = $this->getMatchesSnapshotMock();
171+
172+
$this->expectFailedMatchesSnapshotTest();
173+
174+
$mockTrait->assertMatchesHtmlSnapshot('<!doctype html><html lang="en"><head></head><body><h1>Hallo welt!</h1></body></html>');
175+
}
176+
144177
/** @test */
145178
public function it_can_mismatch_a_xml_snapshot()
146179
{
@@ -257,6 +290,23 @@ public function it_can_update_a_string_snapshot()
257290
);
258291
}
259292

293+
/** @test */
294+
public function it_can_update_a_html_snapshot()
295+
{
296+
$_SERVER['argv'][] = '--update-snapshots';
297+
298+
$mockTrait = $this->getMatchesSnapshotMock();
299+
300+
$this->expectIncompleteMatchesSnapshotTest($mockTrait, function ($mockTrait) {
301+
$mockTrait->assertMatchesHtmlSnapshot('<!doctype html><html lang="en"><head></head><body><h1>Hello, world!</h1></body></html>');
302+
});
303+
304+
$this->assertSnapshotMatchesExample(
305+
'MatchesSnapshotTest__it_can_update_a_html_snapshot__1.html',
306+
'snapshot.html'
307+
);
308+
}
309+
260310
/** @test */
261311
public function it_can_update_a_xml_snapshot()
262312
{
@@ -355,7 +405,7 @@ private function expectFailedMatchesSnapshotTest()
355405
}
356406

357407
/**
358-
* @return \PHPUnit\Framework\MockObject\MockObject
408+
* @return \PHPUnit\Framework\MockObject\MockObject|\Spatie\Snapshots\MatchesSnapshots
359409
*/
360410
private function getMatchesSnapshotMock(): MockObject
361411
{
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head></head>
4+
<body><h1>Hello, world!</h1></body>
5+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head></head>
4+
<body><h1>Hello, world!</h1></body>
5+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head></head>
4+
<body><h1>Hello, world!</h1></body>
5+
</html>

0 commit comments

Comments
 (0)