Completed
Push — master ( 9cd689...a6092f )
by Taosikai
15:30
created

SignalHandlerTest::testGetHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
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
        usleep(100);
25
        $this->assertEquals('foo', $username);
26
    }
27
28
    public function testRegisterMultiSignal()
29
    {
30
        $signalHandler = SignalHandler::getInstance();
31
        $counter = 0;
32
        $signalHandler->register([SIGUSR1, SIGUSR2], function() use(&$counter){
33
            $counter ++;
34
        });
35
        posix_kill(getmypid(), SIGUSR1);
36
        posix_kill(getmypid(), SIGUSR2);
37
        usleep(100);
38
        $this->assertEquals(2, $counter);
39
    }
40
41
    public function testGetHandler()
42
    {
43
        $signalHandler = SignalHandler::getInstance();
44
        $handler = function(){
45
            $this->username = 'foo';
0 ignored issues
show
Bug introduced by
The property username does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
        };
47
        $signalHandler->register(SIGUSR1, $handler);
48
        $handler1 = $signalHandler->getHandler(SIGUSR1);
49
        $this->assertTrue($handler === $handler1);
50
    }
51
}