|
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
|
|
|
class FormatterFactoryTest extends TestCase |
|
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); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
static::assertInstanceOf(CsvFormatter::class, $formatter); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
|
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: