ExpectationManager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 86
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 1
A get() 0 8 2
A getAll() 0 4 1
B run() 0 34 6
1
<?php
2
3
namespace Overwatch\ExpectationBundle\Expectation;
4
5
use Overwatch\ExpectationBundle\Exception as ExpectationException;
6
use Overwatch\ResultBundle\Entity\TestResult;
7
use Overwatch\ResultBundle\Enum\ResultStatus;
8
use Overwatch\TestBundle\Entity\Test;
9
10
/**
11
 * ExpectationManager
12
 * The ExpectationManager keeps a list of all known expectations and provides
13
 * a run() convienience method for testing.
14
 */
15
class ExpectationManager
16
{
17
    private $expectations = [];
18
19
    /**
20
     * Register an expectation
21
     * 
22
     * @param ExpectationInterface $expectation
23
     * @param string $alias
24
     */
25 34
    public function add(ExpectationInterface $expectation, $alias)
26
    {
27 34
        $this->expectations[$alias] = $expectation;
28 34
    }
29
30
    /**
31
     * Get a registered expectation by alias
32
     * 
33
     * @param string $alias
34
     * @return ExpectationInterface
35
     * @throws ExpectationException\ExpectationNotFoundException
36
     */
37 16
    public function get($alias)
38
    {
39 16
        if (!array_key_exists($alias, $this->expectations)) {
40 1
            throw new ExpectationException\ExpectationNotFoundException($alias);
41
        }
42
43 15
        return $this->expectations[$alias];
44
    }
45
46
    /**
47
     * Get a list of all expecations currently registered.
48
     * 
49
     * @return array
50
     */
51 2
    public function getAll()
52
    {
53 2
        return array_keys($this->expectations);
54
    }
55
56
    /**
57
     * A run() convienience method that:
58
     *  - Takes a test
59
     *  - Finds the correct expectation
60
     *  - Passes through the expected and actual values from the test
61
     *  - Constructs a TestResult based on captured output/thrown exceptions
62
     * 
63
     * @param Test $test
64
     * @return TestResult
65
     */
66 12
    public function run(Test $test)
67
    {
68 12
        $testResult = new TestResult();
69 12
        $testResult->setTest($test);
70
71
        // If the first run of the test is not a pass, run once more to check for false positives
72 12
        $i = 0;
73 12
        $result = '';
74
75 12
        while ($i < 2) {
76
            try {
77 12
                $result = $this->get($test->getExpectation())->run($test->getActual(), $test->getExpected());
78 8
                $testResult->setStatus(ResultStatus::PASSED);
79 8
                $testResult->setInfo($result);
80
81 8
                break;
82 7
            } catch (\Exception $ex) {
83 7
                $result = $ex;
84 7
                $testResult->setInfo($ex->getMessage());
85
            }
86
87 7
            $i++;
88 7
        }
89
90 12
        if ($result instanceof ExpectationException\ExpectationFailedException) {
91 3
            $testResult->setStatus(ResultStatus::FAILED);
92 12
        } elseif ($result instanceof ExpectationException\ExpectationUnsatisfactoryException) {
93 3
            $testResult->setStatus(ResultStatus::UNSATISFACTORY);
94 11
        } elseif ($result instanceof \Exception) {
95 2
            $testResult->setStatus(ResultStatus::ERROR);
96 2
        }
97
98 12
        return $testResult;
99
    }
100
}
101