Completed
Push — master ( 2e10c1...bb3077 )
by Aimeos
02:17
created

StandardTest   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 247
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 6
dl 0
loc 247
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 1
A tearDown() 0 11 2
A testGetName() 0 4 1
A testGetDescription() 0 5 1
A testRun() 0 22 2
A testRunUpdate() 0 14 2
A testRunPosition() 0 16 1
B testRunProcessorInvalidPosition() 0 27 1
A testRunProcessorInvalidMapping() 0 13 1
B testRunProcessorInvalidData() 0 30 1
A testRunBackup() 0 18 2
A testRunBackupInvalid() 0 14 2
A delete() 0 19 4
A get() 0 7 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 */
7
8
9
namespace Aimeos\Controller\Jobs\Catalog\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\Factory::setCache( true );
22
23
		$this->context = \TestHelperJobs::getContext();
24
		$this->aimeos = \TestHelperJobs::getAimeos();
25
		$config = $this->context->getConfig();
26
27
		$config->set( 'controller/jobs/catalog/import/csv/skip-lines', 1 );
28
		$config->set( 'controller/jobs/catalog/import/csv/location', __DIR__ . '/_testfiles/valid' );
29
30
		$this->object = new \Aimeos\Controller\Jobs\Catalog\Import\Csv\Standard( $this->context, $this->aimeos );
31
	}
32
33
34
	protected function tearDown()
35
	{
36
		\Aimeos\MShop\Factory::setCache( false );
37
		\Aimeos\MShop\Factory::clear();
38
39
		$this->object = null;
40
41
		if( file_exists( 'tmp/import.zip' ) ) {
42
			unlink( 'tmp/import.zip' );
43
		}
44
	}
45
46
47
	public function testGetName()
48
	{
49
		$this->assertEquals( 'Catalog import CSV', $this->object->getName() );
50
	}
51
52
53
	public function testGetDescription()
54
	{
55
		$text = 'Imports new and updates existing categories from CSV files';
56
		$this->assertEquals( $text, $this->object->getDescription() );
57
	}
58
59
60
	public function testRun()
61
	{
62
		$catcodes = array( 'job_csv_test', 'job_csv_test2', 'job_csv_test3', 'job_csv_test4' );
0 ignored issues
show
Unused Code introduced by
$catcodes is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
		$domains = array( 'media', 'text' );
64
65
		$convert = array(
66
			1 => 'Text/LatinUTF8',
67
		);
68
69
		$this->context->getConfig()->set( 'controller/jobs/catalog/import/csv/converter', $convert );
70
71
		$this->object->run();
72
73
		$tree = $this->get( 'job_csv_test', $domains );
74
		$this->delete( $tree, $domains );
75
76
		$this->assertEquals( 2, count( $tree->getListItems() ) );
77
78
		foreach( $tree->getChildren() as $node ) {
79
			$this->assertEquals( 2, count( $node->getListItems() ) );
80
		}
81
	}
82
83
84
	public function testRunUpdate()
85
	{
86
		$this->object->run();
87
		$this->object->run();
88
89
		$tree = $this->get( 'job_csv_test', ['media', 'text'] );
90
		$this->delete( $tree, ['media', 'text'] );
91
92
		$this->assertEquals( 2, count( $tree->getListItems() ) );
93
94
		foreach( $tree->getChildren() as $node ) {
95
			$this->assertEquals( 2, count( $node->getListItems() ) );
96
		}
97
	}
98
99
100
	public function testRunPosition()
101
	{
102
		$config = $this->context->getConfig();
103
		$mapping = $config->set( 'controller/jobs/catalog/import/csv/mapping', [] );
104
		$mapping['item'] = array( 0 => 'catalog.label', 1 => 'catalog.code', 2 => 'catalog.parent' );
105
106
		$config->set( 'controller/jobs/catalog/import/csv/mapping', $mapping );
107
		$config->set( 'controller/jobs/catalog/import/csv/location', __DIR__ . '/_testfiles/position' );
108
109
		$this->object->run();
110
111
		$tree = $this->get( 'job_csv_test' );
112
		$this->delete( $tree );
113
114
		$this->assertEquals( 1, count( $tree->getChildren() ) );
115
	}
116
117
118
	public function testRunProcessorInvalidPosition()
