Repository navigation
Expand file tree
/
Copy pathclass-mock-store.php
More file actions
116 lines (95 loc) · 3.01 KB
/
Copy pathclass-mock-store.php
File metadata and controls
116 lines (95 loc) · 3.01 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
111
112
113
114
115
116
<?php
/**
* Configurable WP_Secrets_Store test double.
*
* Can be told to fail any operation on demand, and records every value it is ever
* handed so that a test can assert it was never given a plaintext.
*/
class Mock_Store extends WP_Secrets_Store {
/**
* Stored records, keyed by scope and name.
*
* @var array<string, array<mixed>>
*/
private $records = array();
/**
* Which operations are configured to fail.
*
* @var array<string, bool>
*/
private $fail = array(
'get' => false,
'set' => false,
'delete' => false,
'list' => false,
);
/**
* Every record ever passed to set(), in call order.
*
* @var array<int, array<mixed>>
*/
private $received_records = array();
public function get( string $name, bool $network = false ) {
if ( $this->fail['get'] ) {
return new WP_Error( WP_SECRETS_ERROR_STORE_UNAVAILABLE, 'Mock_Store: get() configured to fail.' );
}
$key = $this->key( $name, $network );
return $this->records[ $key ] ?? null;
}
public function set( string $name, array $record, bool $network = false ) {
$this->received_records[] = $record;
if ( $this->fail['set'] ) {
return new WP_Error( WP_SECRETS_ERROR_STORE_UNAVAILABLE, 'Mock_Store: set() configured to fail.' );
}
$this->records[ $this->key( $name, $network ) ] = $record;
return true;
}
public function delete( string $name, bool $network = false ) {
if ( $this->fail['delete'] ) {
return new WP_Error( WP_SECRETS_ERROR_STORE_UNAVAILABLE, 'Mock_Store: delete() configured to fail.' );
}
unset( $this->records[ $this->key( $name, $network ) ] );
return true;
}
public function list_names( bool $network = false ) {
if ( $this->fail['list'] ) {
return new WP_Error( WP_SECRETS_ERROR_STORE_UNAVAILABLE, 'Mock_Store: list_names() configured to fail.' );
}
$prefix = $network ? 'network:' : 'site:';
$names = array();
foreach ( array_keys( $this->records ) as $key ) {
if ( str_starts_with( $key, $prefix ) ) {
$names[] = substr( $key, strlen( $prefix ) );
}
}
return $names;
}
/**
* @param string $operation One of 'get', 'set', 'delete', 'list'.
* @param bool $fail Whether that operation should return WP_Error.
*
* @return $this
*/
public function configure_fail( string $operation, bool $fail = true ): self {
$this->fail[ $operation ] = $fail;
return $this;
}
/**
* Every record ever passed to set(), in call order – including ones set()
* went on to reject, since the assertion this exists for is "was this store
* ever handed a plaintext," not "was a plaintext ever successfully stored."
*
* @return array<int, array<mixed>>
*/
public function get_received_records(): array {
return $this->received_records;
}
/**
* @param string $name The secret's namespaced name.
* @param bool $network Whether this is a network-scope secret.
* @return string The key the record is held under.
*/
private function key( string $name, bool $network ): string {
return ( $network ? 'network:' : 'site:' ) . $name;
}
}