StandardTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 139
dl 0
loc 263
rs 10
c 0
b 0
f 0
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testProcess() 0 45 1
A testProcessUpdate() 0 32 1
A setUp() 0 18 2
A testProcessDelete() 0 22 1
A testProcessMultipleFields() 0 31 2
A create() 0 3 1
A tearDown() 0 3 1
A testProcessMultiple() 0 26 2
A testProcessListtypes() 0 24 1
A testProcessEmpty() 0 21 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 */
7
8
9
namespace Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Media;
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
		$this->context = \TestHelper::context();
22
23
		$fs = $this->context->fs( 'fs-media' );
24
		$fs->has( 'path/to' ) ?: $fs->mkdir( 'path/to' );
25
		$fs->write( 'path/to/new.jpg', 'test' );
26
		$fs->write( 'path/to/file', 'test' );
27
		$fs->write( 'path/to/0', 'test' );
28
		$fs->write( 'path/to/1', 'test' );
29
		$fs->write( 'path/to/2', 'test' );
30
		$fs->write( 'path/to/3', 'test' );
31
32
		$fs = $this->context->fs( 'fs-mimeicon' );
33
		$fs->write( 'unknown.png', 'test' );
34
35
		$this->endpoint = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Done( $this->context, [] );
36
	}
37
38
39
	protected function tearDown() : void
40
	{
41
		\Aimeos\MShop::cache( false );
42
	}
43
44
45
	public function testProcess()
46
	{
47
		$mapping = array(
48
			0 => 'media.languageid',
49
			1 => 'media.label',
50
			2 => 'media.mimetype',
51
			3 => 'media.preview',
52
			4 => 'media.url',
53
			5 => 'media.status',
54
		);
55
56
		$data = array(
57
			0 => 'de',
58
			1 => 'test image',
59
			2 => 'image/jpeg',
60
			3 => 'path/to/preview',
61
			4 => 'path/to/file',
62
			5 => 1,
63
		);
64
65
		$product = $this->create( 'job_csv_test' );
66
67
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
68
		$object->process( $product, $data );
69
70
71
		$listItems = $product->getListItems();
72
		$listItem = $listItems->first();
73
74
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
75
		$this->assertEquals( 1, count( $listItems ) );
76
77
		$this->assertEquals( 1, $listItem->getStatus() );
78
		$this->assertEquals( 0, $listItem->getPosition() );
79
		$this->assertEquals( 'default', $listItem->getType() );
80
81
		$refItem = $listItem->getRefItem();
82
83
		$this->assertEquals( 1, $refItem->getStatus() );
84
		$this->assertEquals( 'default', $refItem->getType() );
85
		$this->assertEquals( 'test image', $refItem->getLabel() );
86
		$this->assertEquals( 'image/jpeg', $refItem->getMimetype() );
87
		$this->assertEquals( 'path/to/preview', $refItem->getPreview() );
88
		$this->assertEquals( 'path/to/file', $refItem->getUrl() );
89
		$this->assertEquals( 'de', $refItem->getLanguageId() );
90
	}
91
92
93
	public function testProcessMultiple()
94
	{
95
		$mapping = array(
96
			0 => 'media.url',
97
		);
98
99
		$data = array(
100
			0 => "path/to/0\npath/to/1\npath/to/2\npath/to/3",
101
		);
102
103
		$product = $this->create( 'job_csv_test' );
104
105
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
106
		$object->process( $product, $data );
107
108
109
		$pos = 0;
110
		$listItems = $product->getListItems();
111
		$expected = ['path/to/0', 'path/to/1', 'path/to/2', 'path/to/3'];
112
113
		$this->assertEquals( 4, count( $listItems ) );
114
115
		foreach( $listItems as $listItem )
116
		{
117
			$this->assertEquals( $expected[$pos], $listItem->getRefItem()->getUrl() );
118
			$pos++;
119
		}
120
	}
121
122
123
	public function testProcessMultipleFields()
