Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileClassScannerTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class FileClassScannerTest extends \PHPUnit\Framework\TestCase
12 {
13 
15  {
16  $this->expectException(InvalidFileException::class);
17  new FileClassScanner(false);
18  }
19 
20  public function testEmptyArrayForFileWithoutNamespaceOrClass()
21  {
22  $scanner = $this->getMockBuilder(FileClassScanner::class)->disableOriginalConstructor()->setMethods([
23  'getFileContents'
24  ])->getMock();
25  $scanner->expects(self::once())->method('getFileContents')->willReturn(
26  <<<PHP
27 <?php
28 
29 echo 'hello world';
30 
31 if (class_exists('some_class')) {
32  \$object = new some_class();
33 }
34 PHP
35  );
38  $result = $scanner->getClassNames();
39  self::assertCount(0, $result);
40  }
41 
42  public function testGetClassName()
43  {
44  $scanner = $this->getMockBuilder(FileClassScanner::class)->disableOriginalConstructor()->setMethods([
45  'getFileContents'
46  ])->getMock();
47  $scanner->expects(self::once())->method('getFileContents')->willReturn(
48  <<<PHP
49 <?php
50 
51 class ThisIsATest {
52 
53 }
54 PHP
55  );
58  $result = $scanner->getClassNames();
59 
60  self::assertCount(1, $result);
61  self::assertContains('ThisIsATest', $result);
62  }
63 
64  public function testGetClassNameAndSingleNamespace()
65  {
66  $scanner = $this->getMockBuilder(FileClassScanner::class)->disableOriginalConstructor()->setMethods([
67  'getFileContents'
68  ])->getMock();
69  $scanner->expects(self::once())->method('getFileContents')->willReturn(
70  <<<PHP
71 <?php
72 
73 namespace NS;
74 
75 class ThisIsMyTest {
76 
77 }
78 PHP
79  );
82  $result = $scanner->getClassNames();
83 
84  self::assertCount(1, $result);
85  self::assertContains('NS\ThisIsMyTest', $result);
86  }
87 
88  public function testGetClassNameAndMultiNamespace()
89  {
90  $scanner = $this->getMockBuilder(FileClassScanner::class)->disableOriginalConstructor()->setMethods([
91  'getFileContents'
92  ])->getMock();
93  $scanner->expects(self::once())->method('getFileContents')->willReturn(
94  <<<PHP
95 <?php
96 
97 namespace This\Is\My\Ns;
98 
99 class ThisIsMyTest {
100 
101  public function __construct()
102  {
103  \This\Is\Another\Ns::class;
104  }
105 
106  public function test()
107  {
108 
109  }
110 }
111 PHP
112  );
115  $result = $scanner->getClassNames();
116 
117  self::assertCount(1, $result);
118  self::assertContains('This\Is\My\Ns\ThisIsMyTest', $result);
119  }
120 
121  public function testGetMultiClassNameAndMultiNamespace()
122  {
123  $scanner = $this->getMockBuilder(FileClassScanner::class)->disableOriginalConstructor()->setMethods([
124  'getFileContents'
125  ])->getMock();
126  $scanner->expects(self::once())->method('getFileContents')->willReturn(
127  <<<PHP
128 <?php
129 
130 namespace This\Is\My\Ns;
131 
132 class ThisIsMyTest {
133 
134  public function __construct()
135  {
136  \$this->get(\This\Is\Another\Ns::class)->method();
137  self:: class;
138  }
139 
140  public function test()
141  {
142 
143  }
144 }
145 
146 class ThisIsForBreaking {
147 
148 }
149 
150 PHP
151  );
154  $result = $scanner->getClassNames();
155 
156  self::assertCount(2, $result);
157  self::assertContains('This\Is\My\Ns\ThisIsMyTest', $result);
158  self::assertContains('This\Is\My\Ns\ThisIsForBreaking', $result);
159  }
160 
161  public function testBracketedNamespacesAndClasses()
162  {
163  $scanner = $this->getMockBuilder(FileClassScanner::class)->disableOriginalConstructor()->setMethods([
164  'getFileContents'
165  ])->getMock();
166  $scanner->expects(self::once())->method('getFileContents')->willReturn(
167  <<<PHP
168 <?php
169 
170 namespace This\Is\My\Ns {
171 
172  class ThisIsMyTest
173  {
174 
175  public function __construct()
176  {
177  \This\Is\Another\Ns::class;
178  self:: class;
179  }
180 
181  }
182 
183  class ThisIsForBreaking
184  {
185  }
186 }
187 
188 namespace This\Is\Not\My\Ns {
189 
190  class ThisIsNotMyTest
191  {
192  }
193 }
194 
195 PHP
196  );
199  $result = $scanner->getClassNames();
200 
201  self::assertCount(3, $result);
202  self::assertContains('This\Is\My\Ns\ThisIsMyTest', $result);
203  self::assertContains('This\Is\My\Ns\ThisIsForBreaking', $result);
204  self::assertContains('This\Is\Not\My\Ns\ThisIsNotMyTest', $result);
205  }
206 
208  {
209  $scanner = $this->getMockBuilder(FileClassScanner::class)->disableOriginalConstructor()->setMethods([
210  'getFileContents'
211  ])->getMock();
212  $scanner->expects(self::once())->method('getFileContents')->willReturn(<<<'PHP'
213 <?php
214 
215 namespace This\Is\My\Ns;
216 
217 use stdClass;
218 
219 class ThisIsMyTest
220 {
221  protected function firstMethod()
222  {
223  $test = 1;
224  $testString = "foo {$test}";
225  $className = stdClass::class;
226  $testString2 = "bar {$test}";
227  }
228 
229  protected function secondMethod()
230  {
231  $this->doMethod(stdClass::class)->runAction();
232  }
233 }
234 
235 PHP
236  );
237 
238  /* @var $scanner FileClassScanner */
239  $result = $scanner->getClassNames();
240 
241  self::assertCount(1, $result);
242  }
243 
244  public function testInvalidPHPCodeThrowsExceptionWhenCannotDetermineBraceOrSemiColon()
245  {
246  $this->expectException(InvalidFileException::class);
247  $scanner = $this->getMockBuilder(FileClassScanner::class)->disableOriginalConstructor()->setMethods([
248  'getFileContents'
249  ])->getMock();
250  $scanner->expects(self::once())->method('getFileContents')->willReturn(
251  <<<PHP
252  <?php
253 
254 namespace This\Is\My\Ns
255 
256 class ThisIsMyTest
257 {
258 }
259 
260 PHP
261  );
264  $scanner->getClassNames();
265  }
266 }
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31