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

ParserFactoryTest::testInstanceOf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 4
loc 4
rs 10
cc 1
eloc 2
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\Format\Parser;
15
16
use Graze\DataFile\Format\CsvFormat;
17
use Graze\DataFile\Format\FormatInterface;
18
use Graze\DataFile\Format\Formatter\CsvFormatter;
19
use Graze\DataFile\Format\Formatter\JsonFormatter;
20
use Graze\DataFile\Format\JsonFormat;
21
use Graze\DataFile\Format\Parser\CsvParser;
22
use Graze\DataFile\Format\Parser\JsonParser;
23
use Graze\DataFile\Format\Parser\ParserFactory;
24
use Graze\DataFile\Format\Parser\ParserFactoryInterface;
25
use Graze\DataFile\Test\TestCase;
26
use InvalidArgumentException;
27
use Mockery as m;
28
29 View Code Duplication
class ParserFactoryTest extends TestCase
0 ignored issues
show
Duplication introduced by
This class 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...
30
{
31
    /**
32
     * @var ParserFactory
33
     */
34
    private $factory;
35
36
    public function setUp()
37
    {
38
        $this->factory = new ParserFactory();
39
    }
40
41
    public function testInstanceOf()
42
    {
43
        static::assertInstanceOf(ParserFactoryInterface::class, $this->factory);
44
    }
45
46
    public function testCsvParser()
47
    {
48
        $csvFormat = m::mock(CsvFormat::class)->makePartial();
49
50
        $formatter = $this->factory->getParser($csvFormat);
0 ignored issues
show
Documentation introduced by
$csvFormat is of type object<Mockery\Mock>, but the function expects a object<Graze\DataFile\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...
51
52
        static::assertInstanceOf(CsvParser::class, $formatter);
53
    }
54
55
    public function testACsvFormatPretendingToBeCsvWillThrowAnException()
56
    {
57
        $csvFormat = m::mock(FormatInterface::class);
58
        $csvFormat->shouldReceive('getType')
59
                  ->andReturn('csv');
60
61
        static::expectException(InvalidArgumentException::class);
62
63
        $this->factory->getParser($csvFormat);
64
    }
65
66
    public function testJsonParser()
67
    {
68
        $jsonFormat = m::mock(JsonFormat::class);
69
        $jsonFormat->shouldReceive('getType')
70
                   ->andReturn('json');
71
        $jsonFormat->shouldReceive('isSingleBlock')
72
                   ->andReturn(false);
73
        $jsonFormat->shouldReceive('getJsonEncodeOptions')
74
                   ->andReturn(0);
75
        $jsonFormat->shouldReceive('isEachLine')
76
                   ->andReturn(true);
77
        $formatter = $this->factory->getParser($jsonFormat);
78
79
        static::assertInstanceOf(JsonParser::class, $formatter);
80
    }
81
82
    public function testAJsonFormatPretendingToBeCsvWillThrowAnException()
83
    {
84
        $csvFormat = m::mock(FormatInterface::class);
85
        $csvFormat->shouldReceive('getType')
86
                  ->andReturn('json');
87
88
        static::expectException(InvalidArgumentException::class);
89
90
        $this->factory->getParser($csvFormat);
91
    }
92
93
    public function testGetFormatterWithUnknownTypeWillThrowException()
94
    {
95
        $format = m::mock(FormatInterface::class);
96
        $format->shouldReceive('getType')
97
               ->andReturn('random');
98
99
        static::expectException(InvalidArgumentException::class);
100
101
        $this->factory->getParser($format);
102
    }
103
}
104