ReporterFactoryTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Report;
  12. use PhpCsFixer\Report\ReporterFactory;
  13. use PHPUnit\Framework\TestCase;
  14. /**
  15. * @author Boris Gorbylev <ekho@ekho.name>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Report\ReporterFactory
  20. */
  21. final class ReporterFactoryTest extends TestCase
  22. {
  23. public function testCreate()
  24. {
  25. $factory = ReporterFactory::create();
  26. $this->assertInstanceOf(\PhpCsFixer\Report\ReporterFactory::class, $factory);
  27. }
  28. public function testInterfaceIsFluent()
  29. {
  30. $builder = new ReporterFactory();
  31. $testInstance = $builder->registerBuiltInReporters();
  32. $this->assertSame($builder, $testInstance);
  33. $double = $this->createReporterDouble('r1');
  34. $testInstance = $builder->registerReporter($double);
  35. $this->assertSame($builder, $testInstance);
  36. }
  37. public function testRegisterBuiltInReports()
  38. {
  39. $builder = new ReporterFactory();
  40. $this->assertCount(0, $builder->getFormats());
  41. $builder->registerBuiltInReporters();
  42. $this->assertSame(
  43. ['checkstyle', 'json', 'junit', 'txt', 'xml'],
  44. $builder->getFormats()
  45. );
  46. }
  47. public function testThatCanRegisterAndGetReports()
  48. {
  49. $builder = new ReporterFactory();
  50. $r1 = $this->createReporterDouble('r1');
  51. $r2 = $this->createReporterDouble('r2');
  52. $r3 = $this->createReporterDouble('r3');
  53. $builder->registerReporter($r1);
  54. $builder->registerReporter($r2);
  55. $builder->registerReporter($r3);
  56. $this->assertSame($r1, $builder->getReporter('r1'));
  57. $this->assertSame($r2, $builder->getReporter('r2'));
  58. $this->assertSame($r3, $builder->getReporter('r3'));
  59. }
  60. public function testGetFormats()
  61. {
  62. $builder = new ReporterFactory();
  63. $r1 = $this->createReporterDouble('r1');
  64. $r2 = $this->createReporterDouble('r2');
  65. $r3 = $this->createReporterDouble('r3');
  66. $builder->registerReporter($r1);
  67. $builder->registerReporter($r2);
  68. $builder->registerReporter($r3);
  69. $this->assertSame(['r1', 'r2', 'r3'], $builder->getFormats());
  70. }
  71. public function testRegisterReportWithOccupiedFormat()
  72. {
  73. $this->setExpectedException(
  74. \UnexpectedValueException::class,
  75. 'Reporter for format "non_unique_name" is already registered.'
  76. );
  77. $factory = new ReporterFactory();
  78. $r1 = $this->createReporterDouble('non_unique_name');
  79. $r2 = $this->createReporterDouble('non_unique_name');
  80. $factory->registerReporter($r1);
  81. $factory->registerReporter($r2);
  82. }
  83. public function testGetNonRegisteredReport()
  84. {
  85. $this->setExpectedException(
  86. \UnexpectedValueException::class,
  87. 'Reporter for format "non_registered_format" is not registered.'
  88. );
  89. $builder = new ReporterFactory();
  90. $builder->getReporter('non_registered_format');
  91. }
  92. private function createReporterDouble($format)
  93. {
  94. $reporter = $this->prophesize(\PhpCsFixer\Report\ReporterInterface::class);
  95. $reporter->getFormat()->willReturn($format);
  96. return $reporter->reveal();
  97. }
  98. }