Failed Conditions
Push — master ( b9670c...4ba57f )
by Zac
16:20 queued 37s
created

ResultCleanupCommandTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 175
Duplicated Lines 8 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 9
dl 14
loc 175
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 14 14 1
A testWithNoOptions() 0 4 1
A testArchiveWhenUnwritable() 0 8 1
A testDelete() 0 16 1
A testCompress() 0 18 1
B testDeleteAndArchive() 0 26 1
B testCompressAndArchive() 0 32 1
A createUnwritableFilePutContentsMock() 0 5 1
A createFilePutContentsMock() 0 14 1
A assertFinishedRunningCountOperations() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
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...
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]);
0 ignored issues
show
Bug introduced by
The property output cannot be accessed from this context as it is declared private in class PHPUnit_Framework_TestCase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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]);
0 ignored issues
show
Bug introduced by
The property output cannot be accessed from this context as it is declared private in class PHPUnit_Framework_TestCase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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]);
0 ignored issues
show
Bug introduced by
The property output cannot be accessed from this context as it is declared private in class PHPUnit_Framework_TestCase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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]);
0 ignored issues
show
Bug introduced by
The property output cannot be accessed from this context as it is declared private in class PHPUnit_Framework_TestCase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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]);
0 ignored issues
show
Bug introduced by
The property output cannot be accessed from this context as it is declared private in class PHPUnit_Framework_TestCase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $count instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
186
        $this->assertEquals('Cleanup finished', $this->output[count($this->output) - 1]);
0 ignored issues
show
Bug introduced by
The property output cannot be accessed from this context as it is declared private in class PHPUnit_Framework_TestCase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
187
    }
188
}
189