protected function doTest($expected, $input = null, \SplFileInfo $file = null)
{
if ($expected === $input) {
throw new \InvalidArgumentException('Input parameter must not be equal to expected parameter.');
}
$file = $file ?: $this->getTestFile();
$fileIsSupported = $this->fixer->supports($file);
if (null !== $input) {
$this->assertNull($this->lintSource($input));
Tokens::clearCache();
$tokens = Tokens::fromCode($input);
if ($fileIsSupported) {
$this->assertTrue($this->fixer->isCandidate($tokens), 'Fixer must be a candidate for input code.');
$fixResult = $this->fixer->fix($file, $tokens);
$this->assertNull($fixResult, '->fix method must return null.');
}
$this->assertThat($tokens->generateCode(), new SameStringsConstraint($expected), 'Code build on input code must match expected code.');
$this->assertTrue($tokens->isChanged(), 'Tokens collection built on input code must be marked as changed after fixing.');
$tokens->clearEmptyTokens();
$this->assertSame(count($tokens), count(array_unique(array_map(function (Token $token) {
return spl_object_hash($token);
}, $tokens->toArray()))), 'Token items inside Tokens collection must be unique.');
Tokens::clearCache();
$expectedTokens = Tokens::fromCode($expected);
$this->assertTokens($expectedTokens, $tokens);
}
$this->assertNull($this->lintSource($expected));
Tokens::clearCache();
$tokens = Tokens::fromCode($expected);
$isCandidate = $this->fixer->isCandidate($tokens);
$this->assertFalse($tokens->isChanged(), 'Fixer should not touch Tokens on candidate check.');
if (!$isCandidate) {
return;
}
if ($fileIsSupported) {
$fixResult = $this->fixer->fix($file, $tokens);
$this->assertNull($fixResult, '->fix method must return null.');
}
$this->assertThat($tokens->generateCode(), new SameStringsConstraint($expected), 'Code build on expected code must not change.');
$this->assertFalse($tokens->isChanged(), 'Tokens collection built on expected code must not be marked as changed after fixing.');
}