Skip to content

Commit ccab7ce

Browse files
committed
refactor(tests): simplificar mensagens de exceção para validação de configuração no JsonBufferPool
1 parent d90a33a commit ccab7ce

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

src/Json/Pool/JsonBufferPool.php

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,15 @@ public static function getStatistics(): array
191191
// Sort pools by capacity for better readability
192192
ksort($poolsByCapacity);
193193

194-
// Sort pool_sizes by extracting numeric capacity for consistent ordering
195-
uksort(
196-
$poolSizes,
197-
function ($a, $b) {
198-
// Extract numeric capacity from formatted strings like "1.0KB (1024 bytes)"
199-
preg_match('/\((\d+) bytes\)/', $a, $matchesA);
200-
preg_match('/\((\d+) bytes\)/', $b, $matchesB);
201-
202-
$capacityA = isset($matchesA[1]) ? (int)$matchesA[1] : 0;
203-
$capacityB = isset($matchesB[1]) ? (int)$matchesB[1] : 0;
204-
205-
return $capacityA <=> $capacityB;
194+
// Sort pool_sizes based on numeric capacities from poolsByCapacity
195+
$sortedPoolSizes = [];
196+
foreach (array_keys($poolsByCapacity) as $capacity) {
197+
$readableKey = $poolsByCapacity[$capacity]['capacity_formatted'];
198+
if (isset($poolSizes[$readableKey])) {
199+
$sortedPoolSizes[$readableKey] = $poolSizes[$readableKey];
206200
}
207-
);
201+
}
202+
$poolSizes = $sortedPoolSizes;
208203

209204
return [
210205
'reuse_rate' => round($reuseRate, 2),
@@ -281,11 +276,12 @@ private static function validateConfiguration(array $config): void
281276
{
282277
// Validate 'max_pool_size'
283278
if (isset($config['max_pool_size'])) {
279+
// First check type
284280
if (!is_int($config['max_pool_size'])) {
285-
throw new \InvalidArgumentException(
286-
"'max_pool_size' must be an integer, got: " . gettype($config['max_pool_size'])
287-
);
281+
throw new \InvalidArgumentException("'max_pool_size' must be an integer");
288282
}
283+
284+
// Then check range
289285
if ($config['max_pool_size'] <= 0) {
290286
throw new \InvalidArgumentException("'max_pool_size' must be a positive integer");
291287
}
@@ -298,11 +294,12 @@ private static function validateConfiguration(array $config): void
298294

299295
// Validate 'default_capacity'
300296
if (isset($config['default_capacity'])) {
297+
// First check type
301298
if (!is_int($config['default_capacity'])) {
302-
throw new \InvalidArgumentException(
303-
"'default_capacity' must be an integer, got: " . gettype($config['default_capacity'])
304-
);
299+
throw new \InvalidArgumentException("'default_capacity' must be an integer");
305300
}
301+
302+
// Then check range
306303
if ($config['default_capacity'] <= 0) {
307304
throw new \InvalidArgumentException("'default_capacity' must be a positive integer");
308305
}
@@ -315,10 +312,9 @@ private static function validateConfiguration(array $config): void
315312

316313
// Validate 'size_categories'
317314
if (isset($config['size_categories'])) {
315+
// First check type
318316
if (!is_array($config['size_categories'])) {
319-
throw new \InvalidArgumentException(
320-
"'size_categories' must be an array, got: " . gettype($config['size_categories'])
321-
);
317+
throw new \InvalidArgumentException("'size_categories' must be an array");
322318
}
323319

324320
if (empty($config['size_categories'])) {
@@ -330,11 +326,14 @@ private static function validateConfiguration(array $config): void
330326
throw new \InvalidArgumentException("Size category names must be non-empty strings");
331327
}
332328

329+
// First check type for each capacity
333330
if (!is_int($capacity)) {
334331
throw new \InvalidArgumentException(
335-
"Size category '{$name}' must have an integer capacity, got: " . gettype($capacity)
332+
"Size category '{$name}' must have an integer capacity"
336333
);
337334
}
335+
336+
// Then check range
338337
if ($capacity <= 0) {
339338
throw new \InvalidArgumentException(
340339
"Size category '{$name}' must have a positive integer capacity"

tests/Json/Pool/JsonBufferPoolConfigValidationTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testMaxPoolSizeZeroInvalid(): void
6969
public function testMaxPoolSizeTypeValidation(): void
7070
{
7171
$this->expectException(InvalidArgumentException::class);
72-
$this->expectExceptionMessage("'max_pool_size' must be an integer, got: string");
72+
$this->expectExceptionMessage("'max_pool_size' must be an integer");
7373
JsonBufferPool::configure(['max_pool_size' => '100']);
7474
}
7575

@@ -93,7 +93,7 @@ public function testDefaultCapacityValidation(): void
9393
public function testDefaultCapacityTypeValidation(): void
9494
{
9595
$this->expectException(InvalidArgumentException::class);
96-
$this->expectExceptionMessage("'default_capacity' must be an integer, got: double");
96+
$this->expectExceptionMessage("'default_capacity' must be an integer");
9797
JsonBufferPool::configure(['default_capacity' => 4096.5]);
9898
}
9999

@@ -110,7 +110,7 @@ public function testDefaultCapacityUpperLimit(): void
110110
public function testSizeCategoriesTypeValidation(): void
111111
{
112112
$this->expectException(InvalidArgumentException::class);
113-
$this->expectExceptionMessage("'size_categories' must be an array, got: string");
113+
$this->expectExceptionMessage("'size_categories' must be an array");
114114
JsonBufferPool::configure(['size_categories' => 'invalid']);
115115
}
116116

@@ -150,7 +150,7 @@ public function testSizeCategoriesEmptyNameInvalid(): void
150150
public function testSizeCategoriesCapacityValidation(): void
151151
{
152152
$this->expectException(InvalidArgumentException::class);
153-
$this->expectExceptionMessage("Size category 'small' must have an integer capacity, got: string");
153+
$this->expectExceptionMessage("Size category 'small' must have an integer capacity");
154154
JsonBufferPool::configure(
155155
[
156156
'size_categories' => [

0 commit comments

Comments
 (0)