LoopedProcessRunnerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 77
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testLoopedProcessRuns() 0 11 1
A testSetProcess() 0 14 1
A testTimeLimits() 0 28 1
1
<?php
2
3
namespace Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use React\EventLoop\Factory;
7
use Wems\LoopRunner\LoopedProcessRunner;
8
9
class LoopedProcessRunnerTest extends TestCase
10
{
11
    private $testVar;
12
13
    /** @var callable */
14
    private $process;
15
16
    /** @var LoopedProcessRunner */
17
    private $runner;
18
19
    protected function setUp()
20
    {
21
        $this->process = function () {
22
            $this->testVar = 42;
23
24
            return true;
25
        };
26
27
        $this->runner = new LoopedProcessRunner(Factory::create(), $this->process);
28
    }
29
30
    public function testLoopedProcessRuns()
31
    {
32
        $this->assertNull($this->testVar);
33
34
        $this->runner
35
            ->setTimeLimit(0)
36
            ->setInterval(0)
37
            ->run();
38
39
        $this->assertEquals(42, $this->testVar);
40
    }
41
42
    public function testSetProcess()
43
    {
44
        $fn = function () {
45
            return 42;
46
        };
47
48
        $process = new \ReflectionProperty($this->runner, 'process');
49
        $process->setAccessible(true);
50
51
        $this->runner->setProcess($fn);
52
53
        $fn2 = $process->getValue($this->runner);
54
        $this->assertEquals(42, $fn2());
55
    }
56
57
    public function testTimeLimits()
58
    {
59
        $timeLimit = new \ReflectionProperty($this->runner, 'timeLimit');
60
        $timeLimit->setAccessible(true);
61
62
        $elapsedTime = new \ReflectionProperty($this->runner, 'elapsedTime');
63
        $elapsedTime->setAccessible(true);
64
65
        $this->assertNull($timeLimit->getValue($this->runner));
66
        $this->assertEquals(0, $elapsedTime->getValue($this->runner));
67
68
        $hasExceededTimeLimit = new \ReflectionMethod($this->runner, 'hasExceededTimeLimit');
69
        $hasExceededTimeLimit->setAccessible(true);
70
71
        $notExceededTimeLimit = new \ReflectionMethod($this->runner, 'notExceededTimeLimit');
72
        $notExceededTimeLimit->setAccessible(true);
73
74
        $this->assertTrue($notExceededTimeLimit->invoke($this->runner));
75
        $this->assertFalse($hasExceededTimeLimit->invoke($this->runner));
76
77
        $this->runner->setTimeLimit(2);
78
        $this->assertEquals(2, $timeLimit->getValue($this->runner));
79
80
        $elapsedTime->setValue($this->runner, 3);
81
82
        $this->assertFalse($notExceededTimeLimit->invoke($this->runner));
83
        $this->assertTrue($hasExceededTimeLimit->invoke($this->runner));
84
    }
85
}
86