Passed
Push — master ( 677585...094812 )
by Aimeos
02:37
created

StandardTest::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2021
6
 */
7
8
9
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $endpoint;
16
	private $product;
17
18
19
	protected function setUp() : void
20
	{
21
		\Aimeos\MShop::cache( true );
22
23
		$this->context = \TestHelperCntl::context();
24
		$this->product = \Aimeos\MShop\Product\Manager\Factory::create( $this->context )->create();
25
		$this->endpoint = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Done( $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 => 'product.lists.type',
39
			1 => 'supplier.code',
40
			2 => 'product.lists.type',
41
			3 => 'supplier.code',
42
		);
43
44
		$data = array(
45
			0 => 'default',
46
			1 => 'unitSupplier001',
47
			2 => 'promotion',
48
			3 => 'unitSupplier002',
49
		);
50
51
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
52
		$object->process( $this->product, $data );
53
54
		$pos = 0;
55
		$types = ['default', 'promotion'];
56
		$listItems = $this->product->getListItems();
57
58
		$this->assertEquals( 2, count( $listItems ) );
59
60
		foreach( $listItems as $listItem )
61
		{
62
			$this->assertEquals( 1, $listItem->getStatus() );
63
			$this->assertEquals( 'supplier', $listItem->getDomain() );
64
			$this->assertEquals( $types[$pos], $listItem->getType() );
65
			$this->assertGreaterThan( 0, $listItem->getRefId() );
66
			$pos++;
67
		}
68
	}
69
70
71
	public function testProcessMultiple()
72
	{
73
		$mapping = array(
74
			0 => 'product.lists.type',
75
			1 => 'supplier.code',
76
			2 => 'product.lists.type',
77
			3 => 'supplier.code',
78
		);
79
80
		$data = array(
81
			0 => 'default',
82
			1 => "unitSupplier001\nunitSupplier002",
83
			2 => 'promotion',
84
			3 => "unitSupplier001\nunitSupplier002",
85
		);
86
87
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
88
		$object->process( $this->product, $data );
89
90
91
		$pos = 0;
92
		$listItems = $this->product->getListItems();
93
		$types = ['default', 'default', 'promotion', 'promotion'];
94
95
		$this->assertEquals( 4, count( $listItems ) );
96
97
		foreach( $listItems as $listItem )
98
		{
99
			$this->assertEquals( 1, $listItem->getStatus() );
100
			$this->assertEquals( 'supplier', $listItem->getDomain() );
101
			$this->assertEquals( $types[$pos], $listItem->getType() );
102
			$this->assertGreaterThan( 0, $listItem->getRefId() );
103
			$pos++;
104
		}
105
	}
106
107
108
	public function testProcessUpdate()
109
	{
110
		$mapping = array(
111
			0 => 'product.lists.type',
112
			1 => 'supplier.code',
113
		);
114
115
		$data = array(
116
			0 => 'default',
117
			1 => 'unitSupplier001',
118
		);
119
120
		$dataUpdate = array(
121
			0 => 'promotion',
122
			1 => 'unitSupplier001',
123
		);
124
125
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
126
		$object->process( $this->product, $data );
127
		$object->process( $this->product, $dataUpdate );
128
129
		$listItems = $this->product->getListItems();
130
		$listItem = $listItems->first();
131
132
		$this->assertEquals( 1, count( $listItems ) );
133
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
134
		$this->assertGreaterThan( 0, $listItem->getRefId() );
135
	}
136
137
138
	public function testProcessDelete()
139
	{
140
		$mapping = array(
141
			0 => 'product.lists.type',
142
			1 => 'supplier.code',
143
		);
144
145
		$data = array(
146
			0 => 'default',
147
			1 => 'unitSupplier001',
148
		);
149
150
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
151
		$object->process( $this->product, $data );
152
153
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, [], $this->endpoint );
154
		$object->process( $this->product, [] );
155
156
		$listItems = $this->product->getListItems();
157
158
		$this->assertEquals( 0, count( $listItems ) );
159
	}
160
161
162
	public function testProcessEmpty()
163
	{
164
		$mapping = array(
165
			0 => 'product.lists.type',
166
			1 => 'supplier.code',
167
			2 => 'product.lists.type',
168
			3 => 'supplier.code',
169
		);
170
171
		$data = array(
172
			0 => '',
173
			1 => '',
174
			2 => 'default',
175
			3 => 'unitSupplier001',
176
		);
177
178
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
179
		$object->process( $this->product, $data );
180
181
		$listItems = $this->product->getListItems();
182
183
		$this->assertEquals( 1, count( $listItems ) );
184
	}
185
186
187
	public function testProcessListtypes()
188
	{
189
		$mapping = array(
190
			0 => 'product.lists.type',
191
			1 => 'supplier.code',
192
			2 => 'product.lists.type',
193
			3 => 'supplier.code',
194
		);
195
196
		$data = array(
197
			0 => 'promotion',
198
			1 => 'unitSupplier001',
199
			2 => 'default',
200
			3 => 'unitSupplier001',
201
		);
202
203
		$this->context->config()->set( 'controller/common/product/import/csv/processor/supplier/listtypes', array( 'default' ) );
204
205
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
206
207
		$this->expectException( '\Aimeos\Controller\Common\Exception' );
208
		$object->process( $this->product, $data );
209
	}
210
211
212
	/**
213
	 * @param string $code
214
	 */
215
	protected function create( $code )
216
	{
217
		$manager = \Aimeos\MShop\Supplier\Manager\Factory::create( $this->context );
218
		return $manager->save( $manager->create()->setCode( $code ) );
219
	}
220
}
221