Completed
Push — master ( 57abee...452e12 )
by Aimeos
30s queued 14s
created

StandardTest::testProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 29
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 41
rs 9.456
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2020
6
 */
7
8
9
namespace Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $endpoint;
16
	protected static $supplierManager;
17
18
19
	protected function setUp() : void
20
	{
21
		\Aimeos\MShop::cache( true );
22
23
		$this->context = \TestHelperCntl::getContext();
24
		$this->endpoint = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Done( $this->context, [] );
25
		self::$supplierManager = \Aimeos\MShop\Supplier\Manager\Factory::create( $this->context );
26
	}
27
28
29
	protected function tearDown() : void
30
	{
31
		\Aimeos\MShop::cache( false );
32
	}
33
34
35
	public function testProcess()
36
	{
37
		$mapping = array(
38
			0 => 'supplier.address.languageid',
39
			1 => 'supplier.address.countryid',
40
			2 => 'supplier.address.city',
41
			3 => 'supplier.address.firstname',
42
			4 => 'supplier.address.lastname',
43
			5 => 'supplier.address.email',
44
		);
45
46
		$data = array(
47
			0 => 'de',
48
			1 => 'de',
49
			2 => 'Berlin',
50
			3 => 'John',
51
			4 => 'Dummy',
52
			5 => '[email protected]',
53
		);
54
55
		$supplier = $this->create( 'job_csv_test' );
56
57
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, $mapping, $this->endpoint );
58
		$object->process( $supplier, $data );
59
60
		self::$supplierManager->saveItem( $supplier );
61
62
		$addressItems = $supplier->getAddressItems();
63
		$address = $addressItems->first();
64
65
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Supplier\\Item\\Address\\Iface', $address );
66
		$this->assertEquals( 1, count( $addressItems ) );
67
68
		$this->assertEquals( 'de', $address->getLanguageid() );
69
		$this->assertEquals( 'DE', $address->getCountryid() );
70
		$this->assertEquals( 'Berlin', $address->getCity() );
71
		$this->assertEquals( 'John', $address->getFirstname() );
72
		$this->assertEquals( 'Dummy', $address->getLastname() );
73
		$this->assertEquals( '[email protected]', $address->getEmail() );
74
75
		$this->delete( $supplier );
76
	}
77
78
79
	/*
80
	 * There are no ability to add several addresses to one Supplier from CSV Import.
81
	 * Because of if Supplier has one address, it will be rewrite instead of add one.
82
	 * Because we cannot identify if it's a new one address or modification of existing one
83
	 *
84
	 * */
85
	public function testProcessMultiple()
86
	{
87
		$this->markTestSkipped( 'There are no ability to add several addresses to one Supplier from CSV Import.' );
88
89
		$mapping = array(
90
			0 => 'address.type',
91
			1 => 'address.content',
92
			2 => 'address.type',
93
			3 => 'address.content',
94
			4 => 'address.type',
95
			5 => 'address.content',
96
			6 => 'address.type',
97
			7 => 'address.content',
98
		);
99
100
		$data = array(
101
			0 => 'name',
102
			1 => 'Job CSV test',
103
			2 => 'short',
104
			3 => 'Short: Job CSV test',
105
			4 => 'long',
106
			5 => 'Long: Job CSV test',
107
			6 => 'long',
108
			7 => 'Long: Job CSV test 2',
109
		);
110
111
		$supplier = $this->create( 'job_csv_test' );
112
113
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, $mapping, $this->endpoint );
114
		$object->process( $supplier, $data );
115
116
117
		$pos = 0;
118
		$listItems = $supplier->getAddressItems();
119
		$expected = array(
120
			0 => array( 'name', 'Job CSV test' ),
121
			1 => array( 'short', 'Short: Job CSV test' ),
122
			2 => array( 'long', 'Long: Job CSV test' ),
123
			3 => array( 'long', 'Long: Job CSV test 2' ),
124
		);
125
126
		$this->assertEquals( 4, count( $listItems ) );
127
128
		foreach( $listItems as $listItem )
