ReporterFactoryTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\Console\Report\FixReport;
  13. use PhpCsFixer\Console\Report\FixReport\ReporterFactory;
  14. use PhpCsFixer\Console\Report\FixReport\ReporterInterface;
  15. use PhpCsFixer\Console\Report\FixReport\ReportSummary;
  16. use PhpCsFixer\Tests\TestCase;
  17. /**
  18. * @author Boris Gorbylev <ekho@ekho.name>
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\Console\Report\FixReport\ReporterFactory
  23. */
  24. final class ReporterFactoryTest extends TestCase
  25. {
  26. public function testInterfaceIsFluent(): void
  27. {
  28. $builder = new ReporterFactory();
  29. $testInstance = $builder->registerBuiltInReporters();
  30. self::assertSame($builder, $testInstance);
  31. $double = $this->createReporterDouble('r1');
  32. $testInstance = $builder->registerReporter($double);
  33. self::assertSame($builder, $testInstance);
  34. }
  35. public function testRegisterBuiltInReports(): void
  36. {
  37. $builder = new ReporterFactory();
  38. self::assertCount(0, $builder->getFormats());
  39. $builder->registerBuiltInReporters();
  40. self::assertSame(
  41. ['checkstyle', 'gitlab', 'json', 'junit', 'txt', 'xml'],
  42. $builder->getFormats()
  43. );
  44. }
  45. public function testThatCanRegisterAndGetReports(): void
  46. {
  47. $builder = new ReporterFactory();
  48. $r1 = $this->createReporterDouble('r1');
  49. $r2 = $this->createReporterDouble('r2');
  50. $r3 = $this->createReporterDouble('r3');
  51. $builder->registerReporter($r1);
  52. $builder->registerReporter($r2);
  53. $builder->registerReporter($r3);
  54. self::assertSame($r1, $builder->getReporter('r1'));
  55. self::assertSame($r2, $builder->getReporter('r2'));
  56. self::assertSame($r3, $builder->getReporter('r3'));
  57. }
  58. public function testGetFormats(): void
  59. {
  60. $builder = new ReporterFactory();
  61. $r1 = $this->createReporterDouble('r1');
  62. $r2 = $this->createReporterDouble('r2');
  63. $r3 = $this->createReporterDouble('r3');
  64. $builder->registerReporter($r1);
  65. $builder->registerReporter($r2);
  66. $builder->registerReporter($r3);
  67. self::assertSame(['r1', 'r2', 'r3'], $builder->getFormats());
  68. }
  69. public function testRegisterReportWithOccupiedFormat(): void
  70. {
  71. $this->expectException(\UnexpectedValueException::class);
  72. $this->expectExceptionMessage('Reporter for format "non_unique_name" is already registered.');
  73. $factory = new ReporterFactory();
  74. $r1 = $this->createReporterDouble('non_unique_name');
  75. $r2 = $this->createReporterDouble('non_unique_name');
  76. $factory->registerReporter($r1);
  77. $factory->registerReporter($r2);
  78. }
  79. public function testGetNonRegisteredReport(): void
  80. {
  81. $this->expectException(\UnexpectedValueException::class);
  82. $this->expectExceptionMessage('Reporter for format "non_registered_format" is not registered.');
  83. $builder = new ReporterFactory();
  84. $builder->getReporter('non_registered_format');
  85. }
  86. private function createReporterDouble(string $format): ReporterInterface
  87. {
  88. return new class($format) implements ReporterInterface {
  89. private string $format;
  90. public function __construct(string $format)
  91. {
  92. $this->format = $format;
  93. }
  94. public function getFormat(): string
  95. {
  96. return $this->format;
  97. }
  98. public function generate(ReportSummary $reportSummary): string
  99. {
  100. throw new \LogicException('Not implemented.');
  101. }
  102. };
  103. }
  104. }