PhpUnitExpectationFixerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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\PhpUnitExpectationFixer
  21. */
  22. final class PhpUnitExpectationFixerTest 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. [
  40. '<?php
  41. final class MyTest extends \PHPUnit_Framework_TestCase
  42. {
  43. function testFnc()
  44. {
  45. aaa();
  46. $this->expectException(\'RuntimeException\');
  47. $this->expectExceptionMessage(\'msg\'/*B*/);
  48. $this->expectExceptionCode(/*C*/123);
  49. zzz();
  50. }
  51. }',
  52. '<?php
  53. final class MyTest extends \PHPUnit_Framework_TestCase
  54. {
  55. function testFnc()
  56. {
  57. aaa();
  58. $this->setExpectedException(\'RuntimeException\', \'msg\'/*B*/, /*C*/123);
  59. zzz();
  60. }
  61. }',
  62. ],
  63. [
  64. '<?php
  65. final class MyTest extends \PHPUnit_Framework_TestCase
  66. {
  67. function testFnc()
  68. {
  69. aaa();
  70. $this->expectException(\'RuntimeException\'/*B*/ /*B2*/);
  71. $this->expectExceptionCode(/*C*/123);
  72. zzz();
  73. }
  74. function testFnc2()
  75. {
  76. aaa();
  77. $this->expectException(\'RuntimeException\');
  78. $this->expectExceptionMessage(/*B*/ null /*B2*/ + 1);
  79. $this->expectExceptionCode(/*C*/123);
  80. zzz();
  81. }
  82. }',
  83. '<?php
  84. final class MyTest extends \PHPUnit_Framework_TestCase
  85. {
  86. function testFnc()
  87. {
  88. aaa();
  89. $this->setExpectedException(\'RuntimeException\',/*B*/ null /*B2*/,/*C*/123);
  90. zzz();
  91. }
  92. function testFnc2()
  93. {
  94. aaa();
  95. $this->setExpectedException(\'RuntimeException\',/*B*/ null /*B2*/ + 1,/*C*/123);
  96. zzz();
  97. }
  98. }',
  99. ],
  100. [
  101. '<?php
  102. final class MyTest extends \PHPUnit_Framework_TestCase
  103. {
  104. function testFnc()
  105. {
  106. $this->expectException(
  107. \Exception::class
  108. );
  109. }
  110. }',
  111. '<?php
  112. final class MyTest extends \PHPUnit_Framework_TestCase
  113. {
  114. function testFnc()
  115. {
  116. $this->setExpectedException(
  117. \Exception::class
  118. );
  119. }
  120. }',
  121. ],
  122. [
  123. '<?php
  124. final class MyTest extends \PHPUnit_Framework_TestCase
  125. {
  126. function testFnc()
  127. {
  128. $this->expectException(
  129. \Exception::class
  130. );
  131. $this->expectExceptionMessage(
  132. "foo"
  133. );
  134. $this->expectExceptionCode(
  135. 123
  136. );
  137. }
  138. }',
  139. '<?php
  140. final class MyTest extends \PHPUnit_Framework_TestCase
  141. {
  142. function testFnc()
  143. {
  144. $this->setExpectedException(
  145. \Exception::class,
  146. "foo",
  147. 123
  148. );
  149. }
  150. }',
  151. ],
  152. [
  153. '<?php
  154. final class MyTest extends \PHPUnit_Framework_TestCase
  155. {
  156. public function testFoo()
  157. {
  158. $this->expectException("RuntimeException");
  159. $this->expectExceptionMessage("Msg");
  160. $this->expectExceptionCode(123);
  161. foo();
  162. }
  163. public function testBar()
  164. {
  165. $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  166. bar();
  167. }
  168. }',
  169. '<?php
  170. final class MyTest extends \PHPUnit_Framework_TestCase
  171. {
  172. public function testFoo()
  173. {
  174. $this->setExpectedException("RuntimeException", "Msg", 123);
  175. foo();
  176. }
  177. public function testBar()
  178. {
  179. $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  180. bar();
  181. }
  182. }',
  183. ['target' => PhpUnitTargetVersion::VERSION_5_2],
  184. ],
  185. [
  186. '<?php
  187. final class MyTest extends \PHPUnit_Framework_TestCase
  188. {
  189. public function testFoo()
  190. {
  191. $this->expectException("RuntimeException");
  192. $this->expectExceptionMessage("Msg");
  193. $this->expectExceptionCode(123);
  194. foo();
  195. }
  196. public function testBar()
  197. {
  198. $this->expectException("RuntimeException");
  199. $this->expectExceptionMessageRegExp("/Msg.*/");
  200. $this->expectExceptionCode(123);
  201. bar();
  202. }
  203. }',
  204. '<?php
  205. final class MyTest extends \PHPUnit_Framework_TestCase
  206. {
  207. public function testFoo()
  208. {
  209. $this->setExpectedException("RuntimeException", "Msg", 123);
  210. foo();
  211. }
  212. public function testBar()
  213. {
  214. $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  215. bar();
  216. }
  217. }',
  218. ['target' => PhpUnitTargetVersion::VERSION_5_6],
  219. ],
  220. [
  221. '<?php
  222. final class MyTest extends \PHPUnit_Framework_TestCase
  223. {
  224. public function testFoo()
  225. {
  226. $this->expectException("RuntimeException");
  227. $this->expectExceptionMessage("Msg");
  228. $this->expectExceptionCode(123);
  229. foo();
  230. }
  231. public function testBar()
  232. {
  233. $this->expectException("RuntimeException");
  234. $this->expectExceptionMessageRegExp("/Msg.*/");
  235. $this->expectExceptionCode(123);
  236. bar();
  237. }
  238. }',
  239. '<?php
  240. final class MyTest extends \PHPUnit_Framework_TestCase
  241. {
  242. public function testFoo()
  243. {
  244. $this->setExpectedException("RuntimeException", "Msg", 123);
  245. foo();
  246. }
  247. public function testBar()
  248. {
  249. $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123);
  250. bar();
  251. }
  252. }',
  253. ['target' => PhpUnitTargetVersion::VERSION_NEWEST],
  254. ],
  255. ];
  256. }
  257. /**
  258. * @param string $expected
  259. * @param null|string $input
  260. *
  261. * @dataProvider provideMessyWhitespacesCases
  262. */
  263. public function testMessyWhitespaces($expected, $input = null)
  264. {
  265. $expected = str_replace([' ', "\n"], ["\t", "\r\n"], $expected);
  266. if (null !== $input) {
  267. $input = str_replace([' ', "\n"], ["\t", "\r\n"], $input);
  268. }
  269. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  270. $this->doTest($expected, $input);
  271. }
  272. public function provideMessyWhitespacesCases()
  273. {
  274. return [
  275. [
  276. '<?php
  277. final class MyTest extends \PHPUnit_Framework_TestCase
  278. {
  279. function testFnc()
  280. {
  281. aaa();
  282. $this->expectException(\'RuntimeException\');
  283. $this->expectExceptionMessage(\'msg\'/*B*/);
  284. $this->expectExceptionCode(/*C*/123);
  285. zzz();
  286. }
  287. }',
  288. '<?php
  289. final class MyTest extends \PHPUnit_Framework_TestCase
  290. {
  291. function testFnc()
  292. {
  293. aaa();
  294. $this->setExpectedException(\'RuntimeException\', \'msg\'/*B*/, /*C*/123);
  295. zzz();
  296. }
  297. }',
  298. ],
  299. ];
  300. }
  301. }