Skip to content

Commit

Permalink
#5 - testing configuration options (#10)
Browse files Browse the repository at this point in the history
* #5 - testing configuration options

* #5 - unpacking options

* #5 - changing Ubuntu to 20.04

* #5 - changing blumilksoftware/php to 8.0.2.1
  • Loading branch information
krzysztofrewak authored Feb 24, 2021
1 parent d35147f commit 57206c9
Show file tree
Hide file tree
Showing 16 changed files with 480 additions and 33 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "Checking the package: testing and linting"

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2

- name: Validate composer.json
run: composer validate

- name: Run the Docker containers
run: docker-compose up -d

- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install dependencies
run: docker-compose run php composer install --prefer-dist --no-progress --no-suggest

- name: Run code style checker
run: docker-compose run php composer ecs

- name: Run tests
run: docker-compose run php composer test
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
}
},
"scripts": {
"ecs": "./vendor/bin/ecs check"
"ecs": "./vendor/bin/ecs check",
"ecs-fix": "./vendor/bin/ecs check --fix",
"test": "./vendor/bin/phpunit tests"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
}
}
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3.7"

services:
php:
image: ghcr.io/blumilksoftware/php:8.0.1.1
image: ghcr.io/blumilksoftware/php:8.0.2.1
container_name: blumilk-codestyle-php
working_dir: /application
user: ${CURRENT_UID}
Expand Down
22 changes: 18 additions & 4 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Blumilk\Codestyle\Configuration\Paths;
use Blumilk\Codestyle\Configuration\SetLists;
use Blumilk\Codestyle\Configuration\SkippedRules;
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator as Container;
use Symplify\EasyCodingStandard\ValueObject\Option;
Expand All @@ -38,10 +39,7 @@ public function __construct(

public function config(): callable
{
$paths = $this->paths->get();
$sets = $this->sets->get();
$skipped = $this->skipped->get();
$rules = $this->rules->get();
list("paths" => $paths, "sets" => $sets, "skipped" => $skipped, "rules" => $rules) = $this->options();

return static function (Container $container) use ($sets, $skipped, $rules, $paths): void {
$parameters = $container->parameters();
Expand All @@ -58,4 +56,20 @@ public function config(): callable
}
};
}

#[ArrayShape([
"paths" => "array",
"sets" => "array",
"skipped" => "array",
"rules" => "array",
])]
public function options(): array
{
return [
"paths" => $this->paths->get(),
"sets" => $this->sets->get(),
"skipped" => $this->skipped->get(),
"rules" => $this->rules->get(),
];
}
}
7 changes: 1 addition & 6 deletions src/Configuration/Defaults/CommonAdditionalRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpCsFixer\Fixer\CastNotation\CastSpacesFixer;
use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer;

class CommonAdditionalRules implements AdditionalRules
class CommonAdditionalRules extends Rules implements AdditionalRules
{
protected array $rules = [
DeclareStrictTypesFixer::class => null,
Expand All @@ -18,9 +18,4 @@ class CommonAdditionalRules implements AdditionalRules
],
DoubleQuoteFixer::class => null,
];

public function get(): array
{
return $this->rules;
}
}
31 changes: 31 additions & 0 deletions src/Configuration/Defaults/CommonSetLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,35 @@ public function get(): array
{
return $this->setLists;
}

public function add(string ...$setLists): self
{
foreach ($setLists as $setList) {
if (!in_array($setList, $this->setLists, true)) {
$this->setLists[] = $setList;
}
}

return $this;
}

public function clear(): self
{
$this->setLists = [];

return $this;
}

public function filter(string ...$setLists): self
{
foreach ($this->setLists as $index => $setList) {
if (in_array($setList, $setLists, true)) {
unset($this->setLists[$index]);
}
}

$this->setLists = array_values($this->setLists);

return $this;
}
}
7 changes: 1 addition & 6 deletions src/Configuration/Defaults/CommonSkippedRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PhpCsFixer\Fixer\ReturnNotation\ReturnAssignmentFixer;
use PhpCsFixer\Fixer\StringNotation\SingleQuoteFixer;

class CommonSkippedRules implements SkippedRules
class CommonSkippedRules extends Rules implements SkippedRules
{
protected array $rules = [
SingleQuoteFixer::class => null,
Expand All @@ -20,9 +20,4 @@ class CommonSkippedRules implements SkippedRules
ReturnAssignmentFixer::class => null,
BinaryOperatorSpacesFixer::class => null,
];

public function get(): array
{
return $this->rules;
}
}
2 changes: 2 additions & 0 deletions src/Configuration/Defaults/Paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function filter(string ...$paths): self
}
}

$this->paths = array_values($this->paths);

return $this;
}
}
46 changes: 46 additions & 0 deletions src/Configuration/Defaults/Rules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Blumilk\Codestyle\Configuration\Defaults;

