StandardTest::testProcessUpdate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 31
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2026
6
 */
7
8
9
namespace Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Price;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $endpoint;
16
17
18
	protected function setUp() : void
19
	{
20
		\Aimeos\MShop::cache( true );
21
22
		$this->context = \TestHelper::context();
23
		$this->endpoint = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Done( $this->context, [] );
24
	}
25
26
27
	protected function tearDown() : void
28
	{
29
		\Aimeos\MShop::cache( false );
30
	}
31
32
33
	public function testProcess()
34
	{
35
		$mapping = array(
36
			0 => 'price.type',
37
			1 => 'price.label',
38
			2 => 'price.currencyid',
39
			3 => 'price.quantity',
40
			4 => 'price.value',
41
			5 => 'price.costs',
42
			6 => 'price.rebate',
43
			7 => 'price.taxrate',
44
			8 => 'price.status',
45
		);
46
47
		$data = array(
48
			0 => 'default',
49
			1 => 'EUR 1.00',
50
			2 => 'EUR',
51
			3 => 5,
52
			4 => '1.00',
53
			5 => '0.20',
54
			6 => '0.10',
55
			7 => '20.00',
56
			8 => 1,
57
		);
58
59
		$product = $this->create( 'job_csv_test' );
60
61
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Price\Standard( $this->context, $mapping, $this->endpoint );
62
		$object->process( $product, $data );
63
64
65
		$listItems = $product->getListItems();
66
		$listItem = $listItems->first();
67
68
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
69
		$this->assertEquals( 1, count( $listItems ) );
70
71
		$this->assertEquals( 1, $listItem->getStatus() );
72
		$this->assertEquals( 0, $listItem->getPosition() );
73
		$this->assertEquals( 'default', $listItem->getType() );
74
75
		$refItem = $listItem->getRefItem();
76
77
		$this->assertEquals( 1, $refItem->getStatus() );
78
		$this->assertEquals( 'default', $refItem->getType() );
79
		$this->assertEquals( 'EUR 1.00', $refItem->getLabel() );
80
		$this->assertEquals( 5, $refItem->getQuantity() );
81
		$this->assertEquals( '1.00', $refItem->getValue() );
82
		$this->assertEquals( '0.20', $refItem->getCosts() );
83
		$this->assertEquals( '0.10', $refItem->getRebate() );
84
		$this->assertEquals( '20.00', $refItem->getTaxrate() );
85
		$this->assertEquals( 1, $refItem->getStatus() );
86
	}
87
88
89
	public function testProcessMultiple()
90
	{
91
		$mapping = array(
92
			0 => 'price.currencyid',
93
			1 => 'price.value',
94
			2 => 'price.currencyid',
95
			3 => 'price.value',
96
			4 => 'price.currencyid',
97
			5 => 'price.value',
98
			6 => 'price.currencyid',
99
			7 => 'price.value',
100
		);
101
102
		$data = array(
103
			0 => 'EUR',
104
			1 => '1.00',
105
			2 => 'EUR',
106
			3 => '2.00',
107
			4 => 'EUR',
108
			5 => '3.00',
109
			6 => 'EUR',
110
			7 => '4.00',
111
		);
112
113
		$product = $this->create( 'job_csv_test' );
114
115
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Price\Standard( $this->context, $mapping, $this->endpoint );
116
		$object->process( $product, $data );
117
118
119
		$pos = 0;
120
		$listItems = $product->getListItems();
121
122
		$this->assertEquals( 4, count( $listItems ) );
123
124
		foreach( $listItems as $listItem )
125
		{
126
			$this->assertEquals( $data[$pos++], $listItem->getRefItem()->getCurrencyId() );
127
			$this->assertEquals( $data[$pos++], $listItem->getRefItem()->getValue() );
128
		}
129
	}
130
131
132
	public function testProcessUpdate()
133
	{
134
		$mapping = array(
135
			0 => 'price.currencyid',
136
			1 => 'price.value',
137
		);
138
139
		$data = array(
140
			0 => 'EUR',
141
			1 => '1.00',
142
		);
143
144
		$dataUpdate = array(
145
			0 => 'EUR',
146
			1 => '2.00',
147
		);
148
149
		$product = $this->create( 'job_csv_test' );
150
151
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Price\Standard( $this->context, $mapping, $this->endpoint );
152
		$object->process( $product, $data );
153
		$object->process( $product, $dataUpdate );
154
155
156
		$listItems = $product->getListItems();
157
		$listItem = $listItems->first();
158
159
		$this->assertEquals( 1, count( $listItems ) );
160
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
161
162
		$this->assertEquals( '2.00', $listItem->getRefItem()->getValue() );
163
	}
164
165
166
	public function testProcessDelete()
167
	{
168
		$mapping = array(
169
			0 => 'price.currencyid',
170
			1 => 'price.value',
171
		);
172
173
		$data = array(
174
			0 => 'EUR',
175
			1 => '1.00',
176
		);
177
178
		$product = $this->create( 'job_csv_test' );
179
180
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Price\Standard( $this->context, $mapping, $this->endpoint );
181
		$object->process( $product, $data );
182
183
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Price\Standard( $this->context, [], $this->endpoint );
184
		$object->process( $product, [] );
185
186
187
		$listItems = $product->getListItems();
188
189
		$this->assertEquals( 0, count( $listItems ) );
190
	}
191
192
193
	public function testProcessEmpty()
194
	{
195
		$mapping = array(
196
			0 => 'price.currencyid',
197
			1 => 'price.value',
198
			2 => 'price.currencyid',
199
			3 => 'price.value',
200
		);
201
202
		$data = array(
203
			0 => 'EUR',
204
			1 => '1.00',
205
			2 => '',
206
			3 => '',
207
		);
208
209
		$product = $this->create( 'job_csv_test' );
210
211
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Price\Standard( $this->context, $mapping, $this->endpoint );
212
		$object->process( $product, $data );
213
214
215
		$listItems = $product->getListItems();
216
217
		$this->assertEquals( 1, count( $listItems ) );
218
	}
219
220
221
	public function testProcessListtypes()
222
	{
223
		$mapping = array(
224
			0 => 'price.currencyid',
225
			1 => 'price.value',
226
			2 => 'product.lists.type',
227
			3 => 'price.currencyid',
228
			4 => 'price.value',
229
			5 => 'product.lists.type',
230
		);
231
232
		$data = array(
233
			0 => 'EUR',
234
			1 => '1.00',
235
			2 => 'test',
236
			3 => 'EUR',
237
			4 => '2.00',
238
			5 => 'default',
239
		);
240
241
		$this->context->config()->set( 'controller/jobs/product/import/csv/processor/price/listtypes', array( 'default' ) );
242
243
		$product = $this->create( 'job_csv_test' );
244
245
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Price\Standard( $this->context, $mapping, $this->endpoint );
246
247
		$this->expectException( '\Aimeos\Controller\Jobs\Exception' );
248
		$object->process( $product, $data );
249
	}
250
251
252
	/**
253
	 * @param string $code
254
	 */
255
	protected function create( $code )
256
	{
257
		return \Aimeos\MShop::create( $this->context, 'product' )->create()->setCode( $code );
258
	}
259
}
260