Passed
Push — master ( f0002b...c777f5 )
by Aimeos
08:04 queued 04:27
created

StandardTest::testProcessDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 24
rs 9.8333
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-2023
6
 */
7
8
9
namespace Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Text;
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 => 'text.type',
37
			1 => 'text.content',
38
			2 => 'text.label',
39
			3 => 'text.languageid',
40
			4 => 'text.status',
41
		);
42
43
		$data = array(
44
			0 => 'name',
45
			1 => 'Job CSV test',
46
			2 => 'test text',
47
			3 => 'de',
48
			4 => 1,
49
		);
50
51
		$product = $this->create( 'job_csv_test' );
52
53
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Text\Standard( $this->context, $mapping, $this->endpoint );
54
		$object->process( $product, $data );
55
56
57
		$listItems = $product->getListItems();
58
		$listItem = $listItems->first();
59
60
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
61
		$this->assertEquals( 1, count( $listItems ) );
62
63
		$this->assertEquals( 1, $listItem->getStatus() );
64
		$this->assertEquals( 0, $listItem->getPosition() );
65
		$this->assertEquals( 'default', $listItem->getType() );
66
67
		$refItem = $listItem->getRefItem();
68
69
		$this->assertEquals( 1, $refItem->getStatus() );
70
		$this->assertEquals( 'name', $refItem->getType() );
71
		$this->assertEquals( 'test text', $refItem->getLabel() );
72
		$this->assertEquals( 'Job CSV test', $refItem->getContent() );
73
		$this->assertEquals( 'de', $refItem->getLanguageId() );
74
		$this->assertEquals( 1, $refItem->getStatus() );
75
	}
76
77
78
	public function testProcessMultiple()
79
	{
80
		$mapping = array(
81
			0 => 'text.type',
82
			1 => 'text.content',
83
			2 => 'text.type',
84
			3 => 'text.content',
85
			4 => 'text.type',
86
			5 => 'text.languageid',
87
			6 => 'text.content',
88
			7 => 'text.type',
89
			8 => 'text.languageid',
90
			9 => 'text.content',
91
		);
92
93
		$data = array(
94
			0 => 'name',
95
			1 => 'Job CSV test',
96
			2 => 'short',
97
			3 => 'Short: Job CSV test',
98
			4 => 'long',
99
			5 => 'de',
100
			6 => 'Long: Job CSV test',
101
			7 => 'long',
102
			8 => 'en',
103
			9 => 'Long: Job CSV test',
104
		);
105
106
		$product = $this->create( 'job_csv_test' );
107
108
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Text\Standard( $this->context, $mapping, $this->endpoint );
109
		$object->process( $product, $data );
110
111
112
		$pos = 0;
113
		$listItems = $product->getListItems();
114
		$expected = array(
115
			0 => array( 'name', null, 'Job CSV test' ),
116
			1 => array( 'short', null, 'Short: Job CSV test' ),
117
			2 => array( 'long', 'de', 'Long: Job CSV test' ),
118
			3 => array( 'long', 'en', 'Long: Job CSV test' ),
119
		);
120
121
		$this->assertEquals( 4, count( $listItems ) );
122
123
		foreach( $listItems as $listItem )
124
		{
125
			$this->assertEquals( $expected[$pos][0], $listItem->getRefItem()->getType() );
126
			$this->assertEquals( $expected[$pos][1], $listItem->getRefItem()->getLanguageId() );
127
			$this->assertEquals( $expected[$pos][2], $listItem->getRefItem()->getContent() );
128
			$pos++;
129
		}
130
	}
131
132
133
	public function testProcessUpdate()
134
	{
135
		$mapping = array(
136
			0 => 'text.type',
137
			1 => 'text.content',
138
		);
139
140
		$data = array(
141
			0 => 'name',
142
			1 => 'Job CSV test',
143
		);
144
145
		$dataUpdate = array(
146
			0 => 'short',
147
			1 => 'Short: Job CSV test',
148
		);
149
150
		$product = $this->create( 'job_csv_test' );
151
152
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Text\Standard( $this->context, $mapping, $this->endpoint );
153
		$object->process( $product, $data );
154
		$object->process( $product, $dataUpdate );
155
156
157
		$listItems = $product->getListItems();
158
		$listItem = $listItems->first();
159
160
		$this->assertEquals( 1, count( $listItems ) );
161
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
162
163
		$this->assertEquals( 'short', $listItem->getRefItem()->getType() );
164
		$this->assertEquals( 'Short: Job CSV test', $listItem->getRefItem()->getContent() );
165
	}
166
167
168
	public function testProcessDelete()
169
	{
170
		$mapping = array(
171
			0 => 'text.type',
172
			1 => 'text.content',
173
		);
174
175
		$data = array(
176
			0 => 'name',
177
			1 => 'Job CSV test',
178
		);
179
180
		$product = $this->create( 'job_csv_test' );
181
182
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Text\Standard( $this->context, $mapping, $this->endpoint );
183
		$object->process( $product, $data );
184
185
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Text\Standard( $this->context, [], $this->endpoint );
186
		$object->process( $product, [] );
187
188
189
		$listItems = $product->getListItems();
190
191
		$this->assertEquals( 0, count( $listItems ) );
192
	}
193
194
195
	public function testProcessEmpty()
196
	{
197
		$mapping = array(
198
			0 => 'text.type',
199
			1 => 'text.content',
200
			2 => 'text.type',
201
			3 => 'text.content',
202
		);
203
204
		$data = array(
205
			0 => 'name',
206
			1 => 'Job CSV test',
207
			2 => '',
208
			3 => '',
209
		);
210
211
		$product = $this->create( 'job_csv_test' );
212
213
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Text\Standard( $this->context, $mapping, $this->endpoint );
214
		$object->process( $product, $data );
215
216
217
		$listItems = $product->getListItems();
218
219
		$this->assertEquals( 1, count( $listItems ) );
220
	}
221
222
223
	public function testProcessListtypes()
224
	{
225
		$mapping = array(
226
			0 => 'text.type',
227
			1 => 'text.content',
228
			2 => 'product.lists.type',
229
			3 => 'text.type',
230
			4 => 'text.content',
231
			5 => 'product.lists.type',
232
		);
233
234
		$data = array(
235
			0 => 'name',
236
			1 => 'test name',
237
			2 => 'test',
238
			3 => 'short',
239
			4 => 'test short',
240
			5 => 'default',
241
		);
242
243
		$this->context->config()->set( 'controller/jobs/product/import/csv/processor/text/listtypes', array( 'default' ) );
244
245
		$product = $this->create( 'job_csv_test' );
246
247
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Text\Standard( $this->context, $mapping, $this->endpoint );
248
249
		$this->expectException( '\Aimeos\Controller\Jobs\Exception' );
250
		$object->process( $product, $data );
251
	}
252
253
254
	/**
255
	 * @param string $code
256
	 */
257
	protected function create( $code )
258
	{
259
		return \Aimeos\MShop::create( $this->context, 'product' )->create()->setCode( $code );
260
	}
261
}
262