ExporterIteratorTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Tests\Service\ServiceIterator;
  3. use App\ApiResources\Export\ExportRequest;
  4. use App\Service\Export\ExporterInterface;
  5. use App\Service\ServiceIterator\ExporterIterator;
  6. use Exception;
  7. use PHPUnit\Framework\TestCase;
  8. class ExporterIteratorTest extends TestCase
  9. {
  10. /**
  11. * @see ExporterIterator::getExporterFor()
  12. */
  13. public function testGetExporterFor(): void
  14. {
  15. $exportRequest = $this->getMockBuilder(ExportRequest::class)
  16. ->disableOriginalConstructor()
  17. ->getMock();
  18. $mocker = $this->getMockBuilder(ExporterInterface::class);
  19. $exporter1 = $mocker->getMock();
  20. $exporter1->method('support')->willReturn(false);
  21. $exporter2 = $mocker->getMock();
  22. $exporter2->expects($this->once())->method('support')->with($exportRequest)->willReturn(true);
  23. $exporter3 = $mocker->getMock();
  24. $exporter3->method('support')->willReturn(false);
  25. $exporters = [$exporter1, $exporter2, $exporter3];
  26. $iterator = $this->getMockBuilder(ExporterIterator::class)
  27. ->setConstructorArgs([$exporters])
  28. ->setMethodsExcept(['getExporterFor'])
  29. ->getMock();
  30. $actualExporter = $iterator->getExporterFor($exportRequest);
  31. $this->assertEquals($exporter2, $actualExporter);
  32. }
  33. /**
  34. * @see ExporterIterator::getExporterFor()
  35. */
  36. public function testGetExporterForError(): void
  37. {
  38. $exportRequest = $this->getMockBuilder(ExportRequest::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $mocker = $this->getMockBuilder(ExporterInterface::class);
  42. $exporter1 = $mocker->getMock();
  43. $exporter1->method('support')->willReturn(false);
  44. $exporter2 = $mocker->getMock();
  45. $exporter2->method('support')->willReturn(false);
  46. $exporters = [$exporter1, $exporter2];
  47. $iterator = $this->getMockBuilder(ExporterIterator::class)
  48. ->setConstructorArgs([$exporters])
  49. ->setMethodsExcept(['getExporterFor'])
  50. ->getMock();
  51. $this->expectException(Exception::class);
  52. $iterator->getExporterFor($exportRequest);
  53. }
  54. }