1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jh\DataImportmagentoTest\ItemConverter; |
4
|
|
|
|
5
|
|
|
use Jh\DataImportMagento\ItemConverter\RemoveUnwantedFieldsConverter; |
6
|
|
|
use MyProject\Proxies\__CG__\stdClass; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class RemoveUnwantedFieldsConverterTest |
10
|
|
|
* @package Ddeboer\DataImport\Tests\ItemConverter |
11
|
|
|
* @author Aydin Hassan <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class RemoveUnwantedFieldsConverterTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
public function testConvert() |
16
|
|
|
{ |
17
|
|
|
$input = array( |
18
|
|
|
'foo' => 'bar', |
19
|
|
|
'keepMe1' => 'foo', |
20
|
|
|
'keepMe2' => 'bar', |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
$fieldsToKeep = array('keepMe1', 'keepMe2'); |
24
|
|
|
$converter = new RemoveUnwantedFieldsConverter($fieldsToKeep); |
25
|
|
|
|
26
|
|
|
$output = $converter->convert($input); |
27
|
|
|
|
28
|
|
|
$expected = array( |
29
|
|
|
'keepMe1' => 'foo', |
30
|
|
|
'keepMe2' => 'bar', |
31
|
|
|
); |
32
|
|
|
$this->assertEquals($expected, $output); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testNestedFieldsConvert() |
36
|
|
|
{ |
37
|
|
|
$input = [ |
38
|
|
|
'foo' => 'bar', |
39
|
|
|
'keepMe1' => 'foo', |
40
|
|
|
'items' => [ |
41
|
|
|
[ |
42
|
|
|
'one' => 'one', |
43
|
|
|
'two' => 'two', |
44
|
|
|
'notNeeded' => 'deleteMe', |
45
|
|
|
], |
46
|
|
|
[ |
47
|
|
|
'one' => 'one', |
48
|
|
|
'two' => 'two', |
49
|
|
|
'doNotNeedThisEither' => 'deleteMe' |
50
|
|
|
] |
51
|
|
|
] |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
$fieldsToKeep = [ |
55
|
|
|
'keepMe1', |
56
|
|
|
'items' => [ |
57
|
|
|
'one', |
58
|
|
|
'two' |
59
|
|
|
], |
60
|
|
|
]; |
61
|
|
|
$converter = new RemoveUnwantedFieldsConverter($fieldsToKeep); |
62
|
|
|
|
63
|
|
|
$expected = [ |
64
|
|
|
'keepMe1' => 'foo', |
65
|
|
|
'items' => [ |
66
|
|
|
[ |
67
|
|
|
'one' => 'one', |
68
|
|
|
'two' => 'two', |
69
|
|
|
], |
70
|
|
|
[ |
71
|
|
|
'one' => 'one', |
72
|
|
|
'two' => 'two', |
73
|
|
|
] |
74
|
|
|
] |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
$output = $converter->convert($input); |
78
|
|
|
$this->assertEquals($expected, $output); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testConverterThrowsExceptionIfInputNotArray() |
82
|
|
|
{ |
83
|
|
|
$converter = new RemoveUnwantedFieldsConverter([]); |
84
|
|
|
$this->setExpectedException( |
85
|
|
|
'Ddeboer\DataImport\Exception\UnexpectedTypeException', |
86
|
|
|
'Expected argument of type "array", "stdClass" given' |
87
|
|
|
); |
88
|
|
|
$converter->convert(new \stdClass()); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testNestedItemWhichDoesNotExistInInputDataIsReplacedWithEmptyArray() |
92
|
|
|
{ |
93
|
|
|
$fieldsToKeep = [ |
94
|
|
|
'items' => [ |
95
|
|
|
'one', |
96
|
|
|
], |
97
|
|
|
]; |
98
|
|
|
$converter = new RemoveUnwantedFieldsConverter($fieldsToKeep); |
99
|
|
|
$input = []; |
100
|
|
|
|
101
|
|
|
$this->assertSame(['items' => []], $converter->convert($input)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testExceptionIsThrownIfNestDataIsNotAnArray() |
105
|
|
|
{ |
106
|
|
|
$fieldsToKeep = [ |
107
|
|
|
'items' => [ |
108
|
|
|
'one', |
109
|
|
|
], |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
$converter = new RemoveUnwantedFieldsConverter($fieldsToKeep); |
113
|
|
|
$input = [ |
114
|
|
|
'items' => new \stdClass |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
$this->setExpectedException( |
118
|
|
|
'Ddeboer\DataImport\Exception\UnexpectedTypeException', |
119
|
|
|
'Expected argument of type "array", "stdClass" given' |
120
|
|
|
); |
121
|
|
|
$converter->convert($input); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testNestedItemIsPopulatedWithDefaultValueIfRequiredFieldDoesNotExist() |
125
|
|
|
{ |
126
|
|
|
$fieldsToKeep = [ |
127
|
|
|
'items' => [ |
128
|
|
|
'one', |
129
|
|
|
], |
130
|
|
|
]; |
131
|
|
|
$converter = new RemoveUnwantedFieldsConverter($fieldsToKeep); |
132
|
|
|
|
133
|
|
|
$input = [ |
134
|
|
|
'items' => [ |
135
|
|
|
[] |
136
|
|
|
] |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
$expected = [ |
140
|
|
|
'items' => [ |
141
|
|
|
['one' => ''] |
142
|
|
|
] |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
$this->assertSame($expected, $converter->convert($input)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testFirstLevelDataIsPopulatedWithDefaultValueIfRequiredFieldDoesNotExist() |
149
|
|
|
{ |
150
|
|
|
$fieldsToKeep = [ |
151
|
|
|
'name' |
152
|
|
|
]; |
153
|
|
|
$converter = new RemoveUnwantedFieldsConverter($fieldsToKeep); |
154
|
|
|
|
155
|
|
|
$input = []; |
156
|
|
|
|
157
|
|
|
$expected = [ |
158
|
|
|
'name' => '' |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
$this->assertSame($expected, $converter->convert($input)); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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: