From 851b5125b9ec1c651144058caf7aae82e1431878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 1 Nov 2020 19:31:17 +0100 Subject: [PATCH] Enhancement: Extract concrete class --- psalm-baseline.xml | 6 - test/Fixture/Config/RuleSet/DummyRuleSet.php | 59 +++++++++ test/Unit/FactoryTest.php | 127 +++---------------- 3 files changed, 78 insertions(+), 114 deletions(-) create mode 100644 test/Fixture/Config/RuleSet/DummyRuleSet.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index bf9900a4..a12a980f 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,11 +1,5 @@ - - - $rules - $rules - - FixerFactory::create() diff --git a/test/Fixture/Config/RuleSet/DummyRuleSet.php b/test/Fixture/Config/RuleSet/DummyRuleSet.php new file mode 100644 index 00000000..c5b3bd9f --- /dev/null +++ b/test/Fixture/Config/RuleSet/DummyRuleSet.php @@ -0,0 +1,59 @@ + + */ + private $rules; + + /** + * @var int + */ + private $phpVersion; + + /** + * @param array $rules + */ + public function __construct(string $name, array $rules, int $phpVersion) + { + $this->name = $name; + $this->rules = $rules; + $this->phpVersion = $phpVersion; + } + + public function name(): string + { + return $this->name; + } + + public function rules(): array + { + return $this->rules; + } + + public function targetPhpVersion(): int + { + return $this->phpVersion; + } +} diff --git a/test/Unit/FactoryTest.php b/test/Unit/FactoryTest.php index 50d568d0..7f426ea2 100644 --- a/test/Unit/FactoryTest.php +++ b/test/Unit/FactoryTest.php @@ -14,6 +14,8 @@ namespace Ergebnis\PhpCsFixer\Config\Test\Unit; use Ergebnis\PhpCsFixer\Config; +use Ergebnis\PhpCsFixer\Config\Test\Fixture; +use Ergebnis\Test\Util; use PHPUnit\Framework; /** @@ -23,36 +25,17 @@ */ final class FactoryTest extends Framework\TestCase { + use Util\Helper; + public function testFromRuleSetThrowsRuntimeExceptionIfCurrentPhpVersionIsLessThanTargetPhpVersion(): void { $targetPhpVersion = \PHP_VERSION_ID + 1; - $ruleSet = new class($targetPhpVersion) implements Config\RuleSet { - /** - * @var int - */ - private $phpVersion; - - public function __construct(int $phpVersion) - { - $this->phpVersion = $phpVersion; - } - - public function name(): string - { - return \spl_object_hash($this); - } - - public function rules(): array - { - return []; - } - - public function targetPhpVersion(): int - { - return $this->phpVersion; - } - }; + $ruleSet = new Fixture\Config\RuleSet\DummyRuleSet( + self::faker()->word, + [], + $targetPhpVersion + ); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage(\sprintf( @@ -69,8 +52,6 @@ public function targetPhpVersion(): int */ public function testFromRuleSetCreatesConfig(int $targetPhpVersion): void { - $name = 'foobarbaz'; - $rules = [ 'foo' => true, 'bar' => [ @@ -78,44 +59,11 @@ public function testFromRuleSetCreatesConfig(int $targetPhpVersion): void ], ]; - $ruleSet = new class($name, $rules, $targetPhpVersion) implements Config\RuleSet { - /** - * @var string - */ - private $name; - - /** - * @var array - */ - private $rules; - - /** - * @var int - */ - private $phpVersion; - - public function __construct(string $name, array $rules, int $phpVersion) - { - $this->name = $name; - $this->rules = $rules; - $this->phpVersion = $phpVersion; - } - - public function name(): string - { - return $this->name; - } - - public function rules(): array - { - return $this->rules; - } - - public function targetPhpVersion(): int - { - return $this->phpVersion; - } - }; + $ruleSet = new Fixture\Config\RuleSet\DummyRuleSet( + self::faker()->word, + $rules, + $targetPhpVersion + ); $config = Config\Factory::fromRuleSet($ruleSet); @@ -143,8 +91,6 @@ public function provideTargetPhpVersion(): \Generator public function testFromRuleSetCreatesConfigWithOverrideRules(): void { - $name = 'foobarbaz'; - $rules = [ 'foo' => true, 'bar' => [ @@ -152,46 +98,11 @@ public function testFromRuleSetCreatesConfigWithOverrideRules(): void ], ]; - $targetPhpVersion = \PHP_VERSION_ID; - - $ruleSet = new class($name, $rules, $targetPhpVersion) implements Config\RuleSet { - /** - * @var string - */ - private $name; - - /** - * @var array - */ - private $rules; - - /** - * @var int - */ - private $phpVersion; - - public function __construct(string $name, array $rules, int $phpVersion) - { - $this->name = $name; - $this->rules = $rules; - $this->phpVersion = $phpVersion; - } - - public function name(): string - { - return $this->name; - } - - public function rules(): array - { - return $this->rules; - } - - public function targetPhpVersion(): int - { - return $this->phpVersion; - } - }; + $ruleSet = new Fixture\Config\RuleSet\DummyRuleSet( + self::faker()->word, + $rules, + \PHP_VERSION_ID + ); $overrideRules = [ 'foo' => false,