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

StreamReaderTest::testFetchWithNoCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
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\Parser\ParserInterface;
18
use Graze\DataFile\IO\StreamReader;
19
use Graze\DataFile\Test\Helper\CreateStreamTrait;
20
use Graze\DataFile\Test\TestCase;
21
use Iterator;
22
use Mockery as m;
23
use Mockery\MockInterface;
24
25
class StreamReaderTest extends TestCase
26
{
27
    use CreateStreamTrait;
28
29
    /**
30
     * @param string   $string
31
     * @param Iterator $iterator
32
     *
33
     * @return StreamReader
34
     */
35
    private function buildReader($string, Iterator $iterator)
36
    {
37
        $stream = $this->createStream($string);
38
        /** @var ParserInterface|MockInterface $parser */
39
        $parser = m::mock(ParserInterface::class);
40
41
        $parser->shouldReceive('parse')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Format\Parser\ParserInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
42
               ->with($stream)
43
               ->andReturn($iterator);
44
45
        return new StreamReader($stream, $parser);
0 ignored issues
show
Bug introduced by
It seems like $parser defined by \Mockery::mock(\Graze\Da...ParserInterface::class) on line 39 can also be of type object<Mockery\MockInterface>; however, Graze\DataFile\IO\StreamReader::__construct() does only seem to accept object<Graze\DataFile\Fo...Parser\ParserInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
46
    }
47
48
    public function testFetchWithNoCallable()
49
    {
50
        $iterator = new ArrayIterator(['some', 'text']);
51
        $reader = $this->buildReader("some text in a stream", $iterator);
52
53
        $actual = $reader->fetch();
54
        static::assertSame($iterator, $actual);
55
    }
56
57 View Code Duplication
    public function testFetchAllWithNoCallable()
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...
58
    {
59
        $iterator = new ArrayIterator(['some', 'text']);
60
        $reader = $this->buildReader("some text in a stream", $iterator);
61
62
        $actual = $reader->fetchAll();
63
        static::assertEquals(['some', 'text'], $actual);
64
    }
65
66
    public function testFetchWithCallable()
67
    {
68
        $iterator = new ArrayIterator(['some', 'text']);
69
        $reader = $this->buildReader("some text in a stream", $iterator);
70
71
        $actual = $reader->fetch(function ($item) {
72
            return $item == 'some';
73
        });
74
        static::assertNotSame($iterator, $actual);
75
76
        $result = iterator_to_array($actual);
77
        static::assertEquals(['some'], $result);
78
    }
79
80 View Code Duplication
    public function testFetchAllWithCallable()
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...
81
    {
82
        $iterator = new ArrayIterator(['some', 'text']);
83
        $reader = $this->buildReader("some text in a stream", $iterator);
84
85
        $actual = $reader->fetchAll(function ($item) {
86
            return $item == 'text';
87
        });
88
        static::assertEquals(['text'], array_values($actual));
89
    }
90
}
91