Passed
Push — master ( b03c3f...2a4432 )
by Tim
02:18
created

TestFreeSpaceTest::testFreeSpaceOut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Module\monitor\Test;
4
5
use \SimpleSAML\Module\monitor\TestCase as TestCase;
6
use \SimpleSAML\Module\monitor\TestData as TestData;
7
use \SimpleSAML\Module\monitor\State as State;
8
9
/**
10
 * Tests for TestCase\FileSystem\FreeSpace
11
 */
12
class TestFreeSpaceTest extends \PHPUnit_Framework_TestCase
13
{
14
    public function testFreeSpaceAvailable()
15
    {
16
        // /tmp/testdisk will be 1MB
17
        $testData = new TestData([
18
            'path' => '/tmp/testdisk',
19
            'category' => 'Test disk',
20
        ]);
21
        $spaceTest = new TestCase\FileSystem\FreeSpace($testData);
22
        $testResult = $spaceTest->getTestResult();
23
        $freePercentage = $testResult->getOutput('free_percentage');
24
25
        $this->assertEquals(93, $freePercentage);
26
        $this->assertEquals(State::OK, $testResult->getState());
27
    }
28
29
    public function testFreeSpaceAlmostOut()
30
    {
31
        // Fill /tmp/testdisk for 90%
32
        $free = (disk_free_space('/tmp/testdisk') / 100) * 90;
33
        file_put_contents('/tmp/testdisk/90filler.txt', str_repeat('a', $free));
0 ignored issues
show
Bug introduced by
$free of type double is incompatible with the type integer expected by parameter $multiplier of str_repeat(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        file_put_contents('/tmp/testdisk/90filler.txt', str_repeat('a', /** @scrutinizer ignore-type */ $free));
Loading history...
34
35
        $testData = new TestData([
36
            'path' => '/tmp/testdisk',
37
            'category' => 'Test disk',
38
        ]);
39
        $spaceTest = new TestCase\FileSystem\FreeSpace($testData);
40
        $testResult = $spaceTest->getTestResult();
41
        $freePercentage = $testResult->getOutput('free_percentage');
42
43
        $this->assertEquals(9.0, $freePercentage);
44
        $this->assertEquals(State::WARNING, $testResult->getState());
45
    }
46
47
    public function testFreeSpaceOut()
48
    {
49
        // Fill /tmp/testdisk for 95%
50
        $free = (disk_free_space('/tmp/testdisk') / 100) * 50;
51
        file_put_contents('/tmp/testdisk/95filler.txt', str_repeat('b', $free));
0 ignored issues
show
Bug introduced by
$free of type double is incompatible with the type integer expected by parameter $multiplier of str_repeat(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        file_put_contents('/tmp/testdisk/95filler.txt', str_repeat('b', /** @scrutinizer ignore-type */ $free));
Loading history...
52
53
        $testData = new TestData([
54
            'path' => '/tmp/testdisk',
55
            'category' => 'Test disk',
56
        ]);
57
        $spaceTest = new TestCase\FileSystem\FreeSpace($testData);
58
        $testResult = $spaceTest->getTestResult();
59
        $freePercentage = $testResult->getOutput('free_percentage');
60
61
        $this->assertEquals(4.0, $freePercentage);
62
        $this->assertEquals(State::ERROR, $testResult->getState());
63
    }
64
}
65