ToPingExpectationTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 15.12 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 13
loc 86
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A testExpectation() 0 9 1
A testExpectationUnsatisfactory() 0 9 1
A testExpectationFailed() 0 5 1
A createSocketMocks() 13 13 2
A createSocketReadMock() 0 21 4

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\ServiceBundle\Tests\Expectation;
4
5
use Overwatch\ServiceBundle\Expectation\ToPingExpectation;
6
use phpmock\phpunit\PHPMock;
7
8
/**
9
 * ToPingExpectationTest
10
 */
11
class ToPingExpectationTest extends \PHPUnit_Framework_TestCase
12
{
13
    use PHPMock;
14
    
15
    private $expectation;
16
    
17
    public function setUp()
18
    {
19
        $this->createSocketMocks();
20
        
21
        $this->expectation = new ToPingExpectation([
22
            'timeout'        => 2,
23
            'unsatisfactory' => 1
24
        ]);
25
    }
26
    
27
    public function testExpectation()
28
    {
29
        $this->createSocketReadMock();
30
        
31
        $this->assertContains(
32
            'Pinged in ',
33
            $this->expectation->run('8.8.8.8')
34
        );
35
    }
36
    
37
    /**
38
     * @expectedException Overwatch\ExpectationBundle\Exception\ExpectationUnsatisfactoryException
39
     * @expectedExceptionMessage above the unsatisfactory threshold 
40
     */
41
    public function testExpectationUnsatisfactory()
42
    {
43
        $expectation = new ToPingExpectation([
44
            'timeout'        => 2,
45
            'unsatisfactory' => 0.2
46
        ]);
47
        $this->createSocketReadMock('delayed');
48
        $expectation->run('8.8.8.8');
49
    }
50
    
51
    /**
52
     * @expectedException Overwatch\ExpectationBundle\Exception\ExpectationFailedException
53
     * @expectedExceptionMessage failed to respond in the timeout threshold 
54
     */
55
    public function testExpectationFailed()
56
    {
57
        $this->createSocketReadMock('failed');
58
        $this->expectation->run('8.8.8.8');
59
    }
60
    
61 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...
62
    {
63
        foreach ([
64
            'socket_create',
65
            'socket_set_option',
66
            'socket_connect',
67
            'socket_send',
68
            'socket_close'
69
        ] as $func) {
70
            $mock = $this->getFunctionMock('Overwatch\ServiceBundle\Expectation', $func);
71
            $mock->expects($this->once())->willReturn(true);
72
        }
73
    }
74
    
75
    private function createSocketReadMock($type = 'normal')
76
    {
77
        $mock = $this->getFunctionMock('Overwatch\ServiceBundle\Expectation', 'socket_read');
78
        
79
        switch ($type) {
80
            case 'normal':
81
                $mock->expects($this->once())->willReturn(true);
82
                break;
83
            
84
            case 'delayed':
85
                $mock->expects($this->once())->willReturnCallback(function() {
86
                    usleep(210000);
87
                    return true;
88
                });
89
                break;
90
            
91
            case 'failed':
92
                $mock->expects($this->once())->willReturn(false);
93
                break;
94
        }
95
    }
96
}
97