Completed
Push — master ( 7a3cb1...e42624 )
by Harry
9s
created

LineStreamIteratorTest::lineStreamData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 46
rs 8.9411
cc 1
eloc 24
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\Helper;
15
16
use Graze\DataFile\Helper\LineStreamIterator;
17
use Graze\DataFile\Test\Helper\CreateStreamTrait;
18
use Graze\DataFile\Test\TestCase;
19
20
class LineStreamIteratorTest extends TestCase
21
{
22
    use CreateStreamTrait;
23
24
    /**
25
     * @dataProvider lineStreamData
26
     *
27
     * @param string   $string
28
     * @param array    $options
29
     * @param string[] $expected
30
     */
31
    public function testLineStream($string, array $options, array $expected)
32
    {
33
        $stream = $this->createStream($string);
34
35
        $iterator = new LineStreamIterator($stream, $options);
36
37
        static::assertEquals($expected, iterator_to_array($iterator));
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function lineStreamData()
44
    {
45
        return [
46
            [
47
                <<<TEXT
48
Line 1
49
Line 2
50
Line 3
51
52
Line 5
53
Line 6
54
TEXT
55
                ,
56
                [
57
                    LineStreamIterator::OPTION_ENDING         => "\n",
58
                    LineStreamIterator::OPTION_IGNORE_BLANK   => false,
59
                    LineStreamIterator::OPTION_INCLUDE_ENDING => false,
60
                ],
61
                [
62
                    "Line 1",
63
                    "Line 2",
64
                    "Line 3",
65
                    "",
66
                    "Line 5",
67
                    "Line 6",
68
                ],
69
            ],
70
            [
71
                <<<TEXT
72
Line 1---Line 2---Line 3---Line 4
73
TEXT
74
                ,
75
                [
76
                    LineStreamIterator::OPTION_ENDING         => '---',
77
                    LineStreamIterator::OPTION_IGNORE_BLANK   => true,
78
                    LineStreamIterator::OPTION_INCLUDE_ENDING => true,
79
                ],
80
                [
81
                    "Line 1---",
82
                    "Line 2---",
83
                    "Line 3---",
84
                    "Line 4",
85
                ],
86
            ],
87
        ];
88
    }
89
90
    public function testIteratorMethods()
91
    {
92
        $stream = $this->createStream("Line 1\nLine 2");
93
94
        $iterator = new LineStreamIterator($stream);
95
96
        $iterator->rewind();
97
        static::assertEquals(0, $iterator->key());
98
        static::assertEquals("Line 1", $iterator->current());
99
100
        $iterator->next();
101
        static::assertEquals(1, $iterator->key());
102
        static::assertEquals("Line 2", $iterator->current());
103
    }
104
105
    public function testDefaultOptions()
106
    {
107
        $stream = $this->createStream("Test");
108
        $iterator = new LineStreamIterator($stream);
109
110
        static::assertEquals("\n", $iterator->getEnding());
111
        static::assertEquals(true, $iterator->isIgnoreBlank());
112
        static::assertEquals(false, $iterator->isIncludeEnding());
113
    }
114
115
    public function testCustomOptions()
116
    {
117
        $stream = $this->createStream("Test");
118
        $iterator = new LineStreamIterator($stream, [
119
            LineStreamIterator::OPTION_ENDING         => '---',
120
            LineStreamIterator::OPTION_IGNORE_BLANK   => false,
121
            LineStreamIterator::OPTION_INCLUDE_ENDING => true,
122
        ]);
123
124
        static::assertEquals("---", $iterator->getEnding());
125
        static::assertEquals(false, $iterator->isIgnoreBlank());
126
        static::assertEquals(true, $iterator->isIncludeEnding());
127
    }
128
129
    public function testSettingOptions()
130
    {
131
        $stream = $this->createStream("Test");
132
        $iterator = new LineStreamIterator($stream);
133
134
        static::assertEquals("\n", $iterator->getEnding());
135
        static::assertEquals(true, $iterator->isIgnoreBlank());
136
        static::assertEquals(false, $iterator->isIncludeEnding());
137
138
        static::assertSame($iterator, $iterator->setEnding("---"));
139
        static::assertEquals('---', $iterator->getEnding());
140
        static::assertSame($iterator, $iterator->setIgnoreBlank(false));
141
        static::assertFalse($iterator->isIgnoreBlank());
142
        static::assertSame($iterator, $iterator->setIncludeEnding(true));
143
        static::assertTrue($iterator->isIncludeEnding());
144
    }
145
}
146