Skip to content

Commit

Permalink
Ensure mutated code is always valid (#301)
Browse files Browse the repository at this point in the history
* Ensure mutated code is always valid

* Use piping instead of creating a file for `php -l` linting commandline
  • Loading branch information
maks-rafalko committed Apr 12, 2018
1 parent 663f6a5 commit b0a34c2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
15 changes: 15 additions & 0 deletions tests/Mutator/AbstractMutatorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function doTest(string $inputCode, string $expectedCode = null)
$realMutatedCode = $this->mutate($inputCode);
if ($expectedCode !== null) {
$this->assertSame($expectedCode, $realMutatedCode);
$this->assertSyntaxIsValid($realMutatedCode);
} else {
$this->assertSame($inputCode, $realMutatedCode);
}
Expand Down Expand Up @@ -81,4 +82,18 @@ protected function mutate(string $code)

return $prettyPrinter->prettyPrintFile($mutatedNodes);
}

private function assertSyntaxIsValid(string $realMutatedCode)
{
exec(sprintf('echo %s | php -l', escapeshellarg($realMutatedCode)), $output, $returnCode);

$this->assertSame(
0,
$returnCode,
sprintf(
'Mutator %s produces invalid code',
$this->getMutator()::getName()
)
);
}
}
15 changes: 12 additions & 3 deletions tests/Mutator/Boolean/Yield_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ public function provideMutationCases(): \Generator
<<<'PHP'
<?php
(yield $a => $b);
function test()
{
(yield $a => $b);
}
PHP
,
<<<'PHP'
<?php
(yield $a > $b);
function test()
{
(yield $a > $b);
}
PHP
,
];
Expand All @@ -42,7 +48,10 @@ public function provideMutationCases(): \Generator
<<<'PHP'
<?php
(yield $b);
function test()
{
(yield $b);
}
PHP
,
];
Expand Down

0 comments on commit b0a34c2

Please sign in to comment.