PhpUnitNoExpectationAnnotationFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\PhpUnit;
  12. use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitNoExpectationAnnotationFixer
  21. */
  22. final class PhpUnitNoExpectationAnnotationFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @param string $expected
  26. * @param null|string $input
  27. * @param array $config
  28. *
  29. * @dataProvider provideTestFixCases
  30. */
  31. public function testFix($expected, $input = null, array $config = [])
  32. {
  33. $this->fixer->configure($config);
  34. $this->doTest($expected, $input);
  35. }
  36. public function provideTestFixCases()
  37. {
  38. return [
  39. 'expecting exception' => [
  40. '<?php
  41. final class MyTest extends \PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. */
  45. public function testFnc()
  46. {
  47. $this->setExpectedException(\FooException::class);
  48. aaa();
  49. }
  50. }',
  51. '<?php
  52. final class MyTest extends \PHPUnit_Framework_TestCase
  53. {
  54. /**
  55. * @expectedException FooException
  56. */
  57. public function testFnc()
  58. {
  59. aaa();
  60. }
  61. }',
  62. ],
  63. 'expecting rooted exception' => [
  64. '<?php
  65. final class MyTest extends \PHPUnit_Framework_TestCase
  66. {
  67. /**
  68. */
  69. public function testFnc()
  70. {
  71. $this->setExpectedException(\FooException::class);
  72. aaa();
  73. }
  74. }',
  75. '<?php
  76. final class MyTest extends \PHPUnit_Framework_TestCase
  77. {
  78. /**
  79. * @expectedException \FooException
  80. */
  81. public function testFnc()
  82. {
  83. aaa();
  84. }
  85. }',
  86. ],
  87. 'expecting exception with msg' => [
  88. '<?php
  89. final class MyTest extends \PHPUnit_Framework_TestCase
  90. {
  91. /**
  92. */
  93. public function testFnc()
  94. {
  95. $this->setExpectedException(\FooException::class, \'foo@bar\');
  96. aaa();
  97. }
  98. }',
  99. '<?php
  100. final class MyTest extends \PHPUnit_Framework_TestCase
  101. {
  102. /**
  103. * @expectedException FooException
  104. * @expectedExceptionMessage foo@bar
  105. */
  106. public function testFnc()
  107. {
  108. aaa();
  109. }
  110. }',
  111. ],
  112. 'expecting exception with code' => [
  113. '<?php
  114. final class MyTest extends \PHPUnit_Framework_TestCase
  115. {
  116. /**
  117. */
  118. public function testFnc()
  119. {
  120. $this->setExpectedException(\FooException::class, null, 123);
  121. aaa();
  122. }
  123. }',
  124. '<?php
  125. final class MyTest extends \PHPUnit_Framework_TestCase
  126. {
  127. /**
  128. * @expectedException FooException
  129. * @expectedExceptionCode 123
  130. */
  131. public function testFnc()
  132. {
  133. aaa();
  134. }
  135. }',
  136. ],
  137. 'expecting exception with msg and code' => [
  138. '<?php
  139. final class MyTest extends \PHPUnit_Framework_TestCase
  140. {
  141. /**
  142. */
  143. public function testFnc()
  144. {
  145. $this->setExpectedException(\FooException::class, \'foo\', 123);
  146. aaa();
  147. }
  148. }',
  149. '<?php
  150. final class MyTest extends \PHPUnit_Framework_TestCase
  151. {
  152. /**
  153. * @expectedException FooException
  154. * @expectedExceptionMessage foo
  155. * @expectedExceptionCode 123
  156. */
  157. public function testFnc()
  158. {
  159. aaa();
  160. }
  161. }',
  162. ],
  163. 'expecting exception with msg regex [but too low target]' => [
  164. '<?php
  165. final class MyTest extends \PHPUnit_Framework_TestCase
  166. {
  167. /**
  168. * @expectedException FooException
  169. * @expectedExceptionMessageRegExp /foo.*$/
  170. */
  171. public function testFnc()
  172. {
  173. aaa();
  174. }
  175. }',
  176. null,
  177. ['target' => PhpUnitTargetVersion::VERSION_3_2],
  178. ],
  179. 'expecting exception with msg regex' => [
  180. '<?php
  181. final class MyTest extends \PHPUnit_Framework_TestCase
  182. {
  183. /**
  184. */
  185. public function testFnc()
  186. {
  187. $this->setExpectedExceptionRegExp(\FooException::class, \'/foo.*$/\');
  188. aaa();
  189. }
  190. }',
  191. '<?php
  192. final class MyTest extends \PHPUnit_Framework_TestCase
  193. {
  194. /**
  195. * @expectedException FooException
  196. * @expectedExceptionMessageRegExp /foo.*$/
  197. */
  198. public function testFnc()
  199. {
  200. aaa();
  201. }
  202. }',
  203. ['target' => PhpUnitTargetVersion::VERSION_4_3],
  204. ],
  205. 'use_class_const=false' => [
  206. '<?php
  207. final class MyTest extends \PHPUnit_Framework_TestCase
  208. {
  209. /**
  210. */
  211. public function testFnc()
  212. {
  213. $this->setExpectedException(\'\FooException\');
  214. aaa();
  215. }
  216. }',
  217. '<?php
  218. final class MyTest extends \PHPUnit_Framework_TestCase
  219. {
  220. /**
  221. * @expectedException FooException
  222. */
  223. public function testFnc()
  224. {
  225. aaa();
  226. }
  227. }',
  228. ['use_class_const' => false],
  229. ],
  230. 'keep rest of docblock' => [
  231. '<?php
  232. final class MyTest extends \PHPUnit_Framework_TestCase
  233. {
  234. /**
  235. * Summary.
  236. *
  237. * @param int $param
  238. * @return void
  239. */
  240. public function testFnc($param)
  241. {
  242. $this->setExpectedException(\FooException::class);
  243. aaa();
  244. }
  245. }',
  246. '<?php
  247. final class MyTest extends \PHPUnit_Framework_TestCase
  248. {
  249. /**
  250. * Summary.
  251. *
  252. * @param int $param
  253. * @expectedException FooException
  254. * @return void
  255. */
  256. public function testFnc($param)
  257. {
  258. aaa();
  259. }
  260. }',
  261. ],
  262. 'fix method without visibility' => [
  263. '<?php
  264. final class MyTest extends \PHPUnit_Framework_TestCase
  265. {
  266. /**
  267. */
  268. function testFnc($param)
  269. {
  270. $this->setExpectedException(\FooException::class);
  271. aaa();
  272. }
  273. }',
  274. '<?php
  275. final class MyTest extends \PHPUnit_Framework_TestCase
  276. {
  277. /**
  278. * @expectedException FooException
  279. */
  280. function testFnc($param)
  281. {
  282. aaa();
  283. }
  284. }',
  285. ],
  286. 'fix final method' => [
  287. '<?php
  288. final class MyTest extends \PHPUnit_Framework_TestCase
  289. {
  290. /**
  291. */
  292. final function testFnc($param)
  293. {
  294. $this->setExpectedException(\FooException::class);
  295. aaa();
  296. }
  297. }',
  298. '<?php
  299. final class MyTest extends \PHPUnit_Framework_TestCase
  300. {
  301. /**
  302. * @expectedException FooException
  303. */
  304. final function testFnc($param)
  305. {
  306. aaa();
  307. }
  308. }',
  309. ],
  310. 'ignore when no docblock' => [
  311. '<?php
  312. final class MyTest extends \PHPUnit_Framework_TestCase
  313. {
  314. final function testFoo($param)
  315. {
  316. aaa();
  317. }
  318. /**
  319. */
  320. final function testFnc($param)
  321. {
  322. $this->setExpectedException(\FooException::class);
  323. aaa();
  324. }
  325. }',
  326. '<?php
  327. final class MyTest extends \PHPUnit_Framework_TestCase
  328. {
  329. final function testFoo($param)
  330. {
  331. aaa();
  332. }
  333. /**
  334. * @expectedException FooException
  335. */
  336. final function testFnc($param)
  337. {
  338. aaa();
  339. }
  340. }',
  341. ],
  342. 'valid docblock but for property, not method' => [
  343. '<?php
  344. final class MyTest extends \PHPUnit_Framework_TestCase
  345. {
  346. /**
  347. * @expectedException FooException
  348. * @expectedExceptionCode 123
  349. */
  350. public $foo;
  351. public function bar()
  352. {
  353. /**
  354. * @expectedException FooException
  355. * @expectedExceptionCode 123
  356. */
  357. $baz = 1;
  358. /**
  359. * @expectedException FooException
  360. * @expectedExceptionCode 123
  361. */
  362. while (false) {}
  363. }
  364. }',
  365. ],
  366. 'respect \' and " in expected msg' => [
  367. '<?php
  368. final class MyTest extends \PHPUnit_Framework_TestCase
  369. {
  370. /**
  371. * Summary.
  372. *
  373. */
  374. public function testFnc($param)
  375. {
  376. $this->setExpectedException(\FooException::class, \'Foo \\\' bar " baz\');
  377. aaa();
  378. }
  379. }',
  380. '<?php
  381. final class MyTest extends \PHPUnit_Framework_TestCase
  382. {
  383. /**
  384. * Summary.
  385. *
  386. * @expectedException FooException
  387. * @expectedExceptionMessage Foo \' bar " baz
  388. */
  389. public function testFnc($param)
  390. {
  391. aaa();
  392. }
  393. }',
  394. ],
  395. 'special \\ handling' => [
  396. <<<'EOT'
  397. <?php
  398. final class MyTest extends \PHPUnit_Framework_TestCase
  399. {
  400. /**
  401. */
  402. public function testElementNonExistentOne()
  403. {
  404. $this->setExpectedException(\Cake\View\Exception\MissingElementException::class, 'A backslash at the end \\');
  405. $this->View->element('non_existent_element');
  406. }
  407. /**
  408. */
  409. public function testElementNonExistentTwo()
  410. {
  411. $this->setExpectedExceptionRegExp(\Cake\View\Exception\MissingElementException::class, '#^Element file "Element[\\\\/]non_existent_element\\.ctp" is missing\\.$#');
  412. $this->View->element('non_existent_element');
  413. }
  414. }
  415. EOT
  416. ,
  417. <<<'EOT'
  418. <?php
  419. final class MyTest extends \PHPUnit_Framework_TestCase
  420. {
  421. /**
  422. * @expectedException \Cake\View\Exception\MissingElementException
  423. * @expectedExceptionMessage A backslash at the end \
  424. */
  425. public function testElementNonExistentOne()
  426. {
  427. $this->View->element('non_existent_element');
  428. }
  429. /**
  430. * @expectedException \Cake\View\Exception\MissingElementException
  431. * @expectedExceptionMessageRegExp #^Element file "Element[\\/]non_existent_element\.ctp" is missing\.$#
  432. */
  433. public function testElementNonExistentTwo()
  434. {
  435. $this->View->element('non_existent_element');
  436. }
  437. }
  438. EOT
  439. ,
  440. ],
  441. ];
  442. }
  443. /**
  444. * @param string $expected
  445. * @param null|string $input
  446. *
  447. * @dataProvider provideMessyWhitespacesCases
  448. */
  449. public function testMessyWhitespaces($expected, $input = null)
  450. {
  451. $expected = str_replace([' ', "\n"], ["\t", "\r\n"], $expected);
  452. if (null !== $input) {
  453. $input = str_replace([' ', "\n"], ["\t", "\r\n"], $input);
  454. }
  455. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  456. $this->doTest($expected, $input);
  457. }
  458. public function provideMessyWhitespacesCases()
  459. {
  460. return [
  461. [
  462. '<?php
  463. final class MyTest extends \PHPUnit_Framework_TestCase
  464. {
  465. /**
  466. */
  467. public function testFnc()
  468. {
  469. $this->setExpectedException(\FooException::class, \'foo\', 123);
  470. aaa();
  471. }
  472. }',
  473. '<?php
  474. final class MyTest extends \PHPUnit_Framework_TestCase
  475. {
  476. /**
  477. * @expectedException FooException
  478. * @expectedExceptionMessage foo
  479. * @expectedExceptionCode 123
  480. */
  481. public function testFnc()
  482. {
  483. aaa();
  484. }
  485. }',
  486. ],
  487. ];
  488. }
  489. }