Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ final class Count extends \PHPUnit\Framework\TestCase
$this->assertEquals(5, $collection->count());
\PHPUnit\Framework\TestCase::assertSame(5, $collection->count());
self::assertSame(5, $collection->count());

$this->assertNotSame(6, $collection->count());
$this->assertNotEquals(6, $collection->count());
\PHPUnit\Framework\TestCase::assertNotSame(6, $collection->count());
self::assertNotSame(6, $collection->count());
}
}

Expand All @@ -33,6 +38,11 @@ final class Count extends \PHPUnit\Framework\TestCase
$this->assertCount(5, $collection);
\PHPUnit\Framework\TestCase::assertCount(5, $collection);
self::assertCount(5, $collection);

$this->assertNotCount(6, $collection);
$this->assertNotCount(6, $collection);
\PHPUnit\Framework\TestCase::assertNotCount(6, $collection);
self::assertNotCount(6, $collection);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ final class DirectCountCall extends \PHPUnit\Framework\TestCase
{
$items = [1, 2, 3];
$this->assertSame(3, count($items));
$this->assertNotSame(4, count($items));
}
}

Expand All @@ -23,6 +24,7 @@ final class DirectCountCall extends \PHPUnit\Framework\TestCase
{
$items = [1, 2, 3];
$this->assertCount(3, $items);
$this->assertNotCount(4, $items);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertCompareOnCountableWithMethodToAssertCountRector\Fixture;

final class OtherCountCall extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertEquals(10, iterator_count($something));

$count = 92;
$this->assertNotEquals($count, sizeof($something), 'third argument');
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertCompareOnCountableWithMethodToAssertCountRector\Fixture;

final class OtherCountCall extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertCount(10, $something);

$count = 92;
$this->assertNotCount($count, $something, 'third argument');
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function getRuleDefinition(): RuleDefinition
$this->assertCount(1, $countable);
CODE_SAMPLE
),
new CodeSample(
'$this->assertSame(10, count($anything), "message");',
'$this->assertCount(10, $anything, "message");'
),
]
);
}
Expand All @@ -57,7 +61,10 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): MethodCall|StaticCall|null
{
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, ['assertSame', 'assertEquals'])) {
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames(
$node,
['assertSame', 'assertNotSame', 'assertEquals', 'assertNotEquals']
)) {
return null;
}

Expand All @@ -72,12 +79,15 @@ public function refactor(Node $node): MethodCall|StaticCall|null

$comparedExpr = $assertArgs[1]->value;

if ($comparedExpr instanceof FuncCall && $this->isName($comparedExpr->name, 'count')) {
if (
$comparedExpr instanceof FuncCall
&& $this->isNames($comparedExpr->name, ['count', 'sizeof', 'iterator_count'])
) {
$countArg = $comparedExpr->getArgs()[0];
$assertArgs[1] = new Arg($countArg->value);

$node->args = $assertArgs;
$node->name = new Identifier('assertCount');
$this->renameMethod($node);

return $node;
}
Expand All @@ -94,10 +104,19 @@ public function refactor(Node $node): MethodCall|StaticCall|null
$args[1] = new Arg($comparedExpr->var);

$node->args = $args;
$node->name = new Identifier('assertCount');
$this->renameMethod($node);
}
}

return null;
}

private function renameMethod(MethodCall|StaticCall $node): void
{
if ($this->isNames($node->name, ['assertSame', 'assertEquals'])) {
$node->name = new Identifier('assertCount');
} elseif ($this->isNames($node->name, ['assertNotSame', 'assertNotEquals'])) {
$node->name = new Identifier('assertNotCount');
}
}
}
Loading