/** * create a bootstrapper to mock file system: * the file mentionned in the mutation will be virtualized in the application and replaced * with own mutated file * * @param MutationInterface $mutation * @return string */ public function createFileSystemMock(MutationInterface $mutation) { // temporary file $temporaryFile = tempnam(sys_get_temp_dir(), 'mutate-mock'); file_put_contents($temporaryFile, $mutation->getTokens()->asString()); // mocking system $bootstrapContent = '' . file_get_contents(__DIR__ . '/../../StreamWrapper/FileMutator.php') . "\n \\Hal\\MutaTesting\\StreamWrapper\\FileMutator::initialize();" . sprintf("\n \\Hal\\MutaTesting\\StreamWrapper\\FileMutator::addMutatedFile('%s', '%s'); ?>", $mutation->getSourceFile(), $temporaryFile); $bootstrapFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'bootstrap-' . md5(uniqid()) . '.php'; file_put_contents($bootstrapFile, $bootstrapContent); return $bootstrapFile; }
public function mutate(MutationInterface $original, $index) { $token = $original->getTokens()->offsetGet($index); if ($token->getType() !== T_ELSE) { throw new \UnexpectedValueException(sprintf('invalid token "%s" given in %s', token_name($token->getType()), get_class($this))); } // look for the closing bracket $tokens = $original->getTokens(); $len = $tokens->count(); $end = false; for ($i = $index; $i < $len; $i++) { $token = $tokens->offsetGet($i); if ($token->getType() === T_STRING && $token->getValue() === '}') { $end = $i; break; } } if (false === $end) { throw new \OutOfRangeException('closing bracket not found for else'); } // remove all concerned tokens $tokens = $tokens->remove($index, $end); $new = new \Hal\MutaTesting\Mutation\Mutation(); $new->setTokens($tokens)->setUnit($original->getUnit())->setSourceFile($original->getSourceFile())->setTestFile($original->getTestFile())->setMutedTokensIndexes(array_merge($original->getMutedTokensIndexes(), range($index, $end))); return $new; }
protected function mutateOne(MutationInterface $original, $index, $expected, Token $newToken) { $token = $original->getTokens()->offsetGet($index); if ($token->getType() !== $expected) { throw new \UnexpectedValueException(sprintf('invalid token "%s" given in %s', token_name($token->getType()), get_class($this))); } $new = new \Hal\MutaTesting\Mutation\Mutation(); $new->setTokens($original->getTokens()->replace($index, $newToken))->setUnit($original->getUnit())->setSourceFile($original->getSourceFile())->setTestFile($original->getTestFile())->setMutedTokensIndexes(array_merge($original->getMutedTokensIndexes(), array($index))); return $new; }
public function isSatisfedBy(MutationInterface $mutation, $index) { $filename = $mutation->getSourceFile(); // // Avoid to make same mutations if (!isset($this->alreadyMuted[$filename])) { $this->alreadyMuted[$filename] = array(); } foreach ($mutation->getMutedTokensIndexes() as $index) { if (isset($this->alreadyMuted[$filename][$index])) { return false; } $this->alreadyMuted[$filename][$index] = 1; } // // keep only complex files if (!isset($this->notes[$filename])) { $bugs = $this->halstead->calculate($filename)->getBugs(); $this->notes[$filename] = $bugs; } return $this->notes[$filename] > $this->limit; }