Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 9 additions & 75 deletions tests/http/stateless/ApplicationMemoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -392,41 +348,19 @@ 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,
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();
self::assertSame($memoryLimit, $app->getMemoryLimit(), $assertionMessage);

$app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals());
$app->setMemoryLimit($smallLimit);

self::assertSame(
$smallLimit,
$app->getMemoryLimit(),
'Memory limit should handle small positive values correctly.',
);
self::assertSame($memoryLimit, $app->getMemoryLimit(), $assertionMessage);
}
}
36 changes: 36 additions & 0 deletions tests/provider/StatelessApplicationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,40 @@ public static function cookies(): array
],
];
}

/**
* @phpstan-return array<string, array{int, string}>
*/
public static function memoryLimitPositive(): array
{
$data = [
'small value 1KB' => [
1024,
"Memory limit should be set to exactly '1024' bytes ('1KB') when positive value is provided.",
],
'medium value 128MB' => [
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,
"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,
'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' builds.",
];
}

return $data;
}
}
Loading