Skip to content

Commit

Permalink
bug #4919 PhpUnitTestAnnotationFixer - fix function starting with "te…
Browse files Browse the repository at this point in the history
…st" and having lowercase letter after (kubawerlos)

This PR was merged into the 2.15 branch.

Discussion
----------

PhpUnitTestAnnotationFixer - fix function starting with "test" and having lowercase letter after

Commits
-------

da22702 PhpUnitTestAnnotationFixer - fix function starting with "test" and having lowercase letter after
  • Loading branch information
julienfalque committed May 31, 2020
2 parents 8d831d3 + da22702 commit 0d05113
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
46 changes: 18 additions & 28 deletions src/Fixer/PhpUnit/PhpUnitTestAnnotationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private function applyTestAnnotation(Tokens $tokens, $startIndex, $endIndex)
$functionNameIndex = $tokens->getNextMeaningfulToken($i);
$functionName = $tokens[$functionNameIndex]->getContent();

if ($this->hasTestPrefix($functionName)) {
if ($this->hasTestPrefix($functionName) && !$this->hasProperTestAnnotation($tokens, $i)) {
$newFunctionName = $this->removeTestPrefix($functionName);
$tokens[$functionNameIndex] = new Token([T_STRING, $newFunctionName]);
}
Expand Down Expand Up @@ -200,7 +200,7 @@ private function isTestMethod(Tokens $tokens, $index)
$functionNameIndex = $tokens->getNextMeaningfulToken($index);
$functionName = $tokens[$functionNameIndex]->getContent();

if ($this->startsWith('test', $functionName)) {
if ($this->hasTestPrefix($functionName)) {
return true;
}
// If the function doesn't have test in its name, and no doc block, its not a test
Expand Down Expand Up @@ -229,19 +229,6 @@ private function isMethod(Tokens $tokens, $index)
return $tokens[$index]->isGivenKind(T_FUNCTION) && !$tokensAnalyzer->isLambda($index);
}

/**
* @param string $needle
* @param string $haystack
*
* @return bool
*/
private function startsWith($needle, $haystack)
{
$len = \strlen($needle);

return substr($haystack, 0, $len) === $needle;
}

/**
* @param int $index
*
Expand Down Expand Up @@ -275,17 +262,20 @@ private function getDocBlockIndex(Tokens $tokens, $index)
*/
private function hasTestPrefix($functionName)
{
if (!$this->startsWith('test', $functionName)) {
return false;
}

if ('test' === $functionName) {
return true;
}
return 0 === strpos($functionName, 'test');
}

$nextCharacter = $functionName[4];
/**
* @param int $index
*
* @return bool
*/
private function hasProperTestAnnotation(Tokens $tokens, $index)
{
$docBlockIndex = $this->getDocBlockIndex($tokens, $index);
$doc = $tokens[$docBlockIndex]->getContent();

return $nextCharacter === strtoupper($nextCharacter);
return 1 === Preg::match('/\*\s+@test\b/', $doc);
}

/**
Expand All @@ -295,9 +285,9 @@ private function hasTestPrefix($functionName)
*/
private function removeTestPrefix($functionName)
{
$remainder = Preg::replace('/^test_?/', '', $functionName);
$remainder = Preg::replace('/^test(?=[A-Z_])_?/', '', $functionName);

if ('' === $remainder || is_numeric($remainder[0])) {
if ('' === $remainder) {
return $functionName;
}

Expand Down Expand Up @@ -471,7 +461,7 @@ private function removeTestPrefixFromDependsAnnotation(Line $line)
$dependsIndex = $this->findWhereDependsFunctionNameStarts($line);
$dependsFunctionName = implode('', \array_slice($line, $dependsIndex));

if ($this->startsWith('test', $dependsFunctionName)) {
if ($this->hasTestPrefix($dependsFunctionName)) {
$dependsFunctionName = $this->removeTestPrefix($dependsFunctionName);
}
array_splice($line, $dependsIndex);
Expand All @@ -488,7 +478,7 @@ private function addTestPrefixToDependsAnnotation(Line $line)
$dependsIndex = $this->findWhereDependsFunctionNameStarts($line);
$dependsFunctionName = implode('', \array_slice($line, $dependsIndex));

if (!$this->startsWith('test', $dependsFunctionName)) {
if (!$this->hasTestPrefix($dependsFunctionName)) {
$dependsFunctionName = $this->addTestPrefix($dependsFunctionName);
}

Expand Down
38 changes: 32 additions & 6 deletions tests/Fixer/PhpUnit/PhpUnitTestAnnotationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,18 @@ public function itDoesSomething() {}
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function testItDoesSomething() {}
}',
public function testItDoesSomethingWithoutPhpDoc() {}
/**
* No annotation, just text
*/
public function testItDoesSomethingWithPhpDoc() {}
public function testingItDoesSomethingWithoutPhpDoc() {}
/**
* No annotation, just text
*/
public function testingItDoesSomethingWithPhpDoc() {}
}',
],
'Annotation is added when it is already present in a weird place' => [
'<?php
Expand Down Expand Up @@ -810,14 +820,14 @@ abstract function fooBar();
}',
['style' => 'prefix'],
],
'Annotation present, but method does not actually have test prefix' => [
'Annotation present, but method already have test prefix' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
*
*/
public function testTestarossaIsFromItaly() {}
public function testarossaIsFromItaly() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
Expand Down Expand Up @@ -880,7 +890,7 @@ public function test123fooBar() {}
}',
['style' => 'annotation'],
],
'Annotation missing, method qualifies as test, but does not actually have test prefix' => [
'Annotation missing, but there is a lowercase character after the test prefix so it keeps the prefix' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
Expand All @@ -896,7 +906,7 @@ public function testarossaIsFromItaly() {}
}',
['style' => 'annotation'],
],
'Annotation present, method qualifies as test, but does not actually have test prefix' => [
'Annotation present, but there is a lowercase character after the test prefix so it keeps the prefix' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
Expand Down Expand Up @@ -952,6 +962,22 @@ public function test_() {}
null,
['style' => 'annotation'],
],
'Annotation missing, method after fix still has "test" prefix' => [
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
/**
* @test
*/
public function test_foo() {}
}',
'<?php
class Test extends \PhpUnit\FrameWork\TestCase
{
public function test_test_foo() {}
}',
['style' => 'annotation'],
],
];
}

Expand Down

0 comments on commit 0d05113

Please sign in to comment.