summaryrefslogtreecommitdiffstats
path: root/test/CsvTest.php
blob: f25a430affc25e595b89b4f754715af276d2589b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

namespace League\Csv\Test;

use DOMDocument;
use IteratorAggregate;
use JsonSerializable;
use League\Csv\Reader;
use SplTempFileObject;

/**
 * @group csv
 */
class CsvTest extends AbstractTestCase
{
    private $csv;

    private $expected = [
        ['john', 'doe', 'john.doe@example.com'],
        ['jane','doe','jane.doe@example.com'],
    ];

    public function setUp()
    {
        $tmp = new SplTempFileObject();
        foreach ($this->expected as $row) {
            $tmp->fputcsv($row);
        }

        $this->csv = Reader::createFromFileObject($tmp);
    }

    public function tearDown()
    {
        $this->csv = null;
    }

    public function testInterface()
    {
        $this->assertInstanceOf(IteratorAggregate::class, $this->csv);
        $this->assertInstanceOf(JsonSerializable::class, $this->csv);
    }

    public function testToHTML()
    {
        $this->assertContains('<table', $this->csv->toHTML());
    }

    public function testToXML()
    {
        $this->assertInstanceOf(DOMDocument::class, $this->csv->toXML());
    }

    public function testJsonSerialize()
    {
        $this->assertSame($this->expected, json_decode(json_encode($this->csv), true));
    }

    /**
     * @param $rawCsv
     *
     * @dataProvider getIso8859Csv
     */
    public function testJsonSerializeAffectedByReaderOptions($rawCsv)
    {
        $csv = Reader::createFromString($rawCsv);
        $csv->setEncodingFrom('iso-8859-15');
        $csv->setOffset(799);
        $csv->setLimit(50);
        json_encode($csv);
        $this->assertEquals(JSON_ERROR_NONE, json_last_error());
    }

    public static function getIso8859Csv()
    {
        return [[file_get_contents(__DIR__.'/data/prenoms.csv')]];
    }

    /**
     * @runInSeparateProcess
     */
    public function testOutputSize()
    {
        $this->assertSame(60, $this->csv->output(__DIR__.'/data/test.csv'));
    }

    /**
     * @runInSeparateProcess
     */
    public function testOutputHeaders()
    {
        if (!function_exists('xdebug_get_headers')) {
            $this->markTestSkipped();
        }
        $this->csv->output('test.csv');
        $headers = \xdebug_get_headers();

        // Due to the variety of ways the xdebug expresses Content-Type of text files,
        // we cannot count on complete string matching.
        $this->assertContains('content-type: text/csv', strtolower($headers[0]));
        $this->assertSame($headers[1], 'Content-Transfer-Encoding: binary');
        $this->assertSame($headers[2], 'Content-Disposition: attachment; filename="test.csv"');
    }

    public function testToString()
    {
        $expected = "john,doe,john.doe@example.com\njane,doe,jane.doe@example.com\n";
        $this->assertSame($expected, $this->csv->__toString());
    }
}