Skip to content

Commit

Permalink
Dep update
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed Jan 3, 2021
1 parent 8053715 commit 2fbc922
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
run: "composer-normalize --dry-run"

- name: "Check composer.json explicit dependencies"
run: "composer-require-checker check --config-file=$(realpath composer-require-checker.json)"
run: "composer-require-checker"

- name: "Check composer.json unused dependencies"
run: "composer-unused"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
vendor/
.php_cs.cache
composer.lock
.phpunit.result.cache
7 changes: 0 additions & 7 deletions composer-require-checker.json

This file was deleted.

17 changes: 11 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,30 @@
"php": "^7.4 || 8.0",
"ext-mbstring": "*",
"ext-tokenizer": "*",
"friendsofphp/php-cs-fixer": "^2.17"
"friendsofphp/php-cs-fixer": "^2.17.3"
},
"require-dev": {
"phpstan/phpstan": "^0.12.59",
"phpstan/phpstan-phpunit": "^0.12.16",
"phpunit/phpunit": "^7.5.20",
"malukenho/mcbumpface": "^1.1.5",
"phpstan/phpstan": "^0.12.64",
"phpstan/phpstan-phpunit": "^0.12.17",
"phpunit/phpunit": "^9.5.0",
"slam/php-debug-r": "^1.6.1",
"slam/phpstan-extensions": "^5.1.0",
"thecodingmachine/phpstan-strict-rules": "^0.12.1"
},
"extra": {
"mc-bumpface": {
"stripVersionPrefixes": true
}
},
"autoload": {
"psr-4": {
"SlamCsFixer\\": "lib/"
}
},
"autoload-dev": {
"psr-4": {
"SlamCsFixer\\Tests\\": "tests/",
"PhpCsFixer\\Tests\\": "vendor/friendsofphp/php-cs-fixer/tests/"
"SlamCsFixer\\Tests\\": "tests/"
}
}
}
38 changes: 21 additions & 17 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
<?xml version="1.0"?>
<phpunit
bootstrap="./vendor/autoload.php"
colors="true"
verbose="true">

<php>
<env name="PHP_CS_FIXER_FUTURE_MODE" value="1"/>
</php>

<testsuite name="SlamCsFixer">
<directory>./tests</directory>
</testsuite>

<filter>
<whitelist>
<directory suffix=".php">./lib</directory>
</whitelist>
</filter>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
verbose="true"
>
<coverage>
<include>
<directory suffix=".php">./lib</directory>
</include>
<report>
<text outputFile="php://stdout" showOnlySummary="true"/>
</report>
</coverage>
<php>
<env name="PHP_CS_FIXER_FUTURE_MODE" value="1"/>
</php>
<testsuite name="SlamCsFixer">
<directory>./tests</directory>
</testsuite>
</phpunit>
132 changes: 124 additions & 8 deletions tests/AbstractFixerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@

namespace SlamCsFixer\Tests;

use Exception;
use InvalidArgumentException;
use PhpCsFixer\Fixer\DefinedFixerInterface;
use PhpCsFixer\Test\AbstractFixerTestCase as PhpCsFixerAbstractFixerTestCase;
use PhpCsFixer\Linter\TokenizerLinter;
use PhpCsFixer\Tests\Test\Assert\AssertTokensTrait;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PHPUnit\Framework\TestCase;
use SplFileInfo;

