I'm running some mutation tests ("infection/infection": "^0.26.1"
) and for some unknown reason PHP Infection claims that there are tests that are not covered even with the unit tests written there.
Here is the error alleged by Infection:
1) /opt/www/app/Infrastructure/Http/Resources/Mobile/PaymentMethods/PaymentMethodsCollection.php:34 [M] ArrayItemRemoval
--- Original
+++ New
@@ @@
$paymentMethods = PaymentMethodsResource::collection($collection['PaymentMethods'] ?? []);
$balance = BalanceResource::make(
$collection['Balance'] ?? ['balance' => '0.00', 'withdraw_balance' => '0.00', 'withdraw_balance_from_credits' => '0.00']
);
$defaultPaymentMethod = DefaultPaymentMethodResource::make($collection['DefaultPaymentMethod'] ?? null);
- return ['data' => ['PaymentMethods' => $paymentMethods, 'Balance' => $balance, 'DefaultPaymentMethod' => $defaultPaymentMethod]];
+ return [];
}
}
Here is the unit test covering such a scenario:
/**
* @dataProvider dataProvider
*/
public function testToArray($input, $output): void
{
$this->service = new PaymentMethodsCollection($input);
$result = $this->service->toArray();
$this->assertNotEmpty($result);
$this->assertArrayHasKey('data', $result);
$this->assertArrayHasKey('PaymentMethods', $result['data']);
$this->assertArrayHasKey('Balance', $result['data']);
$this->assertArrayHasKey('DefaultPaymentMethod', $result['data']);
$this->assertInstanceOf(ResourceCollection::class,
$result['data']['PaymentMethods']
);
$this->assertInstanceOf(BalanceResource::class,
$result['data']['Balance']
);
$this->assertInstanceOf(DefaultPaymentMethodResource::class,
$result['data']['DefaultPaymentMethod']
);
}
If I run the unit tests in isolation, the output is:
Payment Methods Collection (Test\Unit\Infrastructure\Http\Resources\Mobile\PaymentMethods\PaymentMethodsCollection)
✔ To array with only·balance·empty
✔ To array with full·empty
✔ To array with only·default·payment·method·empty
✔ To array with only·payments·methods·empty
✔ To array with filled
Time: 00:01.257, Memory: 28.00 MB
OK (5 tests, 45 assertions)
Segue a configuração do Infection:
{
"$schema": "vendor/infection/infection/resources/schema.json",
"source": {
"directories": [
"app"
]
},
"tmpDir": "tmp",
"logs": {
"text": "test/reports/infection/infection.log",
"html": "test/reports/infection/infection.html"
},
"mutators": {
"@default": true,
"@function_signature": false
},
"testFramework":"phpunit",
"testFrameworkOptions": "--testsuite=Unit",
"phpUnit": {
"configDir": "test/Infection"
}
}
Why is PHP Infection claiming it is not covered?
Where is the problem that I'm not seeing?
The concept of "not covered" is when the unit test doesn't cover that scenario and leaves the mutant alive, correct?