Skip to content

Commit

Permalink
Add more tests examples and fix wrongly capitalized funcation name
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed Oct 18, 2018
1 parent ed48156 commit 9db805b
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/Mutator/Unwrap/AbstractUnwrapMutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ abstract protected function getParameterIndex(): int;
*
* @return Node\Param;
*/
public function mutate(Node $node)
final public function mutate(Node $node)
{
return $node->args[$this->getParameterIndex()];
}

protected function mutatesNode(Node $node): bool
final protected function mutatesNode(Node $node): bool
{
if (!$node instanceof Node\Expr\FuncCall) {
return false;
}

return $node->name->toString() === $this->getFunctionName();
return $node->name->toLowerString() === strtolower($this->getFunctionName());
}
}
104 changes: 99 additions & 5 deletions tests/Mutator/Unwrap/UnwrapArrayFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function test_mutator($input, $expected = null): void

public function provideMutationCases(): \Generator
{
yield [
yield 'It mutates correctly when provided with a array' => [
<<<'PHP'
<?php
Expand All @@ -40,19 +40,113 @@ public function provideMutationCases(): \Generator
PHP
];

yield [
yield 'It mutates correctly when provided with a constant' => [
<<<'PHP'
<?php
$a = array_filter(['A', 1, 'C'], function($var): bool {
return is_int($var);
});
$a = array_filter(\Class_With_Const::Const, 'is_int');
PHP
,
<<<'PHP'
<?php
$a = \Class_With_Const::Const;
PHP
];

yield 'It mutates correctly when a backslash is in front of array_filter' => [
<<<'PHP'
<?php
$a = \array_filter(['A', 1, 'C'], 'is_int');
PHP
,
<<<'PHP'
<?php
$a = ['A', 1, 'C'];
PHP
];

yield 'It does not mutate other array_ calls' => [
<<<'PHP'
<?php
$a = array_map('strtolower', ['A', 'B', 'C']);
PHP
];

yield 'It does not mutate functions named array_filter' => [
<<<'PHP'
<?php
function array_filter($text, $other)
{
}
PHP
];

yield 'It mutates correctly within if statements' => [
<<<'PHP'
<?php
$a = ['A', 1, 'C'];
if (array_filter($a, 'is_int') === $a) {
return true;
}
PHP
,
<<<'PHP'
<?php
$a = ['A', 1, 'C'];
if ($a === $a) {
return true;
}
PHP
];

yield 'It mutates correctly when array_map is wrongly capitalized' => [
<<<'PHP'
<?php
$a = aRrAy_FiLtEr(['A', 1, 'C'], 'is_int');
PHP
,
<<<'PHP'
<?php
$a = ['A', 1, 'C'];
PHP
];

yield 'It mutates correctly when array_map uses another function as input' => [
<<<'PHP'
<?php
$a = array_filter($foo->bar(), 'is_int');
PHP
,
<<<'PHP'
<?php
$a = $foo->bar();
PHP
];

yield 'It mutates correctly when provided with a more complex situation' => [
<<<'PHP'
<?php
$a = array_map('strtolower', array_filter(['A', 1, 'C'], function($char): bool {
return !is_int($char);
}));
PHP
,
<<<'PHP'
<?php
$a = array_map('strtolower', ['A', 1, 'C']);
PHP
];
}
Expand Down
104 changes: 99 additions & 5 deletions tests/Mutator/Unwrap/UnwrapArrayMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function test_mutator($input, $expected = null): void

public function provideMutationCases(): \Generator
{
yield [
yield 'It mutates correctly when provided with a array' => [
<<<'PHP'
<?php
Expand All @@ -40,19 +40,113 @@ public function provideMutationCases(): \Generator
PHP
];

yield [
yield 'It mutates correctly when provided with a constant' => [
<<<'PHP'
<?php
$a = array_map(function(string $letter): string {
return strtolower($letter);
}, ['A', 'B', 'C']);
$a = array_map('strtolower', \Class_With_Const::Const);
PHP
,
<<<'PHP'
<?php
$a = \Class_With_Const::Const;
PHP
];

yield 'It mutates correctly when a backslash is in front of array_map' => [
<<<'PHP'
<?php
$a = \array_map('strtolower', ['A', 'B', 'C']);
PHP
,
<<<'PHP'
<?php
$a = ['A', 'B', 'C'];
PHP
];

yield 'It does not mutate other array_ calls' => [
<<<'PHP'
<?php
$a = array_filter([1, 2, 3], 'is_int');
PHP
];

yield 'It does not mutate functions named array_map' => [
<<<'PHP'
<?php
function array_map($text, $other)
{
}
PHP
];

yield 'It mutates correctly within if statements' => [
<<<'PHP'
<?php
$a = ['A', 'B', 'C'];
if (array_map('strtolower', $a) === $a) {
return true;
}
PHP
,
<<<'PHP'
<?php
$a = ['A', 'B', 'C'];
if ($a === $a) {
return true;
}
PHP
];

yield 'It mutates correctly when array_map is wrongly capitalized' => [
<<<'PHP'
<?php
$a = ArRaY_mAp('strtolower', ['A', 'B', 'C']);
PHP
,
<<<'PHP'
<?php
$a = ['A', 'B', 'C'];
PHP
];

yield 'It mutates correctly when array_map uses another function as input' => [
<<<'PHP'
<?php
$a = array_map('strtolower', $foo->bar());
PHP
,
<<<'PHP'
<?php
$a = $foo->bar();
PHP
];

yield 'It mutates correctly when provided with a more complex situation' => [
<<<'PHP'
<?php
$a = array_filter(array_map(function(string $letter): string {
return strtolower($letter);
}, ['A', 'B', 'C']), 'is_int');
PHP
,
<<<'PHP'
<?php
$a = array_filter(['A', 'B', 'C'], 'is_int');
PHP
];
}
Expand Down

0 comments on commit 9db805b

Please sign in to comment.