abstract class AbstractFixerTestCase extends PhpCsFixerAbstractFixerTestCase
abstract class AbstractFixerTestCase extends TestCase
{
/**
* @var DefinedFixerInterface
*/
protected $fixer;
use AssertTokensTrait;

private TokenizerLinter $linter;
protected DefinedFixerInterface $fixer;

protected function setUp(): void
{
$this->linter = new TokenizerLinter();
$this->fixer = $this->createFixer();
}

final protected function createFixer()
{
Expand All @@ -23,8 +36,111 @@ final protected function createFixer()
return new $fixerClass();
}

protected function assertMatchesRegularExpression(string $format, string $string, string $message = ''): void
/**
* @param string $filename
*
* @return SplFileInfo
*/
final protected function getTestFile($filename = __FILE__)
{
static::assertRegExp($format, $string, $message);
static $files = [];

if (! isset($files[$filename])) {
$files[$filename] = new SplFileInfo($filename);
}

return $files[$filename];
}

/**
* Tests if a fixer fixes a given string to match the expected result.
*
* It is used both if you want to test if something is fixed or if it is not touched by the fixer.
* It also makes sure that the expected output does not change when run through the fixer. That means that you
* do not need two test cases like [$expected] and [$expected, $input] (where $expected is the same in both cases)
* as the latter covers both of them.
* This method throws an exception if $expected and $input are equal to prevent test cases that accidentally do
* not test anything.
*
* @param string $expected The expected fixer output
* @param null|string $input The fixer input, or null if it should intentionally be equal to the output
* @param null|SplFileInfo $file The file to fix, or null if unneeded
*/
final protected function doTest($expected, $input = null, ?SplFileInfo $file = null): void
{
if ($expected === $input) {
throw new InvalidArgumentException('Input parameter must not be equal to expected parameter.');
}

$file = $file ?: $this->getTestFile();
$fileIsSupported = $this->fixer->supports($file);

if (null !== $input) {
static::assertNull($this->lintSource($input));

Tokens::clearCache();
$tokens = Tokens::fromCode($input);

if ($fileIsSupported) {
static::assertTrue($this->fixer->isCandidate($tokens), 'Fixer must be a candidate for input code.');
static::assertFalse($tokens->isChanged(), 'Fixer must not touch Tokens on candidate check.');
$fixResult = $this->fixer->fix($file, $tokens);
static::assertNull($fixResult, '->fix method must return null.');
}

static::assertSame(
$expected,
$tokens->generateCode(),
'Code build on input code must match expected code.'
);
static::assertTrue($tokens->isChanged(), 'Tokens collection built on input code must be marked as changed after fixing.');

$tokens->clearEmptyTokens();

static::assertSame(
\count($tokens),
\count(\array_unique(\array_map(static function (Token $token) {
return \spl_object_hash($token);
}, $tokens->toArray()))),
'Token items inside Tokens collection must be unique.'
);

Tokens::clearCache();
$expectedTokens = Tokens::fromCode($expected);
static::assertTokens($expectedTokens, $tokens);
}

static::assertNull($this->lintSource($expected));

Tokens::clearCache();
$tokens = Tokens::fromCode($expected);

if ($fileIsSupported) {
$fixResult = $this->fixer->fix($file, $tokens);
static::assertNull($fixResult, '->fix method must return null.');
}

static::assertSame(
$expected,
$tokens->generateCode(),
'Code build on expected code must not change.'
);
static::assertFalse($tokens->isChanged(), 'Tokens collection built on expected code must not be marked as changed after fixing.');
}

/**
* @param string $source
*
* @return null|string
*/
private function lintSource($source)
{
try {
$this->linter->lintSource($source)->check();
} catch (Exception $e) {
return $e->getMessage() . "\n\nSource:\n{$source}";
}

return null;
}
}
8 changes: 4 additions & 4 deletions tests/PhpFileOnlyProxyFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public function testFixerInterfaceProxy(): void
->willReturn($name = \uniqid('_name'))
;
$proxyName = $proxy->getName();
self::assertContains('Slam', $proxyName);
self::assertContains($name, $proxyName);
self::assertStringContainsString('Slam', $proxyName);
self::assertStringContainsString($name, $proxyName);

$fixer
->expects(self::once())
Expand Down Expand Up @@ -99,8 +99,8 @@ public function testGetDefinitionIsProxied(): void

$definition = $proxy->getDefinition();

self::assertContains($summary, $definition->getSummary());
self::assertContains('PHP', $definition->getSummary());
self::assertStringContainsString($summary, $definition->getSummary());
self::assertStringContainsString('PHP', $definition->getSummary());
self::assertSame($codeSamples, $definition->getCodeSamples());
self::assertSame($description, $definition->getDescription());
self::assertSame($riskyDescription, $definition->getRiskyDescription());
Expand Down

0 comments on commit 2fbc922

Please sign in to comment.