Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PhpRuleTest.php
Go to the documentation of this file.
1 <?php
7 
8 class PhpRuleTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $model;
14 
15  protected function setUp()
16  {
17  $mapRoutes = ['someModule' => ['Magento\SomeModule'], 'anotherModule' => ['Magento\OneModule']];
18  $mapLayoutBlocks = ['area' => ['block.name' => ['Magento\SomeModule' => 'Magento\SomeModule']]];
19  $pluginMap = [
20  'Magento\Module1\Plugin1' => 'Magento\Module1\Subject',
21  'Magento\Module1\Plugin2' => 'Magento\Module2\Subject',
22  ];
23  $this->model = new PhpRule($mapRoutes, $mapLayoutBlocks, $pluginMap);
24  }
25 
26  public function testNonPhpGetDependencyInfo()
27  {
28  $content = 'any content';
29  $this->assertEmpty($this->model->getDependencyInfo('any', 'not php', 'any', $content));
30  }
31 
38  public function testGetDependencyInfo($class, $content, array $expected)
39  {
40  $file = $this->makeMockFilepath($class);
41  $module = $this->getModuleFromClass($class);
42  $this->assertEquals($expected, $this->model->getDependencyInfo($module, 'php', $file, $content));
43  }
44 
49  {
50  return [
51  'Extend class in same module' => [
52  'Magento\SomeModule\SomeClass',
53  'something extends \Magento\SomeModule\Any\ClassName {',
54  []
55  ],
56  'Extend class in different module' => [
57  'Magento\AnotherModule\SomeClass',
58  'something extends \Magento\SomeModule\Any\ClassName {',
59  [
60  [
61  'module' => 'Magento\SomeModule',
63  'source' => 'Magento\SomeModule\Any\ClassName',
64  ]
65  ]
66  ],
67  'getViewFileUrl in same module' => [
68  'Magento\SomeModule\SomeClass',
69  '$this->getViewFileUrl("Magento_SomeModule::js/order-by-sku-failure.js")',
70  []
71  ],
72  'getViewFileUrl in different module' => [
73  'Magento\AnotherModule\SomeClass',
74  '$this->getViewFileUrl("Magento_SomeModule::js/order-by-sku-failure.js")',
75  [
76  [
77  'module' => 'Magento\SomeModule',
79  'source' => 'Magento_SomeModule',
80  ]
81  ]
82  ],
83  'Helper class from same module' => [
84  'Magento\SomeModule\SomeClass',
85  '$this->helper("Magento\SomeModule\Any\ClassName")',
86  []
87  ],
88  'Helper class from another module' => [
89  'Magento\AnotherModule\SomeClass',
90  '$this->helper("Magento\SomeModule\Any\ClassName")',
91  [
92  [
93  'module' => 'Magento\SomeModule',
95  'source' => 'Magento\SomeModule\Any\ClassName',
96  ]
97  ]
98  ],
99  'getUrl from same module' => [
100  'Magento\SomeModule\SomeClass',
101  '$this->getUrl("someModule")',
102  []
103  ],
104  'getUrl from another module' => [
105  'Magento\SomeModule\SomeClass',
106  '$this->getUrl("anotherModule")',
107  [
108  [
109  'module' => 'Magento\OneModule',
111  'source' => 'getUrl("anotherModule"',
112  ]
113  ]
114  ],
115  'getBlock from same module' => [
116  'Magento\SomeModule\SomeClass',
117  '$this->getLayout()->getBlock(\'block.name\');', []
118  ],
119  'getBlock from another module' => [
120  'Magento\AnotherModule\SomeClass',
121  '$this->getLayout()->getBlock(\'block.name\');',
122  [
123  [
124  'module' => 'Magento\SomeModule',
126  'source' => 'getBlock(\'block.name\')',
127  ]
128  ]
129  ],
130  'Plugin on class in same module' => [
131  'Magento\Module1\Plugin1',
132  ', \Magento\Module1\Subject $variable',
133  []
134  ],
135  'Plugin depends on arbitrary class in same module' => [
136  'Magento\Module1\Plugin1',
137  ', \Magento\Module1\NotSubject $variable',
138  []
139  ],
140  'Plugin on class in different module' => [
141  'Magento\Module1\Plugin2',
142  'Magento\Module2\Subject',
143  [
144  [
145  'module' => 'Magento\Module2',
147  'source' => 'Magento\Module2\Subject',
148  ]
149  ],
150  ],
151  'Plugin depends on arbitrary class in same module as subject' => [
152  'Magento\Module1\Plugin2',
153  'Magento\Module2\NotSubject',
154  [
155  [
156  'module' => 'Magento\Module2',
158  'source' => 'Magento\Module2\NotSubject',
159  ]
160  ]
161  ],
162  'Plugin depends on arbitrary class in arbitrary module' => [
163  'Magento\Module1\Plugin2',
164  'Magento\OtherModule\NotSubject',
165  [
166  [
167  'module' => 'Magento\OtherModule',
169  'source' => 'Magento\OtherModule\NotSubject',
170  ]
171  ]
172  ],
173  ];
174  }
175 
182  public function testGetDefaultModelDependency($module, $content, array $expected)
183  {
184  $mapLayoutBlocks = [
185  'default' => [
186  'block.name' => [
187  'Magento\SomeModule' => 'Magento\SomeModule',
188  ],
189  ],
190  ];
191  $this->model = new PhpRule([], $mapLayoutBlocks);
192  $this->assertEquals($expected, $this->model->getDependencyInfo($module, 'template', 'any', $content));
193  }
194 
196  {
197  return [
198  [
199  'Magento\AnotherModule',
200  '$this->getLayout()->getBlock(\'block.name\');',
201  [
202  [
203  'module' => 'Magento\SomeModule',
205  'source' => 'getBlock(\'block.name\')',
206  ]
207  ],
208  ]
209  ];
210  }
211 
218  private function makeMockFilepath($class)
219  {
220  return 'ClassRoot' . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
221  }
222 
230  private function getModuleFromClass($class)
231  {
232  return substr($class, 0, strpos($class, '\\', 9)); // (strlen('Magento\\') + 1) === 9
233  }
234 }
testGetDefaultModelDependency($module, $content, array $expected)
$_option $_optionId $class
Definition: date.phtml:13
testGetDependencyInfo($class, $content, array $expected)
Definition: PhpRuleTest.php:38