AbstractStreamWriterTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\Writer;
  3. class AbstractStreamWriterTest extends StreamWriterTest
  4. {
  5. protected function setUp()
  6. {
  7. $this->writer = $this->getMockForAbstractClass('Ddeboer\\DataImport\\Writer\\AbstractStreamWriter');
  8. }
  9. public function testItImplementsWriterInterface()
  10. {
  11. $this->assertInstanceOf('Ddeboer\\DataImport\\Writer', $this->writer);
  12. }
  13. public function testItThrowsInvalidArgumentExceptionOnInvalidStream()
  14. {
  15. $invalidStreams = array(0, 1, null, 'stream', new \stdClass());
  16. foreach ($invalidStreams as $invalidStream) {
  17. try {
  18. $this->writer->setStream($invalidStream);
  19. $this->fail('Above call should throw exception');
  20. } catch (\InvalidArgumentException $exception) {
  21. $this->assertContains('Expects argument to be a stream resource', $exception->getMessage());
  22. }
  23. }
  24. }
  25. public function testGetStreamReturnsAStreamResource()
  26. {
  27. $this->assertTrue('resource' == gettype($stream = $this->writer->getStream()), 'getStream should return a resource');
  28. $this->assertEquals('stream', get_resource_type($stream));
  29. }
  30. public function testSetStream()
  31. {
  32. $this->assertSame($this->writer, $this->writer->setStream($this->getStream()));
  33. $this->assertSame($this->getStream(), $this->writer->getStream());
  34. }
  35. public function testCloseOnFinishIsInhibitable()
  36. {
  37. $this->assertTrue($this->writer->getCloseStreamOnFinish());
  38. $this->assertSame($this->writer, $this->writer->setCloseStreamOnFinish(false));
  39. $this->assertFalse($this->writer->getCloseStreamOnFinish());
  40. $this->assertSame($this->writer, $this->writer->setCloseStreamOnFinish(true));
  41. $this->assertTrue($this->writer->getCloseStreamOnFinish());
  42. }
  43. public function testCloseOnFinishIsFalseForAutoOpenedStreams()
  44. {
  45. $this->writer->setCloseStreamOnFinish(true);
  46. $this->writer->getStream();
  47. $this->assertFalse($this->writer->getCloseStreamOnFinish());
  48. }
  49. public function testFinishCloseStreamAccordingToCloseOnFinishState()
  50. {
  51. $stream = $this->getStream();
  52. $this->writer->setStream($stream);
  53. $this->writer->prepare();
  54. $this->writer->setCloseStreamOnFinish(false);
  55. $this->writer->finish();
  56. $this->assertTrue(is_resource($stream));
  57. $this->writer->setCloseStreamOnFinish(true);
  58. $this->writer->finish();
  59. $this->assertFalse(is_resource($stream));
  60. }
  61. }