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