PhpUnitTestCaseStaticMethodCallsFixerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. static::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. * @dataProvider provideTestFixCases
  55. */
  56. public function testFix(string $expected, ?string $input = null, array $config = []): void
  57. {
  58. $this->fixer->configure($config);
  59. $this->doTest($expected, $input);
  60. }
  61. public function provideTestFixCases(): array
  62. {
  63. return [
  64. [
  65. <<<'EOF'
  66. <?php
  67. class MyTest extends \PHPUnit_Framework_TestCase
  68. {
  69. public function testBaseCase()
  70. {
  71. static::assertSame(1, 2);
  72. static::markTestIncomplete('foo');
  73. static::fail('foo');
  74. }
  75. }
  76. EOF
  77. ,
  78. <<<'EOF'
  79. <?php
  80. class MyTest extends \PHPUnit_Framework_TestCase
  81. {
  82. public function testBaseCase()
  83. {
  84. $this->assertSame(1, 2);
  85. $this->markTestIncomplete('foo');
  86. $this->fail('foo');
  87. }
  88. }
  89. EOF
  90. ,
  91. ],
  92. [
  93. <<<'EOF'
  94. <?php
  95. class MyTest extends \PHPUnit_Framework_TestCase
  96. {
  97. public function testMocks()
  98. {
  99. $mock = $this->createMock(MyInterface::class);
  100. $mock
  101. ->expects(static::once())
  102. ->method('run')
  103. ->with(
  104. static::identicalTo(1),
  105. static::stringContains('foo')
  106. )
  107. ->will(static::onConsecutiveCalls(
  108. static::returnSelf(),
  109. static::throwException(new \Exception())
  110. ))
  111. ;
  112. }
  113. }
  114. EOF
  115. ,
  116. <<<'EOF'
  117. <?php
  118. class MyTest extends \PHPUnit_Framework_TestCase
  119. {
  120. public function testMocks()
  121. {
  122. $mock = $this->createMock(MyInterface::class);
  123. $mock
  124. ->expects($this->once())
  125. ->method('run')
  126. ->with(
  127. $this->identicalTo(1),
  128. $this->stringContains('foo')
  129. )
  130. ->will($this->onConsecutiveCalls(
  131. $this->returnSelf(),
  132. $this->throwException(new \Exception())
  133. ))
  134. ;
  135. }
  136. }
  137. EOF
  138. ,
  139. ],
  140. [
  141. <<<'EOF'
  142. <?php
  143. class MyTest extends \PHPUnit_Framework_TestCase
  144. {
  145. public function testWeirdIndentation()
  146. {
  147. static
  148. // @TODO
  149. ::
  150. assertSame
  151. (1, 2);
  152. // $this->markTestIncomplete('foo');
  153. /*
  154. $this->fail('foo');
  155. */
  156. }
  157. }
  158. EOF
  159. ,
  160. <<<'EOF'
  161. <?php
  162. class MyTest extends \PHPUnit_Framework_TestCase
  163. {
  164. public function testWeirdIndentation()
  165. {
  166. $this
  167. // @TODO
  168. ->
  169. assertSame
  170. (1, 2);
  171. // $this->markTestIncomplete('foo');
  172. /*
  173. $this->fail('foo');
  174. */
  175. }
  176. }
  177. EOF
  178. ,
  179. ],
  180. [
  181. <<<'EOF'
  182. <?php
  183. class MyTest extends \PHPUnit_Framework_TestCase
  184. {
  185. public function testBaseCase()
  186. {
  187. $this->assertSame(1, 2);
  188. $this->markTestIncomplete('foo');
  189. $this->fail('foo');
  190. $lambda = function () {
  191. $this->assertSame(1, 23);
  192. self::assertSame(1, 23);
  193. static::assertSame(1, 23);
  194. };
  195. }
  196. }
  197. EOF
  198. ,
  199. <<<'EOF'
  200. <?php
  201. class MyTest extends \PHPUnit_Framework_TestCase
  202. {
  203. public function testBaseCase()
  204. {
  205. $this->assertSame(1, 2);
  206. self::markTestIncomplete('foo');
  207. static::fail('foo');
  208. $lambda = function () {
  209. $this->assertSame(1, 23);
  210. self::assertSame(1, 23);
  211. static::assertSame(1, 23);
  212. };
  213. }
  214. }
  215. EOF
  216. ,
  217. ['call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS],
  218. ],
  219. [
  220. <<<'EOF'
  221. <?php
  222. class MyTest extends \PHPUnit_Framework_TestCase
  223. {
  224. public function testBaseCase()
  225. {
  226. self::assertSame(1, 2);
  227. self::markTestIncomplete('foo');
  228. self::fail('foo');
  229. }
  230. }
  231. EOF
  232. ,
  233. <<<'EOF'
  234. <?php
  235. class MyTest extends \PHPUnit_Framework_TestCase
  236. {
  237. public function testBaseCase()
  238. {
  239. $this->assertSame(1, 2);
  240. self::markTestIncomplete('foo');
  241. static::fail('foo');
  242. }
  243. }
  244. EOF
  245. ,
  246. ['call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_SELF],
  247. ],
  248. [
  249. <<<'EOF'
  250. <?php
  251. class MyTest extends \PHPUnit_Framework_TestCase
  252. {
  253. public function testBaseCase()
  254. {
  255. $this->assertSame(1, 2);
  256. $this->assertSame(1, 2);
  257. static::setUpBeforeClass();
  258. static::setUpBeforeClass();
  259. $otherTest->setUpBeforeClass();
  260. OtherTest::setUpBeforeClass();
  261. }
  262. }
  263. EOF
  264. ,
  265. <<<'EOF'
  266. <?php
  267. class MyTest extends \PHPUnit_Framework_TestCase
  268. {
  269. public function testBaseCase()
  270. {
  271. static::assertSame(1, 2);
  272. $this->assertSame(1, 2);
  273. static::setUpBeforeClass();
  274. $this->setUpBeforeClass();
  275. $otherTest->setUpBeforeClass();
  276. OtherTest::setUpBeforeClass();
  277. }
  278. }
  279. EOF
  280. ,
  281. [
  282. 'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
  283. 'methods' => ['setUpBeforeClass' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_STATIC],
  284. ],
  285. ],
  286. [
  287. <<<'EOF'
  288. <?php
  289. class MyTest extends \PHPUnit_Framework_TestCase
  290. {
  291. public static function foo()
  292. {
  293. $this->assertSame(1, 2);
  294. self::assertSame(1, 2);
  295. static::assertSame(1, 2);
  296. $lambda = function () {
  297. $this->assertSame(1, 2);
  298. self::assertSame(1, 2);
  299. static::assertSame(1, 2);
  300. };
  301. }
  302. public function bar()
  303. {
  304. $lambda = static function () {
  305. $this->assertSame(1, 2);
  306. self::assertSame(1, 2);
  307. static::assertSame(1, 2);
  308. };
  309. $myProphecy->setCount(0)->will(function () {
  310. $this->getCount()->willReturn(0);
  311. });
  312. }
  313. static public function baz()
  314. {
  315. $this->assertSame(1, 2);
  316. self::assertSame(1, 2);
  317. static::assertSame(1, 2);
  318. $lambda = function () {
  319. $this->assertSame(1, 2);
  320. self::assertSame(1, 2);
  321. static::assertSame(1, 2);
  322. };
  323. }
  324. static final protected function xyz()
  325. {
  326. static::assertSame(1, 2);
  327. }
  328. }
  329. EOF
  330. ,
  331. null,
  332. [
  333. 'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
  334. ],
  335. ],
  336. [
  337. <<<'EOF'
  338. <?php
  339. class MyTest extends \PHPUnit_Framework_TestCase
  340. {
  341. public function foo()
  342. {
  343. $this->assertSame(1, 2);
  344. $this->assertSame(1, 2);
  345. $this->assertSame(1, 2);
  346. }
  347. public function bar()
  348. {
  349. $lambdaOne = static function () {
  350. $this->assertSame(1, 21);
  351. self::assertSame(1, 21);
  352. static::assertSame(1, 21);
  353. };
  354. $lambdaTwo = function () {
  355. $this->assertSame(1, 21);
  356. self::assertSame(1, 21);
  357. static::assertSame(1, 21);
  358. };
  359. }
  360. public function baz2()
  361. {
  362. $this->assertSame(1, 22);
  363. $this->assertSame(1, 22);
  364. $this->assertSame(1, 22);
  365. $this->assertSame(1, 23);
  366. }
  367. }
  368. EOF
  369. ,
  370. <<<'EOF'
  371. <?php
  372. class MyTest extends \PHPUnit_Framework_TestCase
  373. {
  374. public function foo()
  375. {
  376. $this->assertSame(1, 2);
  377. self::assertSame(1, 2);
  378. static::assertSame(1, 2);
  379. }
  380. public function bar()
  381. {
  382. $lambdaOne = static function () {
  383. $this->assertSame(1, 21);
  384. self::assertSame(1, 21);
  385. static::assertSame(1, 21);
  386. };
  387. $lambdaTwo = function () {
  388. $this->assertSame(1, 21);
  389. self::assertSame(1, 21);
  390. static::assertSame(1, 21);
  391. };
  392. }
  393. public function baz2()
  394. {
  395. $this->assertSame(1, 22);
  396. self::assertSame(1, 22);
  397. static::assertSame(1, 22);
  398. STATIC::assertSame(1, 23);
  399. }
  400. }
  401. EOF
  402. ,
  403. [
  404. 'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
  405. ],
  406. ],
  407. 'do not change class property and method signature' => [
  408. <<<'EOF'
  409. <?php
  410. class FooTest extends TestCase
  411. {
  412. public function foo()
  413. {
  414. $this->assertSame = 42;
  415. }
  416. public function assertSame($foo, $bar){}
  417. }
  418. EOF
  419. ,
  420. ],
  421. 'do not change when only case is different' => [
  422. <<<'EOF'
  423. <?php
  424. class FooTest extends TestCase
  425. {
  426. public function foo()
  427. {
  428. STATIC::assertSame(1, 1);
  429. }
  430. }
  431. EOF
  432. ,
  433. ],
  434. 'do not crash on abstract static function' => [
  435. <<<'EOF'
  436. <?php
  437. abstract class FooTest extends TestCase
  438. {
  439. abstract public static function dataProvider();
  440. }
  441. EOF
  442. ,
  443. null,
  444. [
  445. 'call_type' => PhpUnitTestCaseStaticMethodCallsFixer::CALL_TYPE_THIS,
  446. ],
  447. ],
  448. 'handle $this with double colon following' => [
  449. '<?php
  450. class FooTest extends TestCase
  451. {
  452. public function testFoo()
  453. {
  454. static::assertTrue(true);
  455. }
  456. }',
  457. '<?php
  458. class FooTest extends TestCase
  459. {
  460. public function testFoo()
  461. {
  462. $this::assertTrue(true);
  463. }
  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. * @requires PHP 8.1
  504. */
  505. public function testFix81(string $expected, ?string $input = null): void
  506. {
  507. $this->doTest($expected, $input);
  508. }
  509. public function provideFix81Cases(): \Generator
  510. {
  511. yield [
  512. '<?php
  513. class FooTest extends TestCase
  514. {
  515. public function testFoo()
  516. {
  517. $a = $this::assertTrue(...);
  518. }
  519. }
  520. ',
  521. ];
  522. }
  523. }