use Blumilk\Codestyle\Configuration\Utils\Rule;

class Rules
{
protected array $rules = [];

public function get(): array
{
return $this->rules;
}

public function add(Rule ...$rules): self
{
foreach ($rules as $rule) {
if (!in_array($rule->getFixerClassName(), array_keys($this->rules), true)) {
$this->rules[$rule->getFixerClassName()] = $rule->getOptions();
}
}

return $this;
}

public function clear(): self
{
$this->rules = [];

return $this;
}

public function filter(string ...$rules): self
{
foreach (array_keys($this->rules) as $rule) {
if (in_array($rule, $rules, true)) {
unset($this->rules[$rule]);
}
}

return $this;
}
}
27 changes: 27 additions & 0 deletions src/Configuration/Utils/Rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Blumilk\Codestyle\Configuration\Utils;

class Rule
{
protected string $fixer;
protected ?array $options;

public function __construct(string $fixer, ?array $options = null)
{
$this->fixer = $fixer;
$this->options = $options;
}

public function getFixerClassName(): string
{
return $this->fixer;
}

public function getOptions(): ?array
{
return $this->options;
}
}
87 changes: 87 additions & 0 deletions tests/AdditionalRulesConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

use Blumilk\Codestyle\Config;
use Blumilk\Codestyle\Configuration\Defaults\CommonAdditionalRules;
use Blumilk\Codestyle\Configuration\Utils\Rule;
use Blumilk\Codestyle\Fixers\DoubleQuoteFixer;
use PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer;
use PhpCsFixer\Fixer\CastNotation\CastSpacesFixer;
use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer;
use PhpCsFixer\Fixer\StringNotation\HeredocToNowdocFixer;
use PHPUnit\Framework\TestCase;

class AdditionalRulesConfigurationTest extends TestCase
{
public function testAdditionalRulesConfiguration(): void
{
$rules = new CommonAdditionalRules();
$config = new Config(rules: $rules);

$this->assertSame([
DeclareStrictTypesFixer::class => null,
CastSpacesFixer::class => [
"space" => "none",
],
DoubleQuoteFixer::class => null,
], $config->options()["rules"]);
}

public function testClearingAdditionalRulesConfiguration(): void
{
$rules = new CommonAdditionalRules();
$config = new Config(rules: $rules->clear());

$this->assertSame([], $config->options()["rules"]);
}

public function testFilteringAdditionalRulesConfiguration(): void
{
$rules = new CommonAdditionalRules();
$config = new Config(rules: $rules->filter(CastSpacesFixer::class));

$this->assertSame([
DeclareStrictTypesFixer::class => null,
DoubleQuoteFixer::class => null,
], $config->options()["rules"]);
}

public function testExtendingAdditionalRulesConfiguration(): void
{
$rules = new CommonAdditionalRules();
$config = new Config(
rules: $rules->add(new Rule(HeredocToNowdocFixer::class))
);

$this->assertSame([
DeclareStrictTypesFixer::class => null,
CastSpacesFixer::class => [
"space" => "none",
],
DoubleQuoteFixer::class => null,
HeredocToNowdocFixer::class => null,
], $config->options()["rules"]);
}

public function testExtendingWithOptionsAdditionalRulesConfiguration(): void
{
$rules = new CommonAdditionalRules();
$config = new Config(
rules: $rules->add(new Rule(NoMixedEchoPrintFixer::class, [
"use" => "echo",
]))
);

$this->assertSame([
DeclareStrictTypesFixer::class => null,
CastSpacesFixer::class => [
"space" => "none",
],
DoubleQuoteFixer::class => null,
NoMixedEchoPrintFixer::class => [
"use" => "echo",
],
], $config->options()["rules"]);
}
}
36 changes: 36 additions & 0 deletions tests/LaravelPathsConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

use Blumilk\Codestyle\Config;
use Blumilk\Codestyle\Configuration\Defaults\LaravelPaths;
use PHPUnit\Framework\TestCase;

class LaravelPathsConfigurationTest extends TestCase
{
public function testLaravelPathsConfiguration(): void
{
$paths = new LaravelPaths();
$config = new Config(paths: $paths);

$this->assertSame(["app", "config", "database", "resources/lang", "routes", "tests"],
$config->options()["paths"]);
}

public function testFilteredLaravelPathsConfiguration(): void
{
$paths = new LaravelPaths();
$config = new Config(paths: $paths->filter("resources/lang"));

$this->assertSame(["app", "config", "database", "routes", "tests"],
$config->options()["paths"]);
}

public function testClearedLaravelPathsConfiguration(): void
{
$paths = new LaravelPaths();
$config = new Config(paths: $paths->clear()->add("src"));

$this->assertSame(["src"], $config->options()["paths"]);
}
}
Loading

0 comments on commit 57206c9

Please sign in to comment.