Passed
Push — master ( aa616b...308afb )
by Aimeos
03:25
created

StandardTest::setUpBeforeClass()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 1
nop 0
dl 0
loc 24
rs 9.1111
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), 2018-2022
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
	public static function setUpBeforeClass() : void
20
	{
21
		$context = \TestHelper::context();
22
23
		$fs = $context->fs( 'fs-import' );
24
		$fs->has( 'catalog' ) ?: $fs->mkdir( 'catalog' );
25
		$fs->writef( 'catalog/empty.csv', __DIR__ . '/_testfiles/empty.csv' );
26
27
		$fs->has( 'catalog/valid' ) ?: $fs->mkdir( 'catalog/valid' );
28
		$fs->writef( 'catalog/valid/catalog.csv', __DIR__ . '/_testfiles/valid/catalog.csv' );
29
30
		$fs->has( 'catalog/invalid' ) ?: $fs->mkdir( 'catalog/invalid' );
31
		$fs->writef( 'catalog/invalid/catalog.csv', __DIR__ . '/_testfiles/invalid/catalog.csv' );
32
33
		$fs->has( 'catalog/position' ) ?: $fs->mkdir( 'catalog/position' );
34
		$fs->writef( 'catalog/position/catalog.csv', __DIR__ . '/_testfiles/position/catalog.csv' );
35
36
		$fs = $context->fs( 'fs-media' );
37
		$fs->has( 'path/to' ) ?: $fs->mkdir( 'path/to' );
38
		$fs->write( 'path/to/file2.jpg', 'test' );
39
		$fs->write( 'path/to/file.jpg', 'test' );
40
41
		$fs = $context->fs( 'fs-mimeicon' );
42
		$fs->write( 'unknown.png', 'icon' );
43
	}
44
45
46
	protected function setUp() : void
47
	{
48
		\Aimeos\MShop::cache( true );
49
50
		$this->context = \TestHelper::context();
51
		$this->aimeos = \TestHelper::getAimeos();
52
53
		$config = $this->context->config();
54
		$config->set( 'controller/jobs/catalog/import/csv/skip-lines', 1 );
55
		$config->set( 'controller/jobs/catalog/import/csv/location', 'catalog/valid' );
56
57
		$this->object = new \Aimeos\Controller\Jobs\Catalog\Import\Csv\Standard( $this->context, $this->aimeos );
58
	}
59
60
61
	protected function tearDown() : void
62
	{
63
		\Aimeos\MShop::cache( false );
64
		unset( $this->object, $this->context, $this->aimeos );
65
	}
66
67
68
	public function testGetName()
69
	{
70
		$this->assertEquals( 'Catalog import CSV', $this->object->getName() );
71
	}
72
73
74
	public function testGetDescription()
75
	{
76
		$text = 'Imports new and updates existing categories from CSV files';
77
		$this->assertEquals( $text, $this->object->getDescription() );
78
	}
79
80
81
	public function testRun()
82
	{
83
		$catcodes = array( 'job_csv_test', 'job_csv_test2', 'job_csv_test3', 'job_csv_test4' );
0 ignored issues
show
Unused Code introduced by
The assignment to $catcodes is dead and can be removed.
Loading history...
84
		$domains = array( 'media', 'text' );
85
86
		$this->object->run();
87
88
		$tree = $this->get( 'job_csv_test', $domains );
89
		$this->delete( $tree, $domains );
90
91
		$this->assertEquals( 2, count( $tree->getListItems() ) );
92
93
		foreach( $tree->getChildren() as $node ) {
94
			$this->assertEquals( 2, count( $node->getListItems() ) );
95
		}
96
	}
97
98
99
	public function testRunUpdate()
100
	{
101
		$fs = $this->context->fs( 'fs-import' );
102
		$fs->writef( 'catalog/valid/catalog.csv', __DIR__ . '/_testfiles/valid/catalog.csv' );
103
104
		$this->object->run();
105
106
		$fs = $this->context->fs( 'fs-import' );
107
		$fs->writef( 'catalog/valid/catalog.csv', __DIR__ . '/_testfiles/valid/catalog.csv' );
108
109
		$this->object->run();
110
111
		$tree = $this->get( 'job_csv_test', ['media', 'text'] );
112
		$this->delete( $tree, ['media', 'text'] );
113
114
		$this->assertEquals( 2, count( $tree->getListItems() ) );
115
116
		foreach( $tree->getChildren() as $node ) {
117
			$this->assertEquals( 2, count( $node->getListItems() ) );
118
		}
119
	}
120
121
122
	public function testRunPosition()
123
	{
124
		$config = $this->context->config();
125
		$mapping = $config->get( 'controller/jobs/catalog/import/csv/mapping', [] );
126
		$mapping['item'] = array( 0 => 'catalog.label', 1 => 'catalog.code', 2 => 'catalog.parent' );
127
128
		$config->set( 'controller/jobs/catalog/import/csv/mapping', $mapping );
129
		$config->set( 'controller/jobs/catalog/import/csv/location', 'catalog/position' );
130
131
		$this->object->run();
132
133
		$tree = $this->get( 'job_csv_test' );
134
		$this->delete( $tree );
135
136
		$this->assertEquals( 1, count( $tree->getChildren() ) );
137
	}
138
139
140
	public function testRunProcessorInvalidMapping()
141
	{
142
		$config = $this->context->config();
143
		$config->set( 'controller/jobs/catalog/import/csv/location', 'catalog' );
144
145
		$mapping = array(
146
			'media' => array(
147
					8 => 'media.url',
148
			),
149
		);
150
151
		$this->context->config()->set( 'controller/jobs/catalog/import/csv/mapping', $mapping );
152
153
		$this->expectException( '\\Aimeos\\Controller\\Jobs\\Exception' );
154
		$this->object->run();
155
	}
156
157
158
	public function testRunProcessorInvalidData()
159
	{
160
		$mapping = array(
161
			'item' => array(
162
				0 => 'catalog.code',
163
				1 => 'catalog.parent',
164
				2 => 'catalog.label',
165
				3 => 'catalog.status',
166
			),
167
			'text' => array(
168
				4 => 'text.type',
169
				5 => 'text.content',
170
			),
171
			'media' => array(
172
				6 => 'media.url',
173
				7 => 'catalog.lists.type',
174
			),
175
		);
176
177
		$this->context->config()->set( 'controller/jobs/catalog/import/csv/mapping', $mapping );
178
179
		$config = $this->context->config();
180
		$config->set( 'controller/jobs/catalog/import/csv/skip-lines', 0 );
181
		$config->set( 'controller/jobs/catalog/import/csv/location', 'catalog/invalid' );
182
183
		$this->object = new \Aimeos\Controller\Jobs\Catalog\Import\Csv\Standard( $this->context, $this->aimeos );
184
185
		$this->expectException( '\\Aimeos\\Controller\\Jobs\\Exception' );
186
		$this->object->run();
187
	}
188
189
190
	public function testRunBackup()
191
	{
192
		$config = $this->context->config();
193
		$config->set( 'controller/jobs/catalog/import/csv/backup', 'backup-%Y-%m-%d.csv' );
194
		$config->set( 'controller/jobs/catalog/import/csv/location', 'catalog' );
195
196
		$this->object->run();
197
198
		$filename = \Aimeos\Base\Str::strtime( 'backup-%Y-%m-%d.csv' );
199
		$this->assertTrue( $this->context->fs( 'fs-import' )->has( $filename ) );
200
201
		$this->context->fs( 'fs-import' )->rm( $filename );
202
	}
203
204
205
	protected function delete( \Aimeos\MShop\Catalog\Item\Iface $tree, array $domains = [] )
206
	{
207
		$catalogManager = \Aimeos\MShop::create( $this->context, 'catalog' );
208
209
		foreach( $domains as $domain )
210
		{
211
			$manager = \Aimeos\MShop::create( $this->context, $domain );
212
213
			foreach( $tree->getListItems( $domain ) as $listItem ) {
214
				$manager->delete( $listItem->getRefItem()->getId() );
215
			}
216
		}
217
218
		foreach( $tree->getChildren() as $node ) {
219
			$this->delete( $node, $domains );
220
		}
221
222
		$catalogManager->delete( $tree->getId() );
223
	}
224
225
226
	protected function get( $catcode, array $domains = [] )
227
	{
228
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
229
		$root = $manager->find( $catcode );
230
231
		return $manager->getTree( $root->getId(), $domains, \Aimeos\MW\Tree\Manager\Base::LEVEL_TREE );
232
	}
233
}
234