Completed
Pull Request — master (#3)
by Harry
05:31
created

StreamWriterTest::testFormatterBlocks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 49
rs 9.2258
cc 1
eloc 36
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 GuzzleHttp\Psr7\Stream;
23
use Mockery as m;
24
use Mockery\MockInterface;
25
use Psr\Http\Message\StreamInterface;
26
27
class StreamWriterTest extends TestCase
28
{
29
    /**
30
     * @var StreamInterface|MockInterface
31
     */
32
    private $stream;
33
34
    public function setUp()
35
    {
36
        $this->stream = m::mock(new Stream(fopen('php://temp', 'r+')))->makePartial();
37
        $this->stream->shouldReceive('close')
38
                     ->andReturnNull();
39
    }
40
41 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...
42
    {
43
        $data = [
44
            ['a', 'b', 'c', 'd'],
45
            ['e', 'f', 'g', 'h'],
46
        ];
47
48
        $writer = new StreamWriter($this->stream, new CsvFormatter(new CsvFormat()));
49
50
        $writer->insertAll($data);
51
52
        $expected = <<<CSV
53
"a","b","c","d"
54
"e","f","g","h"
55
CSV;
56
57
        $this->stream->rewind();
58
        static::assertEquals($expected, $this->stream->getContents());
59
    }
60
61 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...
62
    {
63
        $data = new ArrayIterator([
64
            ['a', 'b', 'c', 'd'],
65
            ['e', 'f', 'g', 'h'],
66
        ]);
67
68
        $writer = new StreamWriter($this->stream, new CsvFormatter(new CsvFormat()));
69
70
        $writer->insertAll($data);
71
72
        $expected = <<<CSV
73
"a","b","c","d"
74
"e","f","g","h"
75
CSV;
76
77
        $this->stream->rewind();
78
        static::assertEquals($expected, $this->stream->getContents());
79
    }
80
81 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...
82
    {
83
        $writer = new StreamWriter($this->stream, new CsvFormatter(new CsvFormat()));
84
85
        $writer->insertOne(['a', 'b', 'c', 'd']);
86
        $writer->insertOne(['e', 'f', 'g', 'h']);
87
88
        $expected = <<<CSV
89
"a","b","c","d"
90
"e","f","g","h"
91
CSV;
92
93
        $this->stream->rewind();
94
        static::assertEquals($expected, $this->stream->getContents());
95
    }
96
97
    public function testFormatterBlocks()
98
    {
99
        /** @var FormatterInterface|MockInterface $formatter */
100
        $formatter = m::mock(FormatterInterface::class);
101
102
        $writer = new StreamWriter($this->stream, $formatter);
0 ignored issues
show
Bug introduced by
It seems like $formatter defined by \Mockery::mock(\Graze\Da...matterInterface::class) on line 100 can also be of type object<Mockery\MockInterface>; however, Graze\DataFile\IO\StreamWriter::__construct() does only seem to accept object<Graze\DataFile\Fo...ter\FormatterInterface>, 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...
103
104
        $formatter->shouldReceive('getInitialBlock')
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in Graze\DataFile\Format\Formatter\FormatterInterface.

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...
105
                  ->andReturn('--init--');
106
        $formatter->shouldReceive('getClosingBlock')
107
                  ->andReturn('--end--');
108
        $formatter->shouldReceive('format')
109
                  ->with(['a', 'b', 'c', 'd'])
110
                  ->andReturn('"a","b","c","d"');
111
        $formatter->shouldReceive('format')
112
                  ->with(['e', 'f', 'g', 'h'])
113
                  ->andReturn('"e","f","g","h"');
114
        $formatter->shouldReceive('format')
115
                  ->with(['i', 'j', 'k', 'l'])
116
                  ->andReturn('"i","j","k","l"');
117
        $formatter->shouldReceive('getRowSeparator')
118
                  ->andReturn("EOL");
119
120
        $writer->insertOne(['a', 'b', 'c', 'd']);
121
        $writer->insertAll([
122
            ['e', 'f', 'g', 'h'],
123
            ['i', 'j', 'k', 'l'],
124
        ]);
125
126
        $expected = <<<CSV
127
--init--"a","b","c","d"EOL"e","f","g","h"EOL"i","j","k","l"--end--
128
CSV;
129
130
        $this->stream->rewind();
131
        static::assertEquals($expected, $this->stream->getContents());
132
133
        $formatter->shouldReceive('format')
134
                  ->with(['m', 'n', 'o', 'p'])
135
                  ->andReturn('"m","n","o","p"');
136
137
        $writer->insertOne(['m', 'n', 'o', 'p']);
138
139
        $expected = <<<CSV
140
--init--"a","b","c","d"EOL"e","f","g","h"EOL"i","j","k","l"EOL"m","n","o","p"--end--
141
CSV;
142
143
        $this->stream->rewind();
144
        static::assertEquals($expected, $this->stream->getContents());
145
    }
146
}
147