DynamicFixtureListener   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 52
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A startTest() 0 7 2
A setUpContext() 0 8 2
A callMethods() 0 10 3
1
<?php
2
3
namespace Nikoms\DynamicFixture;
4
5
use PHPUnit_Framework_Test;
6
7
class DynamicFixtureListener extends \PHPUnit_Framework_BaseTestListener
8
{
9
10
    private $annotationName;
11
12
    /**
13
     * @param string $annotationName
14
     */
15 4
    public function __construct($annotationName = 'setUpContext')
16
    {
17 4
        $this->annotationName = $annotationName;
18 4
    }
19
20
    /**
21
     * A test started.
22
     * @param  PHPUnit_Framework_Test $test
23
     */
24 4
    public function startTest(PHPUnit_Framework_Test $test)
25
    {
26
        //That's nasty, but saved my life :)
27 4
        if ($test instanceof \PHPUnit_Framework_TestCase) {
28 4
            $this->setUpContext($test);
29 4
        }
30 4
    }
31
32
    /**
33
     * @param \PHPUnit_Framework_TestCase $testCase
34
     */
35 4
    private function setUpContext(\PHPUnit_Framework_TestCase $testCase)
36
    {
37 4
        $annotations = $testCase->getAnnotations();
38 4
        if (isset($annotations['method'][$this->annotationName])) {
39 3
            $this->callMethods($testCase, $annotations['method'][$this->annotationName]);
40 3
        }
41
42 4
    }
43
44
    /**
45
     * @param \PHPUnit_Framework_TestCase $testCase
46
     * @param array $methods
47
     */
48 3
    private function callMethods(\PHPUnit_Framework_TestCase $testCase, array $methods)
49
    {
50 3
        foreach ($methods as $method) {
51 3
            if (!method_exists($testCase, $method)) {
52 1
                continue;
53
            }
54 2
            $reflectionMethod = new \ReflectionMethod($testCase, $method);
55 2
            $reflectionMethod->invoke($testCase);
56 3
        }
57 3
    }
58
}
59