TestsRunCommandTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 214
Duplicated Lines 53.27 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 9
dl 114
loc 214
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 15 15 1
A testWhenAllPass() 16 16 1
B testVerboselyWhenAllPass() 26 26 1
A testDiscardsResults() 16 16 1
A testNamedTests() 0 23 1
A testNoNamedTestsFound() 0 8 1
A testMixedResults() 0 23 1
B testVerboseMixedResults() 28 28 1
A createSocketMocks() 13 13 2
A createSocketReadMock() 0 9 1
A createSocketReadMockForMixedResults() 0 11 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\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()
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...
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()
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...
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()
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...
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]);
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...
73
        $this->assertRegExp('/0\.[0-9]+s$/', $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...
74
        
75
        $this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-2'] . ' : PASSED - Pinged in ', $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...
76
        $this->assertRegExp('/0\.[0-9]+s$/', $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...
77
        
78
        $this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-3'] . ' : PASSED - Pinged in ', $this->output[3]);
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...
79
        $this->assertRegExp('/0\.[0-9]+s$/', $this->output[3]);
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...
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()
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...
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]);
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...
153
        $this->assertRegExp('/responded in 1\.[0-9]+ s, above the unsatisfactory threshold \(1 s\)$/', $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...
154
        
155
        $this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-3'] . ' : FAILED - ', $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...
156
        $this->assertStringEndsWith('failed to respond in the timeout threshold (2 s)', $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...
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()
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...
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]);
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...
179
        $this->assertRegExp('/0\.[0-9]+s$/', $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...
180
        
181
        $this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-2'] . ' : UNSATISFACTORY - ', $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...
182
        $this->assertRegExp('/responded in 1\.[0-9]+ s, above the unsatisfactory threshold \(1 s\)$/', $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...
183
        
184
        $this->assertStringStartsWith(' > ' . TestFixtures::$tests['test-3'] . ' : FAILED - ', $this->output[3]);
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...
185
        $this->assertStringEndsWith('failed to respond in the timeout threshold (2 s)', $this->output[3]);
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...
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()
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...
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; }),
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
224
            $this->returnCallback(function() { usleep(1000500); return true; }),
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
225
            $this->returnCallback(function() { usleep(1000500); return true; }),
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
226
            $this->returnValue(false)
227
        );
228
    }
229
}
230