PhpUnitConstructFixerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 provideTestFixCases
  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 function provideTestFixCases(): array
  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] = static::generateTest($case[0]);
  111. if (isset($case[1])) {
  112. $case[1] = static::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. $this->generateCases('$this->assert%s%s($a); //%s %s', '$this->assert%s(%s, $a); //%s %s'),
  135. $this->generateCases('$this->assert%s%s($a, "%s", "%s");', '$this->assert%s(%s, $a, "%s", "%s");'),
  136. $this->generateCases('static::assert%s%s($a); //%s %s', 'static::assert%s(%s, $a); //%s %s'),
  137. $this->generateCases('STATIC::assert%s%s($a); //%s %s', 'STATIC::assert%s(%s, $a); //%s %s'),
  138. $this->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. * @requires PHP 7.3
  149. * @dataProvider provideFix73Cases
  150. */
  151. public function testFix73(string $expected, string $input): void
  152. {
  153. $this->doTest($expected, $input);
  154. }
  155. public function provideFix73Cases(): array
  156. {
  157. return [
  158. [
  159. static::generateTest('$this->assertTrue($a, );'),
  160. static::generateTest('$this->assertSame(true, $a, );'),
  161. ],
  162. [
  163. static::generateTest('$this->assertTrue($a, $message , );'),
  164. static::generateTest('$this->assertSame(true, $a, $message , );'),
  165. ],
  166. ];
  167. }
  168. public function testEmptyAssertions(): void
  169. {
  170. $this->fixer->configure(['assertions' => []]);
  171. $this->doTest(self::generateTest('$this->assertSame(null, $a);'));
  172. }
  173. /**
  174. * @dataProvider provideFix81Cases
  175. * @requires PHP 8.1
  176. */
  177. public function testFix81(string $expected, ?string $input = null): void
  178. {
  179. $this->doTest($expected, $input);
  180. }
  181. public function provideFix81Cases(): \Generator
  182. {
  183. yield [
  184. self::generateTest('$this->assertEquals(...);'),
  185. ];
  186. }
  187. private function generateCases(string $expectedTemplate, string $inputTemplate): array
  188. {
  189. $functionTypes = ['Same' => true, 'NotSame' => false, 'Equals' => true, 'NotEquals' => false];
  190. $cases = [];
  191. foreach (['true', 'false', 'null'] as $type) {
  192. foreach ($functionTypes as $method => $positive) {
  193. $cases[] = [
  194. self::generateTest(sprintf($expectedTemplate, $positive ? '' : 'Not', ucfirst($type), $method, $type)),
  195. self::generateTest(sprintf($inputTemplate, $method, $type, $method, $type)),
  196. ];
  197. }
  198. }
  199. return $cases;
  200. }
  201. private static function generateTest(string $content): string
  202. {
  203. return "<?php final class FooTest extends \\PHPUnit_Framework_TestCase {\n public function testSomething() {\n ".$content."\n }\n}\n";
  204. }
  205. }