129
		{
130
			$this->assertEquals( $expected[$pos][0], $listItem->getRefItem()->getType() );
131
			$this->assertEquals( $expected[$pos][1], $listItem->getRefItem()->getContent() );
132
			$pos++;
133
		}
134
	}
135
136
137
	public function testProcessUpdate()
138
	{
139
		$mapping = array(
140
			0 => 'supplier.address.languageid',
141
			1 => 'supplier.address.countryid',
142
			2 => 'supplier.address.city',
143
		);
144
145
		$data = array(
146
			0 => 'de',
147
			1 => 'de',
148
			2 => 'Berlin',
149
		);
150
151
		$dataUpdate = array(
152
			0 => 'ru',
153
			1 => 'ru',
154
			2 => 'Moscow',
155
		);
156
157
		$supplier = $this->create( 'job_csv_test' );
158
159
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, $mapping, $this->endpoint );
160
		$object->process( $supplier, $data );
161
		self::$supplierManager->saveItem( $supplier );
162
163
		$object->process( $supplier, $dataUpdate );
164
		self::$supplierManager->saveItem( $supplier );
165
166
		$this->reloadSupplier( $supplier );
167
168
		$addressItems = $supplier->getAddressItems();
169
		$address = $addressItems->first();
170
171
		$this->delete( $supplier );
172
173
		$this->assertEquals( 1, count( $addressItems ) );
174
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Supplier\\Item\\Address\\Iface', $address );
175
176
		$this->assertEquals( 'ru', $address->getLanguageid() );
177
		$this->assertEquals( 'RU', $address->getCountryid() );
178
		$this->assertEquals( 'Moscow', $address->getCity() );
179
	}
180
181
	public function testProcessDelete()
182
	{
183
		$mapping = array(
184
			0 => 'address.type',
185
			1 => 'address.content',
186
		);
187
188
		$data = array(
189
			0 => 'name',
190
			1 => 'Job CSV test',
191
		);
192
193
		$supplier = $this->create( 'job_csv_test' );
194
195
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, $mapping, $this->endpoint );
196
		$object->process( $supplier, $data );
197
198
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, [], $this->endpoint );
199
		$object->process( $supplier, [] );
200
201
202
		$listItems = $supplier->getListItems();
203
		$this->assertEquals( 0, count( $listItems ) );
204
205
		$this->delete( $supplier );
206
	}
207
208
209
	public function testProcessEmpty()
210
	{
211
		$mapping = array(
212
			0 => 'supplier.address.languageid',
213
			1 => 'supplier.address.countryid',
214
			2 => 'supplier.address.city',
215
			3 => 'supplier.address.languageid',
216
			4 => 'supplier.address.countryid',
217
			5 => 'supplier.address.city',
218
		);
219
220
		$data = array(
221
			0 => 'de',
222
			1 => 'de',
223
			2 => 'Berlin',
224
			3 => '',
225
			4 => '',
226
			5 => '',
227
		);
228
229
		$supplier = $this->create( 'job_csv_test' );
230
231
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, $mapping, $this->endpoint );
232
		$object->process( $supplier, $data );
233
234
		$listItems = $supplier->getAddressItems();
235
236
		$this->assertEquals( 1, count( $listItems ) );
237
	}
238
239
240
	/**
241
	 * @param string $code
242
	 */
243
	protected function create( $code )
244
	{
245
		$item = self::$supplierManager->createItem();
246
		$item->setCode( $code );
247
248
		self::$supplierManager->saveItem( $item );
249
250
		return $item;
251
	}
252
253
	protected function delete( \Aimeos\MShop\Supplier\Item\Iface $item )
254
	{
255
		$listManager = self::$supplierManager->getSubManager( 'lists' );
256
257
		$listManager->deleteItems( $item->getListItems( 'product' )->keys()->toArray() );
258
		self::$supplierManager->deleteItem( $item->getId() );
259
	}
260
261
	public function reloadSupplier( &$supplier )
262
	{
263
		$supplier = self::$supplierManager->getItem( $supplier->getId(), [ 'supplier/address' ] );
264
	}
265
266
}
267