Skip to content

Commit 339db94

Browse files
authored
Merge pull request #146 from mallardduck/normalize-all-windows-output
Add tests for Text Driver and Windows EOL fixes
2 parents 92a3824 + 84074bf commit 339db94

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/Drivers/TextDriver.php

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ class TextDriver implements Driver
99
{
1010
public function serialize($data): string
1111
{
12+
// Normalize line endings for cross-platform tests.
13+
if (PHP_OS_FAMILY === 'Windows') {
14+
$data = implode("\n", explode("\r\n", $data));
15+
}
16+
1217
return $data;
1318
}
1419

tests/Unit/Drivers/TextDriverTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Test\Unit\Drivers;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Spatie\Snapshots\Drivers\TextDriver;
7+
use Spatie\Snapshots\Exceptions\CantBeSerialized;
8+
9+
class TextDriverTest extends TestCase
10+
{
11+
/** @test */
12+
public function it_can_serialize_laravel_route_list()
13+
{
14+
$driver = new TextDriver();
15+
16+
$expected = implode("\n", [
17+
'',
18+
' GET|HEAD / ..................................................... index',
19+
'',
20+
' Showing [1] routes'
21+
]);
22+
23+
$this->assertEquals($expected, $driver->serialize(<<<EOF
24+
25+
GET|HEAD / ..................................................... index
26+
27+
Showing [1] routes
28+
EOF));
29+
}
30+
31+
/** @test */
32+
public function it_can_serialize_when_given_OS_dependant_line_endings()
33+
{
34+
$driver = new TextDriver();
35+
36+
$expected = implode("\n", [
37+
'',
38+
' GET|HEAD / ..................................................... index',
39+
'',
40+
' Showing [1] routes'
41+
]);
42+
43+
// Due to using PHP_EOL this should fail (conditionally) when run on windows
44+
$actual = implode(PHP_EOL, [
45+
'',
46+
' GET|HEAD / ..................................................... index',
47+
'',
48+
' Showing [1] routes'
49+
]);
50+
51+
$this->assertEquals($expected, $driver->serialize($actual));
52+
}
53+
54+
}

0 commit comments

Comments
 (0)