Completed
Push — master ( 8cc4ca...2aa5db )
by Taosikai
11:59
created

StatusTest::testExit()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 13
nc 4
nop 0
1
<?php
2
namespace Slince\Process\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Slince\Process\Status;
6
7
class StatusTest extends TestCase
8
{
9
    public function setUp()
10
    {
11
        pcntl_signal(SIGUSR1, SIG_DFL);
12
    }
13
14
    public function testExit()
15
    {
16
        $pid = pcntl_fork();
17
        if ($pid > 0) {
18
            pcntl_wait($status);
19
            $status = new Status($status);
20
            $this->assertTrue($status->isExited());
21
            $this->assertEquals(2, $status->getExitCode());
22
            $this->assertNotEmpty($status->getErrorMessage());
23
        } elseif ($pid == 0) {
24
            usleep(100);
25
            if (!file_exists('/file-not-exists')) {
26
                exit(2);
27
            }
28
            exit;
29
        }
30
    }
31
32
    public function testSignal()
33
    {
34
        $pid = pcntl_fork();
35
        if ($pid > 0) {
36
            posix_kill($pid, SIGUSR1);
37
            pcntl_wait($status);
38
            $status = new Status($status);
39
            $this->assertTrue($status->isSignaled());
40
            $this->assertEquals(SIGUSR1, $status->getTerminateSignal());
41
        } elseif ($pid == 0) {
42
            usleep(100);
43
            exit;
44
        }
45
    }
46
47
    public function testIsSuccessful()
48
    {
49
        $pid = pcntl_fork();
50
        if ($pid > 0) {
51
            pcntl_wait($status);
52
            $status = new Status($status);
53
            $this->assertFalse($status->isSuccessful());
54
        } elseif ($pid == 0) {
55
            usleep(100);
56
            exit(2);
57
        }
58
        sleep(1);
59
        $pid = pcntl_fork();
60
        if ($pid > 0) {
61
            pcntl_wait($status2);
62
            $status = new Status($status2);
63
            $this->assertTrue($status->isSuccessful());
64
        } elseif ($pid == 0) {
65
            usleep(100);
66
            exit;
67
        }
68
    }
69
70
    public function testStop()
71
    {
72
    }
73
}
74