Dariusz Ruminski 8 years ago
parent
commit
085e12ce6b

+ 6 - 5
src/Cache/FileHandler.php

@@ -65,11 +65,12 @@ final class FileHandler implements FileHandlerInterface
         if (false === $bytesWritten) {
             $error = error_get_last();
 
-            if (null !== $error) {
-                throw new IOException(sprintf('Failed to write file "%s", "%s".', $this->file, $error['message']), 0, null, $this->file);
-            }
-
-            throw new IOException(sprintf('Failed to write file "%s".', $this->file), 0, null, $this->file);
+            throw new IOException(
+                sprintf('Failed to write file "%s", "%s".', $this->file, $error ? $error['message'] : 'no reason available'),
+                0,
+                null,
+                $this->file
+            );
         }
     }
 }

+ 0 - 4
src/Report/TextReporter.php

@@ -60,10 +60,6 @@ final class TextReporter implements ReporterInterface
      */
     private function getAppliedFixers($isDecoratedOutput, array $fixResult)
     {
-        if (empty($fixResult['appliedFixers'])) {
-            return '';
-        }
-
         return sprintf(
             $isDecoratedOutput ? ' (<comment>%s</comment>)' : ' (%s)',
             implode(', ', $fixResult['appliedFixers'])

+ 6 - 4
src/Runner/Runner.php

@@ -213,11 +213,13 @@ final class Runner
             if (!$this->isDryRun) {
                 if (false === @file_put_contents($file->getRealPath(), $new)) {
                     $error = error_get_last();
-                    if (null !== $error) {
-                        throw new IOException(sprintf('Failed to write file "%s", "%s".', $file->getRealPath(), $error['message']), 0, null, $file->getRealPath());
-                    }
 
-                    throw new IOException(sprintf('Failed to write file "%s".', $file->getRealPath()), 0, null, $file->getRealPath());
+                    throw new IOException(
+                        sprintf('Failed to write file "%s", "%s".', $this->file, $error ? $error['message'] : 'no reason available'),
+                        0,
+                        null,
+                        $file->getRealPath()
+                    );
                 }
             }
 

+ 5 - 0
tests/Fixtures/Linter/invalid.php

@@ -0,0 +1,5 @@
+<?php
+print "line 2";
+print "line 3";
+print "line 4";
+echo echo;

+ 1 - 0
tests/Fixtures/Linter/valid.php

@@ -0,0 +1 @@
+<?php echo 123;

+ 99 - 0
tests/Linter/AbstractLinterTestCase.php

@@ -0,0 +1,99 @@
+<?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\Linter;
+
+use PhpCsFixer\Linter\LinterInterface;
+
+/**
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * @internal
+ */
+abstract class AbstractLinterTestCase extends \PHPUnit_Framework_TestCase
+{
+    abstract public function testIsAsync();
+
+    /**
+     * @param string      $file
+     * @param null|string $errorRegExp
+     *
+     * @dataProvider provideLintFileCases
+     */
+    public function testLintFile($file, $errorRegExp = null)
+    {
+        if (null !== $errorRegExp) {
+            $this->setExpectedExceptionRegExp('\PhpCsFixer\Linter\LintingException', $errorRegExp);
+        }
+
+        $linter = $this->createLinter();
+        $linter->lintFile($file)->check();
+    }
+
+    /**
+     * @return array
+     */
+    public function provideLintFileCases()
+    {
+        return array(
+            array(
+                __DIR__.'/../Fixtures/Linter/valid.php',
+            ),
+            array(
+                __DIR__.'/../Fixtures/Linter/invalid.php',
+                '/syntax error, unexpected.*T_ECHO.*line 5/',
+            ),
+        );
+    }
+
+    /**
+     * @param string      $source
+     * @param null|string $errorRegExp
+     *
+     * @dataProvider provideLintSourceCases
+     */
+    public function testLintSource($source, $errorRegExp = null)
+    {
+        if (null !== $errorRegExp) {
+            $this->setExpectedExceptionRegExp('\PhpCsFixer\Linter\LintingException', $errorRegExp);
+        }
+
+        $linter = $this->createLinter();
+        $linter->lintSource($source)->check();
+    }
+
+    /**
+     * @return array
+     */
+    public function provideLintSourceCases()
+    {
+        return array(
+            array(
+                '<?php echo 123;',
+            ),
+            array(
+                '<?php
+                    print "line 2";
+                    print "line 3";
+                    print "line 4";
+                    echo echo;
+                ',
+                '/syntax error, unexpected.*T_ECHO.*line 5/',
+            ),
+        );
+    }
+
+    /**
+     * @return LinterInterface
+     */
+    abstract protected function createLinter();
+}

+ 38 - 0
tests/Linter/LinterTest.php

@@ -0,0 +1,38 @@
+<?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\Linter;
+
+use PhpCsFixer\Linter\Linter;
+
+/**
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * @internal
+ *
+ * @covers PhpCsFixer\Linter\Linter
+ */
+final class LinterTest extends AbstractLinterTestCase
+{
+    public function testIsAsync()
+    {
+        $this->assertSame(!defined('TOKEN_PARSE'), $this->createLinter()->isAsync());
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function createLinter()
+    {
+        return new Linter();
+    }
+}

+ 12 - 17
tests/Linter/ProcessLinterTest.php

@@ -20,9 +20,17 @@ use Symfony\Component\Process\ProcessUtils;
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  *
  * @internal
+ *
+ * @covers PhpCsFixer\Linter\ProcessLinter
+ * @covers PhpCsFixer\Linter\ProcessLintingResult
  */
-final class ProcessLinterTest extends \PHPUnit_Framework_TestCase
+final class ProcessLinterTest extends AbstractLinterTestCase
 {
+    public function testIsAsync()
+    {
+        $this->assertTrue($this->createLinter()->isAsync());
+    }
+
     public function testPrepareCommandOnPhp()
     {
         if (defined('HHVM_VERSION')) {
@@ -53,24 +61,11 @@ final class ProcessLinterTest extends \PHPUnit_Framework_TestCase
     }
 
     /**
-     * @covers PhpCsFixer\Linter\ProcessLinter::lintSource
-     */
-    public function testLintSourceWithGoodCode()
-    {
-        $linter = new ProcessLinter();
-        $linter->lintSource('<?php echo 123;')->check(); // no exception should be raised
-    }
-
-    /**
-     * @covers PhpCsFixer\Linter\ProcessLinter::lintSource
-     *
-     * @expectedException \PhpCsFixer\Linter\LintingException
-     * @expectedExceptionMessageRegExp /syntax error, unexpected.*T_ECHO/
+     * {@inheritdoc}
      */
-    public function testLintSourceWithBadCode()
+    protected function createLinter()
     {
-        $linter = new ProcessLinter();
-        $linter->lintSource('<?php echo echo;')->check();
+        return new ProcessLinter();
     }
 
     /**

+ 10 - 21
tests/Linter/TokenizerLinterTest.php

@@ -18,34 +18,23 @@ use PhpCsFixer\Linter\TokenizerLinter;
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  *
  * @internal
+ *
+ * @requires PHP 7.0
+ * @covers PhpCsFixer\Linter\TokenizerLinter
+ * @covers PhpCsFixer\Linter\TokenizerLintingResult
  */
-final class TokenizerLinterTest extends \PHPUnit_Framework_TestCase
+final class TokenizerLinterTest extends AbstractLinterTestCase
 {
-    /**
-     * @covers PhpCsFixer\Linter\TokenizerLinter::lintSource
-     * @requires PHP 7.0
-     */
-    public function testLintSourceWithGoodCode()
+    public function testIsAsync()
     {
-        $linter = new TokenizerLinter();
-        $linter->lintSource('<?php echo 123;')->check(); // no exception should be raised
+        $this->assertFalse($this->createLinter()->isAsync());
     }
 
     /**
-     * @covers PhpCsFixer\Linter\TokenizerLinter::lintSource
-     * @requires PHP 7.0
-     *
-     * @expectedException \PhpCsFixer\Linter\LintingException
-     * @expectedExceptionMessageRegExp /syntax error, unexpected.*T_ECHO.*line 5/
+     * {@inheritdoc}
      */
-    public function testLintSourceWithBadCode()
+    protected function createLinter()
     {
-        $linter = new TokenizerLinter();
-        $linter->lintSource('<?php
-            print "line 2";
-            print "line 3";
-            print "line 4";
-            echo echo;
-        ')->check();
+        return new TokenizerLinter();
     }
 }

+ 28 - 0
tests/Report/JsonReporterTest.php

@@ -38,6 +38,34 @@ final class JsonReporterTest extends \PHPUnit_Framework_TestCase
         $this->assertSame('json', $this->reporter->getFormat());
     }
 
+    public function testGenerateNoErrors()
+    {
+        $expectedJson = <<<'JSON'
+{
+    "files": [
+    ],
+    "time": {
+        "total": 0
+    },
+    "memory": 0
+}
+JSON;
+
+        $this->assertJsonStringEqualsJsonString(
+            $expectedJson,
+            $this->reporter->generate(
+                new ReportSummary(
+                    array(),
+                    0,
+                    0,
+                    false,
+                    false,
+                    false
+                )
+            )
+        );
+    }
+
     public function testGenerateSimple()
     {
         $expectedJson = <<<'JSON'

Some files were not shown because too many files changed in this diff