Completed
Pull Request — master (#25)
by
unknown
02:44
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
	public function testProcessDelete()
181
	{
182
		$mapping = array(
183
			0 => 'address.type',
184
			1 => 'address.content',
185
		);
186
187
		$data = array(
188
			0 => 'name',
189
			1 => 'Job CSV test',
190
		);
191
192
		$supplier = $this->create( 'job_csv_test' );
193
194
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, $mapping, $this->endpoint );
195
		$object->process( $supplier, $data );
196
197
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, [], $this->endpoint );
198
		$object->process( $supplier, [] );
199
200
201
		$listItems = $supplier->getListItems();
202
		$this->assertEquals( 0, count( $listItems ) );
203
204
		$this->delete( $supplier );
205
	}
206
207
208
	public function testProcessEmpty()
209
	{
210
		$mapping = array(
211
			0 => 'supplier.address.languageid',
212
			1 => 'supplier.address.countryid',
213
			2 => 'supplier.address.city',
214
			3 => 'supplier.address.languageid',
215
			4 => 'supplier.address.countryid',
216
			5 => 'supplier.address.city',
217
		);
218
219
		$data = array(
220
			0 => 'de',
221
			1 => 'de',
222
			2 => 'Berlin',
223
			3 => '',
224
			4 => '',
225
			5 => '',
226
		);
227
228
		$supplier = $this->create( 'job_csv_test' );
229
230
		$object = new \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Address\Standard( $this->context, $mapping, $this->endpoint );
231
		$object->process( $supplier, $data );
232
233
		$listItems = $supplier->getAddressItems();
234
235
		$this->assertEquals( 1, count( $listItems ) );
236
	}
237
238
239
	/**
240
	 * @param string $code
241
	 */
242
	protected function create( $code )
243
	{
244
		$item = self::$supplierManager->createItem();
245
		$item->setCode( $code );
246
247
		self::$supplierManager->saveItem( $item );
248
249
		return $item;
250
	}
251
252
	protected function delete( \Aimeos\MShop\Supplier\Item\Iface $item )
253
	{
254
		$listManager = self::$supplierManager->getSubManager( 'lists' );
255
256
		$listManager->deleteItems( $item->getListItems( 'product' )->keys()->toArray() );
257
		self::$supplierManager->deleteItem( $item->getId() );
258
	}
259
260
	public function reloadSupplier( &$supplier )
261
	{
262
		$supplier = self::$supplierManager->getItem( $supplier->getId(), ['supplier/address'] );
263
	}
264
265
}
266