Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Force mutators to include remedies #1954

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Mutator/Unwrap/UnwrapArrayMap.php
Expand Up @@ -64,7 +64,13 @@ public static function getDefinition(): ?Definition
TXT
,
MutatorCategory::SEMANTIC_REDUCTION,
null,
<<<'TXT'
This mutation escaping suggests that the `$callback` transformation passed to
`array_map()` does no effect to the passed elements.

Either this transformation is needed in which case additional tests capturing
this need are required, or it is not and should be removed.
TXT,
<<<'DIFF'
- $x = array_map($callback, ['foo', 'bar', 'baz']);
+ $x = ['foo', 'bar', 'baz'];
Expand Down
206 changes: 206 additions & 0 deletions tests/phpunit/Mutator/DefinitionTest.php
Expand Up @@ -35,15 +35,185 @@

namespace Infection\Tests\Mutator;

use function array_diff_key;
use function array_fill_keys;
use function array_flip;
use Infection\Mutator\Definition;
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorCategory;
use Infection\Mutator\ProfileList;
use Infection\Tests\SingletonContainer;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use function sprintf;

#[CoversClass(Definition::class)]
final class DefinitionTest extends TestCase
{
// TODO: address those
private const MUTATORS_WITHOUT_REMEDIES = [
'Assignment',
'AssignmentEqual',
'BitwiseAnd',
'BitwiseNot',
'BitwiseOr',
'BitwiseXor',
'Decrement',
'DivEqual',
'Division',
'Exponentiation',
'Increment',
'Minus',
'MinusEqual',
'ModEqual',
'Modulus',
'MulEqual',
'Multiplication',
'Plus',
'PlusEqual',
'PowEqual',
'RoundingFamily',
'ShiftLeft',
'ShiftRight',
'ArrayItem',
'EqualIdentical',
'FalseValue',
'IdenticalEqual',
'InstanceOf_',
'LogicalAnd',
'LogicalAndAllSubExprNegation',
'LogicalAndNegation',
'LogicalAndSingleSubExprNegation',
'LogicalLowerAnd',
'LogicalLowerOr',
'LogicalNot',
'LogicalOr',
'LogicalOrAllSubExprNegation',
'LogicalOrNegation',
'LogicalOrSingleSubExprNegation',
'NotEqualNotIdentical',
'NotIdenticalNotEqual',
'TrueValue',
'Yield_',
'GreaterThan',
'GreaterThanOrEqualTo',
'LessThan',
'LessThanOrEqualTo',
'Equal',
'GreaterThanNegotiation',
'GreaterThanOrEqualToNegotiation',
'Identical',
'LessThanNegotiation',
'LessThanOrEqualToNegotiation',
'NotEqual',
'NotIdentical',
'ProtectedVisibility',
'PublicVisibility',
'DecrementInteger',
'IncrementInteger',
'OneZeroFloat',
'AssignCoalesce',
'Break_',
'Coalesce',
'Concat',
'Continue_',
'ElseIfNegation',
'Finally_',
'IfNegation',
'NullSafeMethodCall',
'NullSafePropertyCall',
'SpreadAssignment',
'SpreadOneItem',
'SpreadRemoval',
'Ternary',
'Throw_',
'Catch_',
'PregMatchMatches',
'PregMatchRemoveCaret',
'PregMatchRemoveDollar',
'PregMatchRemoveFlags',
'PregQuote',
'ArrayItemRemoval',
'CatchBlockRemoval',
'CloneRemoval',
'ConcatOperandRemoval',
'FunctionCallRemoval',
'MatchArmRemoval',
'MethodCallRemoval',
'SharedCaseRemoval',
'ArrayOneItem',
'FloatNegation',
'FunctionCall',
'IntegerNegation',
'NewObject',
'This',
'YieldValue',
'Spaceship',
'DoWhile',
'Foreach_',
'For_',
'While_',
'CastArray',
'CastBool',
'CastFloat',
'CastInt',
'CastObject',
'CastString',
'UnwrapArrayChangeKeyCase',
'UnwrapArrayChunk',
'UnwrapArrayColumn',
'UnwrapArrayCombine',
'UnwrapArrayDiff',
'UnwrapArrayDiffAssoc',
'UnwrapArrayDiffKey',
'UnwrapArrayDiffUassoc',
'UnwrapArrayDiffUkey',
'UnwrapArrayFilter',
'UnwrapArrayFlip',
'UnwrapArrayIntersect',
'UnwrapArrayIntersectAssoc',
'UnwrapArrayIntersectKey',
'UnwrapArrayIntersectUassoc',
'UnwrapArrayIntersectUkey',
'UnwrapArrayKeys',
'UnwrapArrayMerge',
'UnwrapArrayMergeRecursive',
'UnwrapArrayPad',
'UnwrapArrayReduce',
'UnwrapArrayReplace',
'UnwrapArrayReplaceRecursive',
'UnwrapArrayReverse',
'UnwrapArraySlice',
'UnwrapArraySplice',
'UnwrapArrayUdiff',
'UnwrapArrayUdiffAssoc',
'UnwrapArrayUdiffUassoc',
'UnwrapArrayUintersect',
'UnwrapArrayUintersectAssoc',
'UnwrapArrayUintersectUassoc',
'UnwrapArrayUnique',
'UnwrapArrayValues',
'UnwrapLcFirst',
'UnwrapLtrim',
'UnwrapRtrim',
'UnwrapStrIreplace',
'UnwrapStrRepeat',
'UnwrapStrReplace',
'UnwrapStrRev',
'UnwrapStrShuffle',
'UnwrapStrToLower',
'UnwrapStrToUpper',
'UnwrapSubstr',
'UnwrapTrim',
'UnwrapUcFirst',
'UnwrapUcWords',
'UnwrapFinally',
'BCMath',
'MBString',
'SyntaxError',
];

#[DataProvider('valuesProvider')]
public function test_it_can_be_instantiated(
string $description,
Expand Down Expand Up @@ -75,4 +245,40 @@ public static function valuesProvider(): iterable
'The diff',
];
}

#[DataProvider('mutatorsProvider')]
public function test_it_must_define_remedies(Mutator $mutator): void
{
$this->assertNotNull(
$mutator::getDefinition()->getRemedies(),
sprintf(
'Definition of [%s] must provide remedies.',
$mutator->getName(),
),
);
}

public static function mutatorsProvider(): iterable
{
$mutatorFactory = SingletonContainer::getContainer()->getMutatorFactory();

$mutators = $mutatorFactory->create(
array_fill_keys(
ProfileList::ALL_MUTATORS,
[],
),
false,
);

$checkedMutators = array_diff_key(
$mutators,
array_flip(self::MUTATORS_WITHOUT_REMEDIES),
);

foreach ($checkedMutators as $name => $mutator) {
self::assertInstanceOf(Mutator::class, $mutator);

yield $name => [$mutator];
}
}
}