StandardTest::testRunUpdate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 22
rs 9.8333
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2025
6
 */
7
8
9
namespace Aimeos\Controller\Jobs\Customer\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( 'customer' ) ?: $fs->mkdir( 'customer' );
25
		$fs->writef( 'customer/unittest/empty.csv', __DIR__ . '/_testfiles/empty.csv' );
26
27
		$fs->has( 'customer/valid' ) ?: $fs->mkdir( 'customer/valid' );
28
		$fs->writef( 'customer/valid/unittest/customers.csv', __DIR__ . '/_testfiles/valid/customers.csv' );
29
30
		$fs->has( 'customer/position' ) ?: $fs->mkdir( 'customer/position' );
31
		$fs->writef( 'customer/position/unittest/customers.csv', __DIR__ . '/_testfiles/position/customers.csv' );
32
	}
33
34
35
	protected function setUp() : void
36
	{
37
		\Aimeos\MShop::cache( true );
38
39
		$this->context = \TestHelper::context();
40
		$this->aimeos = \TestHelper::getAimeos();
41
42
		$config = $this->context->config();
43
		$config->set( 'controller/jobs/customer/import/csv/skip-lines', 1 );
44
		$config->set( 'controller/jobs/customer/import/csv/location', 'customer/valid' );
45
46
		$this->object = new \Aimeos\Controller\Jobs\Customer\Import\Csv\Standard( $this->context, $this->aimeos );
47
	}
48
49
50
	protected function tearDown() : void
51
	{
52
		\Aimeos\MShop::cache( false );
53
		unset( $this->object, $this->context, $this->aimeos );
54
	}
55
56
57
	public function testGetName()
58
	{
59
		$this->assertEquals( 'Customer import CSV', $this->object->getName() );
60
	}
61
62
63
	public function testGetDescription()
64
	{
65
		$text = 'Imports new and updates existing customers from CSV files';
66
		$this->assertEquals( $text, $this->object->getDescription() );
67
	}
68
69
70
	public function testRun()
71
	{
72
		$codes = array( '[email protected]', '[email protected]' );
73
74
		$this->object->run();
75
76
		$result = $this->get( $codes, ['customer/address', 'customer/property'] );
77
		$this->delete( $codes );
78
79
		$this->assertEquals( 1, count( $result ) );
80
		$this->assertEquals( 1, count( current( $result )->getPropertyItems() ) );
81
82
		foreach( $result as $customer ) {
83
			$this->assertEquals( 1, count( $customer->getAddressItems() ) );
84
		}
85
	}
86
87
88
	public function testRunUpdate()
89
	{
90
		$codes = array( '[email protected]', '[email protected]' );
91
92
		$fs = $this->context->fs( 'fs-import' );
93
		$fs->writef( 'customer/valid/unittest/customers.csv', __DIR__ . '/_testfiles/valid/customers.csv' );
94
95
		$this->object->run();
96
97
		$fs = $this->context->fs( 'fs-import' );
98
		$fs->writef( 'customer/valid/unittest/customers.csv', __DIR__ . '/_testfiles/valid/customers.csv' );
99
100
		$this->object->run();
101
102
		$result = $this->get( $codes, ['customer/address', 'customer/property'] );
103
		$this->delete( $codes );
104
105
		$this->assertEquals( 1, count( $result ) );
106
		$this->assertEquals( 1, count( current( $result )->getPropertyItems() ) );
107
108
		foreach( $result as $customer ) {
109
			$this->assertEquals( 1, count( $customer->getAddressItems() ) );
110
		}
111
	}
112
113
114
	public function testRunPosition()
