From 8df5ba5745885decad1e303b8f14941e90b59d0f Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 22 Aug 2025 11:20:58 -0400 Subject: [PATCH 1/4] test(http): Add data provider for `memoryLimitPositive` tests in `StatelessApplicationProvider` and refactor related tests in `ApplicationMemoryTest` class. --- .../http/stateless/ApplicationMemoryTest.php | 83 +++---------------- .../provider/StatelessApplicationProvider.php | 30 +++++++ 2 files changed, 40 insertions(+), 73 deletions(-) diff --git a/tests/http/stateless/ApplicationMemoryTest.php b/tests/http/stateless/ApplicationMemoryTest.php index 877964a3..f5c8aacb 100644 --- a/tests/http/stateless/ApplicationMemoryTest.php +++ b/tests/http/stateless/ApplicationMemoryTest.php @@ -4,9 +4,11 @@ namespace yii2\extensions\psrbridge\tests\http\stateless; +use PHPUnit\Framework\Attributes\DataProviderExternal; use PHPUnit\Framework\Attributes\Group; use stdClass; use yii\base\InvalidConfigException; +use yii2\extensions\psrbridge\tests\provider\StatelessApplicationProvider; use yii2\extensions\psrbridge\tests\support\FactoryHelper; use yii2\extensions\psrbridge\tests\TestCase; @@ -310,52 +312,6 @@ public function testReturnPhpIntMaxWhenMemoryLimitIsUnlimited(): void ini_set('memory_limit', $originalLimit); } - /** - * @throws InvalidConfigException if the configuration is invalid or incomplete. - */ - public function testSetMemoryLimitWithLargePositiveValueMaintainsValue(): void - { - $largeLimit = 2_147_483_647; // near '32-bit' 'INT_MAX'; well below '64-bit' 'PHP_INT_MAX' - - $app = $this->statelessApplication(); - - $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); - $app->setMemoryLimit($largeLimit); - - self::assertSame( - $largeLimit, - $app->getMemoryLimit(), - 'Memory limit should handle large positive values correctly without overflow.', - ); - } - - /** - * @throws InvalidConfigException if the configuration is invalid or incomplete. - */ - public function testSetMemoryLimitWithPositiveValueDisablesRecalculation(): void - { - $customLimit = 134_217_728; // '128MB' in bytes - - $originalLimit = ini_get('memory_limit'); - - ini_set('memory_limit', '512M'); - - $app = $this->statelessApplication(); - - $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); - $app->setMemoryLimit($customLimit); - - ini_set('memory_limit', '1G'); - - self::assertSame( - $customLimit, - $app->getMemoryLimit(), - 'Memory limit should remain unchanged when recalculation is disabled after setting positive value.', - ); - - ini_set('memory_limit', $originalLimit); - } - /** * @throws InvalidConfigException if the configuration is invalid or incomplete. */ @@ -392,41 +348,22 @@ public function testSetMemoryLimitWithPositiveValueOverridesSystemRecalculation( ini_set('memory_limit', $originalLimit); } - /** - * @throws InvalidConfigException if the configuration is invalid or incomplete. - */ - public function testSetMemoryLimitWithPositiveValueSetsLimitDirectly(): void - { - $memoryLimit = 268_435_456; // '256MB' in bytes - + #[DataProviderExternal(StatelessApplicationProvider::class, 'memoryLimitPositive')] + public function testSetMemoryLimitWithPositiveValueSetsLimitDirectly( + int $memoryLimit, + int $expectedLimit, + string $assertionMessage, + ): void { $app = $this->statelessApplication(); $app->setMemoryLimit($memoryLimit); - $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); - - self::assertSame( - $memoryLimit, - $app->getMemoryLimit(), - 'Memory limit should be set to the exact value when a positive limit is provided.', - ); - } - - /** - * @throws InvalidConfigException if the configuration is invalid or incomplete. - */ - public function testSetMemoryLimitWithSmallPositiveValueSetsCorrectly(): void - { - $smallLimit = 1024; // '1KB' - - $app = $this->statelessApplication(); $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); - $app->setMemoryLimit($smallLimit); self::assertSame( - $smallLimit, + $expectedLimit, $app->getMemoryLimit(), - 'Memory limit should handle small positive values correctly.', + $assertionMessage, ); } } diff --git a/tests/provider/StatelessApplicationProvider.php b/tests/provider/StatelessApplicationProvider.php index 4d5fc9d0..7a0d7ad4 100644 --- a/tests/provider/StatelessApplicationProvider.php +++ b/tests/provider/StatelessApplicationProvider.php @@ -188,4 +188,34 @@ public static function cookies(): array ], ]; } + + /** + * @phpstan-return array + */ + public static function memoryLimitPositive(): array + { + return [ + 'small value 1KB' => [ + 1024, + 1024, + "Memory limit should be set to exactly '1024' bytes ('1KB') when positive value is provided.", + ], + 'medium value 128MB' => [ + 134_217_728, + 134_217_728, + "Memory limit should be set to exactly '134_217_728' bytes ('128MB') when positive value is provided.", + ], + 'large value 256MB' => [ + 268_435_456, + 268_435_456, + "Memory limit should be set to exactly '268_435_456' bytes ('256MB') when positive value is provided.", + ], + 'very large value near 32-bit INT_MAX' => [ + 2_147_483_647, + 2_147_483_647, + 'Memory limit should handle large positive values correctly without overflow when set to ' . + "'2_147_483_647' bytes.", + ], + ]; + } } From a45ac7ac262de04923dba6503533afac7f157d97 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 22 Aug 2025 11:36:47 -0400 Subject: [PATCH 2/4] test(http): Update `memoryLimitPositive` data provider to include additional test case for `4GiB` limit on `64-bit` systems. --- tests/http/stateless/ApplicationMemoryTest.php | 9 +++------ tests/provider/StatelessApplicationProvider.php | 16 +++++++++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/tests/http/stateless/ApplicationMemoryTest.php b/tests/http/stateless/ApplicationMemoryTest.php index f5c8aacb..624b6282 100644 --- a/tests/http/stateless/ApplicationMemoryTest.php +++ b/tests/http/stateless/ApplicationMemoryTest.php @@ -351,19 +351,16 @@ public function testSetMemoryLimitWithPositiveValueOverridesSystemRecalculation( #[DataProviderExternal(StatelessApplicationProvider::class, 'memoryLimitPositive')] public function testSetMemoryLimitWithPositiveValueSetsLimitDirectly( int $memoryLimit, - int $expectedLimit, string $assertionMessage, ): void { $app = $this->statelessApplication(); $app->setMemoryLimit($memoryLimit); + self::assertSame($memoryLimit, $app->getMemoryLimit(), $assertionMessage); + $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); - self::assertSame( - $expectedLimit, - $app->getMemoryLimit(), - $assertionMessage, - ); + self::assertSame($memoryLimit, $app->getMemoryLimit(), $assertionMessage); } } diff --git a/tests/provider/StatelessApplicationProvider.php b/tests/provider/StatelessApplicationProvider.php index 7a0d7ad4..4577cca6 100644 --- a/tests/provider/StatelessApplicationProvider.php +++ b/tests/provider/StatelessApplicationProvider.php @@ -194,28 +194,34 @@ public static function cookies(): array */ public static function memoryLimitPositive(): array { - return [ + $data = [ 'small value 1KB' => [ - 1024, 1024, "Memory limit should be set to exactly '1024' bytes ('1KB') when positive value is provided.", ], 'medium value 128MB' => [ - 134_217_728, 134_217_728, "Memory limit should be set to exactly '134_217_728' bytes ('128MB') when positive value is provided.", ], 'large value 256MB' => [ - 268_435_456, 268_435_456, "Memory limit should be set to exactly '268_435_456' bytes ('256MB') when positive value is provided.", ], 'very large value near 32-bit INT_MAX' => [ - 2_147_483_647, 2_147_483_647, 'Memory limit should handle large positive values correctly without overflow when set to ' . "'2_147_483_647' bytes.", ], ]; + + if (PHP_INT_SIZE >= 8) { + $data['ultra large value 4GiB (64-bit only)'] = [ + 4_294_967_296, + "Memory limit should be set to exactly '4_294_967_296' bytes ('4GiB') when a positive value is " . + "provided on '64-bit'.", + ]; + } + + return $data; } } From 50bb508e9264f504870ac8986b050062e0a6cd40 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 22 Aug 2025 11:40:54 -0400 Subject: [PATCH 3/4] test(http): Update phpstan return type for `memoryLimitPositive` method to reflect correct data structure. --- tests/provider/StatelessApplicationProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/provider/StatelessApplicationProvider.php b/tests/provider/StatelessApplicationProvider.php index 4577cca6..76f0cd5b 100644 --- a/tests/provider/StatelessApplicationProvider.php +++ b/tests/provider/StatelessApplicationProvider.php @@ -190,7 +190,7 @@ public static function cookies(): array } /** - * @phpstan-return array + * @phpstan-return array */ public static function memoryLimitPositive(): array { From cf9b525725bb76195bb28c47e1a3c1adc2aff897 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 22 Aug 2025 11:47:18 -0400 Subject: [PATCH 4/4] Apply fixed review coderabbitai nitpick comments. --- tests/provider/StatelessApplicationProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/provider/StatelessApplicationProvider.php b/tests/provider/StatelessApplicationProvider.php index 76f0cd5b..afd8c507 100644 --- a/tests/provider/StatelessApplicationProvider.php +++ b/tests/provider/StatelessApplicationProvider.php @@ -218,7 +218,7 @@ public static function memoryLimitPositive(): array $data['ultra large value 4GiB (64-bit only)'] = [ 4_294_967_296, "Memory limit should be set to exactly '4_294_967_296' bytes ('4GiB') when a positive value is " . - "provided on '64-bit'.", + "provided on '64-bit' builds.", ]; }