PhpUnitTestCaseStaticMethodCallsFixerTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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\Fixer\PhpUnit\PhpUnitTestCaseStaticMethodCallsFixer;
  15. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  16. use PHPUnit\Framework\TestCase;
  17. /**
  18. * @author Filippo Tessarotto <zoeslam@gmail.com>
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitTestCaseStaticMethodCallsFixer
  23. */
  24. final class PhpUnitTestCaseStaticMethodCallsFixerTest extends AbstractFixerTestCase
  25. {
  26. public function testFixerContainsAllPhpunitStaticMethodsInItsList(): void
  27. {
  28. $assertionRefClass = new \ReflectionClass(TestCase::class);
  29. $updatedStaticMethodsList = $assertionRefClass->getMethods(\ReflectionMethod::IS_PUBLIC);
  30. $fixerRefClass = new \ReflectionClass(PhpUnitTestCaseStaticMethodCallsFixer::class);
  31. $defaultProperties = $fixerRefClass->getDefaultProperties();
  32. $staticMethods = $defaultProperties['staticMethods'];
  33. $missingMethods = [];
  34. foreach ($updatedStaticMethodsList as $method) {
  35. if ($method->isStatic() && !isset($staticMethods[$method->name])) {
  36. $missingMethods[] = $method->name;
  37. }
  38. }
  39. self::assertSame([], $missingMethods, sprintf('The following static methods from "%s" are missing from "%s::$staticMethods"', TestCase::class, PhpUnitTestCaseStaticMethodCallsFixer::class));
  40. }
  41. public function testWrongConfigTypeForMethodsKey(): void
  42. {
  43. $this->expectException(InvalidFixerConfigurationException::class);
  44. $this->expectExceptionMessageMatches('/Unexpected "methods" key, expected any of ".*", got "integer#123"\.$/');
  45. $this->fixer->configure(['methods' => [123 => 1]]);
  46. }
  47. public function testWrongConfigTypeForMethodsValue(): void
  48. {
  49. $this->expectException(InvalidFixerConfigurationException::class);
  50. $this->expectExceptionMessageMatches('/Unexpected value for method "assertSame", expected any of ".*", got "integer#123"\.$/');
  51. $this->fixer->configure(['methods' => ['assertSame' => 123]]);
  52. }
  53. /**
  54. * @param array<string, mixed> $config
  55. *
  56. * @dataProvider provideFixCases
  57. */
  58. public function testFix(string $expected, ?string $input = null, array $config = []): void
  59. {
  60. $this->fixer->configure($config);
  61. $this->doTest($expected, $input);
  62. }
  63. public static function provideFixCases(): iterable
  64. {
  65. yield [
  66. <<<'EOF'
  67. <?php
  68. class MyTest extends \PHPUnit_Framework_TestCase
  69. {
  70. public function testBaseCase()
  71. {
  72. static::assertSame(1, 2);
  73. static::markTestIncomplete('foo');
  74. static::fail('foo');
  75. }
  76. }
  77. EOF
  78. ,
  79. <<<'EOF'
  80. <?php
  81. class MyTest extends \PHPUnit_Framework_TestCase
  82. {
  83. public function testBaseCase()
  84. {
  85. $this->assertSame(1, 2);
  86. $this->markTestIncomplete('foo');
  87. $this->fail('foo');
  88. }
  89. }
  90. EOF
  91. ,
  92. ];
  93. yield [
  94. <<<'EOF'
  95. <?php
  96. class MyTest extends \PHPUnit_Framework_TestCase
  97. {
  98. public function testMocks()
  99. {
  100. $mock = $this->createMock(MyInterface::class);
  101. $mock
  102. ->expects(static::once())
  103. ->method('run')
  104. ->with(
  105. static::identicalTo(1),
  106. static::stringContains('foo')
  107. )
  108. ->will(static::onConsecutiveCalls(
  109. static::returnSelf(),
  110. static::throwException(new \Exception())
  111. ))
  112. ;
  113. }
  114. }
  115. EOF
  116. ,
  117. <<<'EOF'
  118. <?php
  119. class MyTest extends \PHPUnit_Framework_TestCase
  120. {
  121. public function testMocks()
  122. {
  123. $mock = $this->createMock(MyInterface::class);
  124. $mock
  125. ->expects($this->once())
  126. ->method('run')
  127. ->with(
  128. $this->identicalTo(1),
  129. $this->stringContains('foo')
  130. )
  131. ->will($this->onConsecutiveCalls(
  132. $this->returnSelf(),
  133. $this->throwException(new \Exception())
  134. ))
  135. ;
  136. }
  137. }
  138. EOF
  139. ,
  140. ];
  141. yield [
  142. <<<'EOF'
  143. <?php
  144. class MyTest extends \PHPUnit_Framework_TestCase
  145. {
  146. public function testWeirdIndentation()
  147. {
  148. static
  149. // @TODO
  150. ::
  151. assertSame
  152. (1, 2);
  153. // $this->markTestIncomplete('foo');
  154. /*
  155. $this->fail('foo');
  156. */
  157. }
  158. }
  159. EOF
  160. ,
  161. <<<'EOF'
  162. <?php
  163. class MyTest extends \PHPUnit_Framework_TestCase
  164. {
  165. public function testWeirdIndentation()
  166. {
  167. $this
  168. // @TODO
  169. ->
  170. assertSame
  171. (1, 2);
  172. // $this->markTestIncomplete('foo');
  173. /*
  174. $this->fail('foo');
  175. */
  176. }
  177. }
  178. EOF
  179. ,
  180. ];
  181. yield [
  182. <<<'EOF'
  183. <?php
  184. class MyTest extends \PHPUnit_Framework_TestCase
  185. {
  186. public function testBaseCase()
  187. {
  188. $this->assertSame(1, 2);
  189. $this->markTestIncomplete('foo');
  190. $this->fail('foo');
  191. $lambda = function () {
  192. $this->assertSame(1, 23);
  193. self::assertSame(1, 23);
  194. static::assertSame(1, 23);
  195. };
  196. }
  197. }
  198. EOF
  199. ,
  200. <<<'EOF'
  201. <?php
  202. class MyTest extends \PHPUnit_Framework_TestCase
  203. {
  204. public function testBaseCase()
  205. {
  206. $this->assertSame(1, 2);
  207. self::markTestIncomplete('foo');
  208. static::fail('foo');
  209. $lambda = function () {
  210. $this->assertSame(1, 23);
  211. self::assertSame(1, 23);
  212. static::assertSame(1, 23);
  213. };
  214. }
  215. }
  216. EOF
  217. ,
  218. ['call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS],
  219. ];
  220. yield [
  221. <<<'EOF'
  222. <?php
  223. class MyTest extends \PHPUnit_Framework_TestCase
  224. {
  225. public function testBaseCase()
  226. {
  227. self::assertSame(1, 2);
  228. self::markTestIncomplete('foo');
  229. self::fail('foo');
  230. }
  231. }
  232. EOF
  233. ,
  234. <<<'EOF'
  235. <?php
  236. class MyTest extends \PHPUnit_Framework_TestCase
  237. {
  238. public function testBaseCase()
  239. {
  240. $this->assertSame(1, 2);
  241. self::markTestIncomplete('foo');
  242. static::fail('foo');
  243. }
  244. }
  245. EOF
  246. ,
  247. ['call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_SELF],
  248. ];
  249. yield [
  250. <<<'EOF'
  251. <?php
  252. class MyTest extends \PHPUnit_Framework_TestCase
  253. {
  254. public function testBaseCase()
  255. {
  256. $this->assertSame(1, 2);
  257. $this->assertSame(1, 2);
  258. static::setUpBeforeClass();
  259. static::setUpBeforeClass();
  260. $otherTest->setUpBeforeClass();
  261. OtherTest::setUpBeforeClass();
  262. }
  263. }
  264. EOF
  265. ,
  266. <<<'EOF'
  267. <?php
  268. class MyTest extends \PHPUnit_Framework_TestCase
  269. {
  270. public function testBaseCase()
  271. {
  272. static::assertSame(1, 2);
  273. $this->assertSame(1, 2);
  274. static::setUpBeforeClass();
  275. $this->setUpBeforeClass();
  276. $otherTest->setUpBeforeClass();
  277. OtherTest::setUpBeforeClass();
  278. }
  279. }
  280. EOF
  281. ,
  282. [
  283. 'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
  284. 'methods' => ['setUpBeforeClass' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_STATIC],
  285. ],
  286. ];
  287. yield [
  288. <<<'EOF'
  289. <?php
  290. class MyTest extends \PHPUnit_Framework_TestCase
  291. {
  292. public static function foo()
  293. {
  294. $this->assertSame(1, 2);
  295. self::assertSame(1, 2);
  296. static::assertSame(1, 2);
  297. $lambda = function () {
  298. $this->assertSame(1, 2);
  299. self::assertSame(1, 2);
  300. static::assertSame(1, 2);
  301. };
  302. }
  303. public function bar()
  304. {
  305. $lambda = static function () {
  306. $this->assertSame(1, 2);
  307. self::assertSame(1, 2);
  308. static::assertSame(1, 2);
  309. };
  310. $myProphecy->setCount(0)->will(function () {
  311. $this->getCount()->willReturn(0);
  312. });
  313. }
  314. static public function baz()
  315. {
  316. $this->assertSame(1, 2);
  317. self::assertSame(1, 2);
  318. static::assertSame(1, 2);
  319. $lambda = function () {
  320. $this->assertSame(1, 2);
  321. self::assertSame(1, 2);
  322. static::assertSame(1, 2);
  323. };
  324. }
  325. static final protected function xyz()
  326. {
  327. static::assertSame(1, 2);
  328. }
  329. }
  330. EOF
  331. ,
  332. null,
  333. [
  334. 'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
  335. ],
  336. ];
  337. yield [
  338. <<<'EOF'
  339. <?php
  340. class MyTest extends \PHPUnit_Framework_TestCase
  341. {
  342. public function foo()
  343. {
  344. $this->assertSame(1, 2);
  345. $this->assertSame(1, 2);
  346. $this->assertSame(1, 2);
  347. }
  348. public function bar()
  349. {
  350. $lambdaOne = static function () {
  351. $this->assertSame(1, 21);
  352. self::assertSame(1, 21);
  353. static::assertSame(1, 21);
  354. };
  355. $lambdaTwo = function () {
  356. $this->assertSame(1, 21);
  357. self::assertSame(1, 21);
  358. static::assertSame(1, 21);
  359. };
  360. }
  361. public function baz2()
  362. {
  363. $this->assertSame(1, 22);
  364. $this->assertSame(1, 22);
  365. $this->assertSame(1, 22);
  366. $this->assertSame(1, 23);
  367. }
  368. }
  369. EOF
  370. ,
  371. <<<'EOF'
  372. <?php
  373. class MyTest extends \PHPUnit_Framework_TestCase
  374. {
  375. public function foo()
  376. {
  377. $this->assertSame(1, 2);
  378. self::assertSame(1, 2);
  379. static::assertSame(1, 2);
  380. }
  381. public function bar()
  382. {
  383. $lambdaOne = static function () {
  384. $this->assertSame(1, 21);
  385. self::assertSame(1, 21);
  386. static::assertSame(1, 21);
  387. };
  388. $lambdaTwo = function () {
  389. $this->assertSame(1, 21);
  390. self::assertSame(1, 21);
  391. static::assertSame(1, 21);
  392. };
  393. }
  394. public function baz2()
  395. {
  396. $this->assertSame(1, 22);
  397. self::assertSame(1, 22);
  398. static::assertSame(1, 22);
  399. STATIC::assertSame(1, 23);
  400. }
  401. }
  402. EOF
  403. ,
  404. [
  405. 'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
  406. ],
  407. ];
  408. yield 'do not change class property and method signature' => [
  409. <<<'EOF'
  410. <?php
  411. class FooTest extends TestCase
  412. {
  413. public function foo()
  414. {
  415. $this->assertSame = 42;
  416. }
  417. public function assertSame($foo, $bar){}
  418. }
  419. EOF
  420. ,
  421. ];
  422. yield 'do not change when only case is different' => [
  423. <<<'EOF'
  424. <?php
  425. class FooTest extends TestCase
  426. {
  427. public function foo()
  428. {
  429. STATIC::assertSame(1, 1);
  430. }
  431. }
  432. EOF
  433. ,
  434. ];
  435. yield 'do not crash on abstract static function' => [
  436. <<<'EOF'
  437. <?php
  438. abstract class FooTest extends TestCase
  439. {
  440. abstract public static function dataProvider();
  441. }
  442. EOF
  443. ,
  444. null,
  445. [
  446. 'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
  447. ],
  448. ];
  449. yield 'handle $this with double colon following' => [
  450. '<?php
  451. class FooTest extends TestCase
  452. {
  453. public function testFoo()
  454. {
  455. static::assertTrue(true);
  456. }
  457. }',
  458. '<?php
  459. class FooTest extends TestCase
  460. {
  461. public function testFoo()
  462. {
  463. $this::assertTrue(true);
  464. }
  465. }',
  466. ];
  467. }
  468. public function testAnonymousClassFixing(): void
  469. {
  470. $this->doTest(
  471. '<?php
  472. class MyTest extends \PHPUnit_Framework_TestCase
  473. {
  474. public function testBaseCase()
  475. {
  476. static::assertSame(1, 2);
  477. $foo = new class() {
  478. public function assertSame($a, $b)
  479. {
  480. $this->assertSame(1, 2);
  481. }
  482. };
  483. }
  484. }',
  485. '<?php
  486. class MyTest extends \PHPUnit_Framework_TestCase
  487. {
  488. public function testBaseCase()
  489. {
  490. $this->assertSame(1, 2);
  491. $foo = new class() {
  492. public function assertSame($a, $b)
  493. {
  494. $this->assertSame(1, 2);
  495. }
  496. };
  497. }
  498. }'
  499. );
  500. }
  501. /**
  502. * @dataProvider provideFix81Cases
  503. *
  504. * @requires PHP 8.1
  505. */
  506. public function testFix81(string $expected, ?string $input = null): void
  507. {
  508. $this->doTest($expected, $input);
  509. }
  510. public static function provideFix81Cases(): iterable
  511. {
  512. yield [
  513. '<?php
  514. class FooTest extends TestCase
  515. {
  516. public function testFoo()
  517. {
  518. $a = $this::assertTrue(...);
  519. }
  520. }
  521. ',
  522. ];
  523. }
  524. }