ExporterIteratorTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. use App\ApiResources\Export\ExportRequest;
  3. use App\Service\Export\Encoder\EncoderInterface;
  4. use App\Service\Export\ExporterInterface;
  5. use App\Service\ServiceIterator\EncoderIterator;
  6. use App\Service\ServiceIterator\ExporterIterator;
  7. use PHPUnit\Framework\TestCase;
  8. class ExporterIteratorTest extends TestCase
  9. {
  10. public function testGetExporterFor() {
  11. $exportRequest = $this->getMockBuilder(ExportRequest::class)
  12. ->disableOriginalConstructor()
  13. ->getMock();
  14. $mocker = $this->getMockBuilder(ExporterInterface::class);
  15. $exporter1 = $mocker->getMock();
  16. $exporter1->method('support')->willReturn(false);
  17. $exporter2 = $mocker->getMock();
  18. $exporter2->expects($this->once())->method('support')->with($exportRequest)->willReturn(true);
  19. $exporter3 = $mocker->getMock();
  20. $exporter3->method('support')->willReturn(false);
  21. $exporters = [$exporter1, $exporter2, $exporter3];
  22. $iterator = new ExporterIterator($exporters);
  23. $actualExporter = $iterator->getExporterFor($exportRequest);
  24. $this->assertEquals($exporter2, $actualExporter);
  25. }
  26. public function testGetExporterForError() {
  27. $exportRequest = $this->getMockBuilder(ExportRequest::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $mocker = $this->getMockBuilder(ExporterInterface::class);
  31. $exporter1 = $mocker->getMock();
  32. $exporter1->method('support')->willReturn(false);
  33. $exporter2 = $mocker->getMock();
  34. $exporter2->method('support')->willReturn(false);
  35. $exporters = [$exporter1, $exporter2];
  36. $iterator = new ExporterIterator($exporters);
  37. $this->expectException(Exception::class);
  38. $iterator->getExporterFor($exportRequest);
  39. }
  40. }