PhpUnitDedicateAssertFixerTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. /**
  15. * @author SpacePossum
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertFixer
  20. */
  21. final class PhpUnitDedicateAssertFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @param string $expected
  25. * @param null|string $input
  26. * @param array $config
  27. *
  28. * @dataProvider provideTestFixCases
  29. */
  30. public function testFix($expected, $input = null, array $config = [])
  31. {
  32. $this->fixer->configure($config);
  33. $this->doTest($expected, $input);
  34. }
  35. public function provideTestFixCases()
  36. {
  37. $cases = [
  38. [
  39. '<?php
  40. $this->assertNan($a);
  41. $this->assertNan($a);
  42. $this->assertTrue(test\is_nan($a));
  43. $this->assertTrue(test\a\is_nan($a));
  44. ',
  45. '<?php
  46. $this->assertTrue(is_nan($a));
  47. $this->assertTrue(\is_nan($a));
  48. $this->assertTrue(test\is_nan($a));
  49. $this->assertTrue(test\a\is_nan($a));
  50. ',
  51. ],
  52. [
  53. '<?php
  54. $this->assertFileExists($a);
  55. $this->assertFileNotExists($a);
  56. $this->assertFileExists($a);
  57. $this->assertFileNotExists($a);
  58. ',
  59. '<?php
  60. $this->assertTrue(file_exists($a));
  61. $this->assertFalse(file_exists($a));
  62. $this->assertTrue(\file_exists($a));
  63. $this->assertFalse(\file_exists($a));
  64. ',
  65. ],
  66. [
  67. '<?php
  68. $this->assertNull($a);
  69. $this->assertNotNull($a);
  70. $this->assertNull($a);
  71. $this->assertNotNull($a, "my message");
  72. ',
  73. '<?php
  74. $this->assertTrue(is_null($a));
  75. $this->assertFalse(is_null($a));
  76. $this->assertTrue(\is_null($a));
  77. $this->assertFalse(\is_null($a), "my message");
  78. ',
  79. ],
  80. [
  81. '<?php
  82. $this->assertEmpty($a);
  83. $this->assertNotEmpty($a);
  84. ',
  85. '<?php
  86. $this->assertTrue(empty($a));
  87. $this->ASSERTFALSE(empty($a));
  88. ',
  89. ],
  90. [
  91. '<?php
  92. $this->assertInfinite($a);
  93. $this->assertFinite($a, "my message");
  94. $this->assertInfinite($a);
  95. $this->assertFinite($a, "my message");
  96. ',
  97. '<?php
  98. $this->assertTrue(is_infinite($a));
  99. $this->assertFalse(is_infinite($a), "my message");
  100. $this->assertTrue(\is_infinite($a));
  101. $this->assertFalse(\is_infinite($a), "my message");
  102. ',
  103. ],
  104. [
  105. '<?php
  106. $this->assertArrayHasKey("test", $a);
  107. $this->assertArrayNotHasKey($b, $a, $c);
  108. ',
  109. '<?php
  110. $this->assertTrue(\array_key_exists("test", $a));
  111. $this->ASSERTFALSE(array_key_exists($b, $a), $c);
  112. ',
  113. ],
  114. [
  115. '<?php
  116. $this->assertTrue(is_dir($a));
  117. $this->assertTrue(is_writable($a));
  118. $this->assertTrue(is_readable($a));
  119. ',
  120. null,
  121. ],
  122. [
  123. '<?php
  124. $this->assertTrue(is_dir($a));
  125. $this->assertTrue(is_writable($a));
  126. $this->assertTrue(is_readable($a));
  127. ',
  128. null,
  129. ['target' => PhpUnitTargetVersion::VERSION_3_0],
  130. ],
  131. [
  132. '<?php
  133. $this->assertDirectoryNotExists($a);
  134. $this->assertNotIsWritable($a);
  135. $this->assertNotIsReadable($a);
  136. ',
  137. '<?php
  138. $this->assertFalse(is_dir($a));
  139. $this->assertFalse(is_writable($a));
  140. $this->assertFalse(is_readable($a));
  141. ',
  142. ['target' => PhpUnitTargetVersion::VERSION_5_6],
  143. ],
  144. [
  145. '<?php
  146. $this->assertDirectoryExists($a);
  147. $this->assertIsWritable($a);
  148. $this->assertIsReadable($a);
  149. ',
  150. '<?php
  151. $this->assertTrue(is_dir($a));
  152. $this->assertTrue(is_writable($a));
  153. $this->assertTrue(is_readable($a));
  154. ',
  155. ['target' => PhpUnitTargetVersion::VERSION_NEWEST],
  156. ],
  157. ];
  158. foreach (['array', 'bool', 'callable', 'double', 'float', 'int', 'integer', 'long', 'numeric', 'object', 'resource', 'real', 'scalar', 'string'] as $type) {
  159. $cases[] = [
  160. sprintf('<?php $this->assertInternalType(\'%s\', $a);', $type),
  161. sprintf('<?php $this->assertTrue(is_%s($a));', $type),
  162. ];
  163. $cases[] = [
  164. sprintf('<?php $this->assertNotInternalType(\'%s\', $a);', $type),
  165. sprintf('<?php $this->assertFalse(is_%s($a));', $type),
  166. ];
  167. }
  168. $cases[] = [
  169. '<?php $this->assertInternalType(\'float\', $a, "my message");',
  170. '<?php $this->assertTrue(is_float( $a), "my message");',
  171. ];
  172. $cases[] = [
  173. '<?php $this->assertInternalType(\'float\', $a);',
  174. '<?php $this->assertTrue(\IS_FLOAT($a));',
  175. ];
  176. $cases[] = [
  177. '<?php $this->assertInternalType(#
  178. \'float\'#
  179. , #
  180. $a#
  181. #
  182. )#
  183. ;',
  184. '<?php $this->assertTrue(#
  185. \IS_FLOAT#
  186. (#
  187. $a#
  188. )#
  189. )#
  190. ;',
  191. ];
  192. return $cases;
  193. }
  194. public function provideTestFixLegacyCases()
  195. {
  196. return array_filter($this->provideTestFixCases(), function (array $case) { return !isset($case[2]); });
  197. }
  198. /**
  199. * @param string $expected
  200. *
  201. * @dataProvider provideNotFixCases
  202. */
  203. public function testNotFix($expected)
  204. {
  205. $this->doTest($expected);
  206. }
  207. public function provideNotFixCases()
  208. {
  209. return [
  210. [
  211. '<?php echo $this->assertTrue;',
  212. ],
  213. [
  214. '<?php echo $this->assertTrue?>',
  215. ],
  216. [
  217. '<?php
  218. const is_null = 1;
  219. $this->assertTrue(is_null);
  220. $this->assertTrue(is_int($a) && $b);
  221. $this->assertFalse(is_nan($a));
  222. $this->assertTrue(is_int($a) || \is_bool($b));
  223. $this->assertTrue($a&&is_int($a));
  224. ',
  225. ],
  226. ];
  227. }
  228. }