Skip to content

Commit

Permalink
Enhancement: Run end-to-end tests
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 2, 2023
1 parent 60117fb commit 19ae4dc
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/integrate.yaml
Expand Up @@ -311,3 +311,6 @@ jobs:

- name: "Run unit tests with phpunit/phpunit"
run: "vendor/bin/phpunit --colors=always --configuration=test/Unit/phpunit.xml"

- name: "Run end-to-end tests with phpunit/phpunit"
run: "vendor/bin/phpunit --colors=always --configuration=test/EndToEnd/phpunit.xml"
3 changes: 2 additions & 1 deletion Makefile
Expand Up @@ -32,9 +32,10 @@ static-code-analysis-baseline: vendor ## Generates a baseline for static code an
vendor/bin/psalm --config=psalm.xml --set-baseline=psalm-baseline.xml

.PHONY: tests
tests: vendor ## Runs unit tests with phpunit/phpunit
tests: vendor ## Runs unit and end-to-end tests with phpunit/phpunit
mkdir -p .build/phpunit
vendor/bin/phpunit --configuration=test/Unit/phpunit.xml
vendor/bin/phpunit --configuration=test/EndToEnd/phpunit.xml

vendor: composer.json composer.lock
composer validate --strict
Expand Down
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Expand Up @@ -10,6 +10,11 @@
<code>$rules</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="test/EndToEnd/RuleSet/AbstractRuleSetTestCase.php">
<InternalClass occurrences="1">
<code>Console\Command\FixCommandExitStatusCalculator::EXIT_STATUS_FLAG_HAS_INVALID_CONFIG</code>
</InternalClass>
</file>
<file src="test/Unit/RuleSet/AbstractRuleSetTestCase.php">
<InternalClass occurrences="1">
<code>new FixerFactory()</code>
Expand Down
151 changes: 151 additions & 0 deletions test/EndToEnd/RuleSet/AbstractRuleSetTestCase.php
@@ -0,0 +1,151 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019-2023 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\EndToEnd\RuleSet;

use PhpCsFixer\Console;
use PHPUnit\Framework;
use Symfony\Component\Filesystem;
use Symfony\Component\Process;

/**
* @internal
*/
abstract class AbstractRuleSetTestCase extends Framework\TestCase
{
final protected function setUp(): void
{
self::fileSystem()->mkdir(self::temporaryDirectory());

self::fileSystem()->dumpFile(
self::configPath(),
self::configContents(),
);
}

final protected function tearDown(): void
{
self::fileSystem()->remove(self::temporaryDirectory());
}

final public function testConfigurationIsConsideredValid(): void
{
$process = new Process\Process([
'vendor/bin/php-cs-fixer',
'fix',
\sprintf(
'--config=%s',
self::configPath(),
),
'--dry-run',
]);

$process->run();

$exitCode = $process->getExitCode();

self::assertNotNull($exitCode);
self::assertSame(0, Console\Command\FixCommandExitStatusCalculator::EXIT_STATUS_FLAG_HAS_INVALID_CONFIG & $exitCode, \sprintf(
'Failed asserting that the configuration in "%s" is considered valid by friendsofphp/php-cs-fixer.',
self::className(),
));
}

private static function fileSystem(): Filesystem\Filesystem
{
return new Filesystem\Filesystem();
}

private static function configPath(): string
{
return \sprintf(
'%s/.php-cs-fixer.php',
self::temporaryDirectory(),
);
}

private static function configContents(): string
{
return \sprintf(
<<<'PHP'
<?php
declare(strict_types=1);
$config = Ergebnis\PhpCsFixer\Config\Factory::fromRuleSet(new %s(''));
$config->getFinder()
->exclude([
'.build/',
'.github/',
'.notes/',
])
->ignoreDotFiles(false)
->in('%s')
->name('.php-cs-fixer.php');
$config->setUsingCache(false);
return $config;
PHP
,
self::className(),
self::projectDirectory(),
);
}

/**
* @psalm-return class-string
*
* @throws \RuntimeException
*/
private static function className(): string
{
$className = \preg_replace(
'/Test$/',
'',
\str_replace(
'\Test\EndToEnd',
'',
static::class,
),
);

if (!\is_string($className)) {
throw new \RuntimeException(\sprintf(
'Failed resolving class name from test class name "%s".',
static::class,
));
}

if (!\class_exists($className)) {
throw new \RuntimeException(\sprintf(
'Class name "%s" resolved from test class name "%s" does not reference a class that exists.',
$className,
static::class,
));
}

return $className;
}

private static function projectDirectory(): string
{
return __DIR__ . '/../../..';
}

private static function temporaryDirectory(): string
{
return __DIR__ . '/../../../.build/test';
}
}
25 changes: 25 additions & 0 deletions test/EndToEnd/RuleSet/Php80Test.php
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019-2023 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\EndToEnd\RuleSet;

/**
* @internal
*
* @coversNothing
*
* @requires PHP 8.0
*/
final class Php80Test extends AbstractRuleSetTestCase
{
}
25 changes: 25 additions & 0 deletions test/EndToEnd/RuleSet/Php81Test.php
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019-2023 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\EndToEnd\RuleSet;

/**
* @internal
*
* @coversNothing
*
* @requires PHP 8.1
*/
final class Php81Test extends AbstractRuleSetTestCase
{
}
30 changes: 30 additions & 0 deletions test/EndToEnd/phpunit.xml
@@ -0,0 +1,30 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../vendor/phpunit/phpunit/phpunit.xsd"
beStrictAboutChangesToGlobalState="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutResourceUsageDuringSmallTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
bootstrap="../../vendor/autoload.php"
cacheResult="true"
cacheResultFile="../../.build/phpunit/end-to-end.cache"
colors="true"
columns="max"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
executionOrder="random"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="true"
>
<testsuites>
<testsuite name="EndToEnd">
<directory>.</directory>
</testsuite>
</testsuites>
</phpunit>

0 comments on commit 19ae4dc

Please sign in to comment.