From 78003bc1495cd2b24840b8313b84351c02e6c350 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sat, 23 Aug 2025 06:54:22 -0400 Subject: [PATCH 1/3] test(http): Refactor memory threshold tests in `StatelessApplicationProvider` and update `ApplicationMemoryTest` class for improved validation scenarios. --- .../http/stateless/ApplicationMemoryTest.php | 96 ++++--------------- .../provider/StatelessApplicationProvider.php | 29 ++++++ 2 files changed, 49 insertions(+), 76 deletions(-) diff --git a/tests/http/stateless/ApplicationMemoryTest.php b/tests/http/stateless/ApplicationMemoryTest.php index f22e30cb..1732404f 100644 --- a/tests/http/stateless/ApplicationMemoryTest.php +++ b/tests/http/stateless/ApplicationMemoryTest.php @@ -30,73 +30,36 @@ final class ApplicationMemoryTest extends TestCase /** * @throws InvalidConfigException if the configuration is invalid or incomplete. */ - public function testCleanCalculatesCorrectMemoryThresholdWith90Percent(): void - { + #[DataProviderExternal(StatelessApplicationProvider::class, 'memoryThreshold')] + public function testCleanBehaviorWithDifferentMemoryLimits( + string $memoryLimit, + bool $shouldTriggerSpecialTest, + string $assertionMessage, + ): void { $originalLimit = ini_get('memory_limit'); - ini_set('memory_limit', '100M'); + ini_set('memory_limit', $memoryLimit); $app = $this->statelessApplication(); $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); - $memoryLimit = $app->getMemoryLimit(); - - self::assertFalse( - $app->clean(), - "'clean()' should return 'false' when memory usage is below the '90%' threshold of the configured memory " . - "limit ('100M'), confirming that no cleanup is needed in 'StatelessApplication'.", - ); - self::assertSame( - 104_857_600, - $memoryLimit, - "Memory limit should be exactly '104_857_600' bytes ('100M') for threshold calculation test in" . - "'StatelessApplication'.", - ); - self::assertSame( - 94_371_840.0, - $memoryLimit * 0.9, - "'90%' of '100M' should be exactly '94_371_840' bytes, not a division result like '116_508_444' bytes " . - "('100M' / '0.9') in 'StatelessApplication'.", - ); - - ini_set('memory_limit', $originalLimit); - } - /** - * @throws InvalidConfigException if the configuration is invalid or incomplete. - */ - public function testCleanReturnsTrueWhenMemoryUsageExactlyEqualsThreshold(): void - { - $originalLimit = ini_get('memory_limit'); - - ini_set('memory_limit', '2G'); - - $app = $this->statelessApplication(); - - $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); + if ($shouldTriggerSpecialTest === false) { + self::assertFalse($app->clean(), $assertionMessage); + } - $currentUsage = memory_get_usage(true); + if ($memoryLimit === '2G') { + $currentUsage = memory_get_usage(true); - $artificialLimit = (int) ($currentUsage / 0.9); + $artificialLimit = (int) ($currentUsage / 0.9); - $app->setMemoryLimit($artificialLimit); - $memoryLimit = $app->getMemoryLimit(); + $app->setMemoryLimit($artificialLimit); - self::assertTrue( - $app->clean(), - "'clean()' should return 'true' when memory usage is exactly at or above '90%' threshold ('>=' operator)" . - ", not only when strictly greater than ('>' operator) in 'StatelessApplication'.", - ); - self::assertSame( - $artificialLimit, - $memoryLimit, - "Memory limit should be set to the artificial limit '{$artificialLimit}' for threshold calculation test " . - "in 'StatelessApplication'.", - ); + self::assertTrue($app->clean(), $assertionMessage); + } ini_set('memory_limit', $originalLimit); } - /** * @throws InvalidConfigException if the configuration is invalid or incomplete. */ @@ -224,28 +187,6 @@ public function testGetMemoryLimitHandlesUnlimitedMemoryCorrectly(): void ini_set('memory_limit', $originalLimit); } - /** - * @throws InvalidConfigException if the configuration is invalid or incomplete. - */ - public function testReturnFalseFromCleanWhenMemoryUsageIsBelowThreshold(): void - { - $originalLimit = ini_get('memory_limit'); - - ini_set('memory_limit', '1G'); - - $app = $this->statelessApplication(); - - $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); - - self::assertFalse( - $app->clean(), - "'clean()' should return 'false' when memory usage is below '90%' of the configured 'memory_limit' in " . - "'StatelessApplication'.", - ); - - ini_set('memory_limit', $originalLimit); - } - /** * @throws InvalidConfigException if the configuration is invalid or incomplete. */ @@ -302,7 +243,7 @@ public function testSetMemoryLimitWithNonPositiveValueTriggersRecalculation(int ini_set('memory_limit', '128M'); - // after setting non-positive value, it should recalculate from system + // after setting non-positive value, it should recalculate from the system $secondMemoryLimit = $app->getMemoryLimit(); self::assertSame( @@ -320,6 +261,9 @@ public function testSetMemoryLimitWithNonPositiveValueTriggersRecalculation(int ini_set('memory_limit', $originalLimit); } + /** + * @throws InvalidConfigException if the configuration is invalid or incomplete. + */ #[DataProviderExternal(StatelessApplicationProvider::class, 'memoryLimitPositive')] public function testSetMemoryLimitWithPositiveValueSetsLimitDirectly( int $memoryLimit, diff --git a/tests/provider/StatelessApplicationProvider.php b/tests/provider/StatelessApplicationProvider.php index afd8c507..02a63e25 100644 --- a/tests/provider/StatelessApplicationProvider.php +++ b/tests/provider/StatelessApplicationProvider.php @@ -224,4 +224,33 @@ public static function memoryLimitPositive(): array return $data; } + + /** + * @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.", + ], + 'moderate usage - should not clean' => [ + '512M', + false, + "'clean()' 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, + "Memory configuration with '2G' for exact threshold testing.", + ], + ]; + } } From bce768ec86353920f7972216b6f9e18028368583 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sat, 23 Aug 2025 10:54:57 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- tests/http/stateless/ApplicationMemoryTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/http/stateless/ApplicationMemoryTest.php b/tests/http/stateless/ApplicationMemoryTest.php index 1732404f..d9f10e0c 100644 --- a/tests/http/stateless/ApplicationMemoryTest.php +++ b/tests/http/stateless/ApplicationMemoryTest.php @@ -60,6 +60,7 @@ public function testCleanBehaviorWithDifferentMemoryLimits( ini_set('memory_limit', $originalLimit); } + /** * @throws InvalidConfigException if the configuration is invalid or incomplete. */ From c08fb5728615d3253a0e69b2ef0d4b14bf7d7e40 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sat, 23 Aug 2025 07:06:21 -0400 Subject: [PATCH 3/3] Apply fixed review coderabbitai nitpick comments. --- tests/http/stateless/ApplicationMemoryTest.php | 2 +- tests/provider/StatelessApplicationProvider.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/http/stateless/ApplicationMemoryTest.php b/tests/http/stateless/ApplicationMemoryTest.php index d9f10e0c..9e97d1e4 100644 --- a/tests/http/stateless/ApplicationMemoryTest.php +++ b/tests/http/stateless/ApplicationMemoryTest.php @@ -48,7 +48,7 @@ public function testCleanBehaviorWithDifferentMemoryLimits( self::assertFalse($app->clean(), $assertionMessage); } - if ($memoryLimit === '2G') { + if ($shouldTriggerSpecialTest === true) { $currentUsage = memory_get_usage(true); $artificialLimit = (int) ($currentUsage / 0.9); diff --git a/tests/provider/StatelessApplicationProvider.php b/tests/provider/StatelessApplicationProvider.php index 02a63e25..4b88b28f 100644 --- a/tests/provider/StatelessApplicationProvider.php +++ b/tests/provider/StatelessApplicationProvider.php @@ -249,7 +249,8 @@ public static function memoryThreshold(): array 'high memory setup - 2G' => [ '2G', true, - "Memory configuration with '2G' for exact threshold testing.", + "'clean()' should return 'true' when memory usage equals the calculated '90%' threshold " . + '(using adjusted limit).', ], ]; }