PhpUnitStrictFixerTest.php 5.6 KB

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