PhpUnitStrictFixerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\PhpUnitStrictFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitStrictFixer>
  23. *
  24. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitStrictFixer
  25. */
  26. final class PhpUnitStrictFixerTest extends AbstractFixerTestCase
  27. {
  28. /**
  29. * @dataProvider provideFixCases
  30. */
  31. public function testFix(string $expected, ?string $input = null): void
  32. {
  33. $this->doTest($expected, $input);
  34. $this->fixer->configure(['assertions' => array_keys(self::getMethodsMap())]); // @phpstan-ignore-line
  35. $this->doTest($expected, $input);
  36. }
  37. /**
  38. * @return iterable<int|string, array{0: string, 1?: string}>
  39. */
  40. public static function provideFixCases(): iterable
  41. {
  42. yield ['<?php $self->foo();'];
  43. yield [self::generateTest('$self->foo();')];
  44. yield [self::generateTest('$this->assertEquals;')];
  45. foreach (self::getMethodsMap() as $methodBefore => $methodAfter) {
  46. yield [self::generateTest("\$sth->{$methodBefore}(1, 1);")];
  47. yield [self::generateTest("\$sth->{$methodAfter}(1, 1);")];
  48. yield [self::generateTest("\$this->{$methodBefore}(1, 2, 'message', \$toMuch);")];
  49. yield [
  50. self::generateTest("\$this->{$methodAfter}(1, 2);"),
  51. self::generateTest("\$this->{$methodBefore}(1, 2);"),
  52. ];
  53. yield [
  54. self::generateTest("\$this->{$methodAfter}(1, 2); \$this->{$methodAfter}(1, 2);"),
  55. self::generateTest("\$this->{$methodBefore}(1, 2); \$this->{$methodBefore}(1, 2);"),
  56. ];
  57. yield [
  58. self::generateTest("\$this->{$methodAfter}(1, 2, 'description');"),
  59. self::generateTest("\$this->{$methodBefore}(1, 2, 'description');"),
  60. ];
  61. yield [
  62. self::generateTest("\$this->/*aaa*/{$methodAfter} \t /**bbb*/ ( /*ccc*/1 , 2);"),
  63. self::generateTest("\$this->/*aaa*/{$methodBefore} \t /**bbb*/ ( /*ccc*/1 , 2);"),
  64. ];
  65. yield [
  66. self::generateTest("\$this->{$methodAfter}(\$expectedTokens->count() + 10, \$tokens->count() ? 10 : 20 , 'Test');"),
  67. self::generateTest("\$this->{$methodBefore}(\$expectedTokens->count() + 10, \$tokens->count() ? 10 : 20 , 'Test');"),
  68. ];
  69. yield [
  70. self::generateTest("self::{$methodAfter}(1, 2);"),
  71. self::generateTest("self::{$methodBefore}(1, 2);"),
  72. ];
  73. yield [
  74. self::generateTest("static::{$methodAfter}(1, 2);"),
  75. self::generateTest("static::{$methodBefore}(1, 2);"),
  76. ];
  77. yield [
  78. self::generateTest("STATIC::{$methodAfter}(1, 2);"),
  79. self::generateTest("STATIC::{$methodBefore}(1, 2);"),
  80. ];
  81. }
  82. foreach (self::getMethodsMap() as $methodBefore => $methodAfter) {
  83. yield [
  84. self::generateTest("static::{$methodAfter}(1, 2,);"),
  85. self::generateTest("static::{$methodBefore}(1, 2,);"),
  86. ];
  87. yield [
  88. self::generateTest("self::{$methodAfter}(1, \$a, '', );"),
  89. self::generateTest("self::{$methodBefore}(1, \$a, '', );"),
  90. ];
  91. }
  92. // Only method calls with 2 or 3 arguments should be fixed.
  93. foreach (self::getMethodsMap() as $candidate => $fix) {
  94. yield \sprintf('do not change call to "%s" without arguments.', $candidate) => [
  95. self::generateTest(\sprintf('$this->%s();', $candidate)),
  96. ];
  97. foreach ([1, 4, 5, 10] as $argumentCount) {
  98. yield \sprintf('do not change call to "%s" with #%d arguments.', $candidate, $argumentCount) => [
  99. self::generateTest(
  100. \sprintf(
  101. '$this->%s(%s);',
  102. $candidate,
  103. substr(str_repeat('$a, ', $argumentCount), 0, -2)
  104. )
  105. ),
  106. ];
  107. }
  108. }
  109. }
  110. public function testInvalidConfiguration(): void
  111. {
  112. $this->expectException(InvalidFixerConfigurationException::class);
  113. $this->expectExceptionMessageMatches('/^\[php_unit_strict\] Invalid configuration: The option "assertions" .*\.$/');
  114. $this->fixer->configure(['assertions' => ['__TEST__']]); // @phpstan-ignore-line
  115. }
  116. /**
  117. * @return array<string, string>
  118. */
  119. private static function getMethodsMap(): array
  120. {
  121. return [
  122. 'assertAttributeEquals' => 'assertAttributeSame',
  123. 'assertAttributeNotEquals' => 'assertAttributeNotSame',
  124. 'assertEquals' => 'assertSame',
  125. 'assertNotEquals' => 'assertNotSame',
  126. ];
  127. }
  128. private static function generateTest(string $content): string
  129. {
  130. return "<?php final class FooTest extends \\PHPUnit_Framework_TestCase {\n public function testSomething() {\n ".$content."\n }\n}\n";
  131. }
  132. }