1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\TestBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use Overwatch\TestBundle\Command\TestsRunCommand; |
6
|
|
|
use Overwatch\TestBundle\DataFixtures\ORM\TestFixtures; |
7
|
|
|
use Overwatch\UserBundle\Tests\Base\DatabaseAwareTestCase; |
8
|
|
|
use Symfony\Component\Console\Application; |
9
|
|
|
use Symfony\Component\Console\Output\Output; |
10
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
11
|
|
|
use phpmock\phpunit\PHPMock; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* TestsRunCommandTest |
15
|
|
|
*/ |
16
|
|
|
class TestsRunCommandTest extends DatabaseAwareTestCase |
17
|
|
|
{ |
18
|
|
|
use PHPMock; |
19
|
|
|
use ConsoleTestHelperTrait; |
20
|
|
|
|
21
|
|
|
const COMMAND_NAME = 'overwatch:tests:run'; |
22
|
|
|
|
23
|
|
|
private $application; |
24
|
|
|
private $command; |
25
|
|
|
private $resultRepo; |
26
|
|
|
|
27
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
parent::setUp(); |
30
|
|
|
$this->createSocketMocks(); |
31
|
|
|
|
32
|
|
|
//Set up the console application, command and command test runner |
33
|
|
|
$command = new TestsRunCommand(); |
34
|
|
|
$command->setContainer($this->getContainer()); |
35
|
|
|
|
36
|
|
|
$this->application = new Application('Overwatch', '0.0.1-test.' . time()); |
37
|
|
|
$this->application->add($command); |
38
|
|
|
|
39
|
|
|
$this->command = new CommandTester($command); |
40
|
|
|
$this->resultRepo = $this->em->getRepository('OverwatchResultBundle:TestResult'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function testWhenAllPass() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$this->createSocketReadMock(); |
46
|
|
|
|
47
|
|
|
$returnCode = $this->execute(); |
48
|
|
|
|
49
|
|
|
$this->assertEquals(0, $returnCode); |
50
|
|
|
$this->assertCountLinesOfOutput(2); |
51
|
|
|
|
52
|
|
|
$this->assertHasStandardOutput(); |
53
|
|
|
|
54
|
|
|
$this->assertCountRunTests(); |
55
|
|
|
$this->assertResults(0, 0, 0, count(TestFixtures::$tests)); |
56
|
|
|
|
57
|
|
|
$this->assertRecentResultsPersisted(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function testVerboselyWhenAllPass() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$this->createSocketReadMock(); |
63
|
|
|
|
64
|
|
|
$returnCode = $this->execute([], [ |
65
|
|
|
'verbosity' => Output::VERBOSITY_VERBOSE |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$this->assertEquals(0, $returnCode); |
69
|
|
|
$this->assertCountLinesOfOutput(5); |
70
|
|
|
|
71
|
|
|
$this->assertHasStandardOutput(); |
72
|
|
|
$this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-1'] . ' : PASSED - Pinged in ', $this->output[1]); |
|
|
|
|
73
|
|
|
$this->assertRegExp('/0\.[0-9]+s$/', $this->output[1]); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-2'] . ' : PASSED - Pinged in ', $this->output[2]); |
|
|
|
|
76
|
|
|
$this->assertRegExp('/0\.[0-9]+s$/', $this->output[2]); |
|
|
|
|
77
|
|
|
|
78
|
|
|
$this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-3'] . ' : PASSED - Pinged in ', $this->output[3]); |
|
|
|
|
79
|
|
|
$this->assertRegExp('/0\.[0-9]+s$/', $this->output[3]); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$this->assertCountRunTests(); |
82
|
|
|
$this->assertResults(0, 0, 0, count(TestFixtures::$tests)); |
83
|
|
|
|
84
|
|
|
$this->assertRecentResultsPersisted(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
public function testDiscardsResults() |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$this->createSocketReadMock(); |
90
|
|
|
|
91
|
|
|
$returnCode = $this->execute(['--discard-results']); |
92
|
|
|
|
93
|
|
|
$this->assertEquals(0, $returnCode); |
94
|
|
|
$this->assertCountLinesOfOutput(2); |
95
|
|
|
|
96
|
|
|
$this->assertHasStandardOutput(); |
97
|
|
|
|
98
|
|
|
$this->assertCountRunTests(); |
99
|
|
|
$this->assertResults(0, 0, 0, count(TestFixtures::$tests)); |
100
|
|
|
|
101
|
|
|
$this->assertRecentResultsNotPersisted(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testNamedTests() |
105
|
|
|
{ |
106
|
|
|
$this->createSocketReadMock(); |
107
|
|
|
|
108
|
|
|
$returnCode = $this->execute([ |
109
|
|
|
'--test' => [ |
110
|
|
|
'Group 2', |
111
|
|
|
'Group 1, Test 2' |
112
|
|
|
] |
113
|
|
|
], [ |
114
|
|
|
'verbosity' => Output::VERBOSITY_VERBOSE |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
$this->assertEquals(0, $returnCode); |
118
|
|
|
$this->assertCountLinesOfOutput(4); |
119
|
|
|
|
120
|
|
|
$this->assertHasStandardOutput(); |
121
|
|
|
|
122
|
|
|
$this->assertCountRunTests(2); |
123
|
|
|
$this->assertResults(0, 0, 0, 2); |
124
|
|
|
|
125
|
|
|
$this->assertRecentResultsPersisted(2); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @expectedException \InvalidArgumentException |
130
|
|
|
* @expectedExceptionMessage Could not find any tests to run. |
131
|
|
|
*/ |
132
|
|
|
public function testNoNamedTestsFound() |
133
|
|
|
{ |
134
|
|
|
$this->execute([ |
135
|
|
|
'--test' => [ |
136
|
|
|
'Group 3' |
137
|
|
|
] |
138
|
|
|
]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testMixedResults() |
142
|
|
|
{ |
143
|
|
|
$this->createSocketReadMockForMixedResults(); |
144
|
|
|
|
145
|
|
|
$returnCode = $this->execute(); |
146
|
|
|
|
147
|
|
|
$this->assertEquals(2, $returnCode); |
148
|
|
|
$this->assertCountLinesOfOutput(4); |
149
|
|
|
|
150
|
|
|
$this->assertHasStandardOutput(); |
151
|
|
|
|
152
|
|
|
$this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-2'] . ' : UNSATISFACTORY - ', $this->output[1]); |
|
|
|
|
153
|
|
|
$this->assertRegExp('/responded in 1\.[0-9]+ s, above the unsatisfactory threshold \(1 s\)$/', $this->output[1]); |
|
|
|
|
154
|
|
|
|
155
|
|
|
$this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-3'] . ' : FAILED - ', $this->output[2]); |
|
|
|
|
156
|
|
|
$this->assertStringEndsWith('failed to respond in the timeout threshold (2 s)', $this->output[2]); |
|
|
|
|
157
|
|
|
|
158
|
|
|
|
159
|
|
|
$this->assertCountRunTests(); |
160
|
|
|
$this->assertResults(1, 0, 1, (count(TestFixtures::$tests) - 2)); |
161
|
|
|
|
162
|
|
|
$this->assertRecentResultsPersisted(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
View Code Duplication |
public function testVerboseMixedResults() |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$this->createSocketReadMockForMixedResults(); |
168
|
|
|
|
169
|
|
|
$returnCode = $this->execute([], [ |
170
|
|
|
'verbosity' => Output::VERBOSITY_VERBOSE |
171
|
|
|
]); |
172
|
|
|
|
173
|
|
|
$this->assertEquals(2, $returnCode); |
174
|
|
|
$this->assertCountLinesOfOutput(5); |
175
|
|
|
|
176
|
|
|
$this->assertHasStandardOutput(); |
177
|
|
|
|
178
|
|
|
$this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-1'] . ' : PASSED - Pinged in ', $this->output[1]); |
|
|
|
|
179
|
|
|
$this->assertRegExp('/0\.[0-9]+s$/', $this->output[1]); |
|
|
|
|
180
|
|
|
|
181
|
|
|
$this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-2'] . ' : UNSATISFACTORY - ', $this->output[2]); |
|
|
|
|
182
|
|
|
$this->assertRegExp('/responded in 1\.[0-9]+ s, above the unsatisfactory threshold \(1 s\)$/', $this->output[2]); |
|
|
|
|
183
|
|
|
|
184
|
|
|
$this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-3'] . ' : FAILED - ', $this->output[3]); |
|
|
|
|
185
|
|
|
$this->assertStringEndsWith('failed to respond in the timeout threshold (2 s)', $this->output[3]); |
|
|
|
|
186
|
|
|
|
187
|
|
|
|
188
|
|
|
$this->assertCountRunTests(); |
189
|
|
|
$this->assertResults(1, 0, 1, (count(TestFixtures::$tests) - 2)); |
190
|
|
|
|
191
|
|
|
$this->assertRecentResultsPersisted(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
View Code Duplication |
private function createSocketMocks() |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
foreach ([ |
197
|
|
|
'socket_create', |
198
|
|
|
'socket_set_option', |
199
|
|
|
'socket_connect', |
200
|
|
|
'socket_send', |
201
|
|
|
'socket_close' |
202
|
|
|
] as $func) { |
203
|
|
|
$mock = $this->getFunctionMock('Overwatch\ServiceBundle\Expectation', $func); |
204
|
|
|
$mock->expects($this->any())->willReturn(true); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
private function createSocketReadMock() |
209
|
|
|
{ |
210
|
|
|
$mock = $this->getFunctionMock('Overwatch\ServiceBundle\Expectation', 'socket_read'); |
211
|
|
|
|
212
|
|
|
$mock->expects($this->any())->willReturnCallback(function() { |
213
|
|
|
usleep(rand(500, 300000)); //Sleep randomly between 0.0005 and 0.3s |
214
|
|
|
return true; |
215
|
|
|
}); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
private function createSocketReadMockForMixedResults() |
219
|
|
|
{ |
220
|
|
|
$mock = $this->getFunctionMock('Overwatch\ServiceBundle\Expectation', 'socket_read'); |
221
|
|
|
|
222
|
|
|
$mock->expects($this->any())->willReturnOnConsecutiveCalls( |
223
|
|
|
$this->returnCallback(function() { usleep(500); return true; }), |
|
|
|
|
224
|
|
|
$this->returnCallback(function() { usleep(1000500); return true; }), |
|
|
|
|
225
|
|
|
$this->returnCallback(function() { usleep(1000500); return true; }), |
|
|
|
|
226
|
|
|
$this->returnValue(false) |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
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.