124
	{
125
		$mapping = array(
126
			0 => 'media.url',
127
			1 => 'media.url',
128
			2 => 'media.url',
129
			3 => 'media.url',
130
		);
131
132
		$data = array(
133
			0 => 'path/to/0',
134
			1 => 'path/to/1',
135
			2 => 'path/to/2',
136
			3 => 'path/to/3',
137
		);
138
139
		$product = $this->create( 'job_csv_test' );
140
141
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
142
		$object->process( $product, $data );
143
144
145
		$pos = 0;
146
		$listItems = $product->getListItems();
147
148
		$this->assertEquals( 4, count( $listItems ) );
149
150
		foreach( $listItems as $listItem )
151
		{
152
			$this->assertEquals( $data[$pos], $listItem->getRefItem()->getUrl() );
153
			$pos++;
154
		}
155
	}
156
157
158
	public function testProcessUpdate()
159
	{
160
		$mapping = array(
161
			0 => 'media.url',
162
			1 => 'media.languageid',
163
		);
164
165
		$data = array(
166
			0 => 'path/to/file',
167
			1 => 'de',
168
		);
169
170
		$dataUpdate = array(
171
			0 => 'path/to/new.jpg',
172
			1 => '',
173
		);
174
175
		$product = $this->create( 'job_csv_test' );
176
177
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
178
		$object->process( $product, $data );
179
		$object->process( $product, $dataUpdate );
180
181
182
		$listItems = $product->getListItems();
183
		$listItem = $listItems->first();
184
185
		$this->assertEquals( 1, count( $listItems ) );
186
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
187
188
		$this->assertEquals( 'path/to/new.jpg', $listItem->getRefItem()->getUrl() );
189
		$this->assertEquals( null, $listItem->getRefItem()->getLanguageId() );
190
	}
191
192
193
	public function testProcessDelete()
194
	{
195
		$mapping = array(
196
			0 => 'media.url',
197
		);
198
199
		$data = array(
200
			0 => '/path/to/file',
201
		);
202
203
		$product = $this->create( 'job_csv_test' );
204
205
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
206
		$object->process( $product, $data );
207
208
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Media\Standard( $this->context, [], $this->endpoint );
209
		$object->process( $product, [] );
210
211
212
		$listItems = $product->getListItems();
213
214
		$this->assertEquals( 0, count( $listItems ) );
215
	}
216
217
218
	public function testProcessEmpty()
219
	{
220
		$mapping = array(
221
			0 => 'media.url',
222
			1 => 'media.url',
223
		);
224
225
		$data = array(
226
			0 => 'path/to/file',
227
			1 => '',
228
		);
229
230
		$product = $this->create( 'job_csv_test' );
231
232
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
233
		$object->process( $product, $data );
234
235
236
		$listItems = $product->getListItems();
237
238
		$this->assertEquals( 1, count( $listItems ) );
239
	}
240
241
242
	public function testProcessListtypes()
243
	{
244
		$mapping = array(
245
			0 => 'media.url',
246
			1 => 'product.lists.type',
247
			2 => 'media.url',
248
			3 => 'product.lists.type',
249
		);
250
251
		$data = array(
252
			0 => 'path/to/file',
253
			1 => 'download',
254
			2 => 'path/to/file2',
255
			3 => 'default',
256
		);
257
258
		$this->context->config()->set( 'controller/jobs/product/import/csv/processor/media/listtypes', array( 'default' ) );
259
260
		$product = $this->create( 'job_csv_test' );
261
262
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Media\Standard( $this->context, $mapping, $this->endpoint );
263
264
		$this->expectException( '\Aimeos\Controller\Jobs\Exception' );
265
		$object->process( $product, $data );
266
	}
267
268
269
	/**
270
	 * @param string $code
271
	 */
272
	protected function create( $code )
273
	{
274
		return \Aimeos\MShop::create( $this->context, 'product' )->create()->setCode( $code );
275
	}
276
}
277