Skip to content

Commit 4216632

Browse files
Merge pull request #30 from SjorsO/master
add a file hash driver
2 parents ee82d57 + 9243d0b commit 4216632

File tree

7 files changed

+87
-1
lines changed

7 files changed

+87
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ composer require spatie/phpunit-snapshot-assertions
8686

8787
## Usage
8888

89-
To make snapshot assertions, use the `Spatie\Snapshots\MatchesSnapshots` trait in your test case class. This adds three assertion methods to the class:
89+
To make snapshot assertions, use the `Spatie\Snapshots\MatchesSnapshots` trait in your test case class. This adds four assertion methods to the class:
9090

9191
- `assertMatchesSnapshot($actual)`
9292
- `assertMatchesJsonSnapshot($actual)`
9393
- `assertMatchesXmlSnapshot($actual)`
94+
- `assertMatchesFileHashSnapshot($filePath)`
9495

9596
### Snapshot Testing 101
9697

src/Drivers/FileHashDriver.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Drivers;
4+
5+
use Exception;
6+
use PHPUnit\Framework\Assert;
7+
use Spatie\Snapshots\Driver;
8+
9+
class FileHashDriver implements Driver
10+
{
11+
public function serialize($data): string
12+
{
13+
if (! file_exists($data)) {
14+
throw new Exception('File does not exist');
15+
}
16+
17+
return sha1_file($data);
18+
}
19+
20+
public function extension(): string
21+
{
22+
return 'txt';
23+
}
24+
25+
public function match($expected, $actual)
26+
{
27+
Assert::assertSame($expected, $actual);
28+
}
29+
}

src/MatchesSnapshots.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit_Framework_ExpectationFailedException;
77
use ReflectionClass;
88
use ReflectionObject;
9+
use Spatie\Snapshots\Drivers\FileHashDriver;
910
use Spatie\Snapshots\Drivers\JsonDriver;
1011
use Spatie\Snapshots\Drivers\VarDriver;
1112
use Spatie\Snapshots\Drivers\XmlDriver;
@@ -36,6 +37,17 @@ public function assertMatchesJsonSnapshot($actual)
3637
$this->assertMatchesSnapshot($actual, new JsonDriver());
3738
}
3839

40+
public function assertMatchesFileHashSnapshot($filePath)
41+
{
42+
if (! file_exists($filePath)) {
43+
$this->fail('File does not exist');
44+
}
45+
46+
$actual = sha1_file($filePath);
47+
48+
$this->assertMatchesSnapshot($actual, new FileHashDriver());
49+
}
50+
3951
/**
4052
* Determines the snapshot's id. By default, the test case's class and
4153
* method names are used.

tests/Integration/AssertionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public function can_match_a_json_snapshot()
4141
$this->assertMatchesJsonSnapshot($data);
4242
}
4343

44+
/** @test */
45+
public function can_match_a_file_hash_snapshot()
46+
{
47+
$filePath = __DIR__.'/stubs/example_snapshots/snapshot.json';
48+
49+
$this->assertMatchesFileHashSnapshot($filePath);
50+
}
51+
4452
/** @test */
4553
public function can_do_multiple_snapshot_assertions()
4654
{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f558f8149e33fba2916877b2d3de4a42ebd544b4
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Test\Unit\Drivers;
4+
5+
use Exception;
6+
use PHPUnit\Framework\TestCase;
7+
use Spatie\Snapshots\Drivers\FileHashDriver;
8+
9+
class FileHashDriverTest extends TestCase
10+
{
11+
/** @test */
12+
public function it_can_hash_a_file()
13+
{
14+
$driver = new FileHashDriver();
15+
16+
$filePath = __DIR__.'/files/example-file.txt';
17+
18+
$expected = sha1_file($filePath);
19+
20+
$this->assertEquals($expected, $driver->serialize($filePath));
21+
}
22+
23+
/** @test */
24+
public function it_throws_an_exception_if_the_file_does_not_exist()
25+
{
26+
$driver = new FileHashDriver();
27+
28+
$this->expectException(Exception::class);
29+
30+
$driver->serialize(__DIR__.'/files/fake-file-path');
31+
}
32+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Spatie is a digital allrounder:
2+
we design solid websites and crafty applications in Laravel.
3+
No frills, just expertise.

0 commit comments

Comments
 (0)