Passed
Push — master ( 2e8fc6...fa4386 )
by Aimeos
03:53
created

StandardTest::testRunCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
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
		$this->object->run();
122
123
		$result = $this->get( $codes );
124
		$this->delete( $codes );
125
126
		$this->assertEquals( 2, count( $result ) );
127
	}
128
129
130
	public function testRunProcessorInvalidMapping()
131
	{
132
		$config = $this->context->config();
133
		$config->set( 'controller/jobs/customer/import/csv/location', 'customer' );
134
135
		$mapping = array(
136
			'media' => array(
137
					8 => 'media.url',
138
			),
139
		);
140
141
		$this->context->config()->set( 'controller/jobs/customer/import/csv/mapping', $mapping );
142
143
		$this->expectException( '\\Aimeos\\Controller\\Jobs\\Exception' );
144
		$this->object->run();
145
	}
146
147
148
	public function testRunCheck()
149
	{
150
		$codes = array( '[email protected]', '[email protected]' );
151
		$this->context->config()->set( 'controller/jobs/customer/import/csv/checks', [
152
			'customer.email' => '/^[a-z]+@[a-z]+\.[a-z]{2,3}$/',
153
		] );
154
155
		$this->object->run();
156
157
		$result = $this->get( $codes, ['customer/address', 'customer/property'] );
158
159
		$this->assertEquals( 0, count( $result ) );
160
	}
161
162
163
	public function testRunBackup()
164
	{
165
		$config = $this->context->config();
166
		$config->set( 'controller/jobs/customer/import/csv/backup', 'backup-%Y-%m-%d.csv' );
167
		$config->set( 'controller/jobs/customer/import/csv/location', 'customer' );
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 access( $name )
179
	{
180
		$class = new \ReflectionClass( \Aimeos\Controller\Jobs\Customer\Import\Csv\Standard::class );
181
		$method = $class->getMethod( $name );
182
		$method->setAccessible( true );
183
184
		return $method;
185
	}
186
187
188
	protected function delete( array $codes )
189
	{
190
		$customerManager = \Aimeos\MShop::create( $this->context, 'customer' );
191
192
		foreach( $this->get( $codes ) as $id => $customer ) {
193
			$customerManager->delete( $customer->getId() );
194
		}
195
196
197
		$attrManager = \Aimeos\MShop::create( $this->context, 'attribute' );
198
199
		$search = $attrManager->filter();
200
		$search->setConditions( $search->compare( '==', 'attribute.code', 'import-test' ) );
201
202
		$attrManager->delete( $attrManager->search( $search ) );
203
	}
204
205
206
	protected function get( array $codes, array $domains = [] ) : array
207
	{
208
		$customerManager = \Aimeos\MShop::create( $this->context, 'customer' );
209
210
		$search = $customerManager->filter();
211
		$search->setConditions( $search->compare( '==', 'customer.code', $codes ) );
212
213
		return $customerManager->search( $search, $domains )->all();
214
	}
215
216
217
	protected function getProperties( array $parentIds ) : array
218
	{
219
		$manager = \Aimeos\MShop::create( $this->context, 'customer/property' );
220
221
		$search = $manager->filter()->order( 'customer.property.type' )
222
			->add( ['customer.property.parentid' => $parentIds] );
223
224
		return $manager->search( $search )->all();
225
	}
226
}
227