Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FinalImplementationTest.php
Go to the documentation of this file.
1 <?php
8 
9 use PHPUnit\Framework\TestCase as TestCase;
10 use PHPUnit_Framework_MockObject_MockObject as MockObject;
11 use PHPUnit\Framework\MockObject\Matcher\InvokedRecorder as InvokedRecorder;
12 use PHPUnit\Framework\MockObject\Builder\InvocationMocker as InvocationMocker;
14 use PHPMD\Report;
15 use PHPMD\AbstractNode;
16 use PHPMD\Node\ClassNode;
17 use PHPMD\Node\MethodNode;
18 use BadMethodCallException;
19 
20 class FinalImplementationTest extends TestCase
21 {
27  public function testRuleNotAppliesToNotFinalFinalizable($nodeType)
28  {
29  $finalizableNode = $this->createFinalizableNodeMock($nodeType);
30  $finalizableNode->method('isFinal')->willReturn(false);
31 
32  $rule = new FinalImplementation();
33  $this->expectsRuleViolation($rule, $this->never());
34  $rule->apply($finalizableNode);
35  }
36 
42  public function testRuleAppliesToFinalFinalizable($nodeType)
43  {
44  $finalizableNode = $this->createFinalizableNodeMock($nodeType);
45  $finalizableNode->method('isFinal')->willReturn(true);
46 
47  $rule = new FinalImplementation();
48  $this->expectsRuleViolation($rule, $this->once());
49  $rule->apply($finalizableNode);
50  }
51 
57  public function testRuleVerifiesFinalizableNodes($nodeType)
58  {
59  $finalizableNode = $this->createFinalizableNodeMock($nodeType);
60 
61  $finalizableNode->expects($this->atLeastOnce())
62  ->method('isFinal');
63 
64  $rule = new FinalImplementation();
65  $rule->apply($finalizableNode);
66  }
67 
72  {
73  $someNode = $this->getMockBuilder(AbstractNode::class)
74  ->disableOriginalConstructor()
75  ->getMockForAbstractClass();
76 
77  $rule = new FinalImplementation();
78  $rule->apply($someNode);
79  }
80 
86  public function finalizableNodeTypesProvider()
87  {
88  return [
89  [ClassNode::class],
90  [MethodNode::class],
91  ];
92  }
93 
100  private function createFinalizableNodeMock($nodeType)
101  {
102  $finalizableNode = $this->getMockBuilder($nodeType)
103  ->disableOriginalConstructor()
104  ->disableProxyingToOriginalMethods()
105  ->setMethods([
106  'isFinal',
107  // disable name lookup from AST artifact
108  'getNamespaceName',
109  'getParentName',
110  'getName',
111  ])
112  ->getMock();
113  return $finalizableNode;
114  }
115 
121  private function expectsRuleViolation(FinalImplementation $rule, InvokedRecorder $violationExpectation)
122  {
123  $report = $this->getMockBuilder(Report::class)->getMock();
124  $invokation = $report->expects($violationExpectation)->method('addRuleViolation');
125  $rule->setReport($report);
126  return $invokation;
127  }
128 }