summaryrefslogtreecommitdiff
path: root/tests/units/Core/CsvTest.php
blob: da0be4a34216bb62b2ebd2accb0cd38e43a2d8fb (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
<?php

require_once __DIR__.'/../Base.php';

use Kanboard\Core\Csv;

class CsvTest extends Base
{
    public function testGetBooleanValue()
    {
        $this->assertEquals(1, Csv::getBooleanValue('1'));
        $this->assertEquals(1, Csv::getBooleanValue('True'));
        $this->assertEquals(1, Csv::getBooleanValue('t'));
        $this->assertEquals(1, Csv::getBooleanValue('TRUE'));
        $this->assertEquals(1, Csv::getBooleanValue('true'));
        $this->assertEquals(1, Csv::getBooleanValue('T'));
        $this->assertEquals(1, Csv::getBooleanValue('Y'));
        $this->assertEquals(1, Csv::getBooleanValue('y'));
        $this->assertEquals(1, Csv::getBooleanValue('yes'));
        $this->assertEquals(1, Csv::getBooleanValue('Yes'));

        $this->assertEquals(0, Csv::getBooleanValue('0'));
        $this->assertEquals(0, Csv::getBooleanValue('123'));
        $this->assertEquals(0, Csv::getBooleanValue('anything'));
    }

    public function testGetEnclosures()
    {
        $this->assertCount(3, Csv::getEnclosures());
        $this->assertCount(4, Csv::getDelimiters());
    }

    public function testReadWrite()
    {
        $filename = tempnam(sys_get_temp_dir(), 'UT');
        $rows = array(
            array('Column A', 'Column B'),
            array('value a', 'value b'),
        );

        $csv = new Csv;
        $csv->write($filename, $rows);
        $csv->setColumnMapping(array('A', 'B', 'C'));
        $csv->read($filename, array($this, 'readRow'));

        unlink($filename);

        $this->expectOutputString('"Column A","Column B"'."\n".'"value a","value b"'."\n", $csv->output($rows));
    }

    public function readRow(array $row, $line)
    {
        $this->assertEquals(array('value a', 'value b', ''), $row);
        $this->assertEquals(1, $line);
    }
}