1

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?

José Victor
  • 125
  • 10
  • Doesn't it log that it esacped? Why uncovered? Apart from your knowledge question about infection, trouble-shoorting/debugging guide is here: https://infection.github.io/guide/debugging-issues.html --- And could you please check if you can reproduce https://stackoverflow.com/questions/71259335/php-infection-marks-false-uncovered-mutants exactly? – hakre Jun 30 '22 at 07:30
  • It is not recorded as "escaped". That's the question: Why mark it as "not covered" when I have scenarios in the unit tests that guarantee it! This reported question 71259335 also has the same behavior as mine, bizarre. – José Victor Jul 01 '22 at 15:42
  • mutator might be in the tests data-provider? see https://infection.github.io/guide/caveats.html#Mutations-must-occur-when-CodeCoverage-has-already-started – hakre Jul 01 '22 at 22:40

0 Answers0