summaryrefslogtreecommitdiffstats
path: root/test/FactoryTest.php
blob: f696176d73e91e8e9c1881fd2b0f65e4f9a10fd5 (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
<?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('League\Csv\Reader', $reader);
    }

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

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