Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure mutated code is always valid #301

Merged
merged 2 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Member

@sanmai sanmai Apr 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proc_open seems like a good fit here; there will be more LOC but less syscalls

I'd try with eval_fork first, though.


$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()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this change, this mutator was failing with PHP Fatal error: The "yield" expression can only be used inside a function in /tmp/XXX during linting

{
(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