Skip to content

Commit

Permalink
Only fix in utest class scope
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum committed Oct 20, 2020
1 parent 7ca31dd commit bdc0d83
Show file tree
Hide file tree
Showing 11 changed files with 367 additions and 267 deletions.
43 changes: 28 additions & 15 deletions src/Fixer/PhpUnit/PhpUnitConstructFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
Expand All @@ -40,7 +41,7 @@ final class PhpUnitConstructFixer extends AbstractFixer implements Configuration
*/
public function isCandidate(Tokens $tokens)
{
return $tokens->isTokenKindFound(T_STRING);
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]);
}

/**
Expand All @@ -61,18 +62,26 @@ public function getDefinition()
[
new CodeSample(
'<?php
$this->assertEquals(false, $b);
$this->assertSame(true, $a);
$this->assertNotEquals(null, $c);
$this->assertNotSame(null, $d);
final class FooTest extends \PHPUnit_Framework_TestCase {
public function testSomething() {
$this->assertEquals(false, $b);
$this->assertSame(true, $a);
$this->assertNotEquals(null, $c);
$this->assertNotSame(null, $d);
}
}
'
),
new CodeSample(
'<?php
$this->assertEquals(false, $b);
$this->assertSame(true, $a);
$this->assertNotEquals(null, $c);
$this->assertNotSame(null, $d);
final class FooTest extends \PHPUnit_Framework_TestCase {
public function testSomething() {
$this->assertEquals(false, $b);
$this->assertSame(true, $a);
$this->assertNotEquals(null, $c);
$this->assertNotSame(null, $d);
}
}
',
['assertions' => ['assertSame', 'assertNotSame']]
),
Expand Down Expand Up @@ -102,14 +111,18 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
return;
}

foreach ($this->configuration['assertions'] as $assertionMethod) {
$assertionFixer = self::$assertionFixers[$assertionMethod];
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();

foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
foreach ($this->configuration['assertions'] as $assertionMethod) {
$assertionFixer = self::$assertionFixers[$assertionMethod];

for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
$index = $this->{$assertionFixer}($tokens, $index, $assertionMethod);
for ($index = $indexes[0]; $index < $indexes[1]; ++$index) {
$index = $this->{$assertionFixer}($tokens, $index, $assertionMethod);

if (null === $index) {
break;
if (null === $index) {
break;
}
}
}
}
Expand Down
26 changes: 20 additions & 6 deletions src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function configure(array $configuration = null)
*/
public function isCandidate(Tokens $tokens)
{
return $tokens->isTokenKindFound(T_STRING);
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]);
}

/**
Expand All @@ -146,15 +146,27 @@ public function getDefinition()
[
new CodeSample(
'<?php
$this->assertTrue(is_float( $a), "my message");
$this->assertTrue(is_nan($a));
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testSomeTest()
{
$this->assertTrue(is_float( $a), "my message");
$this->assertTrue(is_nan($a));
}
}
'
),
new CodeSample(
'<?php
$this->assertTrue(is_dir($a));
$this->assertTrue(is_writable($a));
$this->assertTrue(is_readable($a));
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function testSomeTest()
{
$this->assertTrue(is_dir($a));
$this->assertTrue(is_writable($a));
$this->assertTrue(is_readable($a));
}
}
',
['target' => PhpUnitTargetVersion::VERSION_5_6]
),
Expand All @@ -180,6 +192,8 @@ public function getPriority()
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
//findPhpUnitClasses

foreach ($this->getPreviousAssertCall($tokens) as $assertCall) {
// test and fix for assertTrue/False to dedicated asserts
if ('asserttrue' === $assertCall['loweredName'] || 'assertfalse' === $assertCall['loweredName']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testMe()
*/
public function isCandidate(Tokens $tokens)
{
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION]);
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]);
}

/**
Expand Down
46 changes: 25 additions & 21 deletions src/Fixer/PhpUnit/PhpUnitStrictFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
use PhpCsFixer\Tokenizer\Token;
Expand Down Expand Up @@ -84,7 +85,7 @@ public function testSomeTest()
*/
public function isCandidate(Tokens $tokens)
{
return $tokens->isTokenKindFound(T_STRING);
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]);
}

