ExpectationManagerTest::testRunErrorThenPass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace Overwatch\ExpectationBundle\Tests\Expectation;
4
5
use Overwatch\ExpectationBundle\Exception as ExpectationException;
6
use Overwatch\ExpectationBundle\Expectation\ExpectationManager;
7
use Overwatch\ResultBundle\Enum\ResultStatus;
8
use Overwatch\TestBundle\Entity\Test;
9
10
/**
11
 * ExpectationManagerTest
12
 * Unit tests the basic functionality of the ExpectationManager
13
 */
14
class ExpectationManagerTest extends \PHPUnit_Framework_TestCase
15
{
16
    protected $em;
17
18
    protected $toPingMock;
19
20
    protected $test;
21
22
    protected function setUp()
23
    {
24
        $this->em = new ExpectationManager;
25
26
        $this->toPingMock = $this->getMockBuilder('Overwatch\ServiceBundle\Expectation\ToPingExpectation')
27
            ->setConstructorArgs([[
28
                'timeout'        => 1,
29
                'unsatisfactory' => 0.5
30
            ]])
31
            ->getMock();
32
    }
33
34
    private function setUpTestRun($result)
35
    {
36
        if ($result instanceof \Exception) {
37
            $this->toPingMock->expects($this->exactly(2))
38
                ->method('run')
39
                ->will($this->throwException($result));
40
        } elseif (is_array($result)) {
41
            $this->toPingMock->expects($this->exactly(2))
42
                ->method('run')
43
                ->will($this->onConsecutiveCalls($result[0], $result[1]));
44
        } else {
45
            $this->toPingMock->expects($this->once())
46
                ->method('run')
47
                ->will($this->returnValue($result));
48
        }
49
50
        $this->em->add($this->toPingMock, 'toPing');
51
52
        $this->test = new Test;
53
        $this->test
54
            ->setActual('8.8.8.8')
55
            ->setExpectation('toPing')
56
        ;
57
    }
58
59
    public function testAdd()
60
    {
61
        $this->em->add($this->toPingMock, 'toPing');
62
        $this->assertCount(1, $this->em->getAll());
63
        $this->assertEquals(['toPing'], $this->em->getAll());
64
    }
65
66
    public function testGet()
67
    {
68
        $this->em->add($this->toPingMock, 'toPing');
69
        $this->assertInstanceOf('Overwatch\ServiceBundle\Expectation\ToPingExpectation', $this->em->get('toPing'));
70
    }
71
72 View Code Duplication
    public function testRunSuccess()
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...
73
    {
74
        $info = 'Pinged in 0.1s';
75
        $this->setUpTestRun($info);
76
77
        $result = $this->em->run($this->test);
78
79
        $this->assertInstanceOf('Overwatch\ResultBundle\Entity\TestResult', $result);
80
        $this->assertEquals($result->getTest(), $this->test);
81
        $this->assertEquals($result->getStatus(), ResultStatus::PASSED);
82
        $this->assertEquals($result->getInfo(), $info);
83
    }
84
85 View Code Duplication
    public function testRunUnsatisfactory()
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...
86
    {
87
        $info = '8.8.8.8 responded in 0.6 s, above the unsatisfactory threshold (0.5 s)';
88
        $this->setUpTestRun(new ExpectationException\ExpectationUnsatisfactoryException($info));
89
90
        $result = $this->em->run($this->test);
91
92
        $this->assertInstanceOf('Overwatch\ResultBundle\Entity\TestResult', $result);
93
        $this->assertEquals($result->getTest(), $this->test);
94
        $this->assertEquals($result->getStatus(), ResultStatus::UNSATISFACTORY);
95
        $this->assertEquals($result->getInfo(), $info);
96
    }
97
98 View Code Duplication
    public function testRunFailed()
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...
99
    {
100
        $info = '8.8.8.8 failed to respond in the timeout threshold (1 s)';
101
        $this->setUpTestRun(new ExpectationException\ExpectationFailedException($info));
102
103
        $result = $this->em->run($this->test);
104
105
        $this->assertInstanceOf('Overwatch\ResultBundle\Entity\TestResult', $result);
106
        $this->assertEquals($result->getTest(), $this->test);
107
        $this->assertEquals($result->getStatus(), ResultStatus::FAILED);
108
        $this->assertEquals($result->getInfo(), $info);
109
    }
110
111 View Code Duplication
    public function testRunError()
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...
112
    {
113
        $info = 'An exception got thrown and f**k knows why';
114
        $this->setUpTestRun(new \Exception($info));
115
116
        $result = $this->em->run($this->test);
117
118
        $this->assertInstanceOf('Overwatch\ResultBundle\Entity\TestResult', $result);
119
        $this->assertEquals($result->getTest(), $this->test);
120
        $this->assertEquals($result->getStatus(), ResultStatus::ERROR);
121
        $this->assertEquals($result->getInfo(), $info);
122
    }
123
    
124
    public function testRunErrorThenPass()
125
    {
126
        $info = 'Passes the second!';
127
128
        $this->setUpTestRun([
129
            $this->throwException(new \Exception('Fails the first time')),
130
            $this->returnValue($info)
131
        ]);
132
133
        $result = $this->em->run($this->test);
134
135
        $this->assertInstanceOf('Overwatch\ResultBundle\Entity\TestResult', $result);
136
        $this->assertEquals($result->getTest(), $this->test);
137
        $this->assertEquals($result->getStatus(), ResultStatus::PASSED);
138
        $this->assertEquals($result->getInfo(), $info);
139
    }
140
}
141