Skip to content

Commit 1567108

Browse files
committed
add a file hash driver
1 parent ee82d57 commit 1567108

File tree

8 files changed

+89
-1
lines changed

8 files changed

+89
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ build
22
composer.lock
33
docs
44
vendor
5+
.idea

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Spatie\Snapshots;
44

5+
use Exception;
56
use PHPUnit\Framework\ExpectationFailedException;
67
use PHPUnit_Framework_ExpectationFailedException;
78
use ReflectionClass;
89
use ReflectionObject;
10+
use Spatie\Snapshots\Drivers\FileHashDriver;
911
use Spatie\Snapshots\Drivers\JsonDriver;
1012
use Spatie\Snapshots\Drivers\VarDriver;
1113
use Spatie\Snapshots\Drivers\XmlDriver;
@@ -36,6 +38,17 @@ public function assertMatchesJsonSnapshot($actual)
3638
$this->assertMatchesSnapshot($actual, new JsonDriver());
3739
}
3840

41+
public function assertMatchesFileHashSnapshot($filePath)
42+
{
43+
if (! file_exists($filePath)) {
44+
throw new Exception('File does not exist');
45+
}
46+
47+
$actual = sha1_file($filePath);
48+
49+
$this->assertMatchesSnapshot($actual, new FileHashDriver());
50+
}
51+
3952
/**
4053
* Determines the snapshot's id. By default, the test case's class and
4154
* 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)