From 34e7b0dcf2f3ecaf57b33882b994fcabc5efa53b Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 12 Nov 2025 07:40:19 -0300 Subject: [PATCH 1/2] fix(tests): Add test for memory usage at `90%` threshold in `ApplicationMemoryTest` class. --- CHANGELOG.md | 3 ++- .../http/stateless/ApplicationMemoryTest.php | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71d405e..4b061a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,8 @@ - Bug #191: Refactor test imports for consistency and add support files (@terabytesoftw) - Bug #192: Add copyright and license information to `MockerExtension::class` and refactor `tests/support/bootstrap.php` (@terabytesoftw) - Bug #193: Update `.editorconfig` and `.gitignore` for improved consistency and clarity (@terabytesoftw) -- Bug #194: Update symplify/easy-coding-standard requirement from `^12.5` to `^13.0` (@dependabot) +- Dep #194: Update symplify/easy-coding-standard requirement from `^12.5` to `^13.0` (@dependabot) +- Bug #195: Add test for memory usage at `90%` threshold in `ApplicationMemoryTest` class (@terabytesoftw) ## 0.1.1 October 6, 2025 diff --git a/tests/http/stateless/ApplicationMemoryTest.php b/tests/http/stateless/ApplicationMemoryTest.php index 5b49ac7..0350e58 100644 --- a/tests/http/stateless/ApplicationMemoryTest.php +++ b/tests/http/stateless/ApplicationMemoryTest.php @@ -75,6 +75,27 @@ public function testCleanBehaviorWithDifferentMemoryLimits( ini_set('memory_limit', $originalLimit); } + /** + * @throws InvalidConfigException if the configuration is invalid or incomplete. + */ + public function testCleanReturnsTrueWhenMemoryUsageIsExactlyAtNinetyPercentThreshold(): void + { + $app = $this->statelessApplication(); + + $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); + + $currentUsage = memory_get_usage(true); + + $exactLimit = (int) ($currentUsage / 0.9); + + $app->setMemoryLimit($exactLimit); + + self::assertTrue( + $app->clean(), + "Should return 'true' when memory usage is exactly at '90%' threshold (boundary condition).", + ); + } + /** * @throws InvalidConfigException if the configuration is invalid or incomplete. */ From 7a9509d4a856038a1a61c195f8c16a64c01c3bfd Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 12 Nov 2025 07:55:59 -0300 Subject: [PATCH 2/2] Apply nitpick comments Coderabbitai. --- .../http/stateless/ApplicationMemoryTest.php | 23 ++++--------------- .../provider/StatelessApplicationProvider.php | 22 +++++------------- 2 files changed, 10 insertions(+), 35 deletions(-) diff --git a/tests/http/stateless/ApplicationMemoryTest.php b/tests/http/stateless/ApplicationMemoryTest.php index 0350e58..81a7bcb 100644 --- a/tests/http/stateless/ApplicationMemoryTest.php +++ b/tests/http/stateless/ApplicationMemoryTest.php @@ -45,11 +45,8 @@ final class ApplicationMemoryTest extends TestCase * @throws InvalidConfigException if the configuration is invalid or incomplete. */ #[DataProviderExternal(StatelessApplicationProvider::class, 'memoryThreshold')] - public function testCleanBehaviorWithDifferentMemoryLimits( - string $memoryLimit, - bool $shouldTriggerSpecialTest, - string $assertionMessage, - ): void { + public function testCleanBehaviorWithDifferentMemoryLimits(string $memoryLimit, string $assertionMessage): void + { $originalLimit = ini_get('memory_limit'); ini_set('memory_limit', $memoryLimit); @@ -58,19 +55,7 @@ public function testCleanBehaviorWithDifferentMemoryLimits( $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); - if ($shouldTriggerSpecialTest === false) { - self::assertFalse($app->clean(), $assertionMessage); - } - - if ($shouldTriggerSpecialTest === true) { - $currentUsage = memory_get_usage(true); - - $artificialLimit = (int) ($currentUsage / 0.9); - - $app->setMemoryLimit($artificialLimit); - - self::assertTrue($app->clean(), $assertionMessage); - } + self::assertFalse($app->clean(), $assertionMessage); ini_set('memory_limit', $originalLimit); } @@ -78,7 +63,7 @@ public function testCleanBehaviorWithDifferentMemoryLimits( /** * @throws InvalidConfigException if the configuration is invalid or incomplete. */ - public function testCleanReturnsTrueWhenMemoryUsageIsExactlyAtNinetyPercentThreshold(): void + public function testCleanReturnsTrueWhenMemoryUsageIsExactlyPercentThreshold(): void { $app = $this->statelessApplication(); diff --git a/tests/provider/StatelessApplicationProvider.php b/tests/provider/StatelessApplicationProvider.php index 6fa0c1d..84fb184 100644 --- a/tests/provider/StatelessApplicationProvider.php +++ b/tests/provider/StatelessApplicationProvider.php @@ -443,36 +443,26 @@ public static function memoryLimitPositive(): array * This provider supplies test cases for validating the logic that determines whether memory cleanup should be * triggered based on current memory usage and configured memory limits. * - * Each test case includes the memory limit string, the expected boolean result indicating whether cleanup should - * occur, and an assertion message describing the expected outcome. + * Each test case includes the memory limit string and an assertion message describing the expected outcome. * - * @return array test data with memory limit, expected cleanup result, and assertion message. + * @return array test data with memory limit and assertion message. * - * @phpstan-return array + * @phpstan-return array */ public static function memoryThreshold(): array { return [ 'low usage - should not clean' => [ '1G', - false, - "'clean()' should return 'false' when memory usage is below '90%' threshold with '1G' limit.", + "Should return 'false' when memory usage is below '90%' threshold with '1G' limit.", ], 'moderate usage - should not clean' => [ '512M', - false, - "'clean()' should return 'false' when memory usage is below '90%' threshold with '512M' limit.", + "Should return 'false' when memory usage is below '90%' threshold with '512M' limit.", ], 'threshold calculation - 100M' => [ '100M', - false, - "'clean()' should return 'false' with '100M' limit and verify correct '90%' threshold calculation.", - ], - 'high memory setup - 2G' => [ - '2G', - true, - "'clean()' should return 'true' when memory usage equals the calculated '90%' threshold " . - '(using adjusted limit).', + "Should return 'false' with '100M' limit and verify correct '90%' threshold calculation.", ], ]; }