Skip to content

Commit bec67ed

Browse files
claudedeviantintegral
authored andcommitted
test: kill LogicalOrAllSubExprNegation mutation in ServerRequest
Add test to kill the LogicalOrAllSubExprNegation mutation on line 158 of ServerRequest::withParsedBody(). The mutation changes the condition from `is_array($data) || is_object($data)` to `!is_array($data) || !is_object($data)`, which would incorrectly enter the params-setting block for null values. Also enable failOnWarning in phpunit.xml.dist to ensure PHP warnings (like foreach on null) fail tests, which is necessary for this mutation to be detected.
1 parent 79e46f1 commit bec67ed

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false" failOnWarning="false">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false" failOnWarning="true">
33
<logging>
44
<junit outputFile="build/logs/results.xml"/>
55
</logging>

tests/src/Unit/Adapter/Psr7/ServerRequestTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,4 +619,42 @@ public function testGetQueryParamsWithMultipleParams(): void
619619
$this->assertEquals('value2', $queryParams['param2']);
620620
$this->assertEquals('value3', $queryParams['param3']);
621621
}
622+
623+
/**
624+
* Kill LogicalOrAllSubExprNegation mutation in withParsedBody().
625+
*
626+
* Original: if (is_array($data) || is_object($data))
627+
* Mutant: if (!is_array($data) || !is_object($data))
628+
*
629+
* For null: original = false||false = false (skips block)
630+
* mutant = true||true = true (enters block, warning on foreach)
631+
*
632+
* When mutant is applied, foreach(null) triggers E_WARNING which fails the
633+
* test due to failOnWarning="true" in phpunit.xml.dist
634+
*/
635+
public function testWithParsedBodyNullKillsLogicalOrAllSubExprNegation(): void
636+
{
637+
// Start with a request that has existing post params to verify they get cleared
638+
$harRequest = (new Request())
639+
->setMethod('POST')
640+
->setUrl(new Uri('https://www.example.com/'))
641+
->setPostData(
642+
(new PostData())->setParams([
643+
(new Params())->setName('existing')->setValue('value'),
644+
])
645+
);
646+
647+
$serverRequest = new ServerRequest($harRequest);
648+
649+
// Verify pre-condition: there are existing params
650+
$this->assertTrue($serverRequest->getHarRequest()->getPostData()->hasParams());
651+
652+
// Call withParsedBody(null) - this must NOT trigger a warning
653+
// If mutant is applied: foreach(null as ...) triggers E_WARNING
654+
$result = $serverRequest->withParsedBody(null);
655+
656+
// The null branch should have been taken, clearing the params
657+
$this->assertNull($result->getParsedBody());
658+
$this->assertFalse($result->getHarRequest()->getPostData()->hasParams());
659+
}
622660
}

0 commit comments

Comments
 (0)