Skip to content

Commit

Permalink
#24 - testing configured ECS behavior (#29)
Browse files Browse the repository at this point in the history
* #24 - testing configured ECS behavior

* #24 - ecsf

* Update tests/codestyle/CodestyleTest.php

Co-authored-by: Jacek Sawoszczuk <jacek.sawoszczuk@gmail.com>

* #24 - after-cr changes

Co-authored-by: Jacek Sawoszczuk <jacek.sawoszczuk@gmail.com>
  • Loading branch information
krzysztofrewak and jsawo authored Jul 27, 2021
1 parent 494cbdb commit e4868bc
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 12 deletions.
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"symplify/easy-coding-standard": "9.3.12"
},
"require-dev": {
"composer/composer": "2.*",
"jetbrains/phpstorm-attributes": "^1.0",
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"symfony/console": "^5.3"
},
"authors": [
{
Expand All @@ -29,6 +31,10 @@
"scripts": {
"ecs": "./vendor/bin/ecs check",
"ecsf": "./vendor/bin/ecs check --fix",
"test": "./vendor/bin/phpunit tests --colors always"
"test": "./vendor/bin/phpunit tests --colors always",
"unit": "./vendor/bin/phpunit tests/unit --colors always",
"e2e": "./vendor/bin/phpunit tests/codestyle --colors always",
"ecs-tmp": "./vendor/bin/ecs check --config ./tests/codestyle/config.php",
"ecsf-tmp": "./vendor/bin/ecs check --config ./tests/codestyle/config.php --fix"
}
}
2 changes: 1 addition & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Blumilk\Codestyle\Configuration\Defaults\Paths;

$config = new Config(
paths: new Paths("examples", "src", "tests")
paths: new Paths("src", "tests/unit", "tests/codestyle/CodestyleTest.php")
);

return $config->config();
5 changes: 0 additions & 5 deletions examples/arrow_functions.php

This file was deleted.

64 changes: 64 additions & 0 deletions tests/codestyle/CodestyleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

class CodestyleTest extends TestCase
{
protected function setUp(): void
{
$this->clearTempDirectory();
}

protected function tearDown(): void
{
$this->clearTempDirectory();
}

/**
* @throws Exception
*/
public function testFixtures(): void
{
$fixtures = scandir(__DIR__ . "/fixtures");
$fixtures = array_filter($fixtures, fn(string $dir): bool => !str_contains($dir, "."));

foreach ($fixtures as $fixture) {
$this->testFixture($fixture);
}
}

/**
* @throws Exception
*/
protected function runComposerEcsCommand(bool $fix = false): bool
{
$command = $fix ? "ecsf-tmp" : "ecs-tmp";
$result = 0;
$output = null;

exec("./vendor/bin/composer " . $command, $output, $result);

return $result === 0;
}

/**
* @throws Exception
*/
protected function testFixture(string $name): void
{
copy(__DIR__ . "/fixtures/${name}/actual.php", __DIR__ . "/tmp/${name}.php");
$this->assertFalse($this->runComposerEcsCommand());
$this->assertTrue($this->runComposerEcsCommand(true));
$this->assertFileEquals(__DIR__ . "/fixtures/${name}/expected.php", __DIR__ . "/tmp/${name}.php");
}

protected function clearTempDirectory(): void
{
$files = glob(__DIR__ . "/tmp/*.php");
foreach ($files as $file) {
unlink($file);
}
}
}
12 changes: 12 additions & 0 deletions tests/codestyle/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Blumilk\Codestyle\Config;
use Blumilk\Codestyle\Configuration\Defaults\Paths;

$config = new Config(
paths: new Paths("./tests/codestyle/tmp")
);

return $config->config();
8 changes: 8 additions & 0 deletions tests/codestyle/fixtures/singleQuotes/actual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

echo 'Test';

$array = [];
$array['test'] = 'test';
8 changes: 8 additions & 0 deletions tests/codestyle/fixtures/singleQuotes/expected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

echo "Test";

$array = [];
$array["test"] = "test";
5 changes: 5 additions & 0 deletions tests/codestyle/fixtures/strictTypes/actual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class StrictTypesExample
{
}
7 changes: 7 additions & 0 deletions tests/codestyle/fixtures/strictTypes/expected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

class StrictTypesExample
{
}
11 changes: 11 additions & 0 deletions tests/codestyle/fixtures/unionTypes/actual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class UnionTypesExample
{
public int|string $something;

public function do(): void {
$i = 1+1;
json_encode([$i], JSON_THROW_ON_ERROR|JSON_UNESCAPED_SLASHES);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

declare(strict_types=1);

class Whatever
class UnionTypesExample
{
public int|string $something;

/**
* @throws JsonException
*/
public function do(): void
{
$i = 1 + 1;
Expand Down
2 changes: 2 additions & 0 deletions tests/codestyle/tmp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e4868bc

Please sign in to comment.