AbstractApplyTest::assertRule()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace MS\PHPMD\Tests\Unit;
4
5
use PHPMD\AbstractNode;
6
use PHPMD\AbstractRule;
7
use PHPMD\Report;
8
9
/**
10
 * Class AbstractApplyTest
11
 *
12
 * @package MS\PHPMD\Tests\Unit
13
 */
14
abstract class AbstractApplyTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * close Mockery
18
     */
19
    public function tearDown()
20
    {
21
        \Mockery::close();
22
    }
23
24
    /**
25
     * @param AbstractNode $node
26
     * @param int          $violationNumber
27
     */
28
    protected function assertRule(AbstractNode $node, $violationNumber)
29
    {
30
        $rule = $this->getRule();
31
        $rule->setReport($this->getReport($violationNumber));
0 ignored issues
show
Bug introduced by
It seems like $this->getReport($violationNumber) targeting MS\PHPMD\Tests\Unit\AbstractApplyTest::getReport() can also be of type object<Mockery\MockInterface>; however, PHPMD\AbstractRule::setReport() does only seem to accept object<PHPMD\Report>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
32
        $rule->apply($node);
33
    }
34
35
    /**
36
     * @param int $violationNumber
37
     *
38
     * @return \Mockery\MockInterface|Report
39
     */
40
    protected function getReport($violationNumber)
41
    {
42
        $report = \Mockery::mock('PHPMD\Report');
43
        $report->shouldReceive('addRuleViolation')->times($violationNumber);
44
45
        return $report;
46
    }
47
48
    /**
49
     * @param string $className
50
     * @param string $methodName
51
     * @param array  $findChildrenOfType
52
     *
53
     * @return \Mockery\MockInterface
54
     */
55
    protected function getMethodNode($className, $methodName, array $findChildrenOfType = [])
56
    {
57
        $methodNode = \Mockery::mock('PHPMD\Node\MethodNode');
58
        $methodNode->shouldReceive('getImage')->andReturn($methodName);
59
        $methodNode->shouldReceive('getParentName')->andReturn($className);
60
        $methodNode->shouldReceive('getName')->andReturn($methodName);
61
62
        foreach ($findChildrenOfType as $argument => $return) {
63
            $methodNode->shouldReceive('findChildrenOfType')->with($argument)->andReturn($return);
64
        }
65
66
        return $methodNode;
67
    }
68
69
    /**
70
     * @param string $name
71
     *
72
     * @return \Mockery\MockInterface
73
     */
74 View Code Duplication
    protected function getNode($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $node = \Mockery::mock('PHPMD\AbstractNode');
77
        $node->shouldReceive('getName')->andReturn($name);
78
        $node->shouldReceive('getImage')->andReturn($name);
79
80
        return $node;
81
    }
82
83
    /**
84
     * @return AbstractRule
85
     */
86
    abstract protected function getRule();
87
}
88