Completed
Push — master ( 86db96...a2faf8 )
by Nicolas
08:49
created

ArrangeListener::getAnnotationReader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Nikoms\PhpUnit\Listener;
4
5
use Doctrine\Common\Annotations\AnnotationRegistry;
6
use Nikoms\PhpUnit\Annotation\Arrange;
7
use Nikoms\PhpUnit\AnnotationReaderFactory;
8
use PHPUnit_Framework_Test;
9
10
/**
11
 * Class ArrangeListener
12
 * @package Nikoms\PhpUnit\Listener
13
 */
14
class ArrangeListener extends \PHPUnit_Framework_BaseTestListener
15
{
16
    /**
17
     * @var array
18
     */
19
    public static $inputs;
20
21
    /**
22
     * ArrangeListener constructor.
23
     * @param array $ignoredAnnotationNames
24
     */
25
    public function __construct(array $ignoredAnnotationNames = null)
0 ignored issues
show
Unused Code introduced by
The parameter $ignoredAnnotationNames is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
    {
27
        AnnotationRegistry::registerFile(__DIR__.'/../Annotation/Arrange.php');
28
    }
29
30
    /**
31
     * @param  PHPUnit_Framework_Test $test
32
     */
33
    public function startTest(PHPUnit_Framework_Test $test)
34
    {
35
        if ($test instanceof \PHPUnit_Framework_TestCase) {
36
            $this->setUpContext($test);
37
        }
38
    }
39
40
    /**
41
     * @param \PHPUnit_Framework_TestCase $testCase
42
     */
43
    private function setUpContext(\PHPUnit_Framework_TestCase $testCase)
44
    {
45
        $testMethodArguments = array();
46
        $reflectionMethod = new \ReflectionMethod($testCase, $testCase->getName(false));
47
48
        $annotationReader = AnnotationReaderFactory::getAnnotationReader();
49
        $annotations = $annotationReader->getMethodAnnotations($reflectionMethod);
50
        foreach ($annotations as $i => $annotation) {
51
            if ($annotation instanceof Arrange) {
52
                $arrangeOutput = $this->runAnnotations($testCase, $annotation, $i);
53
                if ($arrangeOutput !== null) {
54
                    $testMethodArguments[] = $arrangeOutput;
55
                }
56
            }
57
        }
58
        $testCase->setDependencyInput($testMethodArguments);
59
    }
60
61
    /**
62
     * @param \PHPUnit_Framework_TestCase $testCase
63
     * @param Arrange $annotation
64
     * @param int $annotationId
65
     * @return mixed
66
     */
67
    private function runAnnotations(\PHPUnit_Framework_TestCase $testCase, Arrange $annotation, $annotationId)
68
    {
69
        $arrangeOutput = null;
70
        foreach ($annotation->getMethods() as $method => $annotationArguments) {
71
            if (method_exists($testCase, $method)) {
72
                $givenArgument = array();
73
74
                if ($arrangeOutput !== null) {
75
                    $givenArgument[] = $arrangeOutput;
76
                }
77
78
                $dataProviderArguments = $testCase->readAttribute($testCase, 'data');
79
                if (!empty($dataProviderArguments)) {
80
                    $givenArgument = array_merge($givenArgument, $dataProviderArguments);
81
                }
82
83
                if ($annotationArguments !== null) {
84
                    $givenArgument[] = $annotationArguments;
85
                }
86
                self::$inputs[$testCase->getName(true)][$annotationId][$method] = $givenArgument;
87
                $arrangeOutput = call_user_func_array(array($testCase, $method), $givenArgument);
88
            } else {
89
                trigger_error(
90
                    sprintf('Error on @Arrange annotation: Impossible to call "%s" method', $method),
91
                    E_USER_NOTICE
92
                );
93
            }
94
        }
95
96
        return $arrangeOutput;
97
    }
98
}
99