Failed Conditions
Pull Request — master (#142)
by Zac
04:15
created

ExpectationManager::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
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 8
    public function add(ExpectationInterface $expectation, $alias)
26
    {
27 8
        $this->expectations[$alias] = $expectation;
28 8
    }
29
30
    /**
31
     * Get a registered expectation by alias
32
     * 
33
     * @param string $alias
34
     * @return ExpectationInterface
35
     * @throws ExpectationException\ExpectationNotFoundException
36
     */
37 6
    public function get($alias)
38
    {
39 6
        if (!array_key_exists($alias, $this->expectations)) {
40
            throw new ExpectationException\ExpectationNotFoundException($alias);
41
        }
42
43 6
        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 5
    public function run(Test $test)
67
    {
68 5
        $testResult = new TestResult();
69 5
        $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 5
        $i = 0;
73 5
        $result = '';
74
75 5
        while ($i < 2) {
76
            try {
77 5
                $result = $this->get($test->getExpectation())->run($test->getActual(), $test->getExpected());
78 2
                $testResult->setStatus(ResultStatus::PASSED);
79 2
                $testResult->setInfo($result);
80
81 2
                break;
82 4
            } catch (\Exception $ex) {
83 4
                $result = $ex;
84 4
                $testResult->setInfo($ex->getMessage());
85
            }
86
87 4
            $i++;
88 4
        }
89
90 5
        if ($result instanceof ExpectationException\ExpectationFailedException) {
91 1
            $testResult->setStatus(ResultStatus::FAILED);
92 5
        } elseif ($result instanceof ExpectationException\ExpectationUnsatisfactoryException) {
93 1
            $testResult->setStatus(ResultStatus::UNSATISFACTORY);
94 4
        } elseif ($result instanceof \Exception) {
95 1
            $testResult->setStatus(ResultStatus::ERROR);
96 1
        }
97
98 5
        return $testResult;
99
    }
100
}
101