Skip to content

Commit 1bb9e44

Browse files
committed
refactor(tests): melhorar a formatação e a legibilidade dos testes de validação de configuração e estatísticas do JsonBufferPool
1 parent 4598853 commit 1bb9e44

File tree

4 files changed

+103
-63
lines changed

4 files changed

+103
-63
lines changed

src/Json/Pool/JsonBufferPool.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static function getStatistics(): array
169169
if (preg_match('/^buffer_(\d+)$/', $key, $matches)) {
170170
$capacity = (int)$matches[1];
171171
$readableKey = self::formatCapacity($capacity);
172-
172+
173173
$poolSizes[$readableKey] = $poolSize;
174174
$poolsByCapacity[$capacity] = [
175175
'key' => $key,
@@ -262,29 +262,39 @@ private static function validateConfiguration(array $config): void
262262
// Validate 'max_pool_size'
263263
if (isset($config['max_pool_size'])) {
264264
if (!is_int($config['max_pool_size']) || $config['max_pool_size'] <= 0) {
265-
throw new \InvalidArgumentException("'max_pool_size' must be a positive integer, got: " . gettype($config['max_pool_size']));
265+
throw new \InvalidArgumentException(
266+
"'max_pool_size' must be a positive integer, got: " . gettype($config['max_pool_size'])
267+
);
266268
}
267269
if ($config['max_pool_size'] > 1000) {
268-
throw new \InvalidArgumentException("'max_pool_size' cannot exceed 1000 for memory safety, got: {$config['max_pool_size']}");
270+
throw new \InvalidArgumentException(
271+
"'max_pool_size' cannot exceed 1000 for memory safety, got: {$config['max_pool_size']}"
272+
);
269273
}
270274
}
271275

272276
// Validate 'default_capacity'
273277
if (isset($config['default_capacity'])) {
274278
if (!is_int($config['default_capacity']) || $config['default_capacity'] <= 0) {
275-
throw new \InvalidArgumentException("'default_capacity' must be a positive integer, got: " . gettype($config['default_capacity']));
279+
throw new \InvalidArgumentException(
280+
"'default_capacity' must be a positive integer, got: " . gettype($config['default_capacity'])
281+
);
276282
}
277283
if ($config['default_capacity'] > 1024 * 1024) { // 1MB limit
278-
throw new \InvalidArgumentException("'default_capacity' cannot exceed 1MB (1048576 bytes), got: {$config['default_capacity']}");
284+
throw new \InvalidArgumentException(
285+
"'default_capacity' cannot exceed 1MB (1048576 bytes), got: {$config['default_capacity']}"
286+
);
279287
}
280288
}
281289

282290
// Validate 'size_categories'
283291
if (isset($config['size_categories'])) {
284292
if (!is_array($config['size_categories'])) {
285-
throw new \InvalidArgumentException("'size_categories' must be an array, got: " . gettype($config['size_categories']));
293+
throw new \InvalidArgumentException(
294+
"'size_categories' must be an array, got: " . gettype($config['size_categories'])
295+
);
286296
}
287-
297+
288298
if (empty($config['size_categories'])) {
289299
throw new \InvalidArgumentException("'size_categories' cannot be empty");
290300
}
@@ -293,30 +303,36 @@ private static function validateConfiguration(array $config): void
293303
if (!is_string($name) || empty($name)) {
294304
throw new \InvalidArgumentException("Size category names must be non-empty strings");
295305
}
296-
306+
297307
if (!is_int($capacity) || $capacity <= 0) {
298-
throw new \InvalidArgumentException("Size category '{$name}' must have a positive integer capacity, got: " . gettype($capacity));
308+
throw new \InvalidArgumentException(
309+
"Size category '{$name}' must have a positive integer capacity, got: " . gettype($capacity)
310+
);
299311
}
300-
312+
301313
if ($capacity > 1024 * 1024) { // 1MB limit per category
302-
throw new \InvalidArgumentException("Size category '{$name}' capacity cannot exceed 1MB (1048576 bytes), got: {$capacity}");
314+
throw new \InvalidArgumentException(
315+
"Size category '{$name}' capacity cannot exceed 1MB (1048576 bytes), got: {$capacity}"
316+
);
303317
}
304318
}
305319

306320
// Validate categories are in ascending order for optimal selection
307321
$capacities = array_values($config['size_categories']);
308322
$sortedCapacities = $capacities;
309323
sort($sortedCapacities);
310-
324+
311325
if ($capacities !== $sortedCapacities) {
312-
throw new \InvalidArgumentException("'size_categories' should be ordered from smallest to largest capacity for optimal selection");
326+
throw new \InvalidArgumentException(
327+
"'size_categories' should be ordered from smallest to largest capacity for optimal selection"
328+
);
313329
}
314330
}
315331

316332
// Check for unknown configuration keys
317333
$validKeys = ['max_pool_size', 'default_capacity', 'size_categories'];
318334
$unknownKeys = array_diff(array_keys($config), $validKeys);
319-
335+
320336
if (!empty($unknownKeys)) {
321337
throw new \InvalidArgumentException("Unknown configuration keys: " . implode(', ', $unknownKeys));
322338
}

tests/Json/Pool/JsonBufferPoolConfigValidationTest.php

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function setUp(): void
2222

2323
protected function tearDown(): void
2424
{
25-
// Clear pools and reset configuration after each test
25+
// Clear pools and reset configuration after each test
2626
JsonBufferPool::clearPools();
2727
JsonBufferPool::resetConfiguration();
2828
}
@@ -125,68 +125,82 @@ public function testSizeCategoriesNameValidation(): void
125125
{
126126
$this->expectException(InvalidArgumentException::class);
127127
$this->expectExceptionMessage("Size category names must be non-empty strings");
128-
JsonBufferPool::configure([
129-
'size_categories' => [
130-
123 => 1024 // Invalid numeric key
128+
JsonBufferPool::configure(
129+
[
130+
'size_categories' => [
131+
123 => 1024 // Invalid numeric key
132+
]
131133
]
132-
]);
134+
);
133135
}
134136

135137
public function testSizeCategoriesEmptyNameInvalid(): void
136138
{
137139
$this->expectException(InvalidArgumentException::class);
138140
$this->expectExceptionMessage("Size category names must be non-empty strings");
139-
JsonBufferPool::configure([
140-
'size_categories' => [
141-
'' => 1024 // Empty string key
141+
JsonBufferPool::configure(
142+
[
143+
'size_categories' => [
144+
'' => 1024 // Empty string key
145+
]
142146
]
143-
]);
147+
);
144148
}
145149

146150
public function testSizeCategoriesCapacityValidation(): void
147151
{
148152
$this->expectException(InvalidArgumentException::class);
149153
$this->expectExceptionMessage("Size category 'small' must have a positive integer capacity, got: string");
150-
JsonBufferPool::configure([
151-
'size_categories' => [
152-
'small' => 'invalid'
154+
JsonBufferPool::configure(
155+
[
156+
'size_categories' => [
157+
'small' => 'invalid'
158+
]
153159
]
154-
]);
160+
);
155161
}
156162

157163
public function testSizeCategoriesCapacityZeroInvalid(): void
158164
{
159165
$this->expectException(InvalidArgumentException::class);
160166
$this->expectExceptionMessage("Size category 'small' must have a positive integer capacity");
161-
JsonBufferPool::configure([
162-
'size_categories' => [
163-
'small' => 0
167+
JsonBufferPool::configure(
168+
[
169+
'size_categories' => [
170+
'small' => 0
171+
]
164172
]
165-
]);
173+
);
166174
}
167175

168176
public function testSizeCategoriesCapacityUpperLimit(): void
169177
{
170178
$this->expectException(InvalidArgumentException::class);
171179
$this->expectExceptionMessage("Size category 'huge' capacity cannot exceed 1MB (1048576 bytes), got: 1048577");
172-
JsonBufferPool::configure([
173-
'size_categories' => [
174-
'huge' => 1048577
180+
JsonBufferPool::configure(
181+
[
182+
'size_categories' => [
183+
'huge' => 1048577
184+
]
175185
]
176-
]);
186+
);
177187
}
178188

179189
public function testSizeCategoriesOrderValidation(): void
180190
{
181191
$this->expectException(InvalidArgumentException::class);
182-
$this->expectExceptionMessage("'size_categories' should be ordered from smallest to largest capacity for optimal selection");
183-
JsonBufferPool::configure([
184-
'size_categories' => [
185-
'large' => 16384,
186-
'small' => 1024, // Out of order
187-
'medium' => 4096
192+
$this->expectExceptionMessage(
193+
"'size_categories' should be ordered from smallest to largest capacity for optimal selection"
194+
);
195+
JsonBufferPool::configure(
196+
[
197+
'size_categories' => [
198+
'large' => 16384,
199+
'small' => 1024, // Out of order
200+
'medium' => 4096
201+
]
188202
]
189-
]);
203+
);
190204
}
191205

192206
/**
@@ -196,11 +210,13 @@ public function testUnknownConfigurationKeys(): void
196210
{
197211
$this->expectException(InvalidArgumentException::class);
198212
$this->expectExceptionMessage("Unknown configuration keys: unknown_key, another_unknown");
199-
JsonBufferPool::configure([
200-
'max_pool_size' => 50,
201-
'unknown_key' => 'value',
202-
'another_unknown' => 123
203-
]);
213+
JsonBufferPool::configure(
214+
[
215+
'max_pool_size' => 50,
216+
'unknown_key' => 'value',
217+
'another_unknown' => 123
218+
]
219+
);
204220
}
205221

206222
/**
@@ -242,17 +258,19 @@ public function testPartialConfigurationUpdates(): void
242258
JsonBufferPool::configure(['max_pool_size' => 75]);
243259
$this->assertTrue(true);
244260

245-
// Configure only default_capacity
261+
// Configure only default_capacity
246262
JsonBufferPool::configure(['default_capacity' => 2048]);
247263
$this->assertTrue(true);
248264

249265
// Configure only size_categories
250-
JsonBufferPool::configure([
251-
'size_categories' => [
252-
'custom_small' => 512,
253-
'custom_large' => 8192
266+
JsonBufferPool::configure(
267+
[
268+
'size_categories' => [
269+
'custom_small' => 512,
270+
'custom_large' => 8192
271+
]
254272
]
255-
]);
273+
);
256274
$this->assertTrue(true);
257275
}
258276

@@ -262,10 +280,12 @@ public function testPartialConfigurationUpdates(): void
262280
public function testConfigurationMerging(): void
263281
{
264282
// Set initial config
265-
JsonBufferPool::configure([
266-
'max_pool_size' => 100,
267-
'default_capacity' => 4096
268-
]);
283+
JsonBufferPool::configure(
284+
[
285+
'max_pool_size' => 100,
286+
'default_capacity' => 4096
287+
]
288+
);
269289

270290
// Update only one value
271291
JsonBufferPool::configure(['max_pool_size' => 200]);
@@ -274,4 +294,4 @@ public function testConfigurationMerging(): void
274294
$buffer = JsonBufferPool::getBuffer();
275295
$this->assertInstanceOf(\PivotPHP\Core\Json\Pool\JsonBuffer::class, $buffer);
276296
}
277-
}
297+
}

tests/Json/Pool/JsonBufferPoolStatisticsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function setUp(): void
2121

2222
protected function tearDown(): void
2323
{
24-
// Clear pools and reset configuration after each test
24+
// Clear pools and reset configuration after each test
2525
JsonBufferPool::clearPools();
2626
JsonBufferPool::resetConfiguration();
2727
}
@@ -36,7 +36,7 @@ public function testBasicStatisticsStructure(): void
3636
// Verify all expected keys are present
3737
$expectedKeys = [
3838
'reuse_rate', 'total_operations', 'current_usage', 'peak_usage',
39-
'total_buffers_pooled', 'active_pool_count', 'pool_sizes',
39+
'total_buffers_pooled', 'active_pool_count', 'pool_sizes',
4040
'pools_by_capacity', 'detailed_stats'
4141
];
4242

@@ -242,4 +242,4 @@ public function testStatisticsConsistency(): void
242242
$this->assertEquals(count($stats['pool_sizes']), count($stats['pools_by_capacity']));
243243
$this->assertEquals($stats['active_pool_count'], count($stats['pools_by_capacity']));
244244
}
245-
}
245+
}

tests/Stress/HighPerformanceStressTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ public function testPoolOverflowBehavior(): void
187187
*/
188188
public function testCircuitBreakerUnderFailures(): void
189189
{
190-
$this->markTestSkipped('Circuit breaker behavior is environment-dependent and will be tested in dedicated stress tests');
190+
$this->markTestSkipped(
191+
'Circuit breaker behavior is environment-dependent and will be tested in dedicated stress tests'
192+
);
191193
$this->app->middleware('circuit-breaker');
192194

193195
// Simulate service failures
@@ -249,7 +251,9 @@ function ($req, $res) use ($shouldFail) {
249251
*/
250252
public function testLoadSheddingEffectiveness(): void
251253
{
252-
$this->markTestSkipped('Load shedding behavior is environment-dependent and will be tested in dedicated stress tests');
254+
$this->markTestSkipped(
255+
'Load shedding behavior is environment-dependent and will be tested in dedicated stress tests'
256+
);
253257
$this->app->middleware(
254258
'load-shedder',
255259
[

0 commit comments

Comments
 (0)