Completed
Branch develop (ff0d26)
by Edward
03:17
created

ProcessQueueTest::testConstructWithArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * File ProcessQueueTest.php
4
 *
5
 * @author Edward Pfremmer <[email protected]>
6
 */
7
namespace Epfremme\ProcessQueue\Tests\Process;
8
9
use Epfremme\Collection\Collection;
10
use Epfremme\ProcessQueue\System\ProcessorCounter;
11
use Epfremme\ProcessQueue\Process\ProcessQueue;
12
use GuzzleHttp\Promise\Promise;
13
use Symfony\Component\Process\Process;
14
15
/**
16
 * Class ProcessQueueTest
17
 *
18
 * @package Epfremme\ProcessQueue\Tests\Process
19
 */
20
class ProcessQueueTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * Add optional promise to process options
24
     *
25
     * @param Process $process
26
     * @return Promise
27
     */
28
    private function addPromise(Process $process)
29
    {
30
        /** @var Promise $promise */
31
        $promise = new Promise(function() use ($process, &$promise) {
32
            $process->wait();
33
            $promise->resolve($process);
34
        });
35
36
        $process->setOptions([ProcessQueue::PROMISE_KEY => $promise]);
37
38
        return $promise;
39
    }
40
41
    public function testConstruct()
42
    {
43
        $queue = new ProcessQueue();
44
        $counter = new ProcessorCounter();
45
46
        $this->assertInstanceOf(\Countable::class, $queue);
47
        $this->assertAttributeEquals($counter->getCpuCount(), 'limit', $queue);
48
    }
49
50
    public function testConstructWithArgs()
51
    {
52
        $queue = new ProcessQueue(4);
53
54
        $this->assertInstanceOf(\Countable::class, $queue);
55
        $this->assertAttributeEquals(4, 'limit', $queue);
56
    }
57
58
    /** @expectedException \InvalidArgumentException */
59
    public function testConstructException()
60
    {
61
        new ProcessQueue([new \ArrayObject()]);
0 ignored issues
show
Documentation introduced by
array(new \ArrayObject()) is of type array<integer,object<Arr..."object<ArrayObject>"}>, but the function expects a integer|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
    }
63
64
    public function testAdd()
65
    {
66
        $queue = new ProcessQueue();
67
        $process = new Process('pwd');
68
69
        $queue->add($process);
70
71
        $this->assertNotEmpty($queue);
72
        $this->assertCount(1, $queue);
73
    }
74
75
    /** @depends testAdd */
76
    public function testGetPending()
77
    {
78
        $queue = new ProcessQueue();
79
        $process = new Process('pwd');
80
81
        $queue->add($process);
82
83
        $pending = $queue->getPending();
84
85
        $this->assertInstanceOf(Collection::class, $pending);
86
        $this->assertContainsOnly(Process::class, $pending);
87
        $this->assertInstanceOf(Process::class, $pending->get(0));
88
        $this->assertSame($process, $pending->get(0));
89
    }
90
91
    /** @depends testAdd */
92 View Code Duplication
    public function testGetRunning()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
93
    {
94
        $queue = new ProcessQueue();
95
        $process = new Process('sleep 0.1');
96
97
        $queue->add($process);
98
99
        $this->assertEmpty($queue->getRunning());
100
101
        $process->start();
102
103
        $running = $queue->getRunning();
104
105
        $this->assertInstanceOf(Collection::class, $running);
106
        $this->assertContainsOnly(Process::class, $running);
107
        $this->assertInstanceOf(Process::class, $running->get(0));
108
        $this->assertSame($process, $running->get(0));
109
    }
110
111
    /** @depends testAdd */
112 View Code Duplication
    public function testGetCompleted()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
113
    {
114
        $queue = new ProcessQueue();
115
        $process = new Process('pwd');
116
117
        $queue->add($process);
118
119
        $this->assertEmpty($queue->getCompleted());
120
121
        $process->run();
122
123
        $completed = $queue->getCompleted();
124
125
        $this->assertInstanceOf(Collection::class, $completed);
126
        $this->assertContainsOnly(Process::class, $completed);
127
        $this->assertInstanceOf(Process::class, $completed->get(0));
128
        $this->assertSame($process, $completed->get(0));
129
    }
130
131
    /** @depends testAdd */
132
    public function testResolve()
133
    {
134
        $queue = new ProcessQueue();
135
        $process = new Process('pwd');
136
137
        $queue->add($process);
138
        $process->run();
139
        $queue->resolve($process);
140
141
        $this->assertEmpty($queue);
142
        $this->assertCount(0, $queue);
143
        $this->assertTrue($process->isTerminated());
144
    }
145
146
    /** @depends testAdd */
147
    public function testResolveWithPromise()
148
    {
149
        $queue = new ProcessQueue();
150
        $process = new Process('pwd');
151
        $promise = $this->addPromise($process);
152
153
        $isResolved = false;
154
        $promise->then(function() use (&$isResolved) {
155
            $isResolved = true;
156
        });
157
158
        $this->assertFalse($isResolved);
159
160
        $queue->add($process);
161
        $process->run();
162
        $queue->resolve($process);
163
164
        $this->assertEmpty($queue);
165
        $this->assertCount(0, $queue);
166
        $this->assertTrue($isResolved);
167
        $this->assertTrue($process->isTerminated());
168
    }
169
170
    /** @depends testAdd */
171
    public function testInvoke()
172
    {
173
        $queue = new ProcessQueue();
174
        $process = new Process('pwd');
175
176
        $queue->add($process);
177
178
        /** @var Process $pending */
179
        foreach ($queue() as $pending) {
180
            $this->assertInstanceOf(Process::class, $pending);
181
            $this->assertFalse($pending->isStarted());
182
183
            $pending->start();
184
        }
185
186
        $this->assertEmpty($queue);
187
        $this->assertCount(0, $queue);
188
        $this->assertTrue($process->isTerminated());
189
    }
190
}
191