Skip to content

Commit

Permalink
AbstractProxyFixer - more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum committed Oct 30, 2021
1 parent 9473b38 commit 2f9138f
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/FixerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ public function useRuleSet(RuleSetInterface $ruleSet): self

$fixers[] = $fixer;
$fixersByName[$name] = $fixer;

$conflicts = array_intersect($this->getFixersConflicts($fixer), $fixerNames);

if (\count($conflicts) > 0) {
$fixerConflicts[$name] = $conflicts;
}
Expand Down Expand Up @@ -219,6 +219,7 @@ private function generateConflictMessage(array $fixerConflicts): string
{
$message = 'Rule contains conflicting fixers:';
$report = [];

foreach ($fixerConflicts as $fixer => $fixers) {
// filter mutual conflicts
$report[$fixer] = array_filter(
Expand Down
162 changes: 162 additions & 0 deletions tests/AbstractProxyFixerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Tests;

use PhpCsFixer\AbstractProxyFixer;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tests\Fixtures\Test\AbstractProxyFixerTest\SimpleFixer;
use PhpCsFixer\Tests\Fixtures\Test\AbstractProxyFixerTest\SimpleWhitespacesAwareFixer;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\WhitespacesFixerConfig;

/**
* @internal
*
* @covers \PhpCsFixer\AbstractProxyFixer
*/
final class AbstractProxyFixerTest extends TestCase
{
public function testCandidate(): void
{
$proxyFixer = $this->buildProxyFixer([new SimpleFixer(true)]);
static::assertTrue($proxyFixer->isCandidate(new Tokens()));

$proxyFixer = $this->buildProxyFixer([new SimpleFixer(false)]);
static::assertFalse($proxyFixer->isCandidate(new Tokens()));

$proxyFixer = $this->buildProxyFixer([
new SimpleFixer(false),
new SimpleFixer(true),
]);

static::assertTrue($proxyFixer->isCandidate(new Tokens()));
}

public function testRisky(): void
{
$proxyFixer = $this->buildProxyFixer([new SimpleFixer(true, false)]);
static::assertFalse($proxyFixer->isRisky());

$proxyFixer = $this->buildProxyFixer([new SimpleFixer(true, true)]);
static::assertTrue($proxyFixer->isRisky());

$proxyFixer = $this->buildProxyFixer([
new SimpleFixer(true, false),
new SimpleFixer(true, true),
new SimpleFixer(true, false),
]);

static::assertTrue($proxyFixer->isRisky());
}

public function testSupports(): void
{
$file = new \SplFileInfo(__FILE__);

$proxyFixer = $this->buildProxyFixer([new SimpleFixer(true, false, false)]);
static::assertFalse($proxyFixer->supports($file));

$proxyFixer = $this->buildProxyFixer([new SimpleFixer(true, true, true)]);
static::assertTrue($proxyFixer->supports($file));

$proxyFixer = $this->buildProxyFixer([
new SimpleFixer(true, false, false),
new SimpleFixer(true, true, false),
new SimpleFixer(true, false, true),
]);

static::assertTrue($proxyFixer->supports($file));
}

public function testPrioritySingleFixer(): void
{
$proxyFixer = $this->buildProxyFixer([new SimpleFixer(true, false, false, 123)]);
static::assertSame(123, $proxyFixer->getPriority());
}

public function testPriorityMultipleFixersNotSet(): void
{
$proxyFixer = $this->buildProxyFixer([
new SimpleFixer(true),
new SimpleFixer(true, true),
new SimpleFixer(true, false, true),
]);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('You need to override this method to provide the priority of combined fixers.');

$proxyFixer->getPriority();
}

public function testWhitespacesConfig(): void
{
$config = new WhitespacesFixerConfig();
$whitespacesAwareFixer = new SimpleWhitespacesAwareFixer();

$proxyFixer = $this->buildProxyFixer([
new SimpleFixer(true, true),
$whitespacesAwareFixer,
new SimpleFixer(true, false, true),
]);

$proxyFixer->setWhitespacesConfig($config);

static::assertSame($config, $whitespacesAwareFixer->getWhitespacesFixerConfig());
}

public function testApplyFixInPriorityOrder(): void
{
$fixer1 = new SimpleFixer(true, false, true, 1);
$fixer2 = new SimpleFixer(true, false, true, 10);

$proxyFixer = $this->buildProxyFixer([$fixer1, $fixer2]);
$proxyFixer->fix(new \SplFileInfo(__FILE__), Tokens::fromCode('<?php echo 1;'));

static::assertSame(2, $fixer1->isFixCalled());
static::assertSame(1, $fixer2->isFixCalled());
}

/**
* @param FixerInterface[] $fixers
*/
private function buildProxyFixer(array $fixers): AbstractProxyFixer
{
return new class($fixers) extends AbstractProxyFixer implements WhitespacesAwareFixerInterface {
/**
* @var FixerInterface[]
*/
private $fixers;

public function __construct(array $fixers)
{
$this->fixers = $fixers;

parent::__construct();
}

public function getDefinition(): FixerDefinitionInterface
{
throw new \BadMethodCallException('Not implemented.');
}

protected function createProxyFixers(): array
{
return $this->fixers;
}
};
}
}
1 change: 0 additions & 1 deletion tests/Fixer/Phpdoc/PhpdocNoAccessFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*
* @internal
*
* @covers \PhpCsFixer\AbstractProxyFixer
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocNoAccessFixer
*/
final class PhpdocNoAccessFixerTest extends AbstractFixerTestCase
Expand Down
1 change: 0 additions & 1 deletion tests/Fixer/Phpdoc/PhpdocNoPackageFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*
* @internal
*
* @covers \PhpCsFixer\AbstractProxyFixer
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocNoPackageFixer
*/
final class PhpdocNoPackageFixerTest extends AbstractFixerTestCase
Expand Down
10 changes: 5 additions & 5 deletions tests/Fixtures/Test/AbstractFixerTest/SimpleFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
*/
final class SimpleFixer extends AbstractFixer implements WhitespacesAwareFixerInterface
{
public function getWhitespacesConfig(): WhitespacesFixerConfig
public function getDefinition(): FixerDefinitionInterface
{
return $this->whitespacesConfig;
throw new \BadMethodCallException('Not implemented.');
}

public function isCandidate(Tokens $tokens): bool
public function getWhitespacesConfig(): WhitespacesFixerConfig
{
throw new \BadMethodCallException('Not implemented.');
return $this->whitespacesConfig;
}

public function getDefinition(): FixerDefinitionInterface
public function isCandidate(Tokens $tokens): bool
{
throw new \BadMethodCallException('Not implemented.');
}
Expand Down
109 changes: 109 additions & 0 deletions tests/Fixtures/Test/AbstractProxyFixerTest/SimpleFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <[email protected]>
* Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\Tests\Fixtures\Test\AbstractProxyFixerTest;

use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;

/**
* @internal
*/
final class SimpleFixer implements FixerInterface
{
/**
* @var bool
*/
private $isCandidate;

/**
* @var bool
*/
private $isRisky;

/**
* @var bool
*/
private $supports;

/**
* @var int
*/
private $priority;

/**
* @var int
*/
private $fixCalled = 0;

private static $callCount = 1;

public function __construct(
bool $isCandidate,
bool $isRisky = false,
bool $supports = false,
int $priority = 999
) {
$this->isCandidate = $isCandidate;
$this->isRisky = $isRisky;
$this->supports = $supports;
$this->priority = $priority;
}

public function fix(\SplFileInfo $file, Tokens $tokens): void
{
if (0 !== $this->fixCalled) {
throw new \RuntimeException('Fixer called multiple times.');
}

$this->fixCalled = self::$callCount;
++self::$callCount;
}

public function getDefinition(): FixerDefinitionInterface
{
throw new \BadMethodCallException('Not implemented.');
}

public function getName(): string
{
return uniqid('abstract_proxy_test_');
}

public function getPriority(): int
{
return $this->priority;
}

public function isCandidate(Tokens $tokens): bool
{
return $this->isCandidate;
}

public function isFixCalled(): int
{
return $this->fixCalled;
}

public function isRisky(): bool
{
return $this->isRisky;
}

public function supports(\SplFileInfo $file): bool
{
return $this->supports;
}
}
Loading

0 comments on commit 2f9138f

Please sign in to comment.