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

SignalHandlerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 1
cbo 2
dl 0
loc 45
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 6 2
A testRegister() 0 11 1
A testRegisterMultiSignal() 0 12 1
A testGetHandler() 0 10 1
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
}