84 lines
3.0 KiB
PHP
84 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\Limits;
|
|
|
|
final class LimitsTest extends TestCase
|
|
{
|
|
public function testDefaultsWhenOptionIsMissing(): void
|
|
{
|
|
Functions\when('get_option')->justReturn([]);
|
|
$limits = new Limits();
|
|
|
|
self::assertSame(5 * 1024 * 1024, $limits->uploadBytes());
|
|
self::assertSame(5, $limits->uploadMegabytes());
|
|
self::assertSame(90, $limits->logRetentionDays());
|
|
self::assertSame(20, $limits->snapshotCount());
|
|
}
|
|
|
|
public function testStoredValuesAreClamped(): void
|
|
{
|
|
Functions\when('get_option')->justReturn(['upload_mb' => 500, 'log_retention_days' => 0, 'snapshot_count' => '7']);
|
|
|
|
self::assertSame(['upload_mb' => 32, 'log_retention_days' => 1, 'snapshot_count' => 7], (new Limits())->all());
|
|
}
|
|
|
|
public function testCorruptOptionFallsBackToDefaults(): void
|
|
{
|
|
Functions\when('get_option')->justReturn('garbage');
|
|
|
|
self::assertSame(Limits::DEFAULTS, (new Limits())->all());
|
|
}
|
|
|
|
public function testSanitizeReturnsACompleteClampedArray(): void
|
|
{
|
|
self::assertSame(
|
|
['upload_mb' => 12, 'log_retention_days' => 90, 'snapshot_count' => 100],
|
|
Limits::sanitize(['upload_mb' => '12', 'log_retention_days' => 'x', 'snapshot_count' => 1000])
|
|
);
|
|
self::assertSame(Limits::DEFAULTS, Limits::sanitize(null));
|
|
}
|
|
|
|
public function testBoundsMatchesWhatSanitizeClampsTo(): void
|
|
{
|
|
self::assertSame([1, 32], Limits::bounds()['upload_mb']);
|
|
self::assertSame([1, 3650], Limits::bounds()['log_retention_days']);
|
|
self::assertSame([1, 100], Limits::bounds()['snapshot_count']);
|
|
|
|
// Cross-check against sanitize()'s actual clamping, so the two can't drift apart.
|
|
$tooLow = array_map(static fn (array $bound): int => $bound[0] - 1, Limits::bounds());
|
|
$tooHigh = array_map(static fn (array $bound): int => $bound[1] + 1, Limits::bounds());
|
|
self::assertSame(
|
|
array_map(static fn (array $bound): int => $bound[0], Limits::bounds()),
|
|
Limits::sanitize($tooLow)
|
|
);
|
|
self::assertSame(
|
|
array_map(static fn (array $bound): int => $bound[1], Limits::bounds()),
|
|
Limits::sanitize($tooHigh)
|
|
);
|
|
}
|
|
|
|
public function testInlineTransfersAreCappedAtTenMegabytes(): void
|
|
{
|
|
Functions\when('get_option')->justReturn(['upload_mb' => 32]);
|
|
$limits = new Limits();
|
|
|
|
self::assertSame(32 * 1024 * 1024, $limits->uploadBytes());
|
|
self::assertSame(10 * 1024 * 1024, $limits->inlineBytes());
|
|
self::assertSame(10, $limits->inlineMegabytes());
|
|
}
|
|
|
|
public function testInlineLimitFollowsALowerUploadLimit(): void
|
|
{
|
|
Functions\when('get_option')->justReturn(['upload_mb' => 3]);
|
|
$limits = new Limits();
|
|
|
|
self::assertSame(3 * 1024 * 1024, $limits->inlineBytes());
|
|
self::assertSame(3, $limits->inlineMegabytes());
|
|
}
|
|
}
|