ExporterIteratorTest.php 2.1 KB

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