Browse Source

minor #2938 Tests fix configuration of project (SpacePossum, keradus)

This PR was squashed before being merged into the 2.2 branch (closes #2938).

Discussion
----------

Tests fix configuration of project

- test own configuration
- add getter for the fixer name of an invalid fixer configuration exception
- add tests for new getter
- fix swapped tests names for the configuration exceptions

Commits
-------

e1370cc6 Tests fix configuration of project
Dariusz Ruminski 7 years ago
parent
commit
bbd4f36b8d

+ 14 - 0
src/ConfigurationException/InvalidFixerConfigurationException.php

@@ -23,6 +23,11 @@ use PhpCsFixer\Console\Command\FixCommand;
  */
 class InvalidFixerConfigurationException extends InvalidConfigurationException
 {
+    /**
+     * @var string
+     */
+    private $fixerName;
+
     /**
      * @param string          $fixerName
      * @param string          $message
@@ -35,5 +40,14 @@ class InvalidFixerConfigurationException extends InvalidConfigurationException
             FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG,
             $previous
         );
+        $this->fixerName = $fixerName;
+    }
+
+    /**
+     * @return string
+     */
+    public function getFixerName()
+    {
+        return $this->fixerName;
     }
 }

+ 41 - 0
tests/AutoReview/ProjectFixerConfigurationTest.php

@@ -0,0 +1,41 @@
+<?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\AutoReview;
+
+use PhpCsFixer\Console\ConfigurationResolver;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @author SpacePossum
+ *
+ * @internal
+ *
+ * @coversNothing
+ * @group auto-review
+ */
+final class ProjectFixerConfigurationTest extends TestCase
+{
+    public function testCreate()
+    {
+        /** @var \PhpCsFixer\Config $config */
+        $config = require __DIR__.'/../../.php_cs.dist';
+
+        $this->assertInstanceOf('PhpCsFixer\Config', $config);
+        $this->assertEmpty($config->getCustomFixers());
+        $this->assertNotEmpty($config->getRules());
+
+        // call so the fixers get configured to reveal issue (like deprecated configuration used etc.)
+        $resolver = new ConfigurationResolver($config, array(), __DIR__);
+        $resolver->getFixers();
+    }
+}

+ 13 - 20
tests/ConfigurationException/InvalidConfigurationExceptionTest.php

@@ -12,7 +12,7 @@
 
 namespace PhpCsFixer\Tests\ConfigurationException;
 
-use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
+use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
 use PhpCsFixer\Console\Command\FixCommand;
 use PHPUnit\Framework\TestCase;
 
@@ -21,49 +21,42 @@ use PHPUnit\Framework\TestCase;
  *
  * @internal
  *
- * @covers \PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException
+ * @covers \PhpCsFixer\ConfigurationException\InvalidConfigurationException
  */
 final class InvalidConfigurationExceptionTest extends TestCase
 {
-    public function testIsInvalidConfigurationException()
+    public function testIsInvalidArgumentException()
     {
-        $exception = new InvalidFixerConfigurationException(
-            'hal',
-            'I cannot do that, Dave.'
-        );
+        $exception = new InvalidConfigurationException('I cannot do that, Dave.');
 
-        $this->assertInstanceOf('PhpCsFixer\ConfigurationException\InvalidConfigurationException', $exception);
+        $this->assertInstanceOf('InvalidArgumentException', $exception);
     }
 
     public function testDefaults()
     {
-        $fixerName = 'hal';
         $message = 'I cannot do that, Dave.';
 
-        $exception = new InvalidFixerConfigurationException(
-            $fixerName,
-            $message
-        );
+        $exception = new InvalidConfigurationException($message);
 
-        $this->assertSame(sprintf('[%s] %s', $fixerName, $message), $exception->getMessage());
-        $this->assertSame(FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG, $exception->getCode());
+        $this->assertSame($message, $exception->getMessage());
+        $this->assertSame(FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_CONFIG, $exception->getCode());
         $this->assertNull($exception->getPrevious());
     }
 
     public function testConstructorSetsValues()
     {
-        $fixerName = 'hal';
         $message = 'I cannot do that, Dave.';
+        $code = 9000;
         $previous = new \RuntimeException();
 
-        $exception = new InvalidFixerConfigurationException(
-            $fixerName,
+        $exception = new InvalidConfigurationException(
             $message,
+            $code,
             $previous
         );
 
-        $this->assertSame(sprintf('[%s] %s', $fixerName, $message), $exception->getMessage());
-        $this->assertSame(FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG, $exception->getCode());
+        $this->assertSame($message, $exception->getMessage());
+        $this->assertSame($code, $exception->getCode());
         $this->assertSame($previous, $exception->getPrevious());
     }
 }

+ 18 - 17
tests/ConfigurationException/InvalidFixerConfigurationExceptionTest.php

@@ -12,7 +12,7 @@
 
 namespace PhpCsFixer\Tests\ConfigurationException;
 
-use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
+use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
 use PhpCsFixer\Console\Command\FixCommand;
 use PHPUnit\Framework\TestCase;
 
@@ -21,42 +21,43 @@ use PHPUnit\Framework\TestCase;
  *
  * @internal
  *
- * @covers \PhpCsFixer\ConfigurationException\InvalidConfigurationException
+ * @covers \PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException
  */
 final class InvalidFixerConfigurationExceptionTest extends TestCase
 {
-    public function testIsInvalidArgumentException()
-    {
-        $exception = new InvalidConfigurationException('I cannot do that, Dave.');
-
-        $this->assertInstanceOf('InvalidArgumentException', $exception);
-    }
-
     public function testDefaults()
     {
+        $fixerName = 'hal';
         $message = 'I cannot do that, Dave.';
 
-        $exception = new InvalidConfigurationException($message);
+        $exception = new InvalidFixerConfigurationException(
+            $fixerName,
+            $message
+        );
 
-        $this->assertSame($message, $exception->getMessage());
-        $this->assertSame(FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_CONFIG, $exception->getCode());
+        $this->assertInstanceOf('PhpCsFixer\ConfigurationException\InvalidConfigurationException', $exception);
+        $this->assertSame(sprintf('[%s] %s', $fixerName, $message), $exception->getMessage());
+        $this->assertSame(FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG, $exception->getCode());
+        $this->assertSame($fixerName, $exception->getFixerName());
         $this->assertNull($exception->getPrevious());
     }
 
     public function testConstructorSetsValues()
     {
+        $fixerName = 'hal';
         $message = 'I cannot do that, Dave.';
-        $code = 9000;
         $previous = new \RuntimeException();
 
-        $exception = new InvalidConfigurationException(
+        $exception = new InvalidFixerConfigurationException(
+            $fixerName,
             $message,
-            $code,
             $previous
         );
 
-        $this->assertSame($message, $exception->getMessage());
-        $this->assertSame($code, $exception->getCode());
+        $this->assertInstanceOf('PhpCsFixer\ConfigurationException\InvalidConfigurationException', $exception);
+        $this->assertSame(sprintf('[%s] %s', $fixerName, $message), $exception->getMessage());
+        $this->assertSame(FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG, $exception->getCode());
+        $this->assertSame($fixerName, $exception->getFixerName());
         $this->assertSame($previous, $exception->getPrevious());
     }
 }