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

StandardTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 115
dl 0
loc 222
rs 10
c 0
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 2
A testProcessEmpty() 0 25 1
A testProcess() 0 33 1
A testProcessMultiple() 0 31 2
A testProcessUpdate() 0 31 1
A tearDown() 0 3 1
A testProcessListtypes() 0 24 1
A testProcessDelete() 0 22 1
A create() 0 3 1
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\Product;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $endpoint;
16
	private $products;
17
18
19
	protected function setUp() : void
20
	{
21
		\Aimeos\MShop::cache( true );
22
23
		$this->context = \TestHelper::context();
24
		$this->endpoint = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Done( $this->context, [] );
25
26
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
27
		$search = $manager->filter();
28
		$search->setConditions( $search->compare( '==', 'product.code', ['CNC', 'CNE'] ) );
29
30
		$this->products = [];
31
		foreach( $manager->search( $search ) as $id => $item ) {
32
			$this->products[$item->getCode()] = $id;
33
		}
34
	}
35
36
37
	protected function tearDown() : void
38
	{
39
		\Aimeos\MShop::cache( false );
40
	}
41
42
43
	public function testProcess()
44
	{
45
		$mapping = array(
46
			0 => 'product.lists.type',
47
			1 => 'product.code',
48
			2 => 'product.lists.type',
49
			3 => 'product.code',
50
		);
51
52
		$data = array(
53
			0 => 'default',
54
			1 => 'CNC',
55
			2 => 'suggestion',
56
			3 => 'CNE',
57
		);
58
59
		$product = $this->create( 'job_csv_test' );
60
61
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Product\Standard( $this->context, $mapping, $this->endpoint );
62
		$object->process( $product, $data );
63
64
65
		$listItems1 = $product->getListItems( 'product', 'default' );
66
		$listItems2 = $product->getListItems( 'product', 'suggestion' );
67
68
		$this->assertEquals( 1, count( $listItems1 ) );
69
		$this->assertEquals( 1, count( $listItems2 ) );
70
71
		$this->assertEquals( 1, $listItems1->first()->getStatus() );
72
		$this->assertEquals( 1, $listItems2->first()->getStatus() );
73
74
		$this->assertEquals( $this->products['CNC'], $listItems1->first()->getRefId() );
75
		$this->assertEquals( $this->products['CNE'], $listItems2->first()->getRefId() );
76
	}
77
78
79
	public function testProcessMultiple()
80
	{
81
		$mapping = array(
82
			0 => 'product.lists.type',
83
			1 => 'product.code',
84
		);
85
86
		$data = array(
87
			0 => 'default',
88
			1 => "CNC\nCNE",
89
		);
90
91
		$product = $this->create( 'job_csv_test' );
92
93
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Product\Standard( $this->context, $mapping, $this->endpoint );
94
		$object->process( $product, $data );
95
96
97
		$pos = 0;
98
		$listItems = $product->getListItems();
99
		$prodIds = array( $this->products['CNC'], $this->products['CNE'] );
100
101
		$this->assertEquals( 2, count( $listItems ) );
102
103
		foreach( $listItems as $listItem )
104
		{
105
			$this->assertEquals( 1, $listItem->getStatus() );
106
			$this->assertEquals( 'product', $listItem->getDomain() );
107
			$this->assertEquals( 'default', $listItem->getType() );
108
			$this->assertEquals( $prodIds[$pos], $listItem->getRefId() );
109
			$pos++;
110
		}
111
	}
112
113
114
	public function testProcessUpdate()
115
	{
116
		$mapping = array(
117
			0 => 'product.lists.type',
118
			1 => 'product.code',
119
		);
120
121
		$data = array(
122
			0 => 'default',
123
			1 => 'CNC',
124
		);
125
126
		$dataUpdate = array(
127
			0 => 'default',
128
			1 => 'CNE',
129
		);
130
131
		$product = $this->create( 'job_csv_test' );
132
133
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Product\Standard( $this->context, $mapping, $this->endpoint );
134
		$object->process( $product, $data );
135
		$object->process( $product, $dataUpdate );
136
137
138
		$listItems = $product->getListItems();
139
		$listItem = $listItems->first();
140
141
		$this->assertEquals( 1, count( $listItems ) );
142
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
143
144
		$this->assertEquals( $this->products['CNE'], $listItem->getRefId() );
145
	}
146
147
148
	public function testProcessDelete()
149
	{
150
		$mapping = array(
151
			0 => 'product.lists.type',
152
			1 => 'product.code',
153
		);
154
155
		$data = array(
156
			0 => 'default',
157
			1 => 'CNC',
158
		);
159
160
		$product = $this->create( 'job_csv_test' );
161
162
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Product\Standard( $this->context, $mapping, $this->endpoint );
163
		$object->process( $product, $data );
164
165
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Product\Standard( $this->context, [], $this->endpoint );
166
		$object->process( $product, [] );
167
168
169
		$this->assertEquals( 0, count( $product->getListItems() ) );
170
	}
171
172
173
	public function testProcessEmpty()
174
	{
175
		$mapping = array(
176
			0 => 'product.lists.type',
177
			1 => 'product.code',
178
			2 => 'product.lists.type',
179
			3 => 'product.code',
180
		);
181
182
		$data = array(
183
			0 => '',
184
			1 => '',
185
			2 => 'default',
186
			3 => 'CNE',
187
		);
188
189
		$product = $this->create( 'job_csv_test' );
190
191
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Product\Standard( $this->context, $mapping, $this->endpoint );
192
		$object->process( $product, $data );
193
194
195
		$listItems = $product->getListItems();
196
197
		$this->assertEquals( 1, count( $listItems ) );
198
	}
199
200
201
	public function testProcessListtypes()
202
	{
203
		$mapping = array(
204
			0 => 'product.lists.type',
205
			1 => 'product.code',
206
			2 => 'product.lists.type',
207
			3 => 'product.code',
208
		);
209
210
		$data = array(
211
			0 => 'bought-together',
212
			1 => 'CNC',
213
			2 => 'default',
214
			3 => 'CNE',
215
		);
216
217
		$this->context->config()->set( 'controller/jobs/product/import/csv/processor/product/listtypes', array( 'default' ) );
218
219
		$product = $this->create( 'job_csv_test' );
220
221
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Product\Standard( $this->context, $mapping, $this->endpoint );
222
223
		$this->expectException( '\Aimeos\Controller\Jobs\Exception' );
224
		$object->process( $product, $data );
225
	}
226
227
228
	/**
229
	 * @param string $code
230
	 */
231
	protected function create( $code )
232
	{
233
		return \Aimeos\MShop::create( $this->context, 'product' )->create()->setCode( $code );
234
	}
235
}
236