summaryrefslogtreecommitdiffstats
path: root/test/FactoryTest.php
blob: 50089792c73f35733ad12077f76092deee193284 (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
<?php

namespace League\Csv\Test;

use ArrayIterator;
use League\Csv\Reader;
use SplFileInfo;
use SplFileObject;
use SplTempFileObject;

/**
 * @group factory
 */
class FactoryTest extends AbstractTestCase
{
    public function testCreateFromPathWithFilePath()
    {
        $path = __DIR__.'/data/foo.csv';
        $csv  = Reader::createFromPath($path);
        $this->assertSame($path, $csv->getIterator()->getRealPath());
    }

    public function testCreateFromPathWithSplFileInfo()
    {
        $path = __DIR__.'/data/foo.csv';
        $csv  = Reader::createFromPath(new SplFileInfo($path));
        $this->assertSame($path, $csv->getIterator()->getRealPath());
    }

    public function testCreateFromPathWithPHPWrapper()
    {
        $path = __DIR__.'/data/foo.csv';
        $csv = Reader::createFromPath('php://filter/read=string.toupper/resource='.$path);
        $this->assertFalse($csv->getIterator()->getRealPath());
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function testCreateFromPathWithSplTempFileObject()
    {
        Reader::createFromPath(new SplTempFileObject());
    }

    /**
     * @expectedException InvalidArgumentException
     */
    public function testCreateFromPathWithInvalidObject()
    {
        Reader::createFromPath(new ArrayIterator([]));
    }

    public function testCreateFromString()
    {
        $expected = 'john,doe,john.doe@example.com'.PHP_EOL
            .'jane,doe,jane.doe@example.com'.PHP_EOL;
        $reader = Reader::createFromString($expected);
        $this->assertInstanceof(Reader::class, $reader);
    }

    public function testCreateFromFileObject()
    {
        $reader = Reader::createFromFileObject(new SplTempFileObject());
        $this->assertInstanceof(Reader::class, $reader);
        $this->assertInstanceof(SplTempFileObject::class, $reader->getIterator());
    }

    public function testCreateFromFileObjectWithSplFileObject()
    {
        $path   = __DIR__.'/data/foo.csv';
        $obj    = new SplFileObject($path);
        $reader = Reader::createFromFileObject($obj);
        $this->assertInstanceof(Reader::class, $reader);
        $this->assertInstanceof(SplFileObject::class, $reader->getIterator());
    }


    public function testCreateFromFileObjectPreserveFileObjectCsvControls()
    {
        $delimiter = "\t";
        $enclosure = '?';
        $escape = '^';
        $file = new SplTempFileObject();
        $file->setCsvControl($delimiter, $enclosure, $escape);
        $obj = Reader::createFromFileObject($file);
        $this->assertSame($delimiter, $obj->getDelimiter());
        $this->assertSame($enclosure, $obj->getEnclosure());
        if (3 === count($file->getCsvControl())) {
            $this->assertSame($escape, $obj->getEscape());
        }
    }
}