|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPRealCoverage\Mutator; |
|
4
|
|
|
|
|
5
|
|
|
use PHPRealCoverage\Mutator\Exception\NoMoreMutationsException; |
|
6
|
|
|
|
|
7
|
|
|
class Mutator |
|
8
|
|
|
{ |
|
9
|
|
|
public function testMutations(MutationTester $tester, MutationGenerator $generator) |
|
10
|
|
|
{ |
|
11
|
|
|
try { |
|
12
|
|
|
while (true) { |
|
13
|
|
|
$progress = $generator->getProgress(); |
|
14
|
|
|
if (!is_null($progress)) { |
|
15
|
|
|
echo "Mutation Progress: " . (int)(100 * $progress) . "%\r"; |
|
16
|
|
|
} |
|
17
|
|
|
$this->iterate($tester, $generator); |
|
18
|
|
|
} |
|
19
|
|
|
} catch (NoMoreMutationsException $e) { |
|
20
|
|
|
// stop when all permutations have been tested |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param MutationCommand[] $commands |
|
26
|
|
|
* @param MutationTester $tester |
|
27
|
|
|
* @return bool |
|
28
|
|
|
*/ |
|
29
|
|
|
private function findWorkingPermutations(array $commands, MutationTester $tester) |
|
30
|
|
|
{ |
|
31
|
|
|
if (empty($commands)) { |
|
32
|
|
|
return false; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$lineToTest = $commands[0]; |
|
36
|
|
|
$lineToTest->execute(); |
|
37
|
|
|
if ($tester->isValid()) { |
|
38
|
|
|
return true; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$rest = array_slice($commands, 1); |
|
42
|
|
|
if ($this->findWorkingPermutations($rest, $tester)) { |
|
43
|
|
|
return true; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$lineToTest->undo(); |
|
47
|
|
|
return $this->findWorkingPermutations($rest, $tester); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param MutationTester $tester |
|
52
|
|
|
* @param MutationGenerator $generator |
|
53
|
|
|
*/ |
|
54
|
|
|
public function iterate(MutationTester $tester, MutationGenerator $generator) |
|
55
|
|
|
{ |
|
56
|
|
|
$commands = $generator->getMutationStack(); |
|
57
|
|
|
$lineToTest = $commands[0]; |
|
58
|
|
|
$lineToTest->execute(); |
|
59
|
|
|
|
|
60
|
|
|
$rest = array_slice($commands, 1); |
|
61
|
|
|
$this->findWorkingPermutations($rest, $tester); |
|
62
|
|
|
if (!$tester->isValid()) { |
|
63
|
|
|
foreach ($commands as $command) { |
|
64
|
|
|
$command->undo(); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|