1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tests\Unit\Runner; |
6
|
|
|
|
7
|
|
|
use Paraunit\Lifecycle\ProcessEvent; |
8
|
|
|
use Paraunit\Process\AbstractParaunitProcess; |
9
|
|
|
use Paraunit\Runner\Pipeline; |
10
|
|
|
use Prophecy\Argument; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
12
|
|
|
use Tests\BaseUnitTestCase; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class PipelineTest |
16
|
|
|
* @package Tests\Unit\Runner |
17
|
|
|
*/ |
18
|
|
|
class PipelineTest extends BaseUnitTestCase |
19
|
|
|
{ |
20
|
|
View Code Duplication |
public function testExecute() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class); |
23
|
|
|
$eventDispatcher->dispatch(ProcessEvent::PROCESS_STARTED, Argument::type(ProcessEvent::class)) |
24
|
|
|
->shouldBeCalledTimes(1); |
25
|
|
|
$process = $this->prophesize(AbstractParaunitProcess::class); |
26
|
|
|
$pipeline = new Pipeline($eventDispatcher->reveal(), 5); |
27
|
|
|
|
28
|
|
|
$pipeline->execute($process->reveal()); |
29
|
|
|
|
30
|
|
|
$process->start(5) |
31
|
|
|
->shouldHaveBeenCalledTimes(1); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public function testExecuteWithOccupiedPipeline() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class); |
37
|
|
|
$eventDispatcher->dispatch(ProcessEvent::PROCESS_STARTED, Argument::type(ProcessEvent::class)) |
38
|
|
|
->shouldBeCalledTimes(1); |
39
|
|
|
$process = $this->prophesize(AbstractParaunitProcess::class); |
40
|
|
|
$pipeline = new Pipeline($eventDispatcher->reveal(), 5); |
41
|
|
|
|
42
|
|
|
$pipeline->execute($process->reveal()); |
43
|
|
|
|
44
|
|
|
$this->expectException(\RuntimeException::class); |
45
|
|
|
|
46
|
|
|
$pipeline->execute($process->reveal()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testIsFree() |
50
|
|
|
{ |
51
|
|
|
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class); |
52
|
|
|
$eventDispatcher->dispatch(ProcessEvent::PROCESS_TERMINATED, Argument::cetera()) |
53
|
|
|
->shouldNotBeCalled(); |
54
|
|
|
|
55
|
|
|
$pipeline = new Pipeline($eventDispatcher->reveal(), 5); |
56
|
|
|
|
57
|
|
|
$this->assertTrue($pipeline->isFree(), 'Pipeline should be free to start with'); |
58
|
|
|
$this->assertTrue($pipeline->isTerminated(), 'Pipeline should be considered terminated when empty'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public function testIsTerminated() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class); |
64
|
|
|
$eventDispatcher->dispatch(ProcessEvent::PROCESS_STARTED, Argument::type(ProcessEvent::class)) |
65
|
|
|
->shouldBeCalledTimes(1); |
66
|
|
|
$process = $this->prophesize(AbstractParaunitProcess::class); |
67
|
|
|
$process->start(5) |
68
|
|
|
->shouldBeCalledTimes(1); |
69
|
|
|
$process->isTerminated() |
70
|
|
|
->willReturn(true); |
71
|
|
|
$pipeline = new Pipeline($eventDispatcher->reveal(), 5); |
72
|
|
|
|
73
|
|
|
$this->assertTrue($pipeline->isFree(), 'Pipeline should be free to start with'); |
74
|
|
|
|
75
|
|
|
$pipeline->execute($process->reveal()); |
76
|
|
|
|
77
|
|
|
$this->assertFalse($pipeline->isFree(), 'Pipeline is marked free during execution of process'); |
78
|
|
|
$this->assertTrue($pipeline->isTerminated(), 'I was expecting a termination of the process in the pipeline'); |
79
|
|
|
$this->assertFalse($pipeline->isFree(), 'Pipeline is being freed'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function testTriggerTermination() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class); |
85
|
|
|
$process = $this->prophesize(AbstractParaunitProcess::class); |
86
|
|
|
$process->start(5) |
87
|
|
|
->shouldBeCalledTimes(1); |
88
|
|
|
$process->isTerminated() |
89
|
|
|
->willReturn(true); |
90
|
|
|
$pipeline = new Pipeline($eventDispatcher->reveal(), 5); |
91
|
|
|
|
92
|
|
|
$this->assertTrue($pipeline->isFree(), 'Pipeline should be free to start with'); |
93
|
|
|
|
94
|
|
|
$pipeline->execute($process->reveal()); |
95
|
|
|
|
96
|
|
|
$this->assertFalse($pipeline->isFree(), 'Pipeline is marked free during execution of process'); |
97
|
|
|
$this->assertTrue($pipeline->triggerTermination(), 'I was expecting a termination of the process in the pipeline'); |
98
|
|
|
|
99
|
|
|
$eventDispatcher->dispatch(ProcessEvent::PROCESS_TERMINATED, Argument::cetera()) |
100
|
|
|
->shouldHaveBeenCalledTimes(1); |
101
|
|
|
|
102
|
|
|
$this->assertTrue($pipeline->isFree(), 'Pipeline is marked as not free after termination of process'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
public function testIsTerminatedFalse() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class); |
108
|
|
|
$eventDispatcher->dispatch(ProcessEvent::PROCESS_STARTED, Argument::type(ProcessEvent::class)) |
109
|
|
|
->shouldBeCalledTimes(1); |
110
|
|
|
$eventDispatcher->dispatch(ProcessEvent::PROCESS_TERMINATED, Argument::cetera()) |
111
|
|
|
->shouldNotBeCalled(); |
112
|
|
|
$process = $this->prophesize(AbstractParaunitProcess::class); |
113
|
|
|
$process->start(5) |
114
|
|
|
->shouldBeCalledTimes(1); |
115
|
|
|
$process->isTerminated() |
116
|
|
|
->willReturn(false); |
117
|
|
|
$pipeline = new Pipeline($eventDispatcher->reveal(), 5); |
118
|
|
|
|
119
|
|
|
$this->assertTrue($pipeline->isFree(), 'Pipeline should be free to start with'); |
120
|
|
|
|
121
|
|
|
$pipeline->execute($process->reveal()); |
122
|
|
|
|
123
|
|
|
$this->assertFalse($pipeline->isFree(), 'Pipeline is marked free during execution of process'); |
124
|
|
|
$this->assertFalse($pipeline->isTerminated(), 'Process should not be terminated'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testGetNumber() |
128
|
|
|
{ |
129
|
|
|
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class); |
130
|
|
|
$pipeline = new Pipeline($eventDispatcher->reveal(), 123456); |
131
|
|
|
|
132
|
|
|
$this->assertSame(123456, $pipeline->getNumber()); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
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.