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