FormatterFactoryTest::testJsonFormatter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 12
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\Formatter;
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\FormatterFactory;
20
use Graze\DataFile\Format\Formatter\FormatterFactoryInterface;
21
use Graze\DataFile\Format\Formatter\JsonFormatter;
22
use Graze\DataFile\Format\JsonFormat;
23
use Graze\DataFile\Test\TestCase;
24
use InvalidArgumentException;
25
use Mockery as m;
26
27 View Code Duplication
class FormatterFactoryTest 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...
28
{
29
    /**
30
     * @var FormatterFactory
31
     */
32
    private $factory;
33
34
    public function setUp()
35
    {
36
        $this->factory = new FormatterFactory();
37
    }
38
39
    public function testInstanceOf()
40
    {
41
        static::assertInstanceOf(FormatterFactoryInterface::class, $this->factory);
42
    }
43
44
    public function testCsvFormatter()
45
    {
46
        $csvFormat = m::mock(CsvFormat::class)->makePartial();
47
48
        $formatter = $this->factory->getFormatter($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...
49
50
        static::assertInstanceOf(CsvFormatter::class, $formatter);
51
    }
52
53
    public function testACsvFormatPretendingToBeCsvWillThrowAnException()
54
    {
55
        $csvFormat = m::mock(FormatInterface::class);
56
        $csvFormat->shouldReceive('getType')
57
                  ->andReturn('csv');
58
59
        static::expectException(InvalidArgumentException::class);
60
61
        $this->factory->getFormatter($csvFormat);
62
    }
63
64
    public function testJsonFormatter()
65
    {
66
        $jsonFormat = m::mock(JsonFormat::class);
67
        $jsonFormat->shouldReceive('getType')
68
                   ->andReturn('json');
69
        $jsonFormat->shouldReceive('isSingleBlock')
70
                   ->andReturn(false);
71
        $jsonFormat->shouldReceive('getJsonEncodeOptions')
72
                   ->andReturn(0);
73
        $jsonFormat->shouldReceive('isEachLine')
74
                   ->andReturn(true);
75
        $formatter = $this->factory->getFormatter($jsonFormat);
76
77
        static::assertInstanceOf(JsonFormatter::class, $formatter);
78
    }
79
80
    public function testAJsonFormatPretendingToBeCsvWillThrowAnException()
81
    {
82
        $csvFormat = m::mock(FormatInterface::class);
83
        $csvFormat->shouldReceive('getType')
84
                  ->andReturn('json');
85
86
        static::expectException(InvalidArgumentException::class);
87
88
        $this->factory->getFormatter($csvFormat);
89
    }
90
91
    public function testGetFormatterWithUnknownTypeWillThrowException()
92
    {
93
        $format = m::mock(FormatInterface::class);
94
        $format->shouldReceive('getType')
95
               ->andReturn('random');
96
97
        static::expectException(InvalidArgumentException::class);
98
99
        $this->factory->getFormatter($format);
100
    }
101
}
102