Skip to content

Commit 9784722

Browse files
committed
fix: make possible test when delete a key
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 724eaf2 commit 9784722

1 file changed

Lines changed: 87 additions & 31 deletions

File tree

tests/php/lib/AppConfigOverwrite.php

Lines changed: 87 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44
/**
5-
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
5+
* SPDX-FileCopyrightText: 2020-2025 LibreCode coop and contributors
66
* SPDX-License-Identifier: AGPL-3.0-or-later
77
*/
88

@@ -19,7 +19,8 @@
1919

2020
class AppConfigOverwrite extends AppConfig {
2121
/** @var string|bool|array|float|int[][] */
22-
private $overWrite = [];
22+
private array $overWrite = [];
23+
private array $deleted = [];
2324

2425
public function __construct(
2526
IDBConnection $connection,
@@ -47,11 +48,12 @@ public function getValueMixed(
4748
string $default = '',
4849
?bool $lazy = false,
4950
): string {
50-
if (isset($this->overWrite[$app]) && isset($this->overWrite[$app][$key])) {
51-
return $this->overWrite[$app][$key];
52-
}
53-
54-
return parent::getValue($app, $key, $default);
51+
return $this->getOverwrite(
52+
$app,
53+
$key,
54+
$default,
55+
fn () => parent::getValue($app, $key, (string)$default) // AppConfig::getValue retorna string
56+
);
5557
}
5658

5759
public function setValueMixed(
@@ -61,57 +63,111 @@ public function setValueMixed(
6163
bool $lazy = false,
6264
bool $sensitive = false,
6365
): bool {
64-
$this->overWrite[$app][$key] = $value;
65-
return true;
66+
return $this->setOverwrite($app, $key, $value);
6667
}
6768

6869
public function hasKey(string $app, string $key, ?bool $lazy = false): bool {
69-
if (isset($this->overWrite[$app]) && isset($this->overWrite[$app][$key])) {
70+
if ($this->isDeleted($app, $key)) {
71+
return false;
72+
}
73+
if (isset($this->overWrite[$app]) && array_key_exists($key, $this->overWrite[$app])) {
7074
return true;
7175
}
7276
return parent::hasKey($app, $key, $lazy);
7377
}
7478

7579
public function getValueArray(string $app, string $key, array $default = [], bool $lazy = false): array {
76-
if (isset($this->overWrite[$app]) && isset($this->overWrite[$app][$key])) {
77-
return $this->overWrite[$app][$key];
78-
}
79-
80-
return parent::getValueArray($app, $key, $default);
80+
return $this->getOverwrite(
81+
$app,
82+
$key,
83+
$default,
84+
fn () => parent::getValueArray($app, $key, $default),
85+
);
8186
}
8287

8388
public function setValueArray(string $app, string $key, array $value, bool $lazy = false, bool $sensitive = false): bool {
84-
$this->overWrite[$app][$key] = $value;
85-
return true;
89+
return $this->setOverwrite($app, $key, $value);
90+
}
91+
92+
public function getValueBool(string $app, string $key, bool $default = false, bool $lazy = false): bool {
93+
return $this->getOverwrite(
94+
$app,
95+
$key,
96+
$default,
97+
fn () => parent::getValueBool($app, $key, $default)
98+
);
8699
}
87100

88101
public function setValueBool(string $app, string $key, bool $value, bool $lazy = false): bool {
89-
$this->overWrite[$app][$key] = $value;
90-
return true;
102+
return $this->setOverwrite($app, $key, $value);
91103
}
92104

93-
public function getValueBool(string $app, string $key, bool $default = false, bool $lazy = false): bool {
94-
if (isset($this->overWrite[$app]) && isset($this->overWrite[$app][$key])) {
95-
return $this->overWrite[$app][$key];
96-
}
105+
public function getValueString(string $app, string $key, string $default = '', bool $lazy = false): string {
106+
return $this->getOverwrite(
107+
$app,
108+
$key,
109+
$default,
110+
fn () => parent::getValueString($app, $key, $default)
111+
);
112+
}
97113

98-
return parent::getValueBool($app, $key, $default);
114+
public function setValueString(string $app, string $key, string $value, bool $lazy = false, bool $sensitive = false): bool {
115+
return $this->setOverwrite($app, $key, $value);
99116
}
100117

101-
public function getValueString(string $app, string $key, string $default = '', bool $lazy = false): string {
102-
if (isset($this->overWrite[$app]) && isset($this->overWrite[$app][$key])) {
103-
return $this->overWrite[$app][$key];
118+
public function getValueInt(string $app, string $key, int $default = 0, bool $lazy = false): int {
119+
return $this->getOverwrite(
120+
$app,
121+
$key,
122+
$default,
123+
fn () => parent::getValueInt($app, $key, $default)
124+
);
125+
}
126+
127+
public function setValueInt(string $app, string $key, int $value, bool $lazy = false, bool $sensitive = false): bool {
128+
return $this->setOverwrite($app, $key, $value);
129+
}
130+
131+
public function deleteKey(string $app, string $key): void {
132+
if (isset($this->overWrite[$app])) {
133+
unset($this->overWrite[$app][$key]);
134+
if (empty($this->overWrite[$app])) {
135+
unset($this->overWrite[$app]);
136+
}
104137
}
138+
$this->markDeleted($app, $key);
139+
}
105140

106-
return parent::getValueString($app, $key, $default);
141+
private function isDeleted(string $app, string $key): bool {
142+
return isset($this->deleted[$app][$key]);
107143
}
108144

109-
public function setValueString(string $app, string $key, string $value, bool $lazy = false, bool $sensitive = false): bool {
145+
private function markDeleted(string $app, string $key): void {
146+
$this->deleted[$app][$key] = true;
147+
}
148+
149+
private function clearDeleted(string $app, string $key): void {
150+
if (isset($this->deleted[$app][$key])) {
151+
unset($this->deleted[$app][$key]);
152+
if (empty($this->deleted[$app])) {
153+
unset($this->deleted[$app]);
154+
}
155+
}
156+
}
157+
158+
private function setOverwrite(string $app, string $key, mixed $value): bool {
110159
$this->overWrite[$app][$key] = $value;
160+
$this->clearDeleted($app, $key);
111161
return true;
112162
}
113163

114-
public function deleteKey(string $app, string $key): void {
115-
unset($this->overWrite[$app][$key]);
164+
private function getOverwrite(string $app, string $key, mixed $default, callable $parentGetter): mixed {
165+
if ($this->isDeleted($app, $key)) {
166+
return $default;
167+
}
168+
if (isset($this->overWrite[$app]) && array_key_exists($key, $this->overWrite[$app])) {
169+
return $this->overWrite[$app][$key];
170+
}
171+
return $parentGetter();
116172
}
117173
}

0 commit comments

Comments
 (0)