ErrorsManagerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\Error\ErrorsManager;
  15. use PhpCsFixer\Tests\TestCase;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Error\ErrorsManager
  20. */
  21. final class ErrorsManagerTest extends TestCase
  22. {
  23. public function testDefaults(): void
  24. {
  25. $errorsManager = new ErrorsManager();
  26. self::assertTrue($errorsManager->isEmpty());
  27. self::assertEmpty($errorsManager->getInvalidErrors());
  28. self::assertEmpty($errorsManager->getExceptionErrors());
  29. self::assertEmpty($errorsManager->getLintErrors());
  30. }
  31. public function testThatCanReportAndRetrieveInvalidErrors(): void
  32. {
  33. $error = new Error(
  34. Error::TYPE_INVALID,
  35. 'foo.php'
  36. );
  37. $errorsManager = new ErrorsManager();
  38. $errorsManager->report($error);
  39. self::assertFalse($errorsManager->isEmpty());
  40. $errors = $errorsManager->getInvalidErrors();
  41. self::assertCount(1, $errors);
  42. self::assertSame($error, array_shift($errors));
  43. self::assertCount(0, $errorsManager->getExceptionErrors());
  44. self::assertCount(0, $errorsManager->getLintErrors());
  45. }
  46. public function testThatCanReportAndRetrieveExceptionErrors(): void
  47. {
  48. $error = new Error(
  49. Error::TYPE_EXCEPTION,
  50. 'foo.php'
  51. );
  52. $errorsManager = new ErrorsManager();
  53. $errorsManager->report($error);
  54. self::assertFalse($errorsManager->isEmpty());
  55. $errors = $errorsManager->getExceptionErrors();
  56. self::assertCount(1, $errors);
  57. self::assertSame($error, array_shift($errors));
  58. self::assertCount(0, $errorsManager->getInvalidErrors());
  59. self::assertCount(0, $errorsManager->getLintErrors());
  60. }
  61. public function testThatCanReportAndRetrieveInvalidFileErrors(): void
  62. {
  63. $error = new Error(
  64. Error::TYPE_LINT,
  65. 'foo.php'
  66. );
  67. $errorsManager = new ErrorsManager();
  68. $errorsManager->report($error);
  69. self::assertFalse($errorsManager->isEmpty());
  70. $errors = $errorsManager->getLintErrors();
  71. self::assertCount(1, $errors);
  72. self::assertSame($error, array_shift($errors));
  73. self::assertCount(0, $errorsManager->getInvalidErrors());
  74. self::assertCount(0, $errorsManager->getExceptionErrors());
  75. }
  76. public function testThatCanReportAndRetrieveErrorsForSpecificPath(): void
  77. {
  78. $errorsManager = new ErrorsManager();
  79. // All kind of errors for the same path
  80. $errorsManager->report(new Error(Error::TYPE_LINT, 'foo.php'));
  81. $errorsManager->report(new Error(Error::TYPE_EXCEPTION, 'foo.php'));
  82. $errorsManager->report(new Error(Error::TYPE_INVALID, 'foo.php'));
  83. // Additional errors for a different path
  84. $errorsManager->report(new Error(Error::TYPE_INVALID, 'bar.php'));
  85. $errorsManager->report(new Error(Error::TYPE_LINT, 'baz.php'));
  86. self::assertFalse($errorsManager->isEmpty());
  87. $errors = $errorsManager->forPath('foo.php');
  88. self::assertCount(3, $errors);
  89. }
  90. }