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

SignalHandlerTest::setUpBeforeClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
namespace Slince\Process\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Slince\Process\SignalHandler;
6
7
class SignalHandlerTest extends TestCase
8
{
9
    public static function setUpBeforeClass()
10
    {
11
        if (!function_exists('pcntl_async_signals')) {
12
            declare(ticks = 1);
13
        }
14
    }
15
16
    public function testRegister()
17
    {
18
        $signalHandler = SignalHandler::getInstance();
19
        $username = '';
20
        $signalHandler->register(SIGUSR1, function () use (&$username) {
21
            $username = 'foo';
22
        });
23
        posix_kill(getmypid(), SIGUSR1);
24
        $this->assertEquals('foo', $username);
25
    }
26
27
    public function testRegisterMultiSignal()
28
    {
29
        $signalHandler = SignalHandler::getInstance();
30
        $counter = 0;
31
        $signalHandler->register([SIGUSR1, SIGUSR2], function () use (&$counter) {
32
            $counter ++;
33
        });
34
        posix_kill(getmypid(), SIGUSR1);
35
        posix_kill(getmypid(), SIGUSR2);
36
        $this->assertEquals(2, $counter);
37
    }
38
39
    public function testGetHandler()
40
    {
41
        $signalHandler = SignalHandler::getInstance();
42
        $handler = function () {
43
            echo 'signal';
44
        };
45
        $signalHandler->register(SIGUSR1, $handler);
46
        $handler1 = $signalHandler->getHandler(SIGUSR1);
47
        $this->assertTrue($handler === $handler1);
48
    }
49
}