Completed
Pull Request — master (#25)
by
unknown
04:29
created

StandardTest::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
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
			$this->assertEquals( $expected[$pos][0], $listItem->getRefItem()->getType() );
130
			$this->assertEquals( $expected[$pos][1], $listItem->getRefItem()->getContent() );
131
			$pos++;
132
		}
133
	}
134
135
136
	public function testProcessUpdate()
137
	{
138
		$mapping = array(
139
			0 => 'supplier.address.languageid',
140
			1 => 'supplier.address.countryid',
141
			2 => 'supplier.address.city',
142
		);
143
144
		$data = array(
145
			0 => 'de',
146
			1 => 'de',
147
			2 => 'Berlin',
148
		);
149
150
		$dataUpdate = array(
151
			0 => 'ru',
152
			1 => 'ru',
153
			2 => 'Moscow',
154
		);
155
156
		$supplier = $this->create( 'job_csv_test' );
157
158
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, $mapping, $this->endpoint );
159
		$object->process( $supplier, $data );
160
		self::$supplierManager->saveItem( $supplier );
161
162
		$object->process( $supplier, $dataUpdate );
163
		self::$supplierManager->saveItem( $supplier );
164
165
		$this->reloadSupplier( $supplier );
166
167
		$addressItems = $supplier->getAddressItems();
168
		$address = $addressItems->first();
169
170
		$this->delete( $supplier );
171
172
		$this->assertEquals( 1, count( $addressItems ) );
173
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Supplier\\Item\\Address\\Iface', $address );
174
175
		$this->assertEquals( 'ru', $address->getLanguageid() );
176
		$this->assertEquals( 'RU', $address->getCountryid() );
177
		$this->assertEquals( 'Moscow', $address->getCity() );
178
179
180
	}
181
182
	public function testProcessDelete()
183
	{
184
		$mapping = array(
185
			0 => 'address.type',
186
			1 => 'address.content',
187
		);
188
189
		$data = array(
190
			0 => 'name',
191
			1 => 'Job CSV test',
192
		);
193
194
		$supplier = $this->create( 'job_csv_test' );
195
196
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, $mapping, $this->endpoint );
197
		$object->process( $supplier, $data );
198
199
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, [], $this->endpoint );
200
		$object->process( $supplier, [] );
201
202
203
		$listItems = $supplier->getListItems();
204
		$this->assertEquals( 0, count( $listItems ) );
205
206
		$this->delete( $supplier );
207
	}
208
209
210
	public function testProcessEmpty()
211
	{
212
		$mapping = array(
213
			0 => 'supplier.address.languageid',
214
			1 => 'supplier.address.countryid',
215
			2 => 'supplier.address.city',
216
			3 => 'supplier.address.languageid',
217
			4 => 'supplier.address.countryid',
218
			5 => 'supplier.address.city',
219
		);
220
221
		$data = array(
222
			0 => 'de',
223
			1 => 'de',
224
			2 => 'Berlin',
225
			3 => '',
226
			4 => '',
227
			5 => '',
228
		);
229
230
		$supplier = $this->create( 'job_csv_test' );
231
232
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, $mapping, $this->endpoint );
233
		$object->process( $supplier, $data );
234
235
		$listItems = $supplier->getAddressItems();
236
237
		$this->assertEquals( 1, count( $listItems ) );
238
	}
239
240
241
	/**
242
	 * @param string $code
243
	 */
244
	protected function create( $code )
245
	{
246
		$item = self::$supplierManager->createItem();
247
		$item->setCode( $code );
248
249
		self::$supplierManager->saveItem( $item );
250
251
		return $item;
252
	}
253
254
	protected function delete( \Aimeos\MShop\Supplier\Item\Iface $item )
255
	{
256
		$listManager = self::$supplierManager->getSubManager( 'lists' );
257
258
		$listManager->deleteItems( $item->getListItems( 'product' )->keys()->toArray() );
259
		self::$supplierManager->deleteItem( $item->getId() );
260
	}
261
262
	public function reloadSupplier( &$supplier )
263
	{
264
		$supplier = self::$supplierManager->getItem( $supplier->getId(), ['supplier/address'] );
265
	}
266
267
}
268