PhpUnitConstructFixerTest.php 8.1 KB

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