Completed
Push — master ( b1b515...f3b9b5 )
by Harry
04:21
created

StreamWriterTest::testInsertRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test\Unit\IO;
15
16
use ArrayIterator;
17
use Graze\DataFile\Format\CsvFormat;
18
use Graze\DataFile\Format\Formatter\CsvFormatter;
19
use Graze\DataFile\Format\Formatter\FormatterInterface;
20
use Graze\DataFile\IO\StreamWriter;
21
use Graze\DataFile\Test\TestCase;
22
use Mockery as m;
23
24
class StreamWriterTest extends TestCase
25
{
26
    /**
27
     * @var resource
28
     */
29
    private $stream;
30
31
    public function setUp()
32
    {
33
        $this->stream = fopen('php://temp', 'r+');
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    private function getContents()
40
    {
41
        return stream_get_contents($this->stream);
42
    }
43
44 View Code Duplication
    public function testInsertArray()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $data = [
47
            ['a', 'b', 'c', 'd'],
48
            ['e', 'f', 'g', 'h'],
49
        ];
50
51
        $writer = new StreamWriter($this->stream, new CsvFormatter(new CsvFormat()));
52
53
        $writer->insertAll($data);
54
55
        $expected = <<<CSV
56
"a","b","c","d"
57
"e","f","g","h"
58
CSV;
59
60
        fseek($this->stream, 0, SEEK_SET);
61
        static::assertEquals($expected, $this->getContents());
62
    }
63
64 View Code Duplication
    public function testInsertIterator()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $data = new ArrayIterator([
67
            ['a', 'b', 'c', 'd'],
68
            ['e', 'f', 'g', 'h'],
69
        ]);
70
71
        $writer = new StreamWriter($this->stream, new CsvFormatter(new CsvFormat()));
72
73
        $writer->insertAll($data);
74
75
        $expected = <<<CSV
76
"a","b","c","d"
77
"e","f","g","h"
78
CSV;
79
80
        fseek($this->stream, 0, SEEK_SET);
81
        static::assertEquals($expected, $this->getContents());
82
    }
83
84 View Code Duplication
    public function testInsertRow()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        $writer = new StreamWriter($this->stream, new CsvFormatter(new CsvFormat()));
87
88
        $writer->insert(['a', 'b', 'c', 'd']);
89
        $writer->insert(['e', 'f', 'g', 'h']);
90
91
        $expected = <<<CSV
92
"a","b","c","d"
93
"e","f","g","h"
94
CSV;
95
96
        fseek($this->stream, 0, SEEK_SET);
97
        static::assertEquals($expected, $this->getContents());
98
    }
99
100
    public function testFormatterBlocks()
101
    {
102
        $formatter = m::mock(FormatterInterface::class);
103
104
        $writer = new StreamWriter($this->stream, $formatter);
105
106
        $formatter->shouldReceive('getInitialBlock')
107
                  ->andReturn('--init--');
108
        $formatter->shouldReceive('getClosingBlock')
109
                  ->andReturn('--end--');
110
        $formatter->shouldReceive('format')
111
                  ->with(['a', 'b', 'c', 'd'])
112
                  ->andReturn('"a","b","c","d"');
113
        $formatter->shouldReceive('format')
114
                  ->with(['e', 'f', 'g', 'h'])
115
                  ->andReturn('"e","f","g","h"');
116
        $formatter->shouldReceive('format')
117
                  ->with(['i', 'j', 'k', 'l'])
118
                  ->andReturn('"i","j","k","l"');
119
        $formatter->shouldReceive('getRowSeparator')
120
                  ->andReturn("EOL");
121
122
        $writer->insert(['a', 'b', 'c', 'd']);
123
        $writer->insertAll([
124
            ['e', 'f', 'g', 'h'],
125
            ['i', 'j', 'k', 'l'],
126
        ]);
127
128
        $expected = <<<CSV
129
--init--"a","b","c","d"EOL"e","f","g","h"EOL"i","j","k","l"--end--
130
CSV;
131
132
        fseek($this->stream, 0, SEEK_SET);
133
        static::assertEquals($expected, $this->getContents());
134
135
        $formatter->shouldReceive('format')
136
                  ->with(['m', 'n', 'o', 'p'])
137
                  ->andReturn('"m","n","o","p"');
138
139
        $writer->insert(['m', 'n', 'o', 'p']);
140
141
        $expected = <<<CSV
142
--init--"a","b","c","d"EOL"e","f","g","h"EOL"i","j","k","l"EOL"m","n","o","p"--end--
143
CSV;
144
145
        fseek($this->stream, 0, SEEK_SET);
146
        static::assertEquals($expected, $this->getContents());
147
    }
148
}
149