Skip to content

Commit 81e8b62

Browse files
committed
Issue #110: Add SnapshotterInterface and DiffingMultiSnapshotter.
1 parent e9bfbe1 commit 81e8b62

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Ock\Testing\Snapshotter;
6+
7+
use Ock\Testing\Diff\DifferInterface;
8+
9+
class DiffingMultiSnapshotter implements SnapshotterInterface, DifferInterface {
10+
11+
/**
12+
* Constructor.
13+
*
14+
* @param array<string, \Ock\Testing\Snapshotter\SnapshotterInterface> $snapshotters
15+
* @param \Ock\Testing\Diff\DifferInterface $differ
16+
* Fallback differ.
17+
*/
18+
public function __construct(
19+
private readonly array $snapshotters,
20+
private readonly DifferInterface $differ,
21+
) {}
22+
23+
#[\Override]
24+
public function takeSnapshot(): array {
25+
return array_map(
26+
fn (SnapshotterInterface $snapshotter) => $snapshotter->takeSnapshot(),
27+
$this->snapshotters,
28+
);
29+
}
30+
31+
#[\Override]
32+
public function compare(array $before, array $after): array {
33+
$diff = [];
34+
foreach ($this->snapshotters as $key => $snapshotter) {
35+
assert(is_array($before[$key] ?? null));
36+
assert(is_array($after[$key] ?? null));
37+
if ($snapshotter instanceof DifferInterface) {
38+
$diff[$key] = $snapshotter->compare($before[$key], $after[$key]);
39+
}
40+
else {
41+
$diff[$key] = $this->differ->compare($before[$key], $after[$key]);
42+
}
43+
}
44+
return $diff;
45+
}
46+
47+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Ock\Testing\Snapshotter;
6+
7+
interface SnapshotterInterface {
8+
9+
/**
10+
* Takes an exportable snapshot.
11+
*
12+
* @return array
13+
*/
14+
public function takeSnapshot(): array;
15+
16+
}

0 commit comments

Comments
 (0)