From 2fbc92298bb19d6f51bc59d3f0e42d2b4305ad4f Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Sun, 3 Jan 2021 08:49:02 +0100 Subject: [PATCH] Dep update --- .github/workflows/integrate.yaml | 2 +- .gitignore | 1 + composer-require-checker.json | 7 -- composer.json | 17 ++-- phpunit.xml | 38 ++++---- tests/AbstractFixerTestCase.php | 132 ++++++++++++++++++++++++++-- tests/PhpFileOnlyProxyFixerTest.php | 8 +- 7 files changed, 162 insertions(+), 43 deletions(-) delete mode 100644 composer-require-checker.json diff --git a/.github/workflows/integrate.yaml b/.github/workflows/integrate.yaml index 76ccc54..0de1397 100644 --- a/.github/workflows/integrate.yaml +++ b/.github/workflows/integrate.yaml @@ -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" diff --git a/.gitignore b/.gitignore index def429a..59499fa 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ vendor/ .php_cs.cache composer.lock +.phpunit.result.cache diff --git a/composer-require-checker.json b/composer-require-checker.json deleted file mode 100644 index 5d0e97f..0000000 --- a/composer-require-checker.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "symbol-whitelist" : [ - "null", "true", "false", - "static", "self", "parent", - "array", "string", "int", "float", "bool", "iterable", "callable", "void", "object" - ] -} diff --git a/composer.json b/composer.json index 2bb5044..0212cf1 100644 --- a/composer.json +++ b/composer.json @@ -13,16 +13,22 @@ "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/" @@ -30,8 +36,7 @@ }, "autoload-dev": { "psr-4": { - "SlamCsFixer\\Tests\\": "tests/", - "PhpCsFixer\\Tests\\": "vendor/friendsofphp/php-cs-fixer/tests/" + "SlamCsFixer\\Tests\\": "tests/" } } } diff --git a/phpunit.xml b/phpunit.xml index 419b1c4..d7cae63 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,19 +1,23 @@ + - - - - - - - ./tests - - - - - ./lib - - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" + bootstrap="./vendor/autoload.php" + colors="true" + verbose="true" +> + + + ./lib + + + + + + + + + + ./tests + diff --git a/tests/AbstractFixerTestCase.php b/tests/AbstractFixerTestCase.php index 829c910..57d4e0c 100644 --- a/tests/AbstractFixerTestCase.php +++ b/tests/AbstractFixerTestCase.php @@ -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() { @@ -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; } } diff --git a/tests/PhpFileOnlyProxyFixerTest.php b/tests/PhpFileOnlyProxyFixerTest.php index c82a41c..3eb1d65 100644 --- a/tests/PhpFileOnlyProxyFixerTest.php +++ b/tests/PhpFileOnlyProxyFixerTest.php @@ -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()) @@ -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());