PhpUnitTestCaseStaticMethodCallsFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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\Fixer\PhpUnit\PhpUnitTestCaseStaticMethodCallsFixer;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PHPUnit\Framework\TestCase;
  15. /**
  16. * @author Filippo Tessarotto <zoeslam@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitTestCaseStaticMethodCallsFixer
  21. */
  22. final class PhpUnitTestCaseStaticMethodCallsFixerTest extends AbstractFixerTestCase
  23. {
  24. public function testFixerContainsAllPhpunitStaticMethodsInItsList()
  25. {
  26. $assertionRefClass = new \ReflectionClass(TestCase::class);
  27. $updatedStaticMethodsList = $assertionRefClass->getMethods(\ReflectionMethod::IS_PUBLIC);
  28. $fixerRefClass = new \ReflectionClass(PhpUnitTestCaseStaticMethodCallsFixer::class);
  29. $defaultProperties = $fixerRefClass->getDefaultProperties();
  30. $staticMethods = $defaultProperties['staticMethods'];
  31. $missingMethods = [];
  32. foreach ($updatedStaticMethodsList as $method) {
  33. if ($method->isStatic() && !isset($staticMethods[$method->name])) {
  34. $missingMethods[] = $method->name;
  35. }
  36. }
  37. static::assertSame([], $missingMethods, sprintf('The following static methods from "%s" are missing from "%s::$staticMethods"', TestCase::class, PhpUnitTestCaseStaticMethodCallsFixer::class));
  38. }
  39. public function testWrongConfigTypeForMethodsKey()
  40. {
  41. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  42. $this->expectExceptionMessageMatches('/Unexpected "methods" key, expected any of ".*", got "integer#123"\.$/');
  43. $this->fixer->configure(['methods' => [123 => 1]]);
  44. }
  45. public function testWrongConfigTypeForMethodsValue()
  46. {
  47. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  48. $this->expectExceptionMessageMatches('/Unexpected value for method "assertSame", expected any of ".*", got "integer#123"\.$/');
  49. $this->fixer->configure(['methods' => ['assertSame' => 123]]);
  50. }
  51. /**
  52. * @param string $expected
  53. * @param null|string $input
  54. *
  55. * @dataProvider provideTestFixCases
  56. */
  57. public function testFix($expected, $input = null, array $config = [])
  58. {
  59. $this->fixer->configure($config);
  60. $this->doTest($expected, $input);
  61. }
  62. public function provideTestFixCases()
  63. {
  64. return [
  65. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. [
  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. '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. '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. ];
  436. }
  437. /**
  438. * @requires PHP 7.0
  439. */
  440. public function testAnonymousClassFixing()
  441. {
  442. $this->doTest(
  443. '<?php
  444. class MyTest extends \PHPUnit_Framework_TestCase
  445. {
  446. public function testBaseCase()
  447. {
  448. static::assertSame(1, 2);
  449. $foo = new class() {
  450. public function assertSame($a, $b)
  451. {
  452. $this->assertSame(1, 2);
  453. }
  454. };
  455. }
  456. }',
  457. '<?php
  458. class MyTest extends \PHPUnit_Framework_TestCase
  459. {
  460. public function testBaseCase()
  461. {
  462. $this->assertSame(1, 2);
  463. $foo = new class() {
  464. public function assertSame($a, $b)
  465. {
  466. $this->assertSame(1, 2);
  467. }
  468. };
  469. }
  470. }'
  471. );
  472. }
  473. }