Completed
Branch master (c3c77b)
by Aimeos
03:13
created

StandardTest::delete()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 43
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 7
nop 3
dl 0
loc 43
rs 9.2568
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
6
 */
7
8
9
namespace Aimeos\Controller\Jobs\Product\Import\Csv;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
{
14
	private $object;
15
	private $context;
16
	private $aimeos;
17
18
19
	protected function setUp()
20
	{
21
		\Aimeos\MShop::cache( true );
22
23
		$this->context = \TestHelperJobs::getContext();
24
		$this->aimeos = \TestHelperJobs::getAimeos();
25
		$config = $this->context->getConfig();
26
27
		$config->set( 'controller/jobs/product/import/csv/skip-lines', 1 );
28
		$config->set( 'controller/jobs/product/import/csv/location', __DIR__ . '/_testfiles/valid' );
29
30
		$this->object = new \Aimeos\Controller\Jobs\Product\Import\Csv\Standard( $this->context, $this->aimeos );
31
	}
32
33
34
	protected function tearDown()
35
	{
36
		\Aimeos\MShop::cache( false );
37
		$this->object = null;
38
39
		if( file_exists( 'tmp/import.zip' ) ) {
40
			unlink( 'tmp/import.zip' );
41
		}
42
	}
43
44
45
	public function testGetName()
46
	{
47
		$this->assertEquals( 'Product import CSV', $this->object->getName() );
48
	}
49
50
51
	public function testGetDescription()
52
	{
53
		$text = 'Imports new and updates existing products from CSV files';
54
		$this->assertEquals( $text, $this->object->getDescription() );
55
	}
56
57
58
	public function testRun()
59
	{
60
		$prodcodes = array( 'job_csv_test', 'job_csv_test2' );
61
		$nondelete = array( 'attribute', 'product' );
62
		$delete = array( 'media', 'price', 'text' );
63
64
		$convert = array(
65
			1 => 'Text/LatinUTF8',
66
		);
67
68
		$this->context->getConfig()->set( 'controller/jobs/product/import/csv/converter', $convert );
69
70
		$this->object->run();
71
72
		$result = $this->get( $prodcodes, array_merge( $delete, $nondelete ) );
73
		$properties = $this->getProperties( array_keys( $result ) );
74
		$this->delete( $prodcodes, $delete, $nondelete );
75
76
		$this->assertEquals( 2, count( $result ) );
77
		$this->assertEquals( 2, count( $properties ) );
78
79
		foreach( $result as $product ) {
80
			$this->assertEquals( 5, count( $product->getListItems() ) );
81
		}
82
	}
83
84
85
	public function testRunUpdate()
86
	{
87
		$prodcodes = array( 'job_csv_test', 'job_csv_test2' );
88
		$nondelete = array( 'attribute', 'product' );
89
		$delete = array( 'media', 'price', 'text' );
90
91
		$this->object->run();
92
		$this->object->run();
93
94
		$result = $this->get( $prodcodes, array_merge( $delete, $nondelete ) );
95
		$properties = $this->getProperties( array_keys( $result ) );
96
		$this->delete( $prodcodes, $delete, $nondelete );
97
98
		$this->assertEquals( 2, count( $result ) );
99
		$this->assertEquals( 2, count( $properties ) );
100
101
		foreach( $result as $product ) {
102
			$this->assertEquals( 5, count( $product->getListItems() ) );
103
		}
104
	}
105
106
107
	public function testRunPosition()
108
	{
109
		$prodcodes = array( 'job_csv_test', 'job_csv_test2' );
110
		$nondelete = array( 'attribute', 'product' );
111
		$delete = array( 'media', 'price', 'text' );
112
113
		$config = $this->context->getConfig();
114
		$mapping = $config->set( 'controller/jobs/product/import/csv/mapping', [] );
115
		$mapping['item'] = array( 0 => 'product.label', 1 => 'product.code' );
116
117
		$config->set( 'controller/jobs/product/import/csv/mapping', $mapping );
118
		$config->set( 'controller/jobs/product/import/csv/location', __DIR__ . '/_testfiles/position' );
119
120
		$this->object->run();
121
122
		$result = $this->get( $prodcodes, array_merge( $delete, $nondelete ) );
123
		$this->delete( $prodcodes, $delete, $nondelete );
124
125
		$this->assertEquals( 2, count( $result ) );
126
	}
127
128
129
	public function testRunProcessorInvalidPosition()
130
	{
131
		$prodcodes = array( 'job_csv_test', 'job_csv_test2' );
132
133
		$mapping = array(
134
			'item' => array(
135
				0 => 'product.code',
136
				1 => 'product.label',
137
				2 => 'product.type',
138
				3 => 'product.status',
139
			),
140
			'text' => array(
141
				4 => 'text.type',
142
				5 => 'text.content',
143
				100 => 'text.type',
144
				101 => 'text.content',
145
			),
146
			'media' => array(
147
				8 => 'media.url',
148
			),
149
		);
150
151
		$this->context->getConfig()->set( 'controller/jobs/product/import/csv/mapping', $mapping );
152
153
		$this->object->run();
154
155
		$this->delete( $prodcodes, array( 'text', 'media' ), [] );
156
	}
157
158
159
	public function testRunProcessorInvalidMapping()
160
	{
161
		$mapping = array(
162
			'media' => array(
163
					8 => 'media.url',
164
			),
165
		);
166
167
		$this->context->getConfig()->set( 'controller/jobs/product/import/csv/mapping', $mapping );
168
169
		$this->setExpectedException( '\\Aimeos\\Controller\\Jobs\\Exception' );
170
		$this->object->run();
171
	}
172
173
174
	public function testRunProcessorInvalidData()
175
	{
176
		$mapping = array(
177
			'item' => array(
178
				0 => 'product.code',
179
				1 => 'product.label',
180
				2 => 'product.type',
181
			),
182
			'text' => array(
183
				3 => 'text.type',
184
				4 => 'text.content',
185
			),
186
			'media' => array(
187
				5 => 'media.url',
188
				6 => 'product.lists.type',
189
			),
190
			'price' => array(
191
				7 => 'price.type',
192
				8 => 'price.value',
193
				9 => 'price.taxrate',
194
			),
195
			'attribute' => array(
196
				10 => 'attribute.type',
197
				11 => 'attribute.code',
198
			),
199
			'product' => array(
200
				12 => 'product.code',
201
				13 => 'product.lists.type',
202
			),
203
			'property' => array(
204
				14 => 'product.property.type',
205
				15 => 'product.property.value',
206
			),
207
		);
208
209
		$this->context->getConfig()->set( 'controller/jobs/product/import/csv/mapping', $mapping );
210
211
		$config = $this->context->getConfig();
212
		$config->set( 'controller/jobs/product/import/csv/skip-lines', 0 );
213
		$config->set( 'controller/jobs/product/import/csv/location', __DIR__ . '/_testfiles/invalid' );
214
215
		$this->object = new \Aimeos\Controller\Jobs\Product\Import\Csv\Standard( $this->context, $this->aimeos );
216
217
		$this->setExpectedException( '\\Aimeos\\Controller\\Jobs\\Exception' );
218
		$this->object->run();
219
	}
220
221
222
	public function testRunBackup()
223
	{
224
		$config = $this->context->getConfig();
225
		$config->set( 'controller/jobs/product/import/csv/container/type', 'Zip' );
226
		$config->set( 'controller/jobs/product/import/csv/location', 'tmp/import.zip' );
227
		$config->set( 'controller/jobs/product/import/csv/backup', 'tmp/test-%Y-%m-%d.zip' );
228
229
		if( copy( __DIR__ . '/_testfiles/import.zip', 'tmp/import.zip' ) === false ) {
230
			throw new \RuntimeException( 'Unable to copy test file' );
231
		}
232
233
		$this->object->run();
234
235
		$filename = strftime( 'tmp/test-%Y-%m-%d.zip' );
236
		$this->assertTrue( file_exists( $filename ) );
237
238
		unlink( $filename );
239
	}
240
241
242
	public function testRunBackupInvalid()
243
	{
244
		$config = $this->context->getConfig();
245
		$config->set( 'controller/jobs/product/import/csv/container/type', 'Zip' );
246
		$config->set( 'controller/jobs/product/import/csv/location', 'tmp/import.zip' );
247
		$config->set( 'controller/jobs/product/import/csv/backup', 'tmp/notexist/import.zip' );
248
249
		if( copy( __DIR__ . '/_testfiles/import.zip', 'tmp/import.zip' ) === false ) {
250
			throw new \RuntimeException( 'Unable to copy test file' );
251
		}
252
253
		$this->setExpectedException( '\\Aimeos\\Controller\\Jobs\\Exception' );
254
		$this->object->run();
255
	}
256
257
258
	protected function delete( array $prodcodes, array $delete, array $nondelete )
259
	{
260
		$catListManager = \Aimeos\MShop\Catalog\Manager\Factory::create( $this->context )->getSubmanager( 'lists' );
261
		$productManager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context );
262
		$listManager = $productManager->getSubManager( 'lists' );
263
264
		foreach( $this->get( $prodcodes, $delete + $nondelete ) as $id => $product )
265
		{
266
			foreach( $delete as $domain )
267
			{
268
				$manager = \Aimeos\MShop::create( $this->context, $domain );
269
270
				foreach( $product->getListItems( $domain ) as $listItem )
271
				{
272
					$manager->deleteItem( $listItem->getRefItem()->getId() );
273
					$listManager->deleteItem( $listItem->getId() );
274
				}
275
			}
276
277
			foreach( $nondelete as $domain )
278
			{
279
				$ids = array_keys( $product->getListItems( $domain ) );
280
				$listManager->deleteItems( $ids );
281
			}
282
283
			$productManager->deleteItem( $product->getId() );
284
285
			$search = $catListManager->createSearch();
286
			$search->setConditions( $search->compare( '==', 'catalog.lists.refid', $id ) );
287
			$result = $catListManager->searchItems( $search );
288
289
			$catListManager->deleteItems( array_keys( $result ) );
290
		}
291
292
293
		$attrManager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context );
294
295
		$search = $attrManager->createSearch();
296
		$search->setConditions( $search->compare( '==', 'attribute.code', 'import-test' ) );
297
298
		$result = $attrManager->searchItems( $search );
299
300
		$attrManager->deleteItems( array_keys( $result ) );
301
	}
302
303
304
	protected function get( array $prodcodes, array $domains )
305
	{
306
		$productManager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context );
307
308
		$search = $productManager->createSearch();
309
		$search->setConditions( $search->compare( '==', 'product.code', $prodcodes ) );
310
311
		return $productManager->searchItems( $search, $domains );
312
	}
313
314
315
	protected function getProperties( array $prodids )
316
	{
317
		$manager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context )->getSubManager( 'property' );
318
319
		$search = $manager->createSearch();
320
		$search->setConditions( $search->compare( '==', 'product.property.parentid', $prodids ) );
321
		$search->setSortations( array( $search->sort( '+', 'product.property.type' ) ) );
322
323
		return $manager->searchItems( $search );
324
	}
325
}