119
	{
120
		$mapping = array(
121
			'item' => array(
122
				0 => 'catalog.code',
123
				1 => 'catalog.parent',
124
				2 => 'catalog.label',
125
				3 => 'catalog.status',
126
			),
127
			'text' => array(
128
				4 => 'text.type',
129
				5 => 'text.content',
130
				100 => 'text.type',
131
				101 => 'text.content',
132
			),
133
			'media' => array(
134
				8 => 'media.url',
135
			),
136
		);
137
138
		$this->context->getConfig()->set( 'controller/jobs/catalog/import/csv/mapping', $mapping );
139
140
		$this->object->run();
141
142
		$tree = $this->get( 'job_csv_test' );
143
		$this->delete( $tree );
144
	}
145
146
147
	public function testRunProcessorInvalidMapping()
148
	{
149
		$mapping = array(
150
			'media' => array(
151
					8 => 'media.url',
152
			),
153
		);
154
155
		$this->context->getConfig()->set( 'controller/jobs/catalog/import/csv/mapping', $mapping );
156
157
		$this->setExpectedException( '\\Aimeos\\Controller\\Jobs\\Exception' );
158
		$this->object->run();
159
	}
160
161
162
	public function testRunProcessorInvalidData()
163
	{
164
		$mapping = array(
165
			'item' => array(
166
				0 => 'catalog.code',
167
				1 => 'catalog.parent',
168
				2 => 'catalog.label',
169
				3 => 'catalog.status',
170
			),
171
			'text' => array(
172
				4 => 'text.type',
173
				5 => 'text.content',
174
			),
175
			'media' => array(
176
				6 => 'media.url',
177
				7 => 'catalog.lists.type',
178
			),
179
		);
180
181
		$this->context->getConfig()->set( 'controller/jobs/catalog/import/csv/mapping', $mapping );
182
183
		$config = $this->context->getConfig();
184
		$config->set( 'controller/jobs/catalog/import/csv/skip-lines', 0 );
185
		$config->set( 'controller/jobs/catalog/import/csv/location', __DIR__ . '/_testfiles/invalid' );
186
187
		$this->object = new \Aimeos\Controller\Jobs\Catalog\Import\Csv\Standard( $this->context, $this->aimeos );
188
189
		$this->setExpectedException( '\\Aimeos\\Controller\\Jobs\\Exception' );
190
		$this->object->run();
191
	}
192
193
194
	public function testRunBackup()
195
	{
196
		$config = $this->context->getConfig();
197
		$config->set( 'controller/jobs/catalog/import/csv/container/type', 'Zip' );
198
		$config->set( 'controller/jobs/catalog/import/csv/location', 'tmp/import.zip' );
199
		$config->set( 'controller/jobs/catalog/import/csv/backup', 'tmp/test-%Y-%m-%d.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->object->run();
206
207
		$filename = strftime( 'tmp/test-%Y-%m-%d.zip' );
208
		$this->assertTrue( file_exists( $filename ) );
209
210
		unlink( $filename );
211
	}
212
213
214
	public function testRunBackupInvalid()
215
	{
216
		$config = $this->context->getConfig();
217
		$config->set( 'controller/jobs/catalog/import/csv/container/type', 'Zip' );
218
		$config->set( 'controller/jobs/catalog/import/csv/location', 'tmp/import.zip' );
219
		$config->set( 'controller/jobs/catalog/import/csv/backup', 'tmp/notexist/import.zip' );
220
221
		if( copy( __DIR__ . '/_testfiles/import.zip', 'tmp/import.zip' ) === false ) {
222
			throw new \RuntimeException( 'Unable to copy test file' );
223
		}
224
225
		$this->setExpectedException( '\\Aimeos\\Controller\\Jobs\\Exception' );
226
		$this->object->run();
227
	}
228
229
230
	protected function delete( \Aimeos\MShop\Catalog\Item\Iface $tree, array $domains = [] )
231
	{
232
		$catalogManager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( $this->context );
233
234
		foreach( $domains as $domain )
235
		{
236
			$manager = \Aimeos\MShop\Factory::createManager( $this->context, $domain );
237
238
			foreach( $tree->getListItems( $domain ) as $listItem ) {
239
				$manager->deleteItem( $listItem->getRefItem()->getId() );
240
			}
241
		}
242
243
		foreach( $tree->getChildren() as $node ) {
244
			$this->delete( $node, $domains );
245
		}
246
247
		$catalogManager->deleteItem( $tree->getId() );
248
	}
249
250
251
	protected function get( $catcode, array $domains = [] )
252
	{
253
		$manager = \Aimeos\MShop\Catalog\Manager\Factory::createManager( $this->context );
254
		$root = $manager->findItem( $catcode );
255
256
		return $manager->getTree( $root->getId(), $domains, \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE );
257
	}
258
}