StreamMergeWriterTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\Writer;
  3. use Ddeboer\DataImport\Writer\StreamMergeWriter;
  4. class StreamMergeWriterTest extends AbstractStreamWriterTest
  5. {
  6. /** @var StreamMergeWriter */
  7. protected $writer;
  8. protected function setUp()
  9. {
  10. parent::setUp();
  11. $this->writer = new StreamMergeWriter();
  12. }
  13. public function testItIsInstantiable()
  14. {
  15. $this->assertInstanceOf('Ddeboer\DataImport\Writer\StreamMergeWriter', $this->writer);
  16. }
  17. public function testItIsAStreamWriter()
  18. {
  19. $this->assertInstanceOf('Ddeboer\DataImport\Writer\AbstractStreamWriter', $this->writer);
  20. }
  21. public function testDiscriminantField()
  22. {
  23. $this->assertSame($this->writer, $this->writer->setDiscriminantField('foo'));
  24. $this->assertSame('foo', $this->writer->getDiscriminantField());
  25. }
  26. public function testDiscriminantFieldDefaultsToDiscr()
  27. {
  28. $this->assertSame('discr', $this->writer->getDiscriminantField());
  29. }
  30. public function testSetStreamWriterForSpecificDiscrValue()
  31. {
  32. $fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
  33. ->setMethods(array('setStream'))
  34. ->getMockForAbstractClass();
  35. $this->writer->setStream($stream = $this->getStream());
  36. $fooWriter
  37. ->expects($this->once())
  38. ->method('setStream')
  39. ->with($stream)
  40. ->will($this->returnSelf());
  41. $this->assertSame($this->writer, $this->writer->setStreamWriter('foo', $fooWriter));
  42. $this->assertSame($fooWriter, $this->writer->getStreamWriter('foo'));
  43. }
  44. public function testHasStreamWriter()
  45. {
  46. $fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
  47. ->getMockForAbstractClass();
  48. $this->assertFalse($this->writer->hasStreamWriter('foo'), 'no foo stream writer should be registered');
  49. $this->writer->setStreamWriter('foo', $fooWriter);
  50. $this->assertTrue($this->writer->hasStreamWriter('foo'), 'foo stream writer should be registered');
  51. }
  52. public function testStreamWriters()
  53. {
  54. $fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
  55. ->getMockForAbstractClass();
  56. $barWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
  57. ->getMockForAbstractClass();
  58. $writers = array(
  59. 'foo' => $fooWriter,
  60. 'bar' => $barWriter,
  61. );
  62. $this->assertSame($this->writer, $this->writer->setStreamWriters($writers));
  63. $this->assertSame($writers, $this->writer->getStreamWriters());
  64. }
  65. public function testWriteItem()
  66. {
  67. $fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
  68. ->getMockForAbstractClass();
  69. $barWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
  70. ->getMockForAbstractClass();
  71. $writers = array(
  72. 'foo' => $fooWriter,
  73. 'bar' => $barWriter,
  74. );
  75. $this->writer->setStreamWriters($writers);
  76. $this->writer->setDiscriminantField('foo');
  77. $barItem = array('foo' => 'bar');
  78. $fooWriter->expects($this->never())
  79. ->method('writeItem');
  80. $barWriter->expects($this->once())
  81. ->method('writeItem')
  82. ->with($barItem)
  83. ->will($this->returnSelf());
  84. $this->writer->writeItem($barItem);
  85. }
  86. public function testSetStream()
  87. {
  88. $fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
  89. ->getMockForAbstractClass();
  90. $barWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
  91. ->getMockForAbstractClass();
  92. $writers = array(
  93. 'foo' => $fooWriter,
  94. 'bar' => $barWriter,
  95. );
  96. $stream = $this->writer->getStream();
  97. $this->writer->setStreamWriters($writers);
  98. $this->assertSame($stream, $fooWriter->getStream());
  99. $this->assertSame($stream, $barWriter->getStream());
  100. $this->assertSame($this->writer, $this->writer->setStream($stream = $this->getStream()));
  101. $this->assertSame($stream, $fooWriter->getStream());
  102. $this->assertSame($stream, $barWriter->getStream());
  103. }
  104. public function testSetWriterShouldInhibitStreamClose()
  105. {
  106. $fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
  107. ->setMethods(array('setCloseStreamOnFinish'))
  108. ->getMockForAbstractClass();
  109. $fooWriter
  110. ->expects($this->once())
  111. ->method('setCloseStreamOnFinish')->with(false)
  112. ->will($this->returnArgument(0));
  113. $this->writer->setStreamWriter('foo', $fooWriter);
  114. }
  115. }