Skip to content

Commit

Permalink
Enhancement: Extract concrete class
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Nov 1, 2020
1 parent 733fdae commit 851b512
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 114 deletions.
6 changes: 0 additions & 6 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="3.18.2@19aa905f7c3c7350569999a93c40ae91ae4e1626">
<file src="test/Unit/FactoryTest.php">
<MixedPropertyTypeCoercion occurrences="2">
<code>$rules</code>
<code>$rules</code>
</MixedPropertyTypeCoercion>
</file>
<file src="test/Unit/RuleSet/AbstractRuleSetTestCase.php">
<InternalClass occurrences="2">
<code>FixerFactory::create()</code>
Expand Down
59 changes: 59 additions & 0 deletions test/Fixture/Config/RuleSet/DummyRuleSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019-2020 Andreas M枚ller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/

namespace Ergebnis\PhpCsFixer\Config\Test\Fixture\Config\RuleSet;

use Ergebnis\PhpCsFixer\Config;

final class DummyRuleSet implements Config\RuleSet
{
/**
* @var string
*/
private $name;

/**
* @var array<string, array|bool>
*/
private $rules;

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

/**
* @param array<string, array|bool> $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;
}
}
127 changes: 19 additions & 108 deletions test/Unit/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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(
Expand All @@ -69,53 +52,18 @@ public function targetPhpVersion(): int
*/
public function testFromRuleSetCreatesConfig(int $targetPhpVersion): void
{
$name = 'foobarbaz';

$rules = [
'foo' => true,
'bar' => [
'baz',
],
];

$ruleSet = new class($name, $rules, $targetPhpVersion) implements Config\RuleSet {
/**
* @var string
*/
private $name;

/**
* @var array<string, array|bool>
*/
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);

Expand Down Expand Up @@ -143,55 +91,18 @@ public function provideTargetPhpVersion(): \Generator

public function testFromRuleSetCreatesConfigWithOverrideRules(): void
{
$name = 'foobarbaz';

$rules = [
'foo' => true,
'bar' => [
'baz',
],
];

$targetPhpVersion = \PHP_VERSION_ID;

$ruleSet = new class($name, $rules, $targetPhpVersion) implements Config\RuleSet {
/**
* @var string
*/
private $name;

/**
* @var array<string, array|bool>
*/
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,
Expand Down

0 comments on commit 851b512

Please sign in to comment.