Browse Source

Exceptions - provide utests

Andreas Möller 7 years ago
parent
commit
27ec90d035

+ 0 - 6
tests/AutoReview/ProjectCodeTest.php

@@ -35,10 +35,6 @@ final class ProjectCodeTest extends TestCase
      * @var string[]
      * @var string[]
      */
      */
     private static $classesWithoutTests = array(
     private static $classesWithoutTests = array(
-        'PhpCsFixer\ConfigurationException\InvalidConfigurationException',
-        'PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException',
-        'PhpCsFixer\ConfigurationException\RequiredFixerConfigurationException',
-        'PhpCsFixer\Console\Command\DescribeNameNotFoundException',
         'PhpCsFixer\Console\Command\SelfUpdateCommand',
         'PhpCsFixer\Console\Command\SelfUpdateCommand',
         'PhpCsFixer\Console\Output\NullOutput',
         'PhpCsFixer\Console\Output\NullOutput',
         'PhpCsFixer\Differ\DiffConsoleFormatter',
         'PhpCsFixer\Differ\DiffConsoleFormatter',
@@ -54,10 +50,8 @@ final class ProjectCodeTest extends TestCase
         'PhpCsFixer\Fixer\Operator\AlignDoubleArrowFixerHelper',
         'PhpCsFixer\Fixer\Operator\AlignDoubleArrowFixerHelper',
         'PhpCsFixer\Fixer\Operator\AlignEqualsFixerHelper',
         'PhpCsFixer\Fixer\Operator\AlignEqualsFixerHelper',
         'PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer',
         'PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer',
-        'PhpCsFixer\Linter\LintingException',
         'PhpCsFixer\Linter\ProcessLintingResult',
         'PhpCsFixer\Linter\ProcessLintingResult',
         'PhpCsFixer\Linter\TokenizerLintingResult',
         'PhpCsFixer\Linter\TokenizerLintingResult',
-        'PhpCsFixer\Linter\UnavailableLinterException',
         'PhpCsFixer\Report\ReportSummary',
         'PhpCsFixer\Report\ReportSummary',
         'PhpCsFixer\Runner\FileCachingLintingIterator',
         'PhpCsFixer\Runner\FileCachingLintingIterator',
         'PhpCsFixer\Runner\FileFilterIterator',
         'PhpCsFixer\Runner\FileFilterIterator',

+ 69 - 0
tests/ConfigurationException/InvalidConfigurationExceptionTest.php

@@ -0,0 +1,69 @@
+<?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\ConfigurationException;
+
+use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
+use PhpCsFixer\Console\Command\FixCommand;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @author Andreas Möller <am@localheinz.com>
+ *
+ * @internal
+ *
+ * @covers \PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException
+ */
+final class InvalidConfigurationExceptionTest extends TestCase
+{
+    public function testIsInvalidConfigurationException()
+    {
+        $exception = new InvalidFixerConfigurationException(
+            'hal',
+            'I cannot do that, Dave.'
+        );
+
+        $this->assertInstanceOf('PhpCsFixer\ConfigurationException\InvalidConfigurationException', $exception);
+    }
+
+    public function testDefaults()
+    {
+        $fixerName = 'hal';
+        $message = 'I cannot do that, Dave.';
+
+        $exception = new InvalidFixerConfigurationException(
+            $fixerName,
+            $message
+        );
+
+        $this->assertSame(sprintf('[%s] %s', $fixerName, $message), $exception->getMessage());
+        $this->assertSame(FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG, $exception->getCode());
+        $this->assertNull($exception->getPrevious());
+    }
+
+    public function testConstructorSetsValues()
+    {
+        $fixerName = 'hal';
+        $message = 'I cannot do that, Dave.';
+        $previous = new \RuntimeException();
+
+        $exception = new InvalidFixerConfigurationException(
+            $fixerName,
+            $message,
+            $previous
+        );
+
+        $this->assertSame(sprintf('[%s] %s', $fixerName, $message), $exception->getMessage());
+        $this->assertSame(FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG, $exception->getCode());
+        $this->assertSame($previous, $exception->getPrevious());
+    }
+}

+ 62 - 0
tests/ConfigurationException/InvalidFixerConfigurationExceptionTest.php

@@ -0,0 +1,62 @@
+<?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\ConfigurationException;
+
+use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
+use PhpCsFixer\Console\Command\FixCommand;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @author Andreas Möller <am@localheinz.com>
+ *
+ * @internal
+ *
+ * @covers \PhpCsFixer\ConfigurationException\InvalidConfigurationException
+ */
+final class InvalidFixerConfigurationExceptionTest extends TestCase
+{
+    public function testIsInvalidArgumentException()
+    {
+        $exception = new InvalidConfigurationException('I cannot do that, Dave.');
+
+        $this->assertInstanceOf('InvalidArgumentException', $exception);
+    }
+
+    public function testDefaults()
+    {
+        $message = 'I cannot do that, Dave.';
+
+        $exception = new InvalidConfigurationException($message);
+
+        $this->assertSame($message, $exception->getMessage());
+        $this->assertSame(FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_CONFIG, $exception->getCode());
+        $this->assertNull($exception->getPrevious());
+    }
+
+    public function testConstructorSetsValues()
+    {
+        $message = 'I cannot do that, Dave.';
+        $code = 9000;
+        $previous = new \RuntimeException();
+
+        $exception = new InvalidConfigurationException(
+            $message,
+            $code,
+            $previous
+        );
+
+        $this->assertSame($message, $exception->getMessage());
+        $this->assertSame($code, $exception->getCode());
+        $this->assertSame($previous, $exception->getPrevious());
+    }
+}

+ 69 - 0
tests/ConfigurationException/RequiredFixerConfigurationExceptionTest.php

@@ -0,0 +1,69 @@
+<?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\ConfigurationException;
+
+use PhpCsFixer\ConfigurationException\RequiredFixerConfigurationException;
+use PhpCsFixer\Console\Command\FixCommand;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @author Andreas Möller <am@localheinz.com>
+ *
+ * @internal
+ *
+ * @covers \PhpCsFixer\ConfigurationException\RequiredFixerConfigurationException
+ */
+final class RequiredFixerConfigurationExceptionTest extends TestCase
+{
+    public function testIsInvalidFixerConfigurationException()
+    {
+        $exception = new RequiredFixerConfigurationException(
+            'hal',
+            'I cannot do that, Dave.'
+        );
+
+        $this->assertInstanceOf('PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException', $exception);
+    }
+
+    public function testDefaults()
+    {
+        $fixerName = 'hal';
+        $message = 'I cannot do that, Dave.';
+
+        $exception = new RequiredFixerConfigurationException(
+            $fixerName,
+            $message
+        );
+
+        $this->assertSame(sprintf('[%s] %s', $fixerName, $message), $exception->getMessage());
+        $this->assertSame(FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG, $exception->getCode());
+        $this->assertNull($exception->getPrevious());
+    }
+
+    public function testConstructorSetsValues()
+    {
+        $fixerName = 'hal';
+        $message = 'I cannot do that, Dave.';
+        $previous = new \RuntimeException();
+
+        $exception = new RequiredFixerConfigurationException(
+            $fixerName,
+            $message,
+            $previous
+        );
+
+        $this->assertSame(sprintf('[%s] %s', $fixerName, $message), $exception->getMessage());
+        $this->assertSame(FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG, $exception->getCode());
+        $this->assertSame($previous, $exception->getPrevious());
+    }
+}

+ 50 - 0
tests/Console/Command/DescribeNameNotFoundExceptionTest.php

@@ -0,0 +1,50 @@
+<?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\Console\Command;
+
+use PhpCsFixer\Console\Command\DescribeNameNotFoundException;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @author Andreas Möller <am@localheinz.com>
+ *
+ * @internal
+ *
+ * @covers \PhpCsFixer\Console\Command\DescribeNameNotFoundException
+ */
+final class DescribeNameNotFoundExceptionTest extends TestCase
+{
+    public function testIsInvalidArgumentException()
+    {
+        $exception = new DescribeNameNotFoundException(
+            'Peter',
+            'weird'
+        );
+
+        $this->assertInstanceOf('InvalidArgumentException', $exception);
+    }
+
+    public function testConstructorSetsValues()
+    {
+        $name = 'Peter';
+        $type = 'weird';
+
+        $exception = new DescribeNameNotFoundException(
+            $name,
+            $type
+        );
+
+        $this->assertSame($name, $exception->getName());
+        $this->assertSame($type, $exception->getType());
+    }
+}

+ 50 - 0
tests/Linter/LintingExceptionTest.php

@@ -0,0 +1,50 @@
+<?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\LintingException;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @author Andreas Möller <am@localheinz.com>
+ *
+ * @internal
+ *
+ * @covers \PhpCsFixer\Linter\LintingException
+ */
+final class LintingExceptionTest extends TestCase
+{
+    public function testIsRuntimeException()
+    {
+        $exception = new LintingException();
+
+        $this->assertInstanceOf('RuntimeException', $exception);
+    }
+
+    public function testConstructorSetsValues()
+    {
+        $message = 'Cannot lint this, sorry!';
+        $code = 9001;
+        $previous = new \RuntimeException();
+
+        $exception = new LintingException(
+            $message,
+            $code,
+            $previous
+        );
+
+        $this->assertSame($message, $exception->getMessage());
+        $this->assertSame($code, $exception->getCode());
+        $this->assertSame($previous, $exception->getPrevious());
+    }
+}

+ 50 - 0
tests/Linter/UnavailableLinterExceptionTest.php

@@ -0,0 +1,50 @@
+<?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\UnavailableLinterException;
+use PHPUnit\Framework\TestCase;
+
+/**
+ * @author Andreas Möller <am@localheinz.com>
+ *
+ * @internal
+ *
+ * @covers \PhpCsFixer\Linter\UnavailableLinterException
+ */
+final class UnavailableLinterExceptionTest extends TestCase
+{
+    public function testIsRuntimeException()
+    {
+        $exception = new UnavailableLinterException();
+
+        $this->assertInstanceOf('RuntimeException', $exception);
+    }
+
+    public function testConstructorSetsValues()
+    {
+        $message = 'Never heard of that one, sorry!';
+        $code = 9001;
+        $previous = new \RuntimeException();
+
+        $exception = new UnavailableLinterException(
+            $message,
+            $code,
+            $previous
+        );
+
+        $this->assertSame($message, $exception->getMessage());
+        $this->assertSame($code, $exception->getCode());
+        $this->assertSame($previous, $exception->getPrevious());
+    }
+}