1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2023 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Attribute; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class StandardTest extends \PHPUnit\Framework\TestCase |
13
|
|
|
{ |
14
|
|
|
private $context; |
15
|
|
|
private $endpoint; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
protected function setUp() : void |
19
|
|
|
{ |
20
|
|
|
\Aimeos\MShop::cache( true ); |
21
|
|
|
|
22
|
|
|
$this->context = \TestHelper::context(); |
23
|
|
|
$this->endpoint = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Done( $this->context, [] ); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
protected function tearDown() : void |
28
|
|
|
{ |
29
|
|
|
\Aimeos\MShop::cache( false ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
public function testProcess() |
34
|
|
|
{ |
35
|
|
|
$mapping = array( |
36
|
|
|
0 => 'attribute.type', |
37
|
|
|
1 => 'attribute.code', |
38
|
|
|
2 => 'product.lists.type', |
39
|
|
|
3 => 'attribute.type', |
40
|
|
|
4 => 'attribute.code', |
41
|
|
|
5 => 'product.lists.type', |
42
|
|
|
6 => 'attribute.type', |
43
|
|
|
7 => 'attribute.code', |
44
|
|
|
8 => 'product.lists.type', |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
$data = array( |
48
|
|
|
0 => 'length', |
49
|
|
|
1 => '30', |
50
|
|
|
2 => 'variant', |
51
|
|
|
3 => 'width', |
52
|
|
|
4 => '29', |
53
|
|
|
5 => 'variant', |
54
|
|
|
6 => 'color', |
55
|
|
|
7 => 'white', |
56
|
|
|
8 => 'variant', |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$product = $this->create( 'job_csv_test' ); |
60
|
|
|
|
61
|
|
|
$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Attribute\Standard( $this->context, $mapping, $this->endpoint ); |
62
|
|
|
$object->process( $product, $data ); |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
$pos = 0; |
66
|
|
|
$listItems = $product->getListItems(); |
67
|
|
|
$expected = array( |
68
|
|
|
array( 'variant', 'length', '30' ), |
69
|
|
|
array( 'variant', 'width', '29' ), |
70
|
|
|
array( 'variant', 'color', 'white' ), |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$this->assertEquals( 3, count( $listItems ) ); |
74
|
|
|
|
75
|
|
|
foreach( $listItems as $listItem ) |
76
|
|
|
{ |
77
|
|
|
$this->assertEquals( 1, $listItem->getStatus() ); |
78
|
|
|
$this->assertEquals( 'attribute', $listItem->getDomain() ); |
79
|
|
|
$this->assertEquals( $expected[$pos][0], $listItem->getType() ); |
80
|
|
|
$this->assertEquals( $expected[$pos][1], $listItem->getRefItem()->getType() ); |
81
|
|
|
$this->assertEquals( $expected[$pos][2], $listItem->getRefItem()->getCode() ); |
82
|
|
|
$pos++; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
public function testProcessMultiple() |
88
|
|
|
{ |
89
|
|
|
$mapping = array( |
90
|
|
|
0 => 'attribute.type', |
91
|
|
|
1 => 'attribute.code', |
92
|
|
|
2 => 'product.lists.type', |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$data = array( |
96
|
|
|
0 => 'color', |
97
|
|
|
1 => "white\nblack\naimeos", |
98
|
|
|
2 => 'variant', |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$product = $this->create( 'job_csv_test' ); |
102
|
|
|
|
103
|
|
|
$mock = $this->getMockBuilder( '\Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Attribute\Standard' ) |
104
|
|
|
->setConstructorArgs( [$this->context, $mapping, $this->endpoint] ) |
105
|
|
|
->onlyMethods( ['getAttributeItem'] ) |
106
|
|
|
->getMock(); |
107
|
|
|
|
108
|
|
|
$item = \Aimeos\MShop::create( $this->context, 'attribute' )->create()->setType( 'color' ); |
109
|
|
|
$mock->expects( $this->exactly( 3 ) )->method( 'getAttributeItem' ) |
110
|
|
|
->will( $this->onConsecutiveCalls( clone $item, clone $item, clone $item ) ); |
111
|
|
|
|
112
|
|
|
$mock->process( $product, $data ); |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
$pos = 0; |
116
|
|
|
$listItems = $product->getListItems(); |
117
|
|
|
$codes = array( 'white', 'black', 'aimeos' ); |
118
|
|
|
|
119
|
|
|
$this->assertEquals( 3, count( $listItems ) ); |
120
|
|
|
|
121
|
|
|
foreach( $listItems as $listItem ) |
122
|
|
|
{ |
123
|
|
|
$this->assertEquals( 1, $listItem->getStatus() ); |
124
|
|
|
$this->assertEquals( 'attribute', $listItem->getDomain() ); |
125
|
|
|
$this->assertEquals( 'variant', $listItem->getType() ); |
126
|
|
|
$this->assertEquals( 'color', $listItem->getRefItem()->getType() ); |
127
|
|
|
$this->assertEquals( $codes[$pos], $listItem->getRefItem()->getCode() ); |
128
|
|
|
$pos++; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
public function testProcessUpdate() |
134
|
|
|
{ |
135
|
|
|
$mapping = array( |
136
|
|
|
0 => 'attribute.type', |
137
|
|
|
1 => 'attribute.code', |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$data = array( |
141
|
|
|
0 => 'length', |
142
|
|
|
1 => '30', |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$dataUpdate = array( |
146
|
|
|
0 => 'width', |
147
|
|
|
1 => '29', |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
$product = $this->create( 'job_csv_test' ); |
151
|
|
|
|
152
|
|
|
$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Attribute\Standard( $this->context, $mapping, $this->endpoint ); |
153
|
|
|
$object->process( $product, $data ); |
154
|
|
|
$object->process( $product, $dataUpdate ); |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
$listItems = $product->getListItems(); |
158
|
|
|
$listItem = $listItems->first(); |
159
|
|
|
|
160
|
|
|
$this->assertEquals( 1, count( $listItems ) ); |
161
|
|
|
$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem ); |
162
|
|
|
|
163
|
|
|
$this->assertEquals( 'width', $listItem->getRefItem()->getType() ); |
164
|
|
|
$this->assertEquals( '29', $listItem->getRefItem()->getCode() ); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
public function testProcessDelete() |
169
|
|
|
{ |
170
|
|
|
$mapping = array( |
171
|
|
|
0 => 'attribute.type', |
172
|
|
|
1 => 'attribute.code', |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
$data = array( |
176
|
|
|
0 => 'length', |
177
|
|
|
1 => '30', |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
$product = $this->create( 'job_csv_test' ); |
181
|
|
|
|
182
|
|
|
$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Attribute\Standard( $this->context, $mapping, $this->endpoint ); |
183
|
|
|
$object->process( $product, $data ); |
184
|
|
|
|
185
|
|
|
$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Attribute\Standard( $this->context, [], $this->endpoint ); |
186
|
|
|
$object->process( $product, [] ); |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
$listItems = $product->getListItems(); |
190
|
|
|
|
191
|
|
|
$this->assertEquals( 0, count( $listItems ) ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
public function testProcessEmpty() |
196
|
|
|
{ |
197
|
|
|
$mapping = array( |
198
|
|
|
0 => 'attribute.type', |
199
|
|
|
1 => 'attribute.code', |
200
|
|
|
2 => 'attribute.type', |
201
|
|
|
3 => 'attribute.code', |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
$data = array( |
205
|
|
|
0 => '', |
206
|
|
|
1 => '', |
207
|
|
|
2 => 'length', |
208
|
|
|
3 => '30', |
209
|
|
|
); |
210
|
|
|
|
211
|
|
|
$product = $this->create( 'job_csv_test' ); |
212
|
|
|
|
213
|
|
|
$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Attribute\Standard( $this->context, $mapping, $this->endpoint ); |
214
|
|
|
$object->process( $product, $data ); |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
$listItems = $product->getListItems(); |
218
|
|
|
|
219
|
|
|
$this->assertEquals( 1, count( $listItems ) ); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
public function testProcessListtypes() |
224
|
|
|
{ |
225
|
|
|
$mapping = array( |
226
|
|
|
0 => 'attribute.type', |
227
|
|
|
1 => 'attribute.code', |
228
|
|
|
2 => 'product.lists.type', |
229
|
|
|
3 => 'attribute.type', |
230
|
|
|
4 => 'attribute.code', |
231
|
|
|
5 => 'product.lists.type', |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
$data = array( |
235
|
|
|
0 => 'length', |
236
|
|
|
1 => '32', |
237
|
|
|
2 => 'custom', |
238
|
|
|
3 => 'width', |
239
|
|
|
4 => '30', |
240
|
|
|
5 => 'default', |
241
|
|
|
); |
242
|
|
|
|
243
|
|
|
$this->context->config()->set( 'controller/jobs/product/import/csv/processor/attribute/listtypes', array( 'default' ) ); |
244
|
|
|
|
245
|
|
|
$product = $this->create( 'job_csv_test' ); |
246
|
|
|
|
247
|
|
|
$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Attribute\Standard( $this->context, $mapping, $this->endpoint ); |
248
|
|
|
|
249
|
|
|
$this->expectException( '\Aimeos\Controller\Jobs\Exception' ); |
250
|
|
|
$object->process( $product, $data ); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param string $code |
256
|
|
|
*/ |
257
|
|
|
protected function create( $code ) |
258
|
|
|
{ |
259
|
|
|
return \Aimeos\MShop::create( $this->context, 'product' )->create()->setCode( $code ); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|