Passed
Push — master ( 2b7604...f317aa )
by Aimeos
01:52
created

StandardTest::testRunProcessorInvalidData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 31
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 45
rs 9.424

1 Method

Rating   Name   Duplication   Size   Complexity  
A StandardTest::testRunBackupInvalid() 0 13 2
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
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 testRunBackup()
175
	{
176
		$config = $this->context->getConfig();
177
		$config->set( 'controller/jobs/product/import/csv/container/type', 'Zip' );
178
		$config->set( 'controller/jobs/product/import/csv/location', 'tmp/import.zip' );
179
		$config->set( 'controller/jobs/product/import/csv/backup', 'tmp/test-%Y-%m-%d.zip' );
180
181
		if( copy( __DIR__ . '/_testfiles/import.zip', 'tmp/import.zip' ) === false ) {
182
			throw new \RuntimeException( 'Unable to copy test file' );
183
		}
184
185
		$this->object->run();
186
187
		$filename = strftime( 'tmp/test-%Y-%m-%d.zip' );
188
		$this->assertTrue( file_exists( $filename ) );
189
190
		unlink( $filename );
191
	}
192
193
194
	public function testRunBackupInvalid()
195
	{
196
		$config = $this->context->getConfig();
197
		$config->set( 'controller/jobs/product/import/csv/container/type', 'Zip' );
198
		$config->set( 'controller/jobs/product/import/csv/location', 'tmp/import.zip' );
199
		$config->set( 'controller/jobs/product/import/csv/backup', 'tmp/notexist/import.zip' );
200
201
		if( copy( __DIR__ . '/_testfiles/import.zip', 'tmp/import.zip' ) === false ) {
202
			throw new \RuntimeException( 'Unable to copy test file' );
203
		}
204
205
		$this->setExpectedException( '\\Aimeos\\Controller\\Jobs\\Exception' );
206
		$this->object->run();
207
	}
208
209
210
	protected function delete( array $prodcodes, array $delete, array $nondelete )
211
	{
212
		$catListManager = \Aimeos\MShop\Catalog\Manager\Factory::create( $this->context )->getSubmanager( 'lists' );
213
		$productManager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context );
214
		$listManager = $productManager->getSubManager( 'lists' );
215
216
		foreach( $this->get( $prodcodes, $delete + $nondelete ) as $id => $product )
217
		{
218
			foreach( $delete as $domain )
219
			{
220
				$manager = \Aimeos\MShop::create( $this->context, $domain );
221
222
				foreach( $product->getListItems( $domain ) as $listItem )
223
				{
224
					$manager->deleteItem( $listItem->getRefItem()->getId() );
225
					$listManager->deleteItem( $listItem->getId() );
226
				}
227
			}
228
229
			foreach( $nondelete as $domain )
230
			{
231
				$ids = array_keys( $product->getListItems( $domain ) );
232
				$listManager->deleteItems( $ids );
233
			}
234
235
			$productManager->deleteItem( $product->getId() );
236
237
			$search = $catListManager->createSearch();
238
			$search->setConditions( $search->compare( '==', 'catalog.lists.refid', $id ) );
239
			$result = $catListManager->searchItems( $search );
240
241
			$catListManager->deleteItems( array_keys( $result ) );
242
		}
243
244
245
		$attrManager = \Aimeos\MShop\Attribute\Manager\Factory::create( $this->context );
246
247
		$search = $attrManager->createSearch();
248
		$search->setConditions( $search->compare( '==', 'attribute.code', 'import-test' ) );
249
250
		$result = $attrManager->searchItems( $search );
251
252
		$attrManager->deleteItems( array_keys( $result ) );
253
	}
254
255
256
	protected function get( array $prodcodes, array $domains )
257
	{
258
		$productManager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context );
259
260
		$search = $productManager->createSearch();
261
		$search->setConditions( $search->compare( '==', 'product.code', $prodcodes ) );
262
263
		return $productManager->searchItems( $search, $domains );
264
	}
265
266
267
	protected function getProperties( array $prodids )
268
	{
269
		$manager = \Aimeos\MShop\Product\Manager\Factory::create( $this->context )->getSubManager( 'property' );
270
271
		$search = $manager->createSearch();
272
		$search->setConditions( $search->compare( '==', 'product.property.parentid', $prodids ) );
273
		$search->setSortations( array( $search->sort( '+', 'product.property.type' ) ) );
274
275
		return $manager->searchItems( $search );
276
	}
277
}
278