1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\ResultBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use Overwatch\ResultBundle\Command\ResultCleanupCommand; |
6
|
|
|
use Overwatch\ResultBundle\DataFixtures\ORM\TestResultFixtures; |
7
|
|
|
use Overwatch\UserBundle\Tests\Base\DatabaseAwareTestCase; |
8
|
|
|
use Symfony\Component\Console\Application; |
9
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* ResultCleanupCommandTest |
13
|
|
|
*/ |
14
|
|
|
class ResultCleanupCommandTest extends DatabaseAwareTestCase |
15
|
|
|
{ |
16
|
|
|
use \phpmock\phpunit\PHPMock; |
17
|
|
|
use \Overwatch\TestBundle\Tests\Command\ConsoleTestHelperTrait; |
18
|
|
|
|
19
|
|
|
const COMMAND_NAME = 'overwatch:results:cleanup'; |
20
|
|
|
|
21
|
|
|
private $application; |
22
|
|
|
|
23
|
|
|
private $command; |
24
|
|
|
|
25
|
|
|
private $archive; |
26
|
|
|
|
27
|
|
|
private $resultRepo; |
28
|
|
|
|
29
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
32
|
|
|
|
33
|
|
|
//Set up the console application, command and command test runner |
34
|
|
|
$command = new ResultCleanupCommand(); |
35
|
|
|
$command->setContainer($this->getContainer()); |
36
|
|
|
|
37
|
|
|
$this->application = new Application('Overwatch', '0.0.1-test.' . time()); |
38
|
|
|
$this->application->add($command); |
39
|
|
|
|
40
|
|
|
$this->command = new CommandTester($command); |
41
|
|
|
$this->resultRepo = $this->em->getRepository('OverwatchResultBundle:TestResult'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @expectedException \InvalidArgumentException |
46
|
|
|
* @expectedExceptionMessage Neither the --delete or --compress options were passed. No operation will be completed. |
47
|
|
|
*/ |
48
|
|
|
public function testWithNoOptions() |
49
|
|
|
{ |
50
|
|
|
$this->execute(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @expectedException \InvalidArgumentException |
55
|
|
|
* @expectedExceptionMessage Could not write to the archive file. |
56
|
|
|
*/ |
57
|
|
|
public function testArchiveWhenUnwritable() |
58
|
|
|
{ |
59
|
|
|
$this->createUnwritableFilePutContentsMock(); |
60
|
|
|
$this->execute([ |
61
|
|
|
'--archive' => true, |
62
|
|
|
'--compress' => '-2 days' |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testDelete() |
67
|
|
|
{ |
68
|
|
|
$returnCode = $this->execute([ |
69
|
|
|
'--delete' => '-90 minutes' |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$this->assertEquals(0, $returnCode); |
73
|
|
|
$this->assertCountLinesOfOutput(4); |
74
|
|
|
|
75
|
|
|
$this->assertHasStandardOutput(); |
76
|
|
|
$this->assertContains('Finding results older than', $this->output[1]); |
|
|
|
|
77
|
|
|
$this->assertFinishedRunningCountOperations(1); |
78
|
|
|
|
79
|
|
|
$this->assertFalse($this->resultRepo->getResultsOlderThan(new \DateTime('-90 minutes'))->next()); |
80
|
|
|
$this->assertNotFalse($this->resultRepo->getResultsOlderThan(new \DateTime('-1 minute'))->next()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testCompress() |
84
|
|
|
{ |
85
|
|
|
$returnCode = $this->execute([ |
86
|
|
|
'--compress' => 'now' |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
$this->assertEquals(0, $returnCode); |
90
|
|
|
$this->assertCountLinesOfOutput(4); |
91
|
|
|
|
92
|
|
|
$this->assertHasStandardOutput(); |
93
|
|
|
$this->assertContains('Finding results older than', $this->output[1]); |
|
|
|
|
94
|
|
|
$this->assertFinishedRunningCountOperations(2); |
95
|
|
|
|
96
|
|
|
$results = $this->resultRepo->getResults([]); |
97
|
|
|
$this->assertCount(2, $results); |
98
|
|
|
$this->assertEquals(TestResultFixtures::$results['result-3']->getId(), $results[0]->getId()); |
99
|
|
|
$this->assertEquals(TestResultFixtures::$results['result-2']->getId(), $results[1]->getId()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testDeleteAndArchive() |
103
|
|
|
{ |
104
|
|
|
$this->createFilePutContentsMock(); |
105
|
|
|
|
106
|
|
|
$expectedArchive = (string) $this->resultRepo->getResultsOlderThan(new \DateTime('-1 hour'))->next()[0]; |
107
|
|
|
|
108
|
|
|
$returnCode = $this->execute([ |
109
|
|
|
'--archive' => true, |
110
|
|
|
'--delete' => '-90 minutes' |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
$this->assertEquals(0, $returnCode); |
114
|
|
|
$this->assertCountLinesOfOutput(5); |
115
|
|
|
|
116
|
|
|
$this->assertHasStandardOutput(); |
117
|
|
|
$this->assertContains('Finding results older than', $this->output[2]); |
|
|
|
|
118
|
|
|
$this->assertFinishedRunningCountOperations(1); |
119
|
|
|
|
120
|
|
|
$this->assertFalse($this->resultRepo->getResultsOlderThan(new \DateTime('-90 minutes'))->next()); |
121
|
|
|
$this->assertNotFalse($this->resultRepo->getResultsOlderThan(new \DateTime('-1 minute'))->next()); |
122
|
|
|
|
123
|
|
|
$this->assertCount(2, $this->archive); |
124
|
|
|
$this->assertStringStartsWith($this->application->getName(), $this->archive[0]); |
125
|
|
|
$this->assertContains($this->application->getVersion(), $this->archive[0]); |
126
|
|
|
$this->assertEquals($expectedArchive, $this->archive[1]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testCompressAndArchive() |
130
|
|
|
{ |
131
|
|
|
$this->createFilePutContentsMock(); |
132
|
|
|
|
133
|
|
|
$expectedArchiveLines = [ |
134
|
|
|
(string) $this->resultRepo->find(TestResultFixtures::$results['result-1']->getId()), |
135
|
|
|
(string) $this->resultRepo->find(TestResultFixtures::$results['result-4']->getId()) |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
$returnCode = $this->execute([ |
139
|
|
|
'--archive' => true, |
140
|
|
|
'--compress' => 'now' |
141
|
|
|
]); |
142
|
|
|
|
143
|
|
|
$this->assertEquals(0, $returnCode); |
144
|
|
|
$this->assertCountLinesOfOutput(5); |
145
|
|
|
|
146
|
|
|
$this->assertHasStandardOutput(); |
147
|
|
|
$this->assertContains('Finding results older than', $this->output[2]); |
|
|
|
|
148
|
|
|
$this->assertFinishedRunningCountOperations(2); |
149
|
|
|
|
150
|
|
|
$results = $this->resultRepo->getResults([]); |
151
|
|
|
$this->assertCount(2, $results); |
152
|
|
|
$this->assertEquals(TestResultFixtures::$results['result-3']->getId(), $results[0]->getId()); |
153
|
|
|
$this->assertEquals(TestResultFixtures::$results['result-2']->getId(), $results[1]->getId()); |
154
|
|
|
|
155
|
|
|
$this->assertCount(3, $this->archive); |
156
|
|
|
$this->assertStringStartsWith($this->application->getName(), $this->archive[0]); |
157
|
|
|
$this->assertContains($this->application->getVersion(), $this->archive[0]); |
158
|
|
|
$this->assertEquals($expectedArchiveLines[0], $this->archive[1]); |
159
|
|
|
$this->assertEquals($expectedArchiveLines[1], $this->archive[2]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private function createUnwritableFilePutContentsMock() |
163
|
|
|
{ |
164
|
|
|
$mock = $this->getFunctionMock('Overwatch\ResultBundle\Command', 'file_put_contents'); |
165
|
|
|
$mock->expects($this->any())->willReturn(false); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
private function createFilePutContentsMock() |
169
|
|
|
{ |
170
|
|
|
$this->archive = []; |
171
|
|
|
|
172
|
|
|
$mock = $this->getFunctionMock('Overwatch\ResultBundle\Command', 'file_put_contents'); |
173
|
|
|
$mock->expects($this->any())->willReturnCallback(function ($file, $contents, $mode) { |
174
|
|
|
$this->assertRegExp('/overwatch_archive_[0-9]{14}.log/i', $file); |
175
|
|
|
$this->assertStringEndsWith(PHP_EOL, $contents); |
176
|
|
|
$this->assertEquals(FILE_APPEND, $mode); |
177
|
|
|
|
178
|
|
|
$this->archive[] = trim($contents); |
179
|
|
|
return true; |
180
|
|
|
}); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function assertFinishedRunningCountOperations($count) |
184
|
|
|
{ |
185
|
|
|
$this->assertEquals(" > Applying $count cleanup operations", $this->output[count($this->output) - 2]); |
|
|
|
|
186
|
|
|
$this->assertEquals('Cleanup finished', $this->output[count($this->output) - 1]); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
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.