Completed
Push — master ( 5c5d2c...78e559 )
by Oleg
01:33
created

CsvTest::testNonExistingFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlow\Provider;
5
6
use org\bovigo\vfs\vfsStream;
7
use org\bovigo\vfs\vfsStreamDirectory;
8
use PHPUnit\Framework\TestCase;
9
use SlayerBirden\DataFlow\Provider\Exception\RowInvalid;
10
11
/**
12
 * Required to have this override for testing with realpath
13
 * Inconsistent return to comply with the original PHP function http://php.net/manual/ru/function.realpath.php
14
 *
15
 * @param string $path
16
 * @return string|false
17
 */
18
function realpath(string $path)
19
{
20
    if (CsvTest::getRoot()->hasChild(basename($path))) {
21
        return $path;
22
    }
23
    return false;
24
}
25
26
class CsvTest extends TestCase
27
{
28
    /**
29
     * @var vfsStreamDirectory
30
     */
31
    private static $root;
32
33
    public static function setUpBeforeClass()
34
    {
35
        self::$root = vfsStream::setup();
36
    }
37
38
    public static function getRoot(): vfsStreamDirectory
39
    {
40
        return self::$root;
41
    }
42
43
    protected function setUp()
44
    {
45
        $file = fopen(self::$root->url() . '/users.csv', 'w');
46
        fwrite($file, "firstname,lastname\n");
47
        fwrite($file, "John,Doe\n");
48
        fclose($file);
49
    }
50
51
    /**
52
     * @expectedException \SlayerBirden\DataFlow\Provider\Exception\HeaderMissing
53
     */
54
    public function testNoHeader()
55
    {
56
        $file = new \SplFileObject(self::$root->getChild('users.csv')->url());
57
        $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY);
58
        new Csv('testId', $file, false);
59
    }
60
61
    public function testGetCask()
62
    {
63
        $file = new \SplFileObject(self::$root->getChild('users.csv')->url());
64
        $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY);
65
        $csv = new Csv('testId', $file);
66
67
        $cask = $csv->getCask();
68
        $actual = [];
69
        foreach ($cask as $row) {
70
            $actual[] = $row->toArray();
71
        }
72
73
        $this->assertEquals([
74
            [
75
                'firstname' => 'John',
76
                'lastname' => 'Doe',
77
            ]
78
        ], $actual);
79
    }
80
81
    public function testInvalidHeader()
82
    {
83
        $file = new \SplFileObject(self::$root->getChild('users.csv')->url());
84
        $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY);
85
        $csv = new Csv('testId', $file, true, [
86
            'firstname',
87
            'lastname',
88
            'age'
89
        ]);
90
        $cask = $csv->getCask();
91
        // trigger generator
92
        foreach ($cask as $row) {
93
            $this->assertInstanceOf(RowInvalid::class, $row);
94
        }
95
    }
96
97
    public function testGetCaskWithOverridenHeader()
98
    {
99
        $file = new \SplFileObject(self::$root->getChild('users.csv')->url());
100
        $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY);
101
        $csv = new Csv('testId', $file, true, [
102
            'first',
103
            'last',
104
        ]);
105
        $cask = $csv->getCask();
106
107
        $actual = [];
108
        foreach ($cask as $row) {
109
            $actual[] = $row->toArray();
110
        }
111
112
        $this->assertEquals([
113
            [
114
                'first' => 'John',
115
                'last' => 'Doe',
116
            ]
117
        ], $actual);
118
    }
119
120
    public function testGetEstimatedSize()
121
    {
122
        $file = new \SplFileObject(self::$root->getChild('users.csv')->url());
123
        $file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY);
124
        $csv = new Csv('testId', $file);
125
126
        $this->assertSame(1, $csv->getEstimatedSize());
127
    }
128
}
129