/**
Expand All @@ -100,35 +101,38 @@ public function isRisky()
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
$phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
$argumentsAnalyzer = new ArgumentsAnalyzer();
$functionsAnalyzer = new FunctionsAnalyzer();

foreach ($this->configuration['assertions'] as $methodBefore) {
$methodAfter = self::$assertionMap[$methodBefore];
foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
foreach ($this->configuration['assertions'] as $methodBefore) {
$methodAfter = self::$assertionMap[$methodBefore];

for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
$methodIndex = $tokens->getNextTokenOfKind($index, [[T_STRING, $methodBefore]]);
for ($index = $indexes[0]; $index < $indexes[1]; ++$index) {
$methodIndex = $tokens->getNextTokenOfKind($index, [[T_STRING, $methodBefore]]);

if (null === $methodIndex) {
break;
}
if (null === $methodIndex) {
break;
}

if (!$functionsAnalyzer->isTheSameClassCall($tokens, $methodIndex)) {
continue;
}
if (!$functionsAnalyzer->isTheSameClassCall($tokens, $methodIndex)) {
continue;
}

$openingParenthesisIndex = $tokens->getNextMeaningfulToken($methodIndex);
$argumentsCount = $argumentsAnalyzer->countArguments(
$tokens,
$openingParenthesisIndex,
$tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openingParenthesisIndex)
);
$openingParenthesisIndex = $tokens->getNextMeaningfulToken($methodIndex);
$argumentsCount = $argumentsAnalyzer->countArguments(
$tokens,
$openingParenthesisIndex,
$tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openingParenthesisIndex)
);

if (2 === $argumentsCount || 3 === $argumentsCount) {
$tokens[$methodIndex] = new Token([T_STRING, $methodAfter]);
}
if (2 === $argumentsCount || 3 === $argumentsCount) {
$tokens[$methodIndex] = new Token([T_STRING, $methodAfter]);
}

$index = $methodIndex;
$index = $methodIndex;
}
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/Fixer/PhpUnit/PhpUnitTestCaseStaticMethodCallsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,21 @@ final class PhpUnitTestCaseStaticMethodCallsFixer extends AbstractFixer implemen
'assertDirectoryNotIsReadable' => true,
'assertDirectoryNotIsWritable' => true,
'assertEmpty' => true,
'assertEqualXMLStructure' => true,
'assertEquals' => true,
'assertEqualsCanonicalizing' => true,
'assertEqualsIgnoringCase' => true,
'assertEqualsWithDelta' => true,
'assertEqualXMLStructure' => true,
'assertFalse' => true,
'assertFileEquals' => true,
'assertFileEqualsCanonicalizing' => true,
'assertFileEqualsIgnoringCase' => true,
'assertFileExists' => true,
'assertFileIsReadable' => true,
'assertFileIsWritable' => true,
'assertFileNotEquals' => true,
'assertFileNotEqualsCanonicalizing' => true,
'assertFileNotEqualsIgnoringCase' => true,
'assertFileNotExists' => true,
'assertFileNotIsReadable' => true,
'assertFileNotIsWritable' => true,
Expand Down Expand Up @@ -178,11 +182,15 @@ final class PhpUnitTestCaseStaticMethodCallsFixer extends AbstractFixer implemen
'assertStringEndsNotWith' => true,
'assertStringEndsWith' => true,
'assertStringEqualsFile' => true,
'assertStringEqualsFileCanonicalizing' => true,
'assertStringEqualsFileIgnoringCase' => true,
'assertStringMatchesFormat' => true,
'assertStringMatchesFormatFile' => true,
'assertStringNotContainsString' => true,
'assertStringNotContainsStringIgnoringCase' => true,
'assertStringNotEqualsFile' => true,
'assertStringNotEqualsFileCanonicalizing' => true,
'assertStringNotEqualsFileIgnoringCase' => true,
'assertStringNotMatchesFormat' => true,
'assertStringNotMatchesFormatFile' => true,
'assertStringStartsNotWith' => true,
Expand All @@ -201,6 +209,8 @@ final class PhpUnitTestCaseStaticMethodCallsFixer extends AbstractFixer implemen
'classHasAttribute' => true,
'classHasStaticAttribute' => true,
'contains' => true,
'containsEqual' => true,
'containsIdentical' => true,
'containsOnly' => true,
'containsOnlyInstancesOf' => true,
'countOf' => true,
Expand Down Expand Up @@ -251,8 +261,8 @@ final class PhpUnitTestCaseStaticMethodCallsFixer extends AbstractFixer implemen
'atMost' => true,
'exactly' => true,
'never' => true,
'onConsecutiveCalls' => true,
'once' => true,
'onConsecutiveCalls' => true,
'returnArgument' => true,
'returnCallback' => true,
'returnSelf' => true,
Expand Down
Loading

0 comments on commit bdc0d83

Please sign in to comment.