1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MS\PHPMD\Tests\Unit\CleanCode; |
4
|
|
|
|
5
|
|
|
use MS\PHPMD\Rule\CleanCode\MethodOneTryCatch; |
6
|
|
|
use MS\PHPMD\Tests\Unit\AbstractApplyTest; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class MethodOneTryCatchTest |
10
|
|
|
* |
11
|
|
|
* @package MS\PHPMD\Tests\Unit\CleanCode |
12
|
|
|
*/ |
13
|
|
|
class MethodOneTryCatchTest extends AbstractApplyTest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @covers MS\PHPMD\Rule\CleanCode\MethodOneTryCatch |
17
|
|
|
*/ |
18
|
|
|
public function testMoreTryStatements() |
19
|
|
|
{ |
20
|
|
|
$node = $this->getMethodNode('TestClass', 'tryMore', [ |
21
|
|
|
'TryStatement' => array_fill(0, 2, $this->getNode('try')) |
22
|
|
|
]); |
23
|
|
|
|
24
|
|
|
$this->assertRule($node, 1); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @covers MS\PHPMD\Rule\CleanCode\MethodOneTryCatch |
29
|
|
|
*/ |
30
|
|
|
public function testOneTryStatementWithCatchFinally() |
31
|
|
|
{ |
32
|
|
|
$node = $this->getMethodNode('TestClass', 'tryMore', [ |
33
|
|
|
'TryStatement' => array_fill(0, 1, $this->getNode('try')), |
34
|
|
|
'ScopeStatement' => array_merge( |
35
|
|
|
array_fill(0, 2, $this->getNode('catch')), |
36
|
|
|
array_fill(0, 1, $this->getNode('finally')) |
37
|
|
|
) |
38
|
|
|
]); |
39
|
|
|
|
40
|
|
|
$this->assertRule($node, 0); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @covers MS\PHPMD\Rule\CleanCode\MethodOneTryCatch |
45
|
|
|
*/ |
46
|
|
|
public function testOneTryStatementWithScopeStatements() |
47
|
|
|
{ |
48
|
|
|
$tryNode = \Mockery::mock('PHPMD\AbstractNode'); |
49
|
|
|
$tryNode->shouldReceive('isInstanceOf')->andReturn(true); |
50
|
|
|
|
51
|
|
|
$proxyNode = \Mockery::mock('PHPMD\AbstractNode'); |
52
|
|
|
$proxyNode->shouldReceive('getImage')->andReturn('while'); |
53
|
|
|
$proxyNode->shouldReceive('isInstanceOf')->andReturn(false); |
54
|
|
|
$proxyNode->shouldReceive('getParent')->andReturn($tryNode); |
55
|
|
|
|
56
|
|
|
$notAllowedNode = \Mockery::mock('PHPMD\AbstractNode'); |
57
|
|
|
$notAllowedNode->shouldReceive('getImage')->andReturn('if'); |
58
|
|
|
$notAllowedNode->shouldReceive('getParent')->andReturn(null); |
59
|
|
|
|
60
|
|
|
$allowedNode = \Mockery::mock('PHPMD\AbstractNode'); |
61
|
|
|
$allowedNode->shouldReceive('getImage')->andReturn('if'); |
62
|
|
|
$allowedNode->shouldReceive('getParent')->andReturn($proxyNode); |
63
|
|
|
|
64
|
|
|
$node = $this->getMethodNode('TestClass', 'tryMore', [ |
65
|
|
|
'TryStatement' => array_fill(0, 1, $this->getNode('try')), |
66
|
|
|
'ScopeStatement' => [ |
67
|
|
|
$allowedNode, |
68
|
|
|
$notAllowedNode |
69
|
|
|
] |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$this->assertRule($node, 1); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return MethodOneTryCatch |
77
|
|
|
*/ |
78
|
|
|
protected function getRule() |
79
|
|
|
{ |
80
|
|
|
$rule = new MethodOneTryCatch(); |
81
|
|
|
$rule->addProperty('delimiter', ','); |
82
|
|
|
$rule->addProperty('allowedChildren', 'catch,finally'); |
83
|
|
|
|
84
|
|
|
return $rule; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|