Movatterモバイル変換


[0]ホーム

URL:


Infection

Usage

Configuration

The first time you run Infection for your project, it will ask you several questions to create a config fileinfection.json5, with the following structure:

{
"source": {
"directories": [
"src"
],
"excludes": [
"Config",
"Folder/with/File.php",
"/\\.interface\\.php/",
"{Infrastructure/.*}"
]
},
"timeout":10,
"threads":"max",
"logs": {
"text":"infection.log",
"html":"infection.html",
"summary":"summary.log",
"json":"infection-log.json",
"perMutator":"per-mutator.md",
"github":true,
"gitlab":"gitlab-code-quality.json",
"stryker": {
"badge":"/^release-.*$/"
},
"summaryJson":"summary.json"
},
"tmpDir":"/opt/tmp-folder",
"phpUnit": {
"configDir":"app",
"customPath":"\/path\/to\/phpunit-6.1.phar"
},
"mutators": {
"global-ignore": [
"FooClass::__construct"
],
"global-ignoreSourceCodeByRegex": [
"Assert::.*"
],
"@default":true,
"@function_signature":false,
"TrueValue": {
"ignore": [
"NameSpace\\*\\Class::method"
],
"ignoreSourceCodeByRegex": [
"\\$this->logger.*"
]
}
},
"testFramework":"phpunit",
"bootstrap":"./infection-bootstrap.php",
"initialTestsPhpOptions":"-d zend_extension=xdebug.so",
"testFrameworkOptions":"--filter=Unit"
}

If you want to override settings locally, create and commit to VCSinfection.json5.dist but locally useinfection.json5 which should be ignored (e.g. in.gitignore).

By default, Infection usesjson5 format for configuration file. It allows using comments, ES5-like keys and many more. But if you for some reason can’t use it, Infection also supportsjson format.

Configuration settings

How to use custom autoloader or bootstrap file

If you have a custom autoloader or bootstrap file for your application, you should tell Infection about it.

For example, you have

// custom-autoloader.php

require'NonPsr4CompliantFile.php';

then you have to add it to theinfection.json5 file:

{
"bootstrap":"./custom-autoloader.php"
}

Thus, Infection will know how to autoloadNonPsr4CompliantFile class. Without adding it to the config, Infection will not be able to create Mutations because internally it usesnew \ReflectionClass() objects.

Running Infection

Ensure that your tests are all in a passing state (incomplete and skipped tests are allowed). Infection will quit if any of your tests are failing.

If you have installed Infection as a global composer package, just run it in your project’s root:

infection

or if you cloned it to some folder:

# cd /path/to/project/root

~/infection/bin/infection

Running withXdebug

In order to run Infection with Xdebug, you have several options:

Enable Xdebug globally

In this case just run

./infection.phar --threads=4

Enable Xdebug per process

Since Infection needs Xdebugonly to generate code coverage in a separate process, it is possible to enable debugger just there.

Assuming Xdebug is disabled globally, run

./infection.phar --initial-tests-php-options="-d zend_extension=xdebug.so"

Running withphpdbg

In order to run Infection withphpdbg instead of Xdebug, you need to execute the following command:

phpdbg -qrr infection.phar

Running without debugger

It is possible to run Infection without any debugger enabled. However, in this case you should provide already generated code coverage as an option

./infection.phar --coverage=path/to/coverage

Read more what types of coverage Infection requires and how to do it.

@infection-ignore-all support

Infection supports@infection-ignore-all annotation on class, method, and statement level.

The following class will not be mutated even though it might have few covered lines.

/**
*@infection-ignore-all
*/
classCalculator
{
publicfunctionadd(float $a, float $b):float
{
return $a + $b;
}
}

In this example, methodgenerate() will be skipped from mutation logic, butgetDependencies() will be mutated as usual method.

classProductFixture
{
/**
*@infection-ignore-all
*/
publicfunctiongenerate():void
{
// generate logic
}

publicfunctiongetDependencies():array
{
return [CategoryFixture::class];
}
}

Likewise, given this annotation Infection won’t consider anything in this loop:

/**@infection-ignore-all */
foreach ($fooas $bar) {
//
}

Exposed environment variables

Infection exposes a couple of environment variables:

Caught a mistake or want to contribute to the documentation? Edit this page on Github!

[8]ページ先頭

©2009-2025 Movatter.jp