File: OsHelperTest.php

package info (click to toggle)
php-jolicode-php-os-helper 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 148 kB
  • sloc: php: 145; xml: 20; makefile: 8
file content (88 lines) | stat: -rw-r--r-- 2,380 bytes parent folder | download
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
<?php

namespace JoliCode\PhpOsHelper\tests;

use JoliCode\PhpOsHelper\OsHelper;
use PHPUnit\Framework\TestCase;

class OsHelperTest extends TestCase
{
    public function testIsUnix()
    {
        if ('\\' === \DIRECTORY_SEPARATOR) {
            $this->assertFalse(OsHelper::isUnix());
        }

        if ('/' === \DIRECTORY_SEPARATOR) {
            $this->assertTrue(OsHelper::isUnix());
        }
    }

    public function testIsWindows()
    {
        if ('\\' === \DIRECTORY_SEPARATOR) {
            $this->assertTrue(OsHelper::isWindows());
        }

        if ('/' === \DIRECTORY_SEPARATOR) {
            $this->assertFalse(OsHelper::isWindows());
        }
    }

    public function testIsWindowsSeven()
    {
        if (!OsHelper::isWindows()) {
            $this->markTestSkipped('Can only be run on Windows');
        }

        $isSeven = '6.1' === php_uname('r');

        $this->assertSame($isSeven, OsHelper::isWindowsSeven());
    }

    public function testIsWindowsEightOrHigher()
    {
        if (!OsHelper::isWindows()) {
            $this->markTestSkipped('Can only be run on Windows');
        }

        $eightOrHigher = [
            '6.2', // 8
            '6.3', // 8.1
            '6.4', // 10
            '10', // 10 or 11, yeah microsoft is doing weird things
        ];
        $isEightOrHigher = \in_array(php_uname('r'), $eightOrHigher, true);

        $this->assertSame($isEightOrHigher, OsHelper::isWindowsEightOrHigher());
    }

    public function testIsMacOS()
    {
        $uname = php_uname();
        $isDarwin = 'Darwin' === substr($uname, 0, 6);

        $this->assertSame($isDarwin, OsHelper::isMacOS());
    }

    public function testIsDocker()
    {
        $isDocker = file_exists('/.dockerenv') || (file_exists('/proc/self/cgroup') && false !== mb_strpos(file_get_contents('/proc/self/cgroup') ?: '', 'docker'));

        $this->assertSame($isDocker, OsHelper::isDocker());
    }

    public function testGetMacOSVersion()
    {
        if (!OsHelper::isMacOS()) {
            $this->markTestSkipped('Can only be run on MacOS');
        }

        $expectedMacOsVersion = exec('sw_vers -productVersion', $output);

        $macOsVersion = OsHelper::getMacOSVersion();

        $this->assertRegExp('#\d{1,2}\.\d{1,2}(\.\d{1,2})?#', $macOsVersion);
        $this->assertSame($expectedMacOsVersion, $macOsVersion);
    }
}