CsvTest::testWrite()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlow\Writer;
5
6
use org\bovigo\vfs\vfsStream;
7
use PHPUnit\Framework\TestCase;
8
use SlayerBirden\DataFlow\Data\SimpleBag;
9
10
class CsvTest extends TestCase
11
{
12
    public function testWrite()
13
    {
14
        $root = vfsStream::setup();
15
        $header = [
16
            'firstname',
17
            'lastname',
18
        ];
19
        $url = $root->url() . '/users.csv';
20
        $file = new \SplFileObject($url, 'w');
21
        $file->fputcsv($header);
22
        $csv = new Csv('testId', $file, $header);
23
24
        $bag = new SimpleBag([
25
            'firstname' => 'Bob',
26
            'lastname' => 'Dawson',
27
        ]);
28
29
        $csv->pass($bag);
30
31
        $expected = <<<FILE
32
firstname,lastname
33
Bob,Dawson
34
35
FILE;
36
37
        $this->assertEquals($expected, file_get_contents($url));
38
    }
39
40
    /**
41
     * @expectedException \SlayerBirden\DataFlow\Writer\Exception\WriteErrorException
42
     */
43
    public function testFailedWrite()
44
    {
45
        $root = vfsStream::setup();
46
        $header = [
47
            'firstname',
48
            'lastname',
49
        ];
50
        // create file
51
        $url = $root->url() . '/users.csv';
52
        $h = fopen($url, 'w');
53
        fclose($h);
54
55
        $file = new \SplFileObject($url, 'r');
56
        $csv = new Csv('testId', $file, $header);
57
        $bag = new SimpleBag([
58
            'firstname' => 'Bob',
59
            'lastname' => 'Dawson',
60
        ]);
61
        $csv->pass($bag);
62
    }
63
}
64