PhpUnitStrictFixerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 [self::generateTest('$this->assertEquals;')];
  42. yield [
  43. self::generateTest("\$this->{$methodAfter}(1, 2);"),
  44. self::generateTest("\$this->{$methodBefore}(1, 2);"),
  45. ];
  46. yield [
  47. self::generateTest("\$this->{$methodAfter}(1, 2); \$this->{$methodAfter}(1, 2);"),
  48. self::generateTest("\$this->{$methodBefore}(1, 2); \$this->{$methodBefore}(1, 2);"),
  49. ];
  50. yield [
  51. self::generateTest("\$this->{$methodAfter}(1, 2, 'description');"),
  52. self::generateTest("\$this->{$methodBefore}(1, 2, 'description');"),
  53. ];
  54. yield [
  55. self::generateTest("\$this->/*aaa*/{$methodAfter} \t /**bbb*/ ( /*ccc*/1 , 2);"),
  56. self::generateTest("\$this->/*aaa*/{$methodBefore} \t /**bbb*/ ( /*ccc*/1 , 2);"),
  57. ];
  58. yield [
  59. self::generateTest("\$this->{$methodAfter}(\$expectedTokens->count() + 10, \$tokens->count() ? 10 : 20 , 'Test');"),
  60. self::generateTest("\$this->{$methodBefore}(\$expectedTokens->count() + 10, \$tokens->count() ? 10 : 20 , 'Test');"),
  61. ];
  62. yield [
  63. self::generateTest("self::{$methodAfter}(1, 2);"),
  64. self::generateTest("self::{$methodBefore}(1, 2);"),
  65. ];
  66. yield [
  67. self::generateTest("static::{$methodAfter}(1, 2);"),
  68. self::generateTest("static::{$methodBefore}(1, 2);"),
  69. ];
  70. yield [
  71. self::generateTest("STATIC::{$methodAfter}(1, 2);"),
  72. self::generateTest("STATIC::{$methodBefore}(1, 2);"),
  73. ];
  74. }
  75. foreach (self::getMethodsMap() as $methodBefore => $methodAfter) {
  76. yield [
  77. self::generateTest("static::{$methodAfter}(1, 2,);"),
  78. self::generateTest("static::{$methodBefore}(1, 2,);"),
  79. ];
  80. yield [
  81. self::generateTest("self::{$methodAfter}(1, \$a, '', );"),
  82. self::generateTest("self::{$methodBefore}(1, \$a, '', );"),
  83. ];
  84. }
  85. // Only method calls with 2 or 3 arguments should be fixed.
  86. foreach (self::getMethodsMap() as $candidate => $fix) {
  87. yield sprintf('do not change call to "%s" without arguments.', $candidate) => [
  88. self::generateTest(sprintf('$this->%s();', $candidate)),
  89. ];
  90. foreach ([1, 4, 5, 10] as $argumentCount) {
  91. yield sprintf('do not change call to "%s" with #%d arguments.', $candidate, $argumentCount) => [
  92. self::generateTest(
  93. sprintf(
  94. '$this->%s(%s);',
  95. $candidate,
  96. substr(str_repeat('$a, ', $argumentCount), 0, -2)
  97. )
  98. ),
  99. ];
  100. }
  101. }
  102. }
  103. public function testInvalidConfiguration(): void
  104. {
  105. $this->expectException(InvalidFixerConfigurationException::class);
  106. $this->expectExceptionMessageMatches('/^\[php_unit_strict\] Invalid configuration: The option "assertions" .*\.$/');
  107. $this->fixer->configure(['assertions' => ['__TEST__']]);
  108. }
  109. /**
  110. * @return array<string, string>
  111. */
  112. private static function getMethodsMap(): array
  113. {
  114. return [
  115. 'assertAttributeEquals' => 'assertAttributeSame',
  116. 'assertAttributeNotEquals' => 'assertAttributeNotSame',
  117. 'assertEquals' => 'assertSame',
  118. 'assertNotEquals' => 'assertNotSame',
  119. ];
  120. }
  121. private static function generateTest(string $content): string
  122. {
  123. return "<?php final class FooTest extends \\PHPUnit_Framework_TestCase {\n public function testSomething() {\n ".$content."\n }\n}\n";
  124. }
  125. }