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; }
public function testICanObtainTokens() { $code = '<?php echo ok;'; $mutation = new \Hal\MutaTesting\Mutation\Mutation(); $mutation->setTokens(new \Hal\Component\Token\TokenCollection(token_get_all($code))); $tokens = $mutation->getTokens(); $this->assertEquals(5, sizeof($tokens->asArray())); }
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; }