CsvTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 94
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testRead() 0 19 1
A testWrongRead() 0 11 1
A testReset() 0 19 1
A testWrite() 0 22 1
1
<?php
2
3
namespace Maketok\DataMigration\Input;
4
5
use org\bovigo\vfs\vfsStream;
6
use org\bovigo\vfs\vfsStreamDirectory;
7
8
class CsvTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @var vfsStreamDirectory
12
     */
13
    protected $root;
14
15
    public function setUp()
16
    {
17
        $this->root = vfsStream::setup('root');
18
    }
19
20
    public function testRead()
21
    {
22
        $content = <<<CSV
23
id,name
24
1,John
25
2,Peter
26
CSV;
27
        vfsStream::newFile('test.csv')->setContent($content)->at($this->root);
28
        $csv = new Csv(vfsStream::url('root/test.csv'));
29
        $this->assertSame([
30
            'id' => '1',
31
            'name' => 'John',
32
        ], $csv->get());
33
        $this->assertSame([
34
            'id' => '2',
35
            'name' => 'Peter',
36
        ], $csv->get());
37
        $this->assertSame(false, $csv->get());
38
    }
39
40
    /**
41
     * @expectedException \Maketok\DataMigration\Storage\Exception\ParsingException
42
     * @expectedExceptionMessage Row contains wrong number of columns compared to header
43
     */
44
    public function testWrongRead()
45
    {
46
        $content = <<<CSV
47
id,name
48
1,John,Smith
49
2,Peter
50
CSV;
51
        vfsStream::newFile('test.csv')->setContent($content)->at($this->root);
52
        $csv = new Csv(vfsStream::url('root/test.csv'));
53
        $csv->get();
54
    }
55
56
    /**
57
     * @depends testRead
58
     */
59
    public function testReset()
60
    {
61
        $content = <<<CSV
62
id,name
63
1,John
64
2,Peter
65
CSV;
66
        vfsStream::newFile('test.csv')->setContent($content)->at($this->root);
67
        $csv = new Csv(vfsStream::url('root/test.csv'));
68
        $this->assertSame([
69
            'id' => '1',
70
            'name' => 'John',
71
        ], $csv->get());
72
        $csv->reset();
73
        $this->assertSame([
74
            'id' => '1',
75
            'name' => 'John',
76
        ], $csv->get());
77
    }
78
79
    public function testWrite()
80
    {
81
        $file = vfsStream::newFile('test.csv');
82
        $file->at($this->root);
83
        $csv = new Csv(vfsStream::url('root/test.csv'), 'w');
84
        $csv->add([
85
            'id' => '1',
86
            'name' => 'John',
87
        ]);
88
        $csv->add([
89
            'id' => '2',
90
            'name' => 'Peter',
91
        ]);
92
93
        $expected = <<<CSV
94
id,name
95
1,John
96
2,Peter
97
98
CSV;
99
        $this->assertEquals($expected, $file->getContent());
100
    }
101
}
102