Completed
Push — master ( 85851c...4b020b )
by Harry
03:30
created

testNodeWithFormatWillUseThatFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 20
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 20
loc 20
rs 9.4285
cc 1
eloc 13
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\CsvFormatInterface;
19
use Graze\DataFile\Format\FormatAwareInterface;
20
use Graze\DataFile\Format\FormatInterface;
21
use Graze\DataFile\Format\JsonFormat;
22
use Graze\DataFile\Format\JsonFormatInterface;
23
use Graze\DataFile\Format\Parser\ParserFactoryInterface;
24
use Graze\DataFile\Format\Parser\ParserInterface;
25
use Graze\DataFile\IO\FileReader;
26
use Graze\DataFile\IO\ReaderInterface;
27
use Graze\DataFile\Node\FileNodeInterface;
28
use Graze\DataFile\Node\NodeStreamInterface;
29
use Graze\DataFile\Test\Helper\CreateStreamTrait;
30
use Graze\DataFile\Test\TestCase;
31
use InvalidArgumentException;
32
use Mockery as m;
33
use Psr\Http\Message\StreamInterface;
34
35
class FileReaderTest extends TestCase
36
{
37
    use CreateStreamTrait;
38
39
    /**
40
     * @param StreamInterface $stream
41
     * @param ParserInterface $parser
42
     *
43
     * @return FileReader
44
     */
45
    private function buildFactoryReader(StreamInterface $stream, ParserInterface $parser)
46
    {
47
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
48
        $file->shouldReceive('getStream')
49
             ->with('r')
50
             ->andReturn($stream);
51
52
        $format = m::mock(JsonFormat::class)->makePartial();
53
54
        $factory = m::mock(ParserFactoryInterface::class);
55
        $factory->shouldReceive('getParser')
56
                ->with($format)
57
                ->andReturn($parser);
58
59
        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...
60
    }
61
62
    public function testNodeStreamFileWillGetAStream()
63
    {
64
        $stream = m::mock(StreamInterface::class);
65
66
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
67
        $file->shouldReceive('getStream')
68
             ->with('r')
69
             ->andReturn($stream);
70
71
        $format = m::mock(CsvFormat::class)->makePartial();
72
        $reader = new FileReader($file, $format);
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...
73
74
        static::assertInstanceOf(ReaderInterface::class, $reader);
75
    }
76
77
    public function testFileNodeWillGetAReadStream()
78
    {
79
        $stream = fopen('php://memory', 'r');
80
81
        try {
82
            $file = m::mock(FileNodeInterface::class);
83
            $file->shouldReceive('readStream')
84
                 ->andReturn($stream);
85
86
            $format = m::mock(FormatInterface::class, CsvFormatInterface::class);
87
            $format->shouldReceive('getType')
88
                   ->andReturn('csv');
89
90
            $reader = new FileReader($file, $format);
91
92
            static::assertInstanceOf(ReaderInterface::class, $reader);
93
        } finally {
94
            fclose($stream);
95
        }
96
    }
97
98 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...
99
    {
100
        $stream = m::mock(StreamInterface::class);
101
102
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class);
103
        $file->shouldReceive('getStream')
104
             ->with('r')
105
             ->andReturn($stream);
106
107
        static::expectException(InvalidArgumentException::class);
108
        new FileReader($file);
109
    }
110
111 View Code Duplication
    public function testNodeWithFormatWillUseThatFormat()
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...
112
    {
113
        $stream = m::mock(StreamInterface::class);
114
115
        $file = m::mock(FileNodeInterface::class, NodeStreamInterface::class, FormatAwareInterface::class);
116
        $file->shouldReceive('getStream')
117
             ->with('r')
118
             ->andReturn($stream);
119
120
        $format = m::mock(FormatInterface::class, JsonFormatInterface::class);
121
        $format->shouldReceive('getType')
122
               ->andReturn('json');
123
124
        $file->shouldReceive('getFormat')
125
             ->andReturn($format);
126
127
        $reader = new FileReader($file);
128
129
        static::assertInstanceOf(ReaderInterface::class, $reader);
130
    }
131
132
    public function testProvidingAParserFactoryWillUseTheFactory()
133
    {
134
        $stream = m::mock(StreamInterface::class);
135
        $parser = m::mock(ParserInterface::class);
136
137
        $reader = $this->buildFactoryReader($stream, $parser);
138
139
        static::assertInstanceOf(ReaderInterface::class, $reader);
140
    }
141
142
    public function testFetch()
143
    {
144
        $iterator = new ArrayIterator(['some', 'text']);
145
        $stream = $this->createStream('some text in a stream');
146
147
        $parser = m::mock(ParserInterface::class);
148
        $parser->shouldReceive('parse')
149
               ->with($stream)
150
               ->andReturn($iterator);
151
152
        $reader = $this->buildFactoryReader($stream, $parser);
153
154
        $actual = $reader->fetch();
155
        static::assertSame($iterator, $actual);
156
    }
157
158
    public function testFetchAll()
159
    {
160
        $iterator = new ArrayIterator(['some', 'text']);
161
        $stream = $this->createStream('some text in a stream');
162
163
        $parser = m::mock(ParserInterface::class);
164
        $parser->shouldReceive('parse')
165
               ->with($stream)
166
               ->andReturn($iterator);
167
168
        $reader = $this->buildFactoryReader($stream, $parser);
169
170
        $actual = $reader->fetchAll();
171
        static::assertEquals(['some', 'text'], $actual);
172
    }
173
}
174