Skip to content

Commit 4d73504

Browse files
authored
Merge pull request #99 from nunomaduro/master
Moves default implementation of snapshot directory/id to concerns directory
2 parents 496aaed + af9d78b commit 4d73504

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Concerns;
4+
5+
use ReflectionClass;
6+
7+
trait SnapshotDirectoryAware
8+
{
9+
/*
10+
* Determines the directory where snapshots are stored. By default a
11+
* `__snapshots__` directory is created at the same level as the test
12+
* class.
13+
*/
14+
protected function getSnapshotDirectory(): string
15+
{
16+
return dirname((new ReflectionClass($this))->getFileName()).
17+
DIRECTORY_SEPARATOR.
18+
'__snapshots__';
19+
}
20+
}

src/Concerns/SnapshotIdAware.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Spatie\Snapshots\Concerns;
4+
5+
use ReflectionClass;
6+
7+
trait SnapshotIdAware
8+
{
9+
/*
10+
* Determines the snapshot's id. By default, the test case's class and
11+
* method names are used.
12+
*/
13+
protected function getSnapshotId(): string
14+
{
15+
return (new ReflectionClass($this))->getShortName().'__'.
16+
$this->getName().'__'.
17+
$this->snapshotIncrementor;
18+
}
19+
}

src/MatchesSnapshots.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace Spatie\Snapshots;
44

55
use PHPUnit\Framework\ExpectationFailedException;
6-
use ReflectionClass;
76
use ReflectionObject;
7+
use Spatie\Snapshots\Concerns\SnapshotDirectoryAware;
8+
use Spatie\Snapshots\Concerns\SnapshotIdAware;
89
use Spatie\Snapshots\Drivers\HtmlDriver;
910
use Spatie\Snapshots\Drivers\JsonDriver;
1011
use Spatie\Snapshots\Drivers\ObjectDriver;
@@ -14,6 +15,8 @@
1415

1516
trait MatchesSnapshots
1617
{
18+
use SnapshotDirectoryAware, SnapshotIdAware;
19+
1720
protected int $snapshotIncrementor = 0;
1821

1922
protected array $snapshotChanges = [];
@@ -105,29 +108,6 @@ public function assertMatchesYamlSnapshot($actual): void
105108
$this->assertMatchesSnapshot($actual, new YamlDriver());
106109
}
107110

108-
/*
109-
* Determines the snapshot's id. By default, the test case's class and
110-
* method names are used.
111-
*/
112-
protected function getSnapshotId(): string
113-
{
114-
return (new ReflectionClass($this))->getShortName().'__'.
115-
$this->getName().'__'.
116-
$this->snapshotIncrementor;
117-
}
118-
119-
/*
120-
* Determines the directory where snapshots are stored. By default a
121-
* `__snapshots__` directory is created at the same level as the test
122-
* class.
123-
*/
124-
protected function getSnapshotDirectory(): string
125-
{
126-
return dirname((new ReflectionClass($this))->getFileName()).
127-
DIRECTORY_SEPARATOR.
128-
'__snapshots__';
129-
}
130-
131111
/*
132112
* Determines the directory where file snapshots are stored. By default a
133113
* `__snapshots__/files` directory is created at the same level as the

0 commit comments

Comments
 (0)