Browse Source

Merge branch 'master' into 3.0

# Conflicts:
#	phpunit.xml.dist
#	tests/Test/AbstractFixerTestCase.php
#	tests/Test/AbstractIntegrationTestCase.php
Dariusz Ruminski 7 years ago
parent
commit
9e9feeb795

+ 1 - 0
.appveyor.yml

@@ -5,6 +5,7 @@ clone_folder: C:\projects\php-cs-fixer
 
 environment:
     matrix:
+        - php_ver: 7.1.2
         - php_ver: 5.6.30
 
 cache:

+ 1 - 1
.travis.yml

@@ -128,7 +128,7 @@ jobs:
             php: 7.1
             install: ./dev-tools/build.sh
             script:
-                - vendor/bin/phpunit tests/Smoke/PharTest.php
+                - PHP_CS_FIXER_TEST_ALLOW_SKIPPING_PHAR_TESTS=0 vendor/bin/phpunit tests/Smoke/
             deploy:
                 provider: releases
                 api_key:

+ 1 - 0
phpunit.xml.dist

@@ -48,5 +48,6 @@
     <php>
         <ini name="zend.enable_gc" value="0"/>
         <ini name="memory_limit" value="1G"/>
+        <env name="PHP_CS_FIXER_TEST_ALLOW_SKIPPING_PHAR_TESTS" value="1"/>
     </php>
 </phpunit>

+ 75 - 0
src/Linter/CachingLinter.php

