Skip to content

Commit 13e6e23

Browse files
committed
tweak: improve test coverage
1 parent 43606be commit 13e6e23

File tree

2 files changed

+119
-48
lines changed

2 files changed

+119
-48
lines changed

src/Cache.php

Lines changed: 86 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getDateTime(string $name, callable $callback):DateTimeInterface
6767
public function getArray(string $name, callable $callback):array {
6868
$value = $this->get($name, $callback);
6969
if(!is_array($value)) {
70-
throw new TypeError("Value with key '$name' is not an array");
70+
throw new TypeError("Data '$name' is not an array");
7171
}
7272

7373
return $value;
@@ -81,50 +81,98 @@ public function getArray(string $name, callable $callback):array {
8181
public function getTypedArray(string $name, string $className, callable $callback):array {
8282
$array = $this->get($name, $callback);
8383
if(!is_array($array)) {
84-
throw new TypeError("Value with key '$name' is not an array");
84+
throw new TypeError("Data '$name' is not an array");
8585
}
8686

8787
foreach($array as $key => $value) {
88-
if($className === "int" || $className === "integer") {
89-
if(!is_int($value)) {
90-
if(is_numeric($value)) {
91-
$array[$key] = (int)$value;
92-
}
93-
else {
94-
throw new TypeError("Array value at key '$key' is not an integer");
95-
}
96-
}
97-
}
98-
elseif($className === "float" || $className === "double") {
99-
if(!is_float($value)) {
100-
if(is_numeric($value)) {
101-
$array[$key] = (float)$value;
102-
}
103-
else {
104-
throw new TypeError("Array value at key '$key' is not a float");
105-
}
106-
}
107-
}
108-
elseif($className === "string") {
109-
if(!is_string($value)) {
110-
$array[$key] = (string)$value;
111-
}
112-
}
113-
elseif($className === "bool" || $className === "boolean") {
114-
if(!is_bool($value)) {
115-
$array[$key] = (bool)$value;
116-
}
117-
}
118-
else {
119-
if(!$value instanceof $className) {
120-
throw new TypeError("Array value at key '$key' is not an instance of $className");
121-
}
122-
}
88+
$array[$key] = $this->validateAndConvertValue($value, $className, $key);
12389
}
12490

12591
return $array;
12692
}
12793

94+
/**
95+
* @template T
96+
* @param mixed $value
97+
* @param class-string<T> $className
98+
* @param string|int $key
99+
* @return T
100+
*/
101+
private function validateAndConvertValue(mixed $value, string $className, string|int $key): mixed {
102+
return match(strtolower($className)) {
103+
"int", "integer" => $this->validateAndConvertInt($value, $key),
104+
"float", "double" => $this->validateAndConvertFloat($value, $key),
105+
"string" => $this->convertToString($value),
106+
"bool", "boolean" => $this->convertToBool($value),
107+
default => $this->validateInstance($value, $className, $key),
108+
};
109+
}
110+
111+
/**
112+
* @param mixed $value
113+
* @param string|int $key
114+
* @return int
115+
*/
116+
private function validateAndConvertInt(mixed $value, string|int $key): int {
117+
if(is_int($value)) {
118+
return $value;
119+
}
120+
121+
if(is_numeric($value)) {
122+
return (int)$value;
123+
}
124+
125+
throw new TypeError("Array value at key '$key' is not an integer");
126+
}
127+
128+
/**
129+
* @param mixed $value
130+
* @param string|int $key
131+
* @return float
132+
*/
133+
private function validateAndConvertFloat(mixed $value, string|int $key): float {
134+
if(is_float($value)) {
135+
return $value;
136+
}
137+
138+
if(is_numeric($value)) {
139+
return (float)$value;
140+
}
141+
142+
throw new TypeError("Array value at key '$key' is not a float");
143+
}
144+
145+
/**
146+
* @param mixed $value
147+
* @return string
148+
*/
149+
private function convertToString(mixed $value): string {
150+
return (string)$value;
151+
}
152+
153+
/**
154+
* @param mixed $value
155+
* @return bool
156+
*/
157+
private function convertToBool(mixed $value): bool {
158+
return (bool)$value;
159+
}
160+
161+
/**
162+
* @template T
163+
* @param mixed $value
164+
* @param class-string<T> $className
165+
* @param string|int $key
166+
* @return T
167+
*/
168+
private function validateInstance(mixed $value, string $className, string|int $key): object {
169+
if($value instanceof $className) {
170+
return $value;
171+
}
172+
173+
throw new TypeError("Array value at key '$key' is not an instance of $className");
174+
}
175+
128176
/**
129177
* @template T
130178
* @param class-string<T> $className
@@ -133,7 +181,7 @@ public function getTypedArray(string $name, string $className, callable $callbac
133181
public function getInstance(string $name, string $className, callable $callback):object {
134182
$value = $this->get($name, $callback);
135183
if(get_class($value) !== $className) {
136-
throw new TypeError("Value is not of type $className");
184+
throw new TypeError("Value is not an instance of $className");
137185
}
138186

139187
return $value;

test/phpunit/CacheTest.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ public function testGetInstance():void {
100100
self::assertSame($value->name, $class->name);
101101
}
102102

103+
public function testGetInstance_error():void {
104+
$value = new StdClass();
105+
$value->name = uniqid();
106+
107+
$sut = $this->getSut([
108+
"test" => $value,
109+
]);
110+
self::expectException(TypeError::class);
111+
self::expectExceptionMessage("Value is not an instance of SplFileInfo");
112+
$sut->getInstance("test", SplFileInfo::class, fn() => false);
113+
}
114+
103115
public function testGetArray():void {
104116
$value = [1, 2, 3];
105117
$sut = $this->getSut([
@@ -108,6 +120,26 @@ public function testGetArray():void {
108120
self::assertSame($value, $sut->getArray("numbers", fn() => []));
109121
}
110122

123+
public function testGetArray_notArray():void {
124+
$value = (object)[1, 2, 3];
125+
$sut = $this->getSut([
126+
"numbers" => $value,
127+
]);
128+
self::expectException(TypeError::class);
129+
self::expectExceptionMessage("Data 'numbers' is not an array");
130+
$sut->getArray("numbers", fn() => []);
131+
}
132+
133+
public function testGetTypedArray_notArray():void {
134+
$value = (object)[1, 2, 3];
135+
$sut = $this->getSut([
136+
"numbers" => $value,
137+
]);
138+
self::expectException(TypeError::class);
139+
self::expectExceptionMessage("Data 'numbers' is not an array");
140+
$sut->getTypedArray("numbers", "int", fn() => []);
141+
}
142+
111143
public function testGetTypedArray_int():void {
112144
$value = [1, "2", 3.000];
113145
$sut = $this->getSut([
@@ -186,20 +218,11 @@ public function testGetTypedArray_classError():void {
186218
$sut = $this->getSut([
187219
"files" => $value,
188220
]);
221+
self::expectExceptionMessage("Array value at key '1' is not an instance of SplFileInfo");
189222
self::expectException(TypeError::class);
190223
$sut->getTypedArray("files", SplFileInfo::class, fn() => []);
191224
}
192225

193-
public function testGetArray_notArray():void {
194-
$value = (object)[1, 2, 3];
195-
$sut = $this->getSut([
196-
"numbers" => $value,
197-
]);
198-
self::expectException(TypeError::class);
199-
self::expectExceptionMessage("Value with key 'numbers' is not an array");
200-
$sut->getArray("numbers", fn() => []);
201-
}
202-
203226
private function getSut(array $mockFiles = []):Cache {
204227
$mockFileAccess = null;
205228
if(!empty($mockFiles)) {

0 commit comments

Comments
 (0)