115
	{
116
		$codes = array( '[email protected]', '[email protected]' );
117
118
		$config = $this->context->config();
119
		$config->set( 'controller/jobs/customer/import/csv/location', 'customer/position' );
120
121
		$mapping = array(
122
			'item' => array(
123
				0 => 'customer.label',
124
				1 => 'customer.code',
125
				2 => 'customer.status',
126
			),
127
			'property' => [
128
				3 => [
129
					'_' => 'customer.property.value',
130
					'customer.property.type' => 'testprop',
131
					'customer.property.languageid' => 'de',
132
				],
133
			],
134
		);
135
136
		$this->context->config()->set( 'controller/jobs/customer/import/csv/mapping', $mapping );
137
138
		$this->object->run();
139
140
		$result = $this->get( $codes, ['customer/property'] );
141
		$this->delete( $codes );
142
143
		$this->assertEquals( 2, count( $result ) );
144
145
		foreach( $result as $customer ) {
146
			$props = $customer->getPropertyItems( 'testprop' );
147
			$this->assertEquals( 1, count( $props ) );
148
149
			$prop = $props->first();
150
			$this->assertEquals( 'de', $prop->getLanguageId() );
151
			$this->assertEquals( 'testpropval', $prop->getValue() );
152
		}
153
	}
154
155
156
	public function testRunProcessorInvalidMapping()
157
	{
158
		$config = $this->context->config();
159
		$config->set( 'controller/jobs/customer/import/csv/location', 'customer' );
160
161
		$mapping = array(
162
			'media' => array(
163
					8 => 'media.url',
164
			),
165
		);
166
167
		$this->context->config()->set( 'controller/jobs/customer/import/csv/mapping', $mapping );
168
169
		$this->expectException( '\\Aimeos\\Controller\\Jobs\\Exception' );
170
		$this->object->run();
171
	}
172
173
174
	public function testRunCheck()
175
	{
176
		$codes = array( '[email protected]', '[email protected]' );
177
		$this->context->config()->set( 'controller/jobs/customer/import/csv/checks', [
178
			'customer.email' => '/^[a-z]+@[a-z]+\.[a-z]{2,3}$/',
179
		] );
180
181
		$this->object->run();
182
183
		$result = $this->get( $codes, ['customer/address', 'customer/property'] );
184
185
		$this->assertEquals( 0, count( $result ) );
186
	}
187
188
189
	public function testRunBackup()
190
	{
191
		$config = $this->context->config();
192
		$config->set( 'controller/jobs/customer/import/csv/backup', 'backup-%Y-%m-%d.csv' );
193
		$config->set( 'controller/jobs/customer/import/csv/location', 'customer' );
194
195
		$this->object->run();
196
197
		$filename = \Aimeos\Base\Str::strtime( 'backup-%Y-%m-%d.csv' );
198
		$this->assertTrue( $this->context->fs( 'fs-import' )->has( $filename ) );
199
200
		$this->context->fs( 'fs-import' )->rm( $filename );
201
	}
202
203
204
	protected function access( $name )
205
	{
206
		$class = new \ReflectionClass( \Aimeos\Controller\Jobs\Customer\Import\Csv\Standard::class );
207
		$method = $class->getMethod( $name );
208
		$method->setAccessible( true );
209
210
		return $method;
211
	}
212
213
214
	protected function delete( array $codes )
215
	{
216
		$customerManager = \Aimeos\MShop::create( $this->context, 'customer' );
217
218
		foreach( $this->get( $codes ) as $id => $customer ) {
219
			$customerManager->delete( $customer->getId() );
220
		}
221
222
223
		$attrManager = \Aimeos\MShop::create( $this->context, 'attribute' );
224
225
		$search = $attrManager->filter();
226
		$search->setConditions( $search->compare( '==', 'attribute.code', 'import-test' ) );
227
228
		$attrManager->delete( $attrManager->search( $search ) );
229
	}
230
231
232
	protected function get( array $codes, array $domains = [] ) : array
233
	{
234
		$customerManager = \Aimeos\MShop::create( $this->context, 'customer' );
235
236
		$search = $customerManager->filter();
237
		$search->setConditions( $search->compare( '==', 'customer.code', $codes ) );
238
239
		return $customerManager->search( $search, $domains )->all();
240
	}
241
242
243
	protected function getProperties( array $parentIds ) : array
244
	{
245
		$manager = \Aimeos\MShop::create( $this->context, 'customer/property' );
246
247
		$search = $manager->filter()->order( 'customer.property.type' )
248
			->add( ['customer.property.parentid' => $parentIds] );
249
250
		return $manager->search( $search )->all();
251
	}
252
}
253