ProcessManagerTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 17.31 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 18
loc 104
c 0
b 0
f 0
wmc 12
lcom 1
cbo 6
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A gettersAndSettersWork() 0 22 1
B basicRulesAndTasksWork() 18 57 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AsyncPHP\Doorman\Tests\Manager;
4
5
use AsyncPHP\Doorman\Manager\ProcessManager;
6
use AsyncPHP\Doorman\Rule\InMemoryRule;
7
use AsyncPHP\Doorman\Rules\InMemoryRules;
8
use AsyncPHP\Doorman\Shell\BashShell;
9
use AsyncPHP\Doorman\Task\ProcessCallbackTask;
10
use AsyncPHP\Doorman\Tests\Test;
11
12
/**
13
 * @covers AsyncPHP\Doorman\Manager\ProcessManager
14
 */
15
class ProcessManagerTest extends Test
16
{
17
    /**
18
     * @var ProcessManager
19
     */
20
    protected $manager;
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function setUp()
26
    {
27
        parent::setUp();
28
29
        $this->manager = new ProcessManager();
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function gettersAndSettersWork()
36
    {
37
        $this->manager->setLogPath(__DIR__);
38
39
        $this->assertEquals(__DIR__, $this->manager->getLogPath());
40
41
        $this->assertInstanceOf("AsyncPHP\\Doorman\\Shell", $this->manager->getShell());
42
43
        $shell = new BashShell();
44
45
        $this->manager->setShell($shell);
46
47
        $this->assertEquals($shell, $this->manager->getShell());
48
49
        $this->assertInstanceOf("AsyncPHP\\Doorman\\Rules", $this->manager->getRules());
50
51
        $rules = new InMemoryRules();
52
53
        $this->manager->setRules($rules);
54
55
        $this->assertEquals($rules, $this->manager->getRules());
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function basicRulesAndTasksWork()
62
    {
63 View Code Duplication
        $task1 = new ProcessCallbackTask(function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
            touch(__DIR__."/task1.temp");
65
66
            for ($i = 0; $i < 10; $i++) {
67
                usleep(50000);
68
            }
69
70
            unlink(__DIR__."/task1.temp");
71
        });
72
73 View Code Duplication
        $task2 = new ProcessCallbackTask(function () {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
            touch(__DIR__."/task2.temp");
75
76
            for ($i = 0; $i < 10; $i++) {
77
                usleep(50000);
78
            }
79
80
            unlink(__DIR__."/task2.temp");
81
        });
82
83
        $rule = new InMemoryRule();
84
        $rule->setProcesses(1);
85
        $rule->setMinimumProcessorUsage(0);
86
        $rule->setMaximumProcessorUsage(100);
87
88
        $added = false;
89
90
        $this->manager->addRule($rule);
91
        $this->manager->addTask($task1);
92
93
        while ($this->manager->tick()) {
94
            usleep(50000);
95
96
            if (!$added) {
97
                $this->manager->addTask($task2);
98
                $added = true;
99
            }
100
101
            if (file_exists(__DIR__."/task1.temp") && file_exists(__DIR__."/task2.temp")) {
102
                $this->fail("Tasks should not be run concurrently");
103
            }
104
        }
105
106
        $this->manager->removeRule($rule);
107
        $this->manager->addTask($task1);
108
        $this->manager->addTask($task2);
109
110
        if ($this->manager->tick()) {
111
            usleep(50000);
112
113
            if (!file_exists(__DIR__."/task1.temp") || !file_exists(__DIR__."/task2.temp")) {
114
                $this->fail("Tasks should be run concurrently");
115
            }
116
        }
117
    }
118
}
119