Skip to content

Commit f3d1ebe

Browse files
committed
Setup mutation testing
1 parent 934f573 commit f3d1ebe

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

.github/workflows/platform-test.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ jobs:
2828
- "8.4"
2929
update-packages:
3030
- ""
31+
run-infection:
32+
- ""
33+
3134
include:
3235
- php-version: "8.1"
3336
update-packages: doctrine/orm:^3.0 doctrine/dbal:^4.0 carbonphp/carbon-doctrine-types:^3 gedmo/doctrine-extensions:^3
@@ -37,6 +40,8 @@ jobs:
3740
update-packages: doctrine/orm:^3.0 doctrine/dbal:^4.0 carbonphp/carbon-doctrine-types:^3 gedmo/doctrine-extensions:^3
3841
- php-version: "8.4"
3942
update-packages: doctrine/orm:^3.0 doctrine/dbal:^4.0 carbonphp/carbon-doctrine-types:^3 gedmo/doctrine-extensions:^3
43+
- php-version: "8.4"
44+
run-infection: "true"
4045

4146
steps:
4247
- name: "Checkout"
@@ -63,6 +68,12 @@ jobs:
6368

6469
- name: "Run platform matrix test"
6570
run: vendor/bin/phpunit --group=platform
71+
if: ${{ matrix.run-infection == '' }}
72+
73+
- name: "Run infection"
74+
run: vendor/bin/infection
75+
#run: vendor/bin/infection --git-diff-filter=AM
76+
if: ${{ matrix.run-infection != '' }}
6677

6778
services:
6879
postgres:

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"doctrine/orm": "^2.16.0",
3030
"doctrine/persistence": "^2.2.1 || ^3.2",
3131
"gedmo/doctrine-extensions": "^3.8",
32+
"infection/infection": "^0.31.2",
3233
"nesbot/carbon": "^2.49",
3334
"php-parallel-lint/php-parallel-lint": "^1.2",
3435
"phpstan/phpstan-deprecation-rules": "^2.0.2",
@@ -41,7 +42,8 @@
4142
"config": {
4243
"sort-packages": true,
4344
"allow-plugins": {
44-
"cweagans/composer-patches": true
45+
"cweagans/composer-patches": true,
46+
"infection/extension-installer": false
4547
}
4648
},
4749
"extra": {

infection.json5

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"$schema": "vendor/infection/infection/resources/schema.json",
3+
"source": {
4+
"directories": [
5+
"src"
6+
]
7+
},
8+
"logs": {
9+
"text": "tmp/infection.log"
10+
},
11+
"mutators": {
12+
"@default": false,
13+
"PHPStan\\Infection\\TrinaryLogicMutator": true
14+
}
15+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PHPStan\Infection;
4+
5+
use Infection\Mutator\Definition;
6+
use Infection\Mutator\Mutator;
7+
use Infection\Mutator\MutatorCategory;
8+
use LogicException;
9+
use PhpParser\Node;
10+
11+
/**
12+
* @implements Mutator<Node\Expr\MethodCall>
13+
*/
14+
final class TrinaryLogicMutator implements Mutator {
15+
16+
public static function getDefinition(): Definition
17+
{
18+
return new Definition(
19+
<<<'TXT'
20+
Replaces TrinaryLogic->yes() with !TrinaryLogic->no()
21+
TXT
22+
,
23+
MutatorCategory::ORTHOGONAL_REPLACEMENT,
24+
null,
25+
<<<'DIFF'
26+
- $type->isBoolean()->yes();
27+
+ !$type->isBoolean()->no();
28+
DIFF,
29+
);
30+
}
31+
32+
public function getName(): string
33+
{
34+
return 'TrinaryLogicMutator';
35+
}
36+
37+
public function canMutate(Node $node): bool
38+
{
39+
if (!$node instanceof Node\Expr\MethodCall) {
40+
return false;
41+
}
42+
43+
if (!$node->name instanceof Node\Identifier) {
44+
return false;
45+
}
46+
47+
if (!in_array($node->name->name, ['yes', 'no'], true)) {
48+
return false;
49+
}
50+
51+
return true;
52+
}
53+
54+
public function mutate(Node $node): iterable
55+
{
56+
if (!$node->name instanceof Node\Identifier) {
57+
throw new LogicException();
58+
}
59+
60+
if ($node->name->name === 'yes') {
61+
yield new Node\Expr\BooleanNot(new Node\Expr\MethodCall($node->var, 'no'));
62+
} else {
63+
yield new Node\Expr\BooleanNot(new Node\Expr\MethodCall($node->var, 'yes'));
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)