diff --git a/tests/http/stateless/ApplicationMemoryTest.php b/tests/http/stateless/ApplicationMemoryTest.php index f22e30cb..9e97d1e4 100644 --- a/tests/http/stateless/ApplicationMemoryTest.php +++ b/tests/http/stateless/ApplicationMemoryTest.php @@ -30,69 +30,33 @@ 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 ($shouldTriggerSpecialTest === true) { + $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); } @@ -224,28 +188,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 +244,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 +262,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..4b88b28f 100644 --- a/tests/provider/StatelessApplicationProvider.php +++ b/tests/provider/StatelessApplicationProvider.php @@ -224,4 +224,34 @@ 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, + "'clean()' should return 'true' when memory usage equals the calculated '90%' threshold " . + '(using adjusted limit).', + ], + ]; + } }