EntitySimpleGetterSetterTest::getClassNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace MS\PHPMD\Tests\Unit\Symfony2;
4
5
use MS\PHPMD\Rule\Symfony2\EntitySimpleGetterSetter;
6
use MS\PHPMD\Tests\Unit\AbstractApplyTest;
7
8
/**
9
 * Class EntitySimpleGetterSetterTest
10
 *
11
 * @package MS\PHPMD\Tests\Unit\Symfony2
12
 */
13
class EntitySimpleGetterSetterTest extends AbstractApplyTest
14
{
15
    const CLASS_NAME = 'TestEntity';
16
17
    /**
18
     * @covers MS\PHPMD\Rule\Symfony2\EntitySimpleGetterSetter
19
     * @covers MS\PHPMD\Rule\Symfony2\AbstractEntityRule
20
     */
21
    public function testApplyNoEntity()
22
    {
23
        $node = \Mockery::mock('PHPMD\Node\ClassNode');
24
        $node->shouldReceive('getDocComment')->andReturn('');
25
        $node->shouldReceive('isAbstract')->andReturn(false);
26
27
        $this->assertRule($node, 0);
28
29
        $node = \Mockery::mock('PHPMD\Node\ClassNode');
30
        $node->shouldReceive('getDocComment')->andReturn('* @covers ORM\Entity');
31
        $node->shouldReceive('isAbstract')->andReturn(false);
32
33
        $this->assertRule($node, 0);
34
    }
35
36
    /**
37
     * @covers MS\PHPMD\Rule\Symfony2\EntitySimpleGetterSetter
38
     * @covers MS\PHPMD\Rule\Symfony2\AbstractEntityRule
39
     */
40 View Code Duplication
    public function testValidEntity()
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...
41
    {
42
        $methodNode = $this->getMethodNode(self::CLASS_NAME, 'getData', [
43
            'ScopeStatement' => [],
44
            'ReturnStatement' => array_fill(0, 1, $this->getNode('return')),
45
            'Variable' => array_fill(0, 1, $this->getNode('$this')),
46
        ]);
47
        $classNode = $this->getClassNode([$methodNode]);
48
49
        $this->assertRule($classNode, 0);
50
    }
51
52
    /**
53
     * @covers MS\PHPMD\Rule\Symfony2\EntitySimpleGetterSetter
54
     * @covers MS\PHPMD\Rule\Symfony2\AbstractEntityRule
55
     */
56 View Code Duplication
    public function testValidEntityWithWhitelist()
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...
57
    {
58
        $methodNode = $this->getMethodNode(self::CLASS_NAME, '__construct');
59
        $classNode = $this->getClassNode([$methodNode]);
60
61
        $this->assertRule($classNode, 0);
62
    }
63
64
    /**
65
     * @covers MS\PHPMD\Rule\Symfony2\EntitySimpleGetterSetter
66
     * @covers MS\PHPMD\Rule\Symfony2\AbstractEntityRule
67
     */
68 View Code Duplication
    public function testWrongMethodPrefix()
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...
69
    {
70
        $methodNode = $this->getMethodNode(self::CLASS_NAME, 'doSomething');
71
        $classNode = $this->getClassNode([$methodNode]);
72
73
        $this->assertRule($classNode, 1);
74
    }
75
76
    /**
77
     * @covers MS\PHPMD\Rule\Symfony2\EntitySimpleGetterSetter
78
     * @covers MS\PHPMD\Rule\Symfony2\AbstractEntityRule
79
     */
80
    public function testForbiddenScope()
81
    {
82
        $methodNode = $this->getMethodNode(self::CLASS_NAME, 'getData', [
83
            'ScopeStatement' => array_fill(0, 1, $this->getNode('if')),
84
        ]);
85
        $classNode = $this->getClassNode([$methodNode]);
86
87
        $this->assertRule($classNode, 1);
88
    }
89
90
    /**
91
     * @covers MS\PHPMD\Rule\Symfony2\EntitySimpleGetterSetter
92
     * @covers MS\PHPMD\Rule\Symfony2\AbstractEntityRule
93
     */
94 View Code Duplication
    public function testMultipleReturns()
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...
95
    {
96
        $methodNode = $this->getMethodNode(self::CLASS_NAME, 'getData', [
97
            'ScopeStatement' => [],
98
            'ReturnStatement' => array_fill(0, 2, $this->getNode('return')),
99
            'Variable' => array_fill(0, 1, $this->getNode('$this')),
100
        ]);
101
        $classNode = $this->getClassNode([$methodNode]);
102
103
        $this->assertRule($classNode, 1);
104
    }
105
106
    /**
107
     * @covers MS\PHPMD\Rule\Symfony2\EntitySimpleGetterSetter
108
     * @covers MS\PHPMD\Rule\Symfony2\AbstractEntityRule
109
     */
110
    public function testRelationReturnToThis()
111
    {
112
        $methodNode = $this->getMethodNode(self::CLASS_NAME, 'getData', [
113
            'ScopeStatement' => [],
114
            'ReturnStatement' => [],
115
            'Variable' => array_fill(0, 2, $this->getNode('$this')),
116
        ]);
117
        $classNode = $this->getClassNode([$methodNode]);
118
119
        $this->assertRule($classNode, 1);
120
121
        $methodNode = $this->getMethodNode(self::CLASS_NAME, 'getData', [
122
            'ScopeStatement' => [],
123
            'ReturnStatement' => array_fill(0, 1, $this->getNode('return')),
124
            'Variable' => array_fill(0, 3, $this->getNode('$this')),
125
        ]);
126
        $classNode = $this->getClassNode([$methodNode]);
127
128
        $this->assertRule($classNode, 1);
129
    }
130
131
    /**
132
     * @covers MS\PHPMD\Rule\Symfony2\EntitySimpleGetterSetter
133
     * @covers MS\PHPMD\Rule\Symfony2\AbstractEntityRule
134
     */
135 View Code Duplication
    public function testAbstractClass()
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...
136
    {
137
        $node = \Mockery::mock('PHPMD\Node\ClassNode');
138
        $node->shouldReceive('getDocComment')->andReturn('');
139
        $node->shouldReceive('isAbstract')->andReturn(true);
140
141
        $this->assertRule($node, 0);
142
    }
143
144
    /**
145
     * @covers MS\PHPMD\Rule\Symfony2\EntitySimpleGetterSetter
146
     * @covers MS\PHPMD\Rule\Symfony2\AbstractEntityRule
147
     */
148
    public function testForceClassIsAnEntity()
149
    {
150
        $methodNode = $this->getMethodNode(self::CLASS_NAME, 'getData', [
151
            'ScopeStatement' => [],
152
            'ReturnStatement' => array_fill(0, 1, $this->getNode('return')),
153
            'Variable' => array_fill(0, 3, $this->getNode('$this')),
154
        ]);
155
156
        $classNode = \Mockery::mock('PHPMD\Node\ClassNode');
157
        $classNode->shouldReceive('getDocComment')->andReturn('* @isEntity');
158
        $classNode->shouldReceive('getImage')->andReturn(self::CLASS_NAME);
159
        $classNode->shouldReceive('isAbstract')->andReturn(true);
160
        $classNode->shouldReceive('getMethods')->andReturn([$methodNode]);
161
162
        $this->assertRule($classNode, 1);
163
    }
164
165
    /**
166
     * @param array $methodNodes
167
     *
168
     * @return \Mockery\MockInterface
169
     */
170
    private function getClassNode(array $methodNodes)
171
    {
172
        $classNode = \Mockery::mock('PHPMD\Node\ClassNode');
173
        $classNode->shouldReceive('getDocComment')->andReturn('* @ORM\Entity()');
174
        $classNode->shouldReceive('getImage')->andReturn(self::CLASS_NAME);
175
        $classNode->shouldReceive('isAbstract')->andReturn(false);
176
        $classNode->shouldReceive('getMethods')->andReturn($methodNodes);
177
178
        return $classNode;
179
    }
180
181
    /**
182
     * @return EntitySimpleGetterSetter
183
     */
184
    protected function getRule()
185
    {
186
        $rule = new EntitySimpleGetterSetter();
187
        $rule->addProperty('delimiter', ',');
188
        $rule->addProperty('prefixes', 'get,set');
189
        $rule->addProperty('whitelist', '__construct');
190
        $rule->addProperty('entityRegex', '(\*\s*@\S*Entity)i');
191
        $rule->addProperty('classIsEntityRegex', '(\*\s*@isEntity)i');
192
193
        return $rule;
194
    }
195
}
196