ProcessManagerTest::testRunWithTick()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 12
loc 12
rs 9.4286
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/**
3
 * File ProcessManagerTest.php
4
 *
5
 * @author Edward Pfremmer <[email protected]>
6
 */
7
namespace Epfremme\ProcessQueue\Tests\Process;
8
9
use Epfremme\ProcessQueue\Process\ProcessFactory;
10
use Epfremme\ProcessQueue\Process\ProcessManager;
11
use Epfremme\ProcessQueue\Process\ProcessQueue;
12
use GuzzleHttp\Promise\PromiseInterface;
13
use Symfony\Component\Process\Process;
14
15
/**
16
 * Class ProcessManagerTest
17
 *
18
 * @package Epfremme\ProcessQueue\Tests\Process
19
 */
20
class ProcessManagerTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @var ProcessFactory
24
     */
25
    private $factory;
26
27
    /**
28
     * @var \SplFileInfo|\Mockery\MockInterface
29
     */
30
    private $directory;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function setUp()
36
    {
37
        parent::setUp();
38
39
        $this->factory = new ProcessFactory('pwd');
40
        $this->directory = \Mockery::mock(\SplFileInfo::class);
41
42
        $this->directory->shouldReceive('__toString')->andReturn('/tmp');
43
    }
44
45
    public function testConstruct()
46
    {
47
        $processManager = new ProcessManager($this->factory);
48
49
        $this->assertAttributeSame($this->factory, 'factory', $processManager);
50
        $this->assertAttributeInstanceOf(ProcessQueue::class, 'queue', $processManager);
51
    }
52
53
    public function testConstructWithLimit()
54
    {
55
        $processManager = new ProcessManager($this->factory, 4);
56
        $expectedQueue = new ProcessQueue(4);
57
58
        $this->assertAttributeEquals($expectedQueue, 'queue', $processManager);
59
    }
60
61 View Code Duplication
    public function testEnqueue()
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...
62
    {
63
        $processManager = new ProcessManager($this->factory);
64
        $promise = $processManager->enqueue($this->directory);
65
66
        $this->assertAttributeCount(1, 'queue', $processManager);
67
        $this->assertInstanceOf(PromiseInterface::class, $promise);
68
    }
69
70 View Code Duplication
    public function testEnqueueWithoutCwd()
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...
71
    {
72
        $processManager = new ProcessManager($this->factory);
73
        $promise = $processManager->enqueue();
74
75
        $this->assertAttributeCount(1, 'queue', $processManager);
76
        $this->assertInstanceOf(PromiseInterface::class, $promise);
77
    }
78
79
    public function testEnqueueFulfilledPromise()
80
    {
81
        $processManager = new ProcessManager($this->factory);
82
        $promise = $processManager->enqueue($this->directory);
83
84
        $resolved = false;
85
        $promise->then(function() use (&$resolved) {
86
            $this->assertInstanceOf(Process::class, func_get_arg(0));
87
            $resolved = true;
88
        });
89
90
        $processManager->run();
91
92
        $this->assertTrue($resolved);
93
    }
94
95
    public function testEnqueueRejectedPromise()
96
    {
97
        $processFactory = new ProcessFactory('noop');
98
        $processManager = new ProcessManager($processFactory);
99
        $promise = $processManager->enqueue($this->directory);
100
101
        $rejected = false;
102
        $promise->otherwise(function() use (&$rejected) {
103
            $this->assertInstanceOf(Process::class, func_get_arg(0));
104
            $rejected = true;
105
        });
106
107
        $processManager->run();
108
109
        $this->assertTrue($rejected);
110
    }
111
112
    /** @depends testEnqueue */
113
    public function testRun()
114
    {
115
        $processManager = new ProcessManager($this->factory);
116
        $promise = $processManager->enqueue($this->directory);
117
118
        $promise->then(function($process) {
119
            /** @var Process $process */
120
            $this->assertInstanceOf(Process::class, $process);
121
            $this->assertArrayHasKey(ProcessQueue::PROMISE_KEY, $process->getOptions());
122
123
            /** @var PromiseInterface $promise */
124
            $promise = $process->getOptions()[ProcessQueue::PROMISE_KEY];
125
            $this->assertInstanceOf(PromiseInterface::class, $promise);
126
            $this->assertTrue($process->isTerminated());
127
            $this->assertTrue($process->isStarted());
128
            $this->assertFalse($process->isRunning());
129
            $this->assertEquals(PromiseInterface::FULFILLED, $promise->getState());
130
        });
131
132
        $processManager->run();
133
    }
134
135
    /** @depends testEnqueue */
136 View Code Duplication
    public function testRunWithTick()
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...
137
    {
138
        $processManager = new ProcessManager($this->factory);
139
        $processManager->enqueue($this->directory);
140
141
        $ticks = 0;
142
        $processManager->run(function() use (&$ticks) {
143
            $ticks++;
144
        });
145
146
        $this->assertGreaterThan(0, $ticks);
147
    }
148
}
149