Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.

Commit 974723b

Browse files
committed
add redactedProperties
1 parent c0febc9 commit 974723b

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/SnapshotDriver/SnapshotJsonObjectDriver.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
class SnapshotJsonObjectDriver implements Driver
1616
{
17-
public function __construct(private array $ignoredProperties = [])
17+
public function __construct(
18+
private array $ignoredProperties = [],
19+
private array $redactedProperties = []
20+
)
1821
{
1922
}
2023

@@ -39,7 +42,8 @@ public function serialize($data): string
3942
AbstractNormalizer::IGNORED_ATTRIBUTES => $this->ignoredProperties,
4043
]);
4144

42-
$this->unsetIgnoredKeys($normalizedData, $this->ignoredProperties);
45+
$this->ignoreKeys($normalizedData, $this->ignoredProperties);
46+
$this->redactKeys($normalizedData, $this->redactedProperties);
4347

4448
return $serializer->serialize($normalizedData, JsonEncoder::FORMAT, [
4549
'json_encode_options' => JSON_PRETTY_PRINT
@@ -56,14 +60,27 @@ public function match($expected, $actual)
5660
Assert::assertEquals($expected, $this->serialize($actual));
5761
}
5862

59-
private function unsetIgnoredKeys(array &$values, array $ignoredKeys): void
63+
private function ignoreKeys(array &$values, array $ignoredKeys): void
6064
{
6165
foreach($values as $key => $value) {
6266
if (in_array($key, $ignoredKeys)) {
6367
unset($values[$key]);
6468
}
6569
else if(is_array($value)) {
66-
$this->unsetIgnoredKeys($value, $ignoredKeys);
70+
$this->ignoreKeys($value, $ignoredKeys);
71+
$values[$key] = $value;
72+
}
73+
}
74+
}
75+
76+
private function redactKeys(array &$values, array $redactedKeys): void
77+
{
78+
foreach($values as $key => $value) {
79+
if (in_array($key, $redactedKeys)) {
80+
$values[$key] = 'REDACTED';
81+
}
82+
else if(is_array($value)) {
83+
$this->redactKeys($value, $redactedKeys);
6784
$values[$key] = $value;
6885
}
6986
}

src/Traits/AssertMatchesJsonObjectSnapshotTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ trait AssertMatchesJsonObjectSnapshotTrait
99
{
1010
use MatchesSnapshots;
1111

12-
public function assertMatchesJsonObjectSnapshot($actual, array $ignoredProperties = []): void
12+
public function assertMatchesJsonObjectSnapshot($actual, array $ignoredProperties = [], array $redactedProperties = []): void
1313
{
14-
$this->assertMatchesSnapshot($actual, new SnapshotJsonObjectDriver($ignoredProperties));
14+
$this->assertMatchesSnapshot($actual, new SnapshotJsonObjectDriver($ignoredProperties, $redactedProperties));
1515
}
1616
}

0 commit comments

Comments
 (0)