forked from laminas/laminas-diagnostics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiskUsageTest.php
More file actions
96 lines (80 loc) · 3.14 KB
/
DiskUsageTest.php
File metadata and controls
96 lines (80 loc) · 3.14 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
93
94
95
96
<?php
namespace LaminasTest\Diagnostics;
use InvalidArgumentException;
use Laminas\Diagnostics\Check\DiskUsage;
use Laminas\Diagnostics\Result\FailureInterface;
use Laminas\Diagnostics\Result\SuccessInterface;
use Laminas\Diagnostics\Result\WarningInterface;
use PHPUnit\Framework\TestCase;
use function disk_free_space;
use function disk_total_space;
use function is_writable;
use function sys_get_temp_dir;
/** @covers \Laminas\Diagnostics\Check\DiskUsage */
final class DiskUsageTest extends TestCase
{
/**
* @dataProvider invalidArgumentProvider
* @param int|string $warningThreshold
* @param int|string $criticalThreshold
* @param string|array $path
*/
public function testInvalidArguments($warningThreshold, $criticalThreshold, $path): void
{
$this->expectException(InvalidArgumentException::class);
new DiskUsage($warningThreshold, $criticalThreshold, $path);
}
public function testCheck(): void
{
$df = disk_free_space($this->getTempDir());
$dt = disk_total_space($this->getTempDir());
$du = $dt - $df;
$dp = ($du / $dt) * 100;
$check = new DiskUsage($dp + 1, $dp + 2, $this->getTempDir());
$result = $check->check();
self::assertInstanceof(SuccessInterface::class, $result);
$data = $result->getData();
self::assertNotNull($data);
self::assertSame($dp, $data['utilization']['value']);
self::assertSame('percentage', $data['utilization']['valueType']);
$check = new DiskUsage($dp - 1, 100, $this->getTempDir());
$result = $check->check();
self::assertInstanceof(WarningInterface::class, $result);
$data = $result->getData();
self::assertNotNull($data);
self::assertSame($dp, $data['utilization']['value']);
self::assertSame('percentage', $data['utilization']['valueType']);
$check = new DiskUsage(0, $dp - 1, $this->getTempDir());
$result = $check->check();
self::assertInstanceof(FailureInterface::class, $result);
$data = $result->getData();
self::assertNotNull($data);
self::assertSame($dp, $data['utilization']['value']);
self::assertSame('percentage', $data['utilization']['valueType']);
}
public function invalidArgumentProvider(): array
{
return [
['Not an integer.', 'Not an integer.', $this->getTempDir()],
[5, 'Not an integer.', $this->getTempDir()],
['Not an integer.', 100, $this->getTempDir()],
[5, 100, []],
[-10, 100, $this->getTempDir()],
[105, 100, $this->getTempDir()],
[10, -10, $this->getTempDir()],
[10, 105, $this->getTempDir()],
];
}
protected function getTempDir(): string
{
// try to retrieve tmp dir
$tmp = sys_get_temp_dir();
// make sure there is any space there
if (! $tmp || ! is_writable($tmp) || ! disk_free_space($tmp)) {
self::markTestSkipped(
'Cannot find a writable temporary directory with free disk space for Check\DiskUsage tests'
);
}
return $tmp;
}
}