File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed
Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments