Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AllPurposeActionTest.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
14 use PHPUnit\Framework\TestCase as TestCase;
15 use PHPUnit_Framework_MockObject_MockObject as MockObject;
16 use PHPUnit\Framework\MockObject\Builder\InvocationMocker as InvocationMocker;
17 use PHPMD\Report;
18 use PHPMD\Node\ClassNode;
19 
20 class AllPurposeActionTest extends TestCase
21 {
28  public function testApply($fakeAction, bool $violates)
29  {
30  $node = $this->createNodeMock($fakeAction);
31  $rule = new AllPurposeAction();
32  $this->expectsRuleViolation($rule, $violates);
33  $rule->apply($node);
34  }
35 
39  public function getCases(): array
40  {
41  return [
42  [
43  new class implements ActionInterface, HttpGetActionInterface {
47  public function execute()
48  {
49  return null;
50  }
51  },
52  false
53  ],
54  [
55  new class implements ActionInterface {
59  public function execute()
60  {
61  return null;
62  }
63  },
64  true
65  ],
66  [
67  new class implements HttpGetActionInterface {
71  public function execute()
72  {
73  return null;
74  }
75  },
76  false
77  ],
78  [
79  new class {
80 
81  },
82  false
83  ]
84  ];
85  }
86 
92  private function createNodeMock($dynamicObject): MockObject
93  {
94  $node = $this->getMockBuilder(ClassNode::class)
95  ->disableOriginalConstructor()
96  ->disableProxyingToOriginalMethods()
97  ->setMethods([
98  // disable name lookup from AST artifact
99  'getNamespaceName',
100  'getParentName',
101  'getName',
102  'getFullQualifiedName',
103  ])
104  ->getMock();
105  $node->expects($this->any())
106  ->method('getFullQualifiedName')
107  ->willReturn(get_class($dynamicObject));
108 
109  return $node;
110  }
111 
117  private function expectsRuleViolation(
118  AllPurposeAction $rule,
119  bool $expects
120  ): InvocationMocker {
122  $report = $this->getMockBuilder(Report::class)->getMock();
123  if ($expects) {
124  $violationExpectation = $this->atLeastOnce();
125  } else {
126  $violationExpectation = $this->never();
127  }
128  $invokation = $report->expects($violationExpectation)
129  ->method('addRuleViolation');
130  $rule->setReport($report);
131 
132  return $invokation;
133  }
134 }
return false
Definition: gallery.phtml:36