-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathArrayRedactorTest.php
More file actions
110 lines (97 loc) · 3.2 KB
/
Copy pathArrayRedactorTest.php
File metadata and controls
110 lines (97 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
use PHPUnit\Framework\TestCase;
use Mtownsend\ArrayRedactor\ArrayRedactor;
class ArrayRedactorTest extends TestCase
{
protected function setUp(): void
{
$this->content = [
'email' => 'mtownsend5512@gmail.com',
'password' => 'secret123',
'changes' => [
'account' => [
'old_password' => 'secret321',
'new_password' => 'secret789'
],
]
];
}
/** @test */
public function can_instantiate_class_through_helper()
{
$result = array_redactor($this->content, ['password'], '[REDACTED]')->redact();
$this->assertEquals(array_merge($this->content, [
'password' => '[REDACTED]'
]), $result);
}
/** @test */
public function can_accept_array_content()
{
$result = (new ArrayRedactor($this->content, ['password'], '[REDACTED]'))->redact();
$this->assertEquals(array_merge($this->content, [
'password' => '[REDACTED]'
]), $result);
}
/** @test */
public function can_accept_json_content()
{
$result = (new ArrayRedactor(json_encode($this->content), ['password'], '[REDACTED]'))->redact();
$this->assertEquals(array_merge($this->content, [
'password' => '[REDACTED]'
]), $result);
}
/** @test */
public function can_be_invoked()
{
$result = new ArrayRedactor($this->content, ['password'], '[REDACTED]');
$this->assertEquals(array_merge($this->content, [
'password' => '[REDACTED]'
]), $result());
}
/** @test */
public function can_be_cast_as_json()
{
$result = new ArrayRedactor($this->content, ['password'], '[REDACTED]');
$this->assertEquals(json_encode(array_merge($this->content, [
'password' => '[REDACTED]'
])), (string) $result);
}
/** @test */
public function can_redact_nested_keys()
{
$result = (new ArrayRedactor($this->content, ['old_password', 'new_password'], '[REDACTED]'))->redact();
$this->assertEquals(array_merge($this->content, [
'changes' => ['account' => [
'old_password' => '[REDACTED]',
'new_password' => '[REDACTED]'
]]
]), $result);
}
/** @test */
public function can_change_redaction_ink()
{
$result = (new ArrayRedactor($this->content, ['password'], null))->redact();
$this->assertEquals(array_merge($this->content, [
'password' => null
]), $result);
}
/** @test */
public function can_omit_constructor_arguments_in_favor_of_methods()
{
$result = (new ArrayRedactor())->content($this->content)->keys(['password'])->ink(null)->redact();
$this->assertEquals(array_merge($this->content, [
'password' => null
]), $result);
}
/** @test */
public function can_accept_closure_in_ink()
{
$func = function(string $val, string $key): string {
return $key === 'email' ? substr($val, stripos($val, '@')) : $val;
};
$result = (new ArrayRedactor())->content($this->content)->keys(['email'])->ink($func)->redact();
$this->assertEquals(array_merge($this->content, [
'email' => '@gmail.com'
]), $result);
}
}