FileReaderTest::buildFactoryReader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 2
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\FormatAwareInterface;
19
use Graze\DataFile\Format\JsonFormat;
20
use Graze\DataFile\Format\Parser\ParserFactoryInterface;
21
use Graze\DataFile\Format\Parser\ParserInterface;
22
use Graze\DataFile\IO\FileReader;
23
use Graze\DataFile\IO\ReaderInterface;
24
use Graze\DataFile\Node\FileNodeInterface;
25
use Graze\DataFile\Node\NodeStreamInterface;
26
use Graze\DataFile\Test\Helper\CreateStreamTrait;
27
use Graze\DataFile\Test\TestCase;
28
use InvalidArgumentException;
29
use Iterator;
30
use Mockery as m;
31
use Psr\Http\Message\StreamInterface;
32
33
class FileReaderTest extends TestCase
34
{
35
    use CreateStreamTrait;
36
37
    /**
38
     * @param resource        $stream
39
     * @param ParserInterface $parser
40
     *
41
     * @return FileReader
42
     */
43 View Code Duplication
    private function buildFactoryReader($stream, ParserInterface $parser)
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...
44
    {
45
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
46
        $file->shouldReceive('getStream')
47
             ->with('r')
48
             ->andReturn($stream);
49
50
        $format = m::mock(JsonFormat::class)->makePartial();
51
52
        $factory = m::mock(ParserFactoryInterface::class);
53
        $factory->shouldReceive('getParser')
54
                ->with($format)
55
                ->andReturn($parser);
56
57
        return new FileReader($file, $format, $factory);
0 ignored issues
show
Documentation introduced by
$format is of type object<Mockery\Mock>, but the function expects a null|object<Graze\DataFi...Format\FormatInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
    }
59
60
    public function testNodeStreamFileWillGetAStream()
61
    {
62
        $stream = m::mock(StreamInterface::class);
63
64
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
65
        $file->shouldReceive('getStream')
66
             ->with('r')
67
             ->andReturn($stream);
68
69
        $format = new CsvFormat();
70
        $reader = new FileReader($file, $format);
71
72
        static::assertInstanceOf(ReaderInterface::class, $reader);
73
    }
74
75
    public function testFileNodeWillGetAReadStream()
76
    {
77
        $stream = fopen('php://memory', 'r');
78
79
        try {
80
            $file = m::mock(FileNodeInterface::class);
81
            $file->shouldReceive('readStream')
82
                 ->andReturn($stream);
83
84
            $format = new CsvFormat();
85
86
            $reader = new FileReader($file, $format);
87
88
            static::assertInstanceOf(ReaderInterface::class, $reader);
89
        } finally {
90
            fclose($stream);
91
        }
92
    }
93
94 View Code Duplication
    public function testNodeWithNoFormatAndNoFormatSpecifiedWillThrowAnException()
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...
95
    {
96
        $stream = fopen('php://memory', 'r');
97
98
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
99
        $file->shouldReceive('getStream')
100
             ->with('r')
101
             ->andReturn($stream);
102
103
        static::expectException(InvalidArgumentException::class);
104
        new FileReader($file);
105
    }
106
107
    public function testNodeWithFormatWillUseThatFormat()
108
    {
109
        $stream = fopen('php://memory', 'r');
110
111
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class, FormatAwareInterface::class);
112
        $file->shouldReceive('getStream')
113
             ->with('r')
114
             ->andReturn($stream);
115
116
        $format = new JsonFormat(['fileType' => JsonFormat::JSON_FILE_TYPE_EACH_LINE]);
117
118
        $file->shouldReceive('getFormat')
119
             ->andReturn($format);
120
121
        $reader = new FileReader($file);
122
123
        static::assertInstanceOf(ReaderInterface::class, $reader);
124
    }
125
126 View Code Duplication
    public function testProvidingAParserFactoryWillUseTheFactory()
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...
127
    {
128
        $stream = fopen('php://memory', 'r');
129
        $iterator = m::mock(Iterator::class);
130
        $parser = m::mock(ParserInterface::class);
131
        $parser->shouldReceive('parse')
132
               ->andReturn($iterator);
133
134
        $reader = $this->buildFactoryReader($stream, $parser);
135
136
        static::assertInstanceOf(ReaderInterface::class, $reader);
137
    }
138
139
    public function testFetch()
140
    {
141
        $iterator = new ArrayIterator(['some', 'text']);
142
        $stream = $this->createStream('some text in a stream');
143
144
        $parser = m::mock(ParserInterface::class);
145
        $parser->shouldReceive('parse')
146
               ->with($stream)
147
               ->andReturn($iterator);
148
149
        $reader = $this->buildFactoryReader($stream, $parser);
150
151
        $actual = $reader->fetch();
152
        static::assertSame($reader, $actual);
153
        static::assertEquals(['some', 'text'], iterator_to_array($actual));
154
    }
155
156
    public function testFetchAll()
157
    {
158
        $iterator = new ArrayIterator(['some', 'text']);
159
        $stream = $this->createStream('some text in a stream');
160
161
        $parser = m::mock(ParserInterface::class);
162
        $parser->shouldReceive('parse')
163
               ->with($stream)
164
               ->andReturn($iterator);
165
166
        $reader = $this->buildFactoryReader($stream, $parser);
167
168
        $actual = $reader->fetchAll();
169
        static::assertEquals(['some', 'text'], $actual);
170
    }
171
}
172