DynamicFixtureListener::setUpContext()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
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