PhpUnitConstructFixerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\PhpUnit;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitConstructFixer
  21. */
  22. final class PhpUnitConstructFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @dataProvider provideFixCases
  26. */
  27. public function testFix(string $expected, ?string $input = null): void
  28. {
  29. $this->fixer->configure(['assertions' => [
  30. 'assertEquals',
  31. 'assertSame',
  32. 'assertNotEquals',
  33. 'assertNotSame',
  34. ]]);
  35. $this->doTest($expected, $input);
  36. foreach (['assertSame', 'assertEquals', 'assertNotEquals', 'assertNotSame'] as $method) {
  37. $this->fixer->configure(['assertions' => [$method]]);
  38. $this->doTest(
  39. $expected,
  40. null !== $input && str_contains($input, $method) ? $input : null
  41. );
  42. }
  43. }
  44. public static function provideFixCases(): iterable
  45. {
  46. $cases = [
  47. ['$sth->assertSame(true, $foo);'],
  48. ['$this->assertSame($b, null);'],
  49. [
  50. '$this->assertNull(/*bar*/ $a);',
  51. '$this->assertSame(null /*foo*/, /*bar*/ $a);',
  52. ],
  53. [
  54. '$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException());',
  55. ],
  56. [
  57. '$this->assertSame(null /*comment*/ === $eventException ? $exception : $eventException, $event->getException());',
  58. ],
  59. [
  60. '
  61. $this->assertTrue(
  62. $a,
  63. "foo" . $bar
  64. );',
  65. '
  66. $this->assertSame(
  67. true,
  68. $a,
  69. "foo" . $bar
  70. );',
  71. ],
  72. [
  73. '
  74. $this->assertTrue(#
  75. #
  76. $a,#
  77. "foo" . $bar#
  78. );',
  79. '
  80. $this->assertSame(#
  81. true,#
  82. $a,#
  83. "foo" . $bar#
  84. );',
  85. ],
  86. [
  87. '$this->assertSame("a", $a); $this->assertTrue($b);',
  88. '$this->assertSame("a", $a); $this->assertSame(true, $b);',
  89. ],
  90. [
  91. '$this->assertSame(true || $a, $b); $this->assertTrue($c);',
  92. '$this->assertSame(true || $a, $b); $this->assertSame(true, $c);',
  93. ],
  94. [
  95. '$this->assertFalse($foo);',
  96. '$this->assertEquals(FALSE, $foo);',
  97. ],
  98. [
  99. '$this->assertTrue($foo);',
  100. '$this->assertEquals(TruE, $foo);',
  101. ],
  102. [
  103. '$this->assertNull($foo);',
  104. '$this->assertEquals(NULL, $foo);',
  105. ],
  106. ];
  107. array_walk(
  108. $cases,
  109. static function (&$case): void {
  110. $case[0] = self::generateTest($case[0]);
  111. if (isset($case[1])) {
  112. $case[1] = self::generateTest($case[1]);
  113. }
  114. }
  115. );
  116. return array_merge(
  117. $cases,
  118. [
  119. 'not in a class' => ['<?php $this->assertEquals(NULL, $foo);'],
  120. 'not phpunit class' => ['<?php class Foo { public function testFoo(){ $this->assertEquals(NULL, $foo); }}'],
  121. 'multiple candidates in multiple classes ' => [
  122. '<?php
  123. class FooTest1 extends PHPUnit_Framework_TestCase { public function testFoo(){ $this->assertNull($foo); }}
  124. class FooTest2 extends PHPUnit_Framework_TestCase { public function testFoo(){ $this->assertNull($foo); }}
  125. class FooTest3 extends PHPUnit_Framework_TestCase { public function testFoo(){ $this->assertNull($foo); }}
  126. ',
  127. '<?php
  128. class FooTest1 extends PHPUnit_Framework_TestCase { public function testFoo(){ $this->assertEquals(NULL, $foo); }}
  129. class FooTest2 extends PHPUnit_Framework_TestCase { public function testFoo(){ $this->assertEquals(NULL, $foo); }}
  130. class FooTest3 extends PHPUnit_Framework_TestCase { public function testFoo(){ $this->assertEquals(NULL, $foo); }}
  131. ',
  132. ],
  133. ],
  134. self::generateCases('$this->assert%s%s($a); //%s %s', '$this->assert%s(%s, $a); //%s %s'),
  135. self::generateCases('$this->assert%s%s($a, "%s", "%s");', '$this->assert%s(%s, $a, "%s", "%s");'),
  136. self::generateCases('static::assert%s%s($a); //%s %s', 'static::assert%s(%s, $a); //%s %s'),
  137. self::generateCases('STATIC::assert%s%s($a); //%s %s', 'STATIC::assert%s(%s, $a); //%s %s'),
  138. self::generateCases('self::assert%s%s($a); //%s %s', 'self::assert%s(%s, $a); //%s %s')
  139. );
  140. }
  141. public function testInvalidConfig(): void
  142. {
  143. $this->expectException(InvalidFixerConfigurationException::class);
  144. $this->expectExceptionMessageMatches('/^\[php_unit_construct\] Invalid configuration: The option "assertions" .*\.$/');
  145. $this->fixer->configure(['assertions' => ['__TEST__']]);
  146. }
  147. /**
  148. * @dataProvider provideFix73Cases
  149. */
  150. public function testFix73(string $expected, string $input): void
  151. {
  152. $this->doTest($expected, $input);
  153. }
  154. public static function provideFix73Cases(): iterable
  155. {
  156. yield [
  157. self::generateTest('$this->assertTrue($a, );'),
  158. self::generateTest('$this->assertSame(true, $a, );'),
  159. ];
  160. yield [
  161. self::generateTest('$this->assertTrue($a, $message , );'),
  162. self::generateTest('$this->assertSame(true, $a, $message , );'),
  163. ];
  164. }
  165. public function testEmptyAssertions(): void
  166. {
  167. $this->fixer->configure(['assertions' => []]);
  168. $this->doTest(self::generateTest('$this->assertSame(null, $a);'));
  169. }
  170. /**
  171. * @dataProvider provideFix81Cases
  172. *
  173. * @requires PHP 8.1
  174. */
  175. public function testFix81(string $expected, ?string $input = null): void
  176. {
  177. $this->doTest($expected, $input);
  178. }
  179. public static function provideFix81Cases(): iterable
  180. {
  181. yield [
  182. self::generateTest('$this->assertEquals(...);'),
  183. ];
  184. }
  185. /**
  186. * @return list<array{string, string}>
  187. */
  188. private static function generateCases(string $expectedTemplate, string $inputTemplate): array
  189. {
  190. $functionTypes = ['Same' => true, 'NotSame' => false, 'Equals' => true, 'NotEquals' => false];
  191. $cases = [];
  192. foreach (['true', 'false', 'null'] as $type) {
  193. foreach ($functionTypes as $method => $positive) {
  194. $cases[] = [
  195. self::generateTest(sprintf($expectedTemplate, $positive ? '' : 'Not', ucfirst($type), $method, $type)),
  196. self::generateTest(sprintf($inputTemplate, $method, $type, $method, $type)),
  197. ];
  198. }
  199. }
  200. return $cases;
  201. }
  202. private static function generateTest(string $content): string
  203. {
  204. return "<?php final class FooTest extends \\PHPUnit_Framework_TestCase {\n public function testSomething() {\n ".$content."\n }\n}\n";
  205. }
  206. }