@@ -0,0 +1,75 @@
+<?php
+
+/*
+ * This file is part of PHP CS Fixer.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace PhpCsFixer\Linter;
+
+/**
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * @internal
+ */
+final class CachingLinter implements LinterInterface
+{
+    /**
+     * @var LinterInterface
+     */
+    private $sublinter;
+
+    /**
+     * @var array<string, LintingResultInterface>
+     */
+    private $cache = [];
+
+    /**
+     * @param LinterInterface $linter Linter instance
+     */
+    public function __construct(LinterInterface $linter)
+    {
+        $this->sublinter = $linter;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function isAsync()
+    {
+        return $this->sublinter->isAsync();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function lintFile($path)
+    {
+        $checksum = crc32(file_get_contents($path));
+
+        if (!isset($this->cache[$checksum])) {
+            $this->cache[$checksum] = $this->sublinter->lintFile($path);
+        }
+
+        return $this->cache[$checksum];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function lintSource($source)
+    {
+        $checksum = crc32($source);
+
+        if (!isset($this->cache[$checksum])) {
+            $this->cache[$checksum] = $this->sublinter->lintSource($source);
+        }
+
+        return $this->cache[$checksum];
+    }
+}

+ 1 - 0
tests/AutoReview/ProjectCodeTest.php

@@ -48,6 +48,7 @@ final class ProjectCodeTest extends TestCase
         \PhpCsFixer\FileReader::class,
         \PhpCsFixer\FileRemoval::class,
         \PhpCsFixer\Indicator\PhpUnitTestCaseIndicator::class,
+        \PhpCsFixer\Linter\CachingLinter::class,
         \PhpCsFixer\Runner\FileCachingLintingIterator::class,
         \PhpCsFixer\Runner\FileLintingIterator::class,
         \PhpCsFixer\StdinFileInfo::class,

+ 78 - 0
tests/Smoke/InstallViaComposerTest.php

@@ -0,0 +1,78 @@
+<?php
+
+/*
+ * This file is part of PHP CS Fixer.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace PhpCsFixer\Tests\Smoke;
+
+use Keradus\CliExecutor\CommandExecutor;
+use PhpCsFixer\Utils;
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\Filesystem\Filesystem;
+
+/**
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * @internal
+ *
+ * @coversNothing
+ * @group covers-nothing
+ * @large
+ */
+final class InstallViaComposerTest extends TestCase
+{
+    public static function setUpBeforeClass()
+    {
+        try {
+            CommandExecutor::create('php --version', __DIR__)->getResult();
+        } catch (\RuntimeException $e) {
+            self::markTestSkipped('Missing `php` env script. Details:'."\n".$e->getMessage());
+        }
+
+        try {
+            CommandExecutor::create('composer --version', __DIR__)->getResult();
+        } catch (\RuntimeException $e) {
+            self::markTestSkipped('Missing `composer` env script. Details:'."\n".$e->getMessage());
+        }
+    }
+
+    public function testFoo()
+    {
+        $fs = new Filesystem();
+
+        $tmpPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
+        unlink($tmpPath);
+        $fs->mkdir($tmpPath);
+
+        $initialComposerFileState = [
+            'repositories' => [
+                [
+                    'type' => 'path',
+                    'url' => __DIR__.'/../..',
+                ],
+            ],
+            'require' => [
+                'friendsofphp/php-cs-fixer' => '*@dev',
+            ],
+        ];
+
+        file_put_contents(
+            $tmpPath.'/composer.json',
+            json_encode($initialComposerFileState, Utils::calculateBitmask(['JSON_PRETTY_PRINT']))
+        );
+
+        $this->assertSame(0, CommandExecutor::create('composer install -q', $tmpPath)->getResult()->getCode());
+        $this->assertSame(0, CommandExecutor::create('composer dump-autoload --optimize', $tmpPath)->getResult()->getCode());
+        $this->assertSame(0, CommandExecutor::create('php vendor/autoload.php', $tmpPath)->getResult()->getCode());
+        $this->assertSame(0, CommandExecutor::create('vendor/bin/php-cs-fixer --version', $tmpPath)->getResult()->getCode());
+
+        $fs->remove($tmpPath);
+    }
+}

+ 5 - 1
tests/Smoke/PharTest.php

@@ -41,7 +41,11 @@ final class PharTest extends TestCase
         self::$pharPath = self::$pharCwd.'/'.self::$pharName;
 
         if (!file_exists(self::$pharPath)) {
-            static::markTestSkipped('No phar file available.');
+            if (getenv('PHP_CS_FIXER_TEST_ALLOW_SKIPPING_PHAR_TESTS')) {
+                self::markTestSkipped('No phar file available.');
+            }
+
+            self::fail('No phar file available. Failing as test is obligatory because of `PHP_CS_FIXER_TEST_ALLOW_SKIPPING_PHAR_TESTS=0`.');
         }
     }
 

+ 2 - 1
tests/Test/AbstractFixerTestCase.php

@@ -15,6 +15,7 @@ namespace PhpCsFixer\Tests\Test;
 use PhpCsFixer\Fixer\ConfigurableFixerInterface;
 use PhpCsFixer\Fixer\FixerInterface;
 use PhpCsFixer\FixerFactory;
+use PhpCsFixer\Linter\CachingLinter;
 use PhpCsFixer\Linter\Linter;
 use PhpCsFixer\Linter\LinterInterface;
 use PhpCsFixer\RuleSet;
@@ -223,7 +224,7 @@ abstract class AbstractFixerTestCase extends TestCase
         static $linter = null;
 
         if (null === $linter) {
-            $linter = new Linter();
+            $linter = new CachingLinter(new Linter());
         }
 
         return $linter;

+ 2 - 1
tests/Test/AbstractIntegrationTestCase.php

@@ -19,6 +19,7 @@ use PhpCsFixer\Error\ErrorsManager;
 use PhpCsFixer\FileRemoval;
 use PhpCsFixer\Fixer\FixerInterface;
 use PhpCsFixer\FixerFactory;
+use PhpCsFixer\Linter\CachingLinter;
 use PhpCsFixer\Linter\Linter;
 use PhpCsFixer\Linter\LinterInterface;
 use PhpCsFixer\Runner\Runner;
@@ -362,7 +363,7 @@ abstract class AbstractIntegrationTestCase extends TestCase
         static $linter = null;
 
         if (null === $linter) {
-            $linter = new Linter();
+            $linter = new CachingLinter(new Linter());
         }
 
         return $linter;