ErrorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\Error;
  14. use PhpCsFixer\Tests\TestCase;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Error\Error
  19. */
  20. final class ErrorTest extends TestCase
  21. {
  22. public function testConstructorSetsValues(): void
  23. {
  24. $type = 1;
  25. $filePath = 'foo.php';
  26. $error = new Error(
  27. $type,
  28. $filePath
  29. );
  30. self::assertSame($type, $error->getType());
  31. self::assertSame($filePath, $error->getFilePath());
  32. self::assertNull($error->getSource());
  33. self::assertSame([], $error->getAppliedFixers());
  34. self::assertNull($error->getDiff());
  35. }
  36. public function testConstructorSetsValues2(): void
  37. {
  38. $type = 2;
  39. $filePath = __FILE__;
  40. $source = new \Exception();
  41. $appliedFixers = ['some_rule'];
  42. $diff = '__diff__';
  43. $error = new Error(
  44. $type,
  45. $filePath,
  46. $source,
  47. $appliedFixers,
  48. $diff
  49. );
  50. self::assertSame($type, $error->getType());
  51. self::assertSame($filePath, $error->getFilePath());
  52. self::assertSame($source, $error->getSource());
  53. self::assertSame($appliedFixers, $error->getAppliedFixers());
  54. self::assertSame($diff, $error->getDiff());
  55. }
  56. public function testErrorCanBeSerialised(): void
  57. {
  58. $type = 2;
  59. $filePath = __FILE__;
  60. $source = new \Exception();
  61. $appliedFixers = ['some_rule'];
  62. $diff = '__diff__';
  63. $error = new Error(
  64. $type,
  65. $filePath,
  66. $source,
  67. $appliedFixers,
  68. $diff
  69. );
  70. $serialisedError = $error->jsonSerialize();
  71. self::assertSame($type, $serialisedError['type']);
  72. self::assertSame($filePath, $serialisedError['filePath']);
  73. self::assertSame($source->getMessage(), $serialisedError['source']['message']);
  74. self::assertSame($source->getLine(), $serialisedError['source']['line']);
  75. self::assertSame($source->getFile(), $serialisedError['source']['file']);
  76. self::assertSame($source->getCode(), $serialisedError['source']['code']);
  77. self::assertSame($appliedFixers, $serialisedError['appliedFixers']);
  78. self::assertSame($diff, $serialisedError['diff']);
  79. }
  80. }