StandardTest::testRunProcessorInvalidMapping()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 15
rs 10
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-2026
6
 */
7
8
9
namespace Aimeos\Controller\Jobs\Supplier\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( 'supplier/unittest' ) ?: $fs->mkdir( 'supplier/unittest' );
25
		$fs->writef( 'supplier/unittest/empty.csv', __DIR__ . '/_testfiles/empty.csv' );
26
27
		$fs->has( 'supplier/valid' ) ?: $fs->mkdir( 'supplier/valid' );
28
		$fs->writef( 'supplier/valid/unittest/suppliers.csv', __DIR__ . '/_testfiles/valid/suppliers.csv' );
29
30
		$fs->has( 'supplier/position' ) ?: $fs->mkdir( 'supplier/position' );
31
		$fs->writef( 'supplier/position/unittest/suppliers.csv', __DIR__ . '/_testfiles/position/suppliers.csv' );
32
33
		$fs = $context->fs( 'fs-media' );
34
		$fs->has( 'path/to' ) ?: $fs->mkdir( 'path/to' );
35
		$fs->write( 'path/to/file2.jpg', 'test' );
36
		$fs->write( 'path/to/file.jpg', 'test' );
37
38
		$fs = $context->fs( 'fs-mimeicon' );
39
		$fs->write( 'unknown.png', 'icon' );
40
	}
41
42
43
	protected function setUp() : void
44
	{
45
		\Aimeos\MShop::cache( true );
46
47
		$this->aimeos = \TestHelper::getAimeos();
48
		$this->context = \TestHelper::context();
49
50
		$config = $this->context->config();
51
		$config->set( 'controller/jobs/supplier/import/csv/skip-lines', 1 );
52
		$config->set( 'controller/jobs/supplier/import/csv/location', 'supplier/valid' );
53
54
		$this->object = new \Aimeos\Controller\Jobs\Supplier\Import\Csv\Standard( $this->context, $this->aimeos );
55
	}
56
57
58
	protected function tearDown() : void
59
	{
60
		\Aimeos\MShop::cache( false );
61
		unset( $this->object, $this->context, $this->aimeos );
62
	}
63
64
65
	public function testGetName()
66
	{
67
		$this->assertEquals( 'Supplier import CSV', $this->object->getName() );
68
	}
69
70
71
	public function testGetDescription()
72
	{
73
		$text = 'Imports new and updates existing suppliers from CSV files';
74
		$this->assertEquals( $text, $this->object->getDescription() );
75
	}
76
77
78
	public function testRun()
79
	{
80
		$codes = ['job_csv_test', 'job_csv_test2'];
81
82
		$this->object->run();
83
84
		$result = $this->get( $codes, ['address', 'media', 'text'] );
85
		$addresses = $this->getAddresses( array_keys( $result ) );
86
87
		$this->delete( $codes, ['media', 'text'] );
88
89
		$this->assertEquals( 2, count( $result ) );
90
		$this->assertEquals( 2, count( $addresses ) );
91
92
		foreach( $result as $supplier ) {
93
			$this->assertEquals( 2, count( $supplier->getListItems() ) );
94
		}
95
	}
96
97
98
	public function testRunUpdate()
99
	{
100
		$fs = $this->context->fs( 'fs-import' );
101
		$fs->writef( 'supplier/valid/unittest/suppliers.csv', __DIR__ . '/_testfiles/valid/suppliers.csv' );
102
103
		$this->object->run();
104
105
		$fs = $this->context->fs( 'fs-import' );
106
		$fs->writef( 'supplier/valid/unittest/suppliers.csv', __DIR__ . '/_testfiles/valid/suppliers.csv' );
107
108
		$this->object->run();
109
110
		$codes = ['job_csv_test', 'job_csv_test2'];
111
		$result = $this->get( $codes, ['address', 'media', 'text'] );
112
		$addresses = $this->getAddresses( array_keys( $result ) );
113
114
		$this->delete( $codes, ['media', 'text'] );
115
116
		$this->assertEquals( 2, count( $result ) );
117
		$this->assertEquals( 2, count( $addresses ) );
118
119
		foreach( $result as $supplier ) {
120
			$this->assertEquals( 2, count( $supplier->getListItems() ) );
121
		}
122
	}
123
124
125
	public function testRunPosition()
126
	{
127
		$codes = ['job_csv_test', 'job_csv_test2'];
128
129
		$config = $this->context->config();
130
		$mapping = $config->get( 'controller/jobs/supplier/import/csv/mapping', [] );
131
		$mapping['item'] = array( 0 => 'supplier.label', 1 => 'supplier.code' );
132
133
		$config->set( 'controller/jobs/supplier/import/csv/mapping', $mapping );
134
		$config->set( 'controller/jobs/supplier/import/csv/location', 'supplier/position' );
135
136
		$this->object->run();
137
138
		$result = $this->get( $codes, ['address', 'media', 'text'] );
139
		$this->delete( $codes, ['media', 'text'] );
140
141
		$this->assertEquals( 2, count( $result ) );
142
	}
143
144
145
	public function testRunProcessorInvalidMapping()
146
	{
147
		$config = $this->context->config();
148
		$config->set( 'controller/jobs/supplier/import/csv/location', 'supplier' );
149
150
		$mapping = array(
151
			'media' => array(
152
				8 => 'media.url',
153
			),
154
		);
155
156
		$this->context->config()->set( 'controller/jobs/supplier/import/csv/mapping', $mapping );
157
158
		$this->expectException( '\\Aimeos\\Controller\\Jobs\\Exception' );
159
		$this->object->run();
160
	}
161
162
163
	public function testRunBackup()
164
	{
165
		$config = $this->context->config();
166
		$config->set( 'controller/jobs/supplier/import/csv/backup', 'backup-%Y-%m-%d.csv' );
167
		$config->set( 'controller/jobs/supplier/import/csv/location', 'supplier' );
168
169
		$this->object->run();
170
171
		$filename = \Aimeos\Base\Str::strtime( 'backup-%Y-%m-%d.csv' );
172
		$this->assertTrue( $this->context->fs( 'fs-import' )->has( $filename ) );
173
174
		$this->context->fs( 'fs-import' )->rm( $filename );
175
	}
176
177
178
	protected function delete( array $codes, array $delete )
179
	{
180
		$supplierManager = \Aimeos\MShop::create( $this->context, 'supplier' );
181
182
		foreach( $this->get( $codes, $delete ) as $id => $supplier )
183
		{
184
			foreach( $delete as $domain )
185
			{
186
				$manager = \Aimeos\MShop::create( $this->context, $domain );
187
188
				foreach( $supplier->getListItems( $domain ) as $listItem ) {
189
					$manager->delete( $listItem->getRefItem()->getId() );
190
				}
191
			}
192
193
			$supplierManager->delete( $id );
194
		}
195
196
197
		$attrManager = \Aimeos\MShop::create( $this->context, 'attribute' );
198
		$search = $attrManager->filter()->add( ['attribute.code' => 'import-test'] );
199
200
		$attrManager->delete( $attrManager->search( $search ) );
201
	}
202
203
204
	protected function get( array $codes, array $domains ) : array
205
	{
206
		$supplierManager = \Aimeos\MShop::create( $this->context, 'supplier' );
207
		$search = $supplierManager->filter()->add( ['supplier.code' => $codes] );
208
209
		return $supplierManager->search( $search, $domains )->all();
210
	}
211
212
213
	protected function getAddresses( array $prodids ) : array
214
	{
215
		$manager = \Aimeos\MShop::create( $this->context, 'supplier/address' );
216
		$search = $manager->filter()->add( ['supplier.address.parentid' => $prodids] );
217
218
		return $manager->search( $search )->all();
219
	}
220
}
221