SourceExceptionFactoryTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Error;
  13. use PhpCsFixer\Error\SourceExceptionFactory;
  14. use PhpCsFixer\Linter\LintingException;
  15. use PhpCsFixer\Runner\Parallel\WorkerException;
  16. use PhpCsFixer\Tests\TestCase;
  17. /**
  18. * @covers \PhpCsFixer\Error\SourceExceptionFactory
  19. *
  20. * @internal
  21. */
  22. final class SourceExceptionFactoryTest extends TestCase
  23. {
  24. public function testFromArrayWithInstantiableException(): void
  25. {
  26. $exception = SourceExceptionFactory::fromArray([
  27. 'class' => LintingException::class,
  28. 'message' => 'foo',
  29. 'code' => 1,
  30. 'file' => 'foo.php',
  31. 'line' => 1,
  32. ]);
  33. self::assertInstanceOf(LintingException::class, $exception);
  34. self::assertSame('foo', $exception->getMessage());
  35. self::assertSame(1, $exception->getCode());
  36. self::assertSame('foo.php', $exception->getFile());
  37. self::assertSame(1, $exception->getLine());
  38. }
  39. public function testFromArrayWithInstantiableError(): void
  40. {
  41. $error = SourceExceptionFactory::fromArray([
  42. 'class' => \ParseError::class,
  43. 'message' => 'foo',
  44. 'code' => 1,
  45. 'file' => 'foo.php',
  46. 'line' => 1,
  47. ]);
  48. self::assertInstanceOf(\ParseError::class, $error);
  49. self::assertSame('foo', $error->getMessage());
  50. self::assertSame(1, $error->getCode());
  51. self::assertSame('foo.php', $error->getFile());
  52. self::assertSame(1, $error->getLine());
  53. }
  54. public function testFromArrayWithNonInstantiableException(): void
  55. {
  56. $exception = SourceExceptionFactory::fromArray([
  57. 'class' => WorkerException::class,
  58. 'message' => 'foo',
  59. 'code' => 1,
  60. 'file' => 'foo.php',
  61. 'line' => 1,
  62. ]);
  63. self::assertInstanceOf(\RuntimeException::class, $exception);
  64. self::assertSame('[PhpCsFixer\Runner\Parallel\WorkerException] foo', $exception->getMessage());
  65. self::assertSame(1, $exception->getCode());
  66. self::assertSame('foo.php', $exception->getFile());
  67. self::assertSame(1, $exception->getLine());
  68. }
  69. }