Completed
Push — master ( 77d7a1...2c21c0 )
by Edward
04:31 queued 02:12
created

testConstructLessThenZeroException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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
    public function testConstructWithNumericString()
59
    {
60
        $queue = new ProcessQueue('4');
61
62
        $this->assertInstanceOf(\Countable::class, $queue);
63
        $this->assertAttributeEquals(4, 'limit', $queue);
64
    }
65
66
    /** @expectedException \InvalidArgumentException */
67
    public function testConstructNonNumericException()
68
    {
69
        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...
70
    }
71
72
    /** @expectedException \InvalidArgumentException */
73
    public function testConstructLessThenZeroException()
74
    {
75
        new ProcessQueue(0);
76
    }
77
78
    public function testAdd()
79
    {
80
        $queue = new ProcessQueue();
81
        $process = new Process('pwd');
82
83
        $queue->add($process);
84
85
        $this->assertNotEmpty($queue);
86
        $this->assertCount(1, $queue);
87
    }
88
89
    /** @depends testAdd */
90
    public function testGetPending()
91
    {
92
        $queue = new ProcessQueue();
93
        $process = new Process('pwd');
94
95
        $queue->add($process);
96
97
        $pending = $queue->getPending();
98
99
        $this->assertInstanceOf(Collection::class, $pending);
100
        $this->assertContainsOnly(Process::class, $pending);
101
        $this->assertInstanceOf(Process::class, $pending->get(0));
102
        $this->assertSame($process, $pending->get(0));
103
    }
104
105
    /** @depends testAdd */
106 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...
107
    {
108
        $queue = new ProcessQueue();
109
        $process = new Process('sleep 0.1');
110
111
        $queue->add($process);
112
113
        $this->assertEmpty($queue->getRunning());
114
115
        $process->start();
116
117
        $running = $queue->getRunning();
118
119
        $this->assertInstanceOf(Collection::class, $running);
120
        $this->assertContainsOnly(Process::class, $running);
121
        $this->assertInstanceOf(Process::class, $running->get(0));
122
        $this->assertSame($process, $running->get(0));
123
    }
124
125
    /** @depends testAdd */
126 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...
127
    {
128
        $queue = new ProcessQueue();
129
        $process = new Process('pwd');
130
131
        $queue->add($process);
132
133
        $this->assertEmpty($queue->getCompleted());
134
135
        $process->run();
136
137
        $completed = $queue->getCompleted();
138
139
        $this->assertInstanceOf(Collection::class, $completed);
140
        $this->assertContainsOnly(Process::class, $completed);
141
        $this->assertInstanceOf(Process::class, $completed->get(0));
142
        $this->assertSame($process, $completed->get(0));
143
    }
144
145
    /** @depends testAdd */
146
    public function testResolve()
147
    {
148
        $queue = new ProcessQueue();
149
        $process = new Process('pwd');
150
151
        $queue->add($process);
152
        $process->start();
153
        $queue->resolve($process);
154
155
        $this->assertEmpty($queue);
156
        $this->assertCount(0, $queue);
157
        $this->assertTrue($process->isTerminated());
158
    }
159
160
    /** @depends testAdd */
161
    public function testResolveWithPromise()
162
    {
163
        $queue = new ProcessQueue();
164
        $process = new Process('pwd');
165
        $promise = $this->addPromise($process);
166
167
        $isResolved = false;
168
        $promise->then(function() use (&$isResolved) {
169
            $isResolved = true;
170
        });
171
172
        $this->assertFalse($isResolved);
173
174
        $queue->add($process);
175
        $process->start();
176
        $queue->resolve($process);
177
178
        $this->assertEmpty($queue);
179
        $this->assertCount(0, $queue);
180
        $this->assertTrue($isResolved);
181
        $this->assertTrue($process->isTerminated());
182
    }
183
184
    /** @depends testAdd */
185
    public function testInvoke()
186
    {
187
        $queue = new ProcessQueue();
188
        $process = new Process('pwd');
189
190
        $queue->add($process);
191
192
        /** @var Process $pending */
193
        foreach ($queue() as $pending) {
194
            $this->assertInstanceOf(Process::class, $pending);
195
            $this->assertFalse($pending->isStarted());
196
197
            $pending->start();
198
        }
199
200
        $this->assertEmpty($queue);
201
        $this->assertCount(0, $queue);
202
        $this->assertTrue($process->isTerminated());
203
    }
204
}
205