Skip to content

Commit

Permalink
integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Jul 27, 2024
1 parent 1200618 commit 9548dde
Show file tree
Hide file tree
Showing 14 changed files with 1,826 additions and 1 deletion.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,28 @@ jobs:
composer update --prefer-dist --no-interaction --no-progress ${{ matrix.flags }}
- name: Execute tests
run: ./vendor/bin/phpunit

integration:
runs-on: ubuntu-latest
strategy:
fail-fast: false

name: integration
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
coverage: xdebug
- name: Install dependencies
uses: nick-fields/retry@v3
with:
timeout_minutes: 5
max_attempts: 5
command: |
composer -V
composer update --prefer-dist --no-interaction --no-progress ${{ matrix.flags }}
- name: Execute tests
run: ./vendor/bin/phpunit -c phpunit.integration.xml
3 changes: 2 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

$finder = Finder::base()
->in(__DIR__)
->append(['.php-cs-fixer.dist.php', 'bin/relax']);
->append(['.php-cs-fixer.dist.php', 'bin/relax'])
->notName('*_actual.php');

return Config::create('relax')
->setRules($localRules)
Expand Down
6 changes: 6 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
"command": "./vendor/bin/phpunit",
"problemMatcher": []
},
{
"label": "[Tests] PHPUnit (integration)",
"type": "shell",
"command": "./vendor/bin/phpunit -c phpunit.integration.xml",
"problemMatcher": []
},
{
"label": "[Tests] PHPUnit (coverage-html)",
"type": "shell",
Expand Down
14 changes: 14 additions & 0 deletions phpunit.integration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".tmp/PHPUnit/.phpunit.result.cache"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Codestyle</directory>
</testsuite>
</testsuites>
</phpunit>
90 changes: 90 additions & 0 deletions tests/Codestyle/CodestyleTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Realodix\Relax\Tests\Codestyle;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\StringInput;

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

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

/**
* @throws \Exception
*/
protected function testFixture(string $name, ?string $suffix = null): void
{
$fileName = $name;

if (! is_null($suffix)) {
$fileName = $fileName.'-'.$suffix;
}

copy(__DIR__."/../Fixtures/Ruleset/{$fileName}_actual.php", __DIR__."/tmp/{$fileName}.php");

$this->assertTrue(
file_exists(__DIR__."/tmp/{$fileName}.php"),
"Fixture fixtures/{$fileName} does not exist.",
);

$this->assertTrue(
is_writable(__DIR__."/tmp/{$fileName}.php"),
"Fixture fixtures/{$fileName} is not writable.",
);

$this->assertFalse(
$this->runFixer($name),
"Fixture fixtures/{$fileName} returned invalid true result.",
);

$this->assertTrue(
$this->runFixer($name, true),
"Fixture fixtures/{$fileName} was not proceeded properly.",
);

$this->assertFileEquals(
__DIR__."/../Fixtures/Ruleset/{$fileName}_expected.php",
__DIR__."/tmp/{$fileName}.php",
"Result of proceeded fixture fixtures/{$fileName} is not equal to expected.",
);
}

/**
* @throws \Exception
*/
protected function runFixer(string $name, bool $fix = false): bool
{
$dryRun = $fix ? '' : '--dry-run';

$application = new \PhpCsFixer\Console\Application;
$application->setAutoExit(false);

$output = new \Symfony\Component\Console\Output\BufferedOutput;
$config = "tests/Codestyle/Config/config_{$name}.php";
$result = $application->run(new StringInput("fix {$dryRun} --diff --config={$config} --quiet"), $output);

return $result === 0;
}

protected function clearTempDirectory(): void
{
$files = glob(__DIR__.'/tmp/*.php');

foreach ($files as $file) {
unlink($file);
}
}

protected function getConfigPath(): string
{
return 'tests/codestyle/config/config_relax.php';
}
}
10 changes: 10 additions & 0 deletions tests/Codestyle/Config/config_relax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use PhpCsFixer\Finder;
use Realodix\Relax\Config;

$finder = (new Finder)->in('./tests/Codestyle/tmp');

return Config::create('relax')
->setFinder($finder)
->setUsingCache(false);
21 changes: 21 additions & 0 deletions tests/Codestyle/RelaxRulesetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Realodix\Relax\Tests\Codestyle;

class RelaxRulesetTest extends CodestyleTestCase
{
public function testRelaxRuleset(): void
{
$this->testFixture('relax');
}

public function testRelaxCustom(): void
{
$this->testFixture('relax', 'custom');
}

public function testRelaxCleanup(): void
{
$this->testFixture('relax', 'deprecated');
}
}
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
50 changes: 50 additions & 0 deletions tests/Fixtures/Ruleset/relax-custom_actual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace Realodix\Relax;

use DateTime;

/**
* FooRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class RelaxCustom
{
/**
* @var RelaxCustom PhpdocSelfAccessorFixer
*/
private $instance;

/**
* @var array<int,string>
*/
private $phpdocTypesCommaSpacesFixer;

/** Hello
* World!
*/
public function multilineCommentOpeningClosingAloneFixer() {}

public function noImportFromGlobalNamespaceFixer(DateTime $dateTime) {}

/**
* Created by PhpStorm.
* User: root * Date: 01.01.70
* Time: 12:00
*/
public function noPhpStormGeneratedCommentFixer() {}

/**
* @param bool $b
* @param int $i
* @param string $s this is string
* @param string $s duplicated
*/
public function phpdocNoSuperfluousParamFixer() {}

/**
* @param null | string $x
*/
public function phpdocTypesTrimFixer($x) {}
}
33 changes: 33 additions & 0 deletions tests/Fixtures/Ruleset/relax-custom_expected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Realodix\Relax;

class RelaxCustom
{
/**
* @var self PhpdocSelfAccessorFixer
*/
private $instance;

/**
* @var array<int, string>
*/
private $phpdocTypesCommaSpacesFixer;

/**
* Hello
* World!
*/
public function multilineCommentOpeningClosingAloneFixer() {}

public function noImportFromGlobalNamespaceFixer(\DateTime $dateTime) {}

public function noPhpStormGeneratedCommentFixer() {}

public function phpdocNoSuperfluousParamFixer() {}

/**
* @param null|string $x
*/
public function phpdocTypesTrimFixer($x) {}
}
23 changes: 23 additions & 0 deletions tests/Fixtures/Ruleset/relax-deprecated_actual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Realodix\Relax;

class RelaxDeprecatedSets extends Config
{
public function cast_notation()
{
$b = 2;

// no_unset_cast
$a = (unset) $b;
}

public function string_notation__simple_to_complex_string_variable()
{
$name = 'World';

echo "Hello ${name}!";
echo <<<TEST
-Hello ${name}!
TEST;
}
}
24 changes: 24 additions & 0 deletions tests/Fixtures/Ruleset/relax-deprecated_expected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Realodix\Relax;

class RelaxDeprecatedSets extends Config
{
public function cast_notation()
{
$b = 2;

// no_unset_cast
$a = null;
}

public function string_notation__simple_to_complex_string_variable()
{
$name = 'World';

echo "Hello {$name}!";
echo <<<TEST
-Hello {$name}!
TEST;
}
}
Loading

0 comments on commit 9548dde

Please sign in to comment.