testHeaderPrependedWhenOptionSetToTrue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace Ddeboer\DataImport\Tests\Writer;
4
5
use Ddeboer\DataImport\Writer\CsvWriter;
6
7
class CsvWriterTest extends StreamWriterTest
8
{
9
    public function testWriteItem()
10
    {
11
        $writer = new CsvWriter(';', '"', $this->getStream());
12
13
        $writer->prepare();
14
        $writer->writeItem(array('first', 'last'));
15
16
        $writer->writeItem(array(
17
            'first' => 'James',
18
            'last'  => 'Bond'
19
        ));
20
21
        $writer->writeItem(array(
22
            'first' => '',
23
            'last'  => 'Dr. No'
24
        ));
25
26
        $this->assertContentsEquals(
27
            "first;last\nJames;Bond\n;\"Dr. No\"\n",
28
            $writer
29
        );
30
31
        $writer->finish();
32
    }
33
34
    public function testWriteUtf8Item()
35
    {
36
        $writer = new CsvWriter(';', '"', $this->getStream(), true);
37
38
        $writer->prepare();
39
        $writer->writeItem(array('Précédent', 'Suivant'));
40
41
        $this->assertContentsEquals(
42
            chr(0xEF) . chr(0xBB) . chr(0xBF) . "Précédent;Suivant\n",
43
            $writer
44
        );
45
46
        $writer->finish();
47
    }
48
49
    /**
50
     * Test that column names not prepended to first row
51
     * if CsvWriter's 5-th parameter not given
52
     *
53
     * @author  Igor Mukhin <[email protected]>
54
     */
55
    public function testHeaderNotPrependedByDefault()
56
    {
57
        $writer = new CsvWriter(';', '"', $this->getStream(), false);
58
        $writer->prepare();
59
        $writer->writeItem(array(
60
            'col 1 name'=>'col 1 value',
61
            'col 2 name'=>'col 2 value',
62
            'col 3 name'=>'col 3 value'
63
        ));
64
65
        # Values should be at first line
66
        $this->assertContentsEquals(
67
            "\"col 1 value\";\"col 2 value\";\"col 3 value\"\n",
68
            $writer
69
        );
70
        $writer->finish();
71
    }
72
73
    /**
74
     * Test that column names prepended at first row
75
     * and values have been written at second line
76
     * if CsvWriter's 5-th parameter set to true
77
     *
78
     * @author  Igor Mukhin <[email protected]>
79
     */
80
    public function testHeaderPrependedWhenOptionSetToTrue()
81
    {
82
        $writer = new CsvWriter(';', '"', $this->getStream(), false, true);
83
        $writer->prepare();
84
        $writer->writeItem(array(
85
            'col 1 name'=>'col 1 value',
86
            'col 2 name'=>'col 2 value',
87
            'col 3 name'=>'col 3 value'
88
        ));
89
90
        # Column names should be at second line
91
        # Values should be at second line
92
        $this->assertContentsEquals(
93
            "\"col 1 name\";\"col 2 name\";\"col 3 name\"\n" .
94
            "\"col 1 value\";\"col 2 value\";\"col 3 value\"\n",
95
            $writer
96
        );
97
        $writer->finish();
98
    }
99
}
100