Skip to content

Commit

Permalink
Merge branch 'master' into feature/ignore-code-by-regex
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Sep 15, 2020
2 parents 44257b4 + d6bd395 commit e4d7088
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 32 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/tests.yaml
@@ -0,0 +1,41 @@
name: Tests

on:
push:
pull_request:

jobs:
tests:
runs-on: ubuntu-latest

strategy:
matrix:
php-version: ['7.3', '7.4']
dependencies: ['']
include:
- { php-version: '7.3', dependencies: '--prefer-lowest' }

name: PHP ${{ matrix.php-version }} ${{ matrix.dependencies }}

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions:
coverage: pcov
tools: composer:v2

- name: Install dependencies
run: |
composer update --no-interaction ${{ matrix.dependencies }}
- name: Run tests
run: make test-unit

- name: Run Infection
run: |
./bin/infection -j$(nproc) --test-framework-options="--group=default"
1 change: 1 addition & 0 deletions devTools/phpstan-tests.neon
Expand Up @@ -10,6 +10,7 @@ parameters:
ignoreErrors:
- '#Return type \(void\) of method .*(Visitor|Traverser).* should be compatible with return type .* of method PhpParser\\Node.*#'
- '#^Cannot call method (willReturn|shouldBeCalledTimes)\(\) on iterable<Infection\\Mutation\\Mutation>\.$#'
- '#Offset .*\\.* does not exist on array<class-string<.*>.*#'
- '#^PHPDoc tag @var for variable \$sourceFilesCollectorProphecy contains unresolvable type#'
-
path: '../tests/phpunit/TestFramework/PhpUnit/Config/Builder/InitialConfigBuilderTest.php'
Expand Down
7 changes: 1 addition & 6 deletions src/Mutator/GetMutatorName.php
Expand Up @@ -35,18 +35,13 @@

namespace Infection\Mutator;

use function end;
use function explode;

/**
* @internal
*/
trait GetMutatorName
{
final public function getName(): string
{
$parts = explode('\\', static::class);

return (string) end($parts);
return MutatorFactory::getMutatorNameForClassName(static::class);
}
}
40 changes: 31 additions & 9 deletions src/Mutator/MutatorFactory.php
Expand Up @@ -35,9 +35,10 @@

namespace Infection\Mutator;

use function in_array;
use function end;
use function explode;
use function is_a;
use function Safe\array_flip;
use function Safe\class_implements;
use function Safe\sprintf;
use Webmozart\Assert\Assert;

Expand All @@ -47,7 +48,7 @@
final class MutatorFactory
{
/**
* @param array<string, mixed[]> $resolvedMutators
* @param array<class-string<Mutator&ConfigurableMutator>, mixed[]> $resolvedMutators
*
* @return array<string, Mutator>
*/
Expand Down Expand Up @@ -76,20 +77,41 @@ public function create(array $resolvedMutators): array
/** @var string[] $ignored */
$ignored = $config['ignore'] ?? [];

if (in_array(ConfigurableMutator::class, class_implements($mutatorClassName), true)) {
$configClassName = $mutatorClassName::getConfigClassName();
$mutator =
is_a($mutatorClassName, ConfigurableMutator::class, true) ?
self::getConfigurableMutator($mutatorClassName, $settings) :
new $mutatorClassName();

$mutator = new $mutatorClassName(new $configClassName($settings));
} else {
$mutator = new $mutatorClassName();
if ($ignored === []) {
$mutators[$mutator->getName()] = $mutator;

continue;
}

$mutators[(string) $mutator->getName()] = new IgnoreMutator(
$mutators[$mutator->getName()] = new IgnoreMutator(
new IgnoreConfig($ignored),
$mutator
);
}

return $mutators;
}

public static function getMutatorNameForClassName(string $className): string
{
$parts = explode('\\', $className);

return (string) end($parts);
}

/**
* @param class-string<ConfigurableMutator> $mutatorClassName
* @param mixed[] $settings
*/
private static function getConfigurableMutator(string $mutatorClassName, array $settings): ConfigurableMutator
{
$configClassName = $mutatorClassName::getConfigClassName();

return new $mutatorClassName(new $configClassName($settings));
}
}
2 changes: 1 addition & 1 deletion src/Mutator/MutatorResolver.php
Expand Up @@ -58,7 +58,7 @@ final class MutatorResolver
*
* @param array<string, bool|stdClass> $mutatorSettings
*
* @return array<string, mixed[]>
* @return array<class-string<Mutator&ConfigurableMutator>, mixed[]>
*/
public function resolve(array $mutatorSettings): array
{
Expand Down
15 changes: 3 additions & 12 deletions tests/phpunit/Configuration/ConfigurationFactoryTest.php
Expand Up @@ -585,14 +585,8 @@ public function valueProvider(): iterable
'AssignmentEqual,EqualIdentical',
(static function (): array {
return [
'AssignmentEqual' => new IgnoreMutator(
new IgnoreConfig([]),
new AssignmentEqual()
),
'EqualIdentical' => new IgnoreMutator(
new IgnoreConfig([]),
new EqualIdentical()
),
'AssignmentEqual' => new AssignmentEqual(),
'EqualIdentical' => new EqualIdentical(),
];
})()
);
Expand Down Expand Up @@ -733,10 +727,7 @@ public function valueProvider(): iterable
),
(static function (): array {
return [
'TrueValue' => new IgnoreMutator(
new IgnoreConfig([]),
new TrueValue(new TrueValueConfig([]))
),
'TrueValue' => new TrueValue(new TrueValueConfig([])),
];
})(),
'phpspec',
Expand Down
21 changes: 17 additions & 4 deletions tests/phpunit/Mutator/MutatorFactoryTest.php
Expand Up @@ -45,6 +45,7 @@
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorFactory;
use Infection\Mutator\ProfileList;
use Infection\Mutator\Sort\Spaceship;
use Infection\PhpParser\Visitor\ReflectionVisitor;
use Infection\Reflection\ClassReflection;
use Infection\Tests\SingletonContainer;
Expand Down Expand Up @@ -96,6 +97,7 @@ public function test_it_can_create_a_mutator_with_the_given_settings(): void
],
]);

$this->assertContainsOnlyInstancesOf(IgnoreMutator::class, $mutators);
$this->assertSameMutatorsByClass([TrueValue::class], $mutators);

/** @var MockObject|ClassReflection $reflectionMock */
Expand Down Expand Up @@ -155,17 +157,24 @@ public function test_it_can_create_the_mutators_with_settings_as_std_class(): vo
public function test_it_cannot_create_an_unknown_mutator(): void
{
try {
$this->mutatorFactory->create(['Unknwon\Mutator' => []]);
$this->mutatorFactory->create(['Unknown\Mutator' => []]);

$this->fail();
} catch (InvalidArgumentException $exception) {
$this->assertSame(
'Unknown mutator "Unknwon\Mutator"',
'Unknown mutator "Unknown\Mutator"',
$exception->getMessage()
);
}
}

public function test_it_can_parse_name(): void
{
$name = $this->mutatorFactory::getMutatorNameForClassName(Spaceship::class);

$this->assertSame('Spaceship', $name);
}

private function createBoolNode(string $boolean, string $functionName, ClassReflection $reflectionMock): Node
{
return new Node\Expr\ConstFetch(
Expand All @@ -191,10 +200,14 @@ private function assertSameMutatorsByClass(
$decoratedMutatorReflection->setAccessible(true);

foreach (array_values($actualMutators) as $index => $mutator) {
$this->assertInstanceOf(IgnoreMutator::class, $mutator);
$this->assertInstanceOf(Mutator::class, $mutator);

$expectedMutatorClass = $expectedMutatorClassNames[$index];
$actualMutatorClass = get_class($decoratedMutatorReflection->getValue($mutator));
$actualMutatorClass = get_class(
$mutator instanceof IgnoreMutator ?
$decoratedMutatorReflection->getValue($mutator) :
$mutator
);

$this->assertSame(
$expectedMutatorClass,
Expand Down

0 comments on commit e4d7088

Please sign in to comment.