generated from spawnia/php-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryMonitorTest.php
More file actions
92 lines (75 loc) · 2.75 KB
/
MemoryMonitorTest.php
File metadata and controls
92 lines (75 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php declare(strict_types=1);
namespace MLL\Utils\Tests;
use MLL\Utils\MemoryMonitor;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
final class MemoryMonitorTest extends TestCase
{
/** @dataProvider convertToBytesProvider */
#[DataProvider('convertToBytesProvider')]
public function testConvertToBytes(int $expected, string $memoryLimit): void
{
self::assertSame($expected, MemoryMonitor::convertToBytes($memoryLimit));
}
/** @return iterable<array{int, string}> */
public static function convertToBytesProvider(): iterable
{
yield [134217728, '128M'];
yield [1073741824, '1G'];
yield [1024, '1K'];
yield [268435456, '256M'];
yield [2147483648, '2G'];
}
public function testConvertToBytesThrowsOnUnexpectedUnit(): void
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('Unexpected unit value: 8.');
MemoryMonitor::convertToBytes('128');
}
/** @dataProvider formatBytesProvider */
#[DataProvider('formatBytesProvider')]
public function testFormatBytes(string $expected, int $bytes): void
{
self::assertSame($expected, MemoryMonitor::formatBytes($bytes));
}
/** @return iterable<array{string, int}> */
public static function formatBytesProvider(): iterable
{
yield ['0 B', 0];
yield ['1 B', 1];
yield ['1 KB', 1024];
yield ['1 MB', 1048576];
yield ['1 GB', 1073741824];
yield ['128 MB', 134217728];
yield ['256 MB', 268435456];
yield ['2 GB', 2147483648];
}
public function testFormatBytesHandlesNegativeValues(): void
{
self::assertSame('0 B', MemoryMonitor::formatBytes(-1));
}
public function testUsageReturnsPositiveInt(): void
{
self::assertGreaterThan(0, MemoryMonitor::usage());
}
public function testPeakUsageIsAtLeastCurrentUsage(): void
{
self::assertGreaterThanOrEqual(MemoryMonitor::usage(), MemoryMonitor::peakUsage());
}
public function testAvailableMemoryReturnsIntOrUnlimited(): void
{
$available = MemoryMonitor::availableMemory();
self::assertTrue($available === -1 || $available > 0);
}
public function testFormattedUsageContainsMemoryUsageLabel(): void
{
$formatted = MemoryMonitor::formattedUsage();
self::assertStringStartsWith('Memory Usage:', $formatted);
self::assertStringContainsString('/', $formatted);
}
public function testFormattedUsageWithContext(): void
{
$formatted = MemoryMonitor::formattedUsage('After loading worklist');
self::assertStringStartsWith('After loading worklist - Memory Usage:', $formatted);
}
}