Passed
Pull Request — master (#25)
by
unknown
02:30
created

StandardTest::testProcessEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 9.7
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\Product\Import\Csv\Processor\Supplier;
10
11
12
use Aimeos\Shop\Base\Aimeos;
0 ignored issues
show
Bug introduced by
The type Aimeos\Shop\Base\Aimeos was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class StandardTest extends \PHPUnit\Framework\TestCase
15
{
16
	private static $product;
17
	private static $supplier;
18
	private $context;
19
	private $endpoint;
20
21
22
	public static function setUpBeforeClass(): void
23
	{
24
		$manager = \Aimeos\MShop\Product\Manager\Factory::create( \TestHelperCntl::getContext() );
25
26
		$item = $manager->createItem();
27
		$item->setCode( 'job_csv_prod' );
28
		$item->setType( 'default' );
29
		$item->setStatus( 1 );
30
31
		self::$product = $manager->saveItem( $item );
0 ignored issues
show
Bug introduced by
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean saveItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
		/** @scrutinizer ignore-call */ 
32
  self::$product = $manager->saveItem( $item );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
	}
33
34
35
	public static function tearDownAfterClass(): void
36
	{
37
		$manager = \Aimeos\MShop\Product\Manager\Factory::create( \TestHelperCntl::getContext() );
38
		$manager->deleteItem( self::$product->getId() );
39
	}
40
41
42
	protected function setUp(): void
43
	{
44
		\Aimeos\MShop::cache( true );
45
46
		$this->context = \TestHelperCntl::getContext();
47
		$this->endpoint = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Done( $this->context, [] );
48
	}
49
50
51
	protected function tearDown(): void
52
	{
53
		if( self::$supplier != null ){
54
			$this->delete( self::$supplier );
55
		}
56
57
		\Aimeos\MShop::cache( false );
58
	}
59
60
61
	public function testProcess()
62
	{
63
		$mapping = array(
64
			0 => 'supplier.lists.type',
65
			1 => 'supplier.code',
66
			2 => 'supplier.lists.type',
67
			3 => 'supplier.code',
68
		);
69
70
		$data = array(
71
			0 => 'default',
72
			1 => 'job_csv_test',
73
			2 => 'default',
74
			3 => 'job_csv_test2',
75
		);
76
77
		$suppliersCodes = ['job_csv_test', 'job_csv_test2'];
78
79
		foreach( $suppliersCodes as $code ){
80
			$this->create( $code );
81
		}
82
83
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
84
		$object->process( self::$product, $data );
85
86
		foreach( $suppliersCodes as $code ) {
87
			$supplier = $this->get( $code );
88
			$this->delete( $supplier );
0 ignored issues
show
Bug introduced by
It seems like $supplier can also be of type null; however, parameter $item of Aimeos\Controller\Common...\StandardTest::delete() does only seem to accept Aimeos\MShop\Supplier\Item\Iface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
			$this->delete( /** @scrutinizer ignore-type */ $supplier );
Loading history...
89
90
			$actualProductItem = $supplier->getRefItems()->first()[self::$product->getId()];
91
92
			$this->assertEquals( self::$product->getId(), $actualProductItem->getId() );
93
		}
94
	}
95
96
97
	public function testProcessMultiple()
98
	{
99
		$mapping = array(
100
			0 => 'supplier.lists.type',
101
			1 => 'supplier.code',
102
			2 => 'supplier.lists.type',
103
			3 => 'supplier.code',
104
		);
105
106
		$data = array(
107
			0 => 'default',
108
			1 => "job_csv_test\njob_csv_test2",
109
		);
110
111
		$suppliersCodes = ['job_csv_test', 'job_csv_test2'];
112
113
		foreach( $suppliersCodes as $code ){
114
			$this->create( $code );
115
		}
116
117
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
118
		$object->process( self::$product, $data );
119
120
		foreach( $suppliersCodes as $code ) {
121
			$supplier = $this->get( $code );
122
			$this->delete( $supplier );
0 ignored issues
show
Bug introduced by
It seems like $supplier can also be of type null; however, parameter $item of Aimeos\Controller\Common...\StandardTest::delete() does only seem to accept Aimeos\MShop\Supplier\Item\Iface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

122
			$this->delete( /** @scrutinizer ignore-type */ $supplier );
Loading history...
123
124
			$actualProductItem = $supplier->getRefItems()->first()[self::$product->getId()];
125
126
			$this->assertEquals( self::$product->getId(), $actualProductItem->getId() );
127
		}
128
	}
129
130
131
	public function testProcessUpdate()
132
	{
133
		$mapping = array(
134
			0 => 'supplier.lists.type',
135
			1 => 'supplier.code',
136
		);
137
138
		$data = array(
139
			0 => 'default',
140
			1 => 'job_csv_test',
141
		);
142
143
		$dataUpdate = array(
144
			0 => 'promotion',
145
			1 => 'job_csv_test',
146
		);
147
148
		$listType = $this->createListType( 'promotion' );
149
		$this->create( 'job_csv_test' );
150
151
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
152
		$object->process( self::$product, $data );
153
		$object->process( self::$product, $dataUpdate );
154
155
		$supplier = $this->get( 'job_csv_test' );
156
		$this->delete( $supplier );
0 ignored issues
show
Bug introduced by
It seems like $supplier can also be of type null; however, parameter $item of Aimeos\Controller\Common...\StandardTest::delete() does only seem to accept Aimeos\MShop\Supplier\Item\Iface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
		$this->delete( /** @scrutinizer ignore-type */ $supplier );
Loading history...
157
		$this->deleteListType( $listType );
158
159
160
		$listItems = $supplier->getListItems();
161
		$listItem = $listItems->first();
162
163
		$this->assertEquals( 1, count( $listItems ) );
164
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
165
166
		$this->assertEquals( 'job_csv_prod', $listItem->getRefItem()->getCode() );
167
	}
168
169
170
	public function testProcessDelete()
171
	{
172
		$mapping = array(
173
			0 => 'supplier.lists.type',
174
			1 => 'supplier.code',
175
		);
176
177
		$data = array(
178
			0 => 'default',
179
			1 => 'job_csv_test',
180
		);
181
182
		$this->create( 'job_csv_test' );
183
184
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
185
		$object->process( self::$product, $data );
186
187
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, [], $this->endpoint );
188
		$object->process( self::$product, [] );
189
190
		$supplier = $this->get( 'job_csv_test' );
191
		$this->delete( $supplier );
0 ignored issues
show
Bug introduced by
It seems like $supplier can also be of type null; however, parameter $item of Aimeos\Controller\Common...\StandardTest::delete() does only seem to accept Aimeos\MShop\Supplier\Item\Iface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

191
		$this->delete( /** @scrutinizer ignore-type */ $supplier );
Loading history...
192
193
194
		$listItems = $supplier->getListItems();
195
196
		$this->assertEquals( 0, count( $listItems ) );
197
	}
198
199
200
	public function testProcessEmpty()
201
	{
202
		$mapping = array(
203
			0 => 'supplier.lists.type',
204
			1 => 'supplier.code',
205
			2 => 'supplier.lists.type',
206
			3 => 'supplier.code',
207
		);
208
209
		$data = array(
210
			0 => '',
211
			1 => '',
212
			2 => 'default',
213
			3 => 'job_csv_test',
214
		);
215
216
		$this->create( 'job_csv_test' );
217
218
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
219
		$object->process( self::$product, $data );
220
221
		$supplier = $this->get( 'job_csv_test' );
222
		$this->delete( $supplier );
0 ignored issues
show
Bug introduced by
It seems like $supplier can also be of type null; however, parameter $item of Aimeos\Controller\Common...\StandardTest::delete() does only seem to accept Aimeos\MShop\Supplier\Item\Iface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

222
		$this->delete( /** @scrutinizer ignore-type */ $supplier );
Loading history...
223
224
225
		$listItems = $supplier->getListItems();
226
227
		$this->assertEquals( 1, count( $listItems ) );
228
	}
229
230
231
	public function testProcessListtypes()
232
	{
233
		$mapping = array(
234
			0 => 'supplier.lists.type',
235
			1 => 'supplier.code',
236
			2 => 'supplier.lists.type',
237
			3 => 'supplier.code',
238
		);
239
240
		$data = array(
241
			0 => 'promotion',
242
			1 => 'job_csv_test',
243
			2 => 'default',
244
			3 => 'job_csv_test',
245
		);
246
247
		$this->context->getConfig()->set( 'controller/common/product/import/csv/processor/supplier/listtypes', array('default') );
248
249
		self::$supplier = $this->create( 'job_csv_test' );
250
251
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
252
253
		$this->expectException( '\Aimeos\Controller\Common\Exception' );
254
		$object->process( self::$product, $data );
255
	}
256
257
258
	/**
259
	 * @param string $code
260
	 */
261
	protected function create( $code )
262
	{
263
		$manager = \Aimeos\MShop\Supplier\Manager\Factory::create( $this->context );
264
265
		$item = $manager->createItem();
266
		$item->setCode( $code );
267
268
		$manager->saveItem( $item );
269
270
		return $item;
271
	}
272
273
	/**
274
	 * @param string $code
275
	 */
276
	protected function createListType( $code )
277
	{
278
		$manager = \Aimeos\MShop\Supplier\Manager\Factory::create( $this->context );
279
		$supplierListManager = $manager->getSubManager( 'lists' );
280
		$supplierListTypeManager = $supplierListManager->getSubmanager( 'type' );
281
282
		$item = $supplierListTypeManager->createItem();
283
		$item->setCode( $code );
284
		$item->setDomain( 'product' );
285
		$item->setLabel( $code );
286
		$item->setStatus( 1 );
287
288
		$supplierListTypeManager->saveItem( $item );
289
290
		return $item;
291
	}
292
293
	protected function delete( \Aimeos\MShop\Supplier\Item\Iface $item )
294
	{
295
		$manager = \Aimeos\MShop\Supplier\Manager\Factory::create( $this->context );
296
		$listManager = $manager->getSubManager( 'lists' );
297
298
		$listManager->deleteItems( $item->getListItems( 'product' )->keys()->toArray() );
299
		$manager->deleteItem( $item->getId() );
300
	}
301
302
303
	protected function deleteListType( \Aimeos\MShop\Common\Item\Iface $item )
304
	{
305
		$manager = \Aimeos\MShop\Supplier\Manager\Factory::create( $this->context );
306
		$listManager = $manager->getSubManager( 'lists' );
307
		$listTypeManager = $listManager->getSubmanager( 'type' );
308
309
		$listTypeManager->deleteItem( $item->getId() );
310
	}
311
312
313
	/**
314
	 * @param string $code
315
	 */
316
	protected function get( $code )
317
	{
318
		$manager = \Aimeos\MShop\Supplier\Manager\Factory::create( $this->context );
319
320
		$search = $manager->createSearch();
321
		$search->setConditions( $search->compare( '==', 'supplier.code', $code ) );
322
323
		if( ($item = $manager->searchItems( $search, ['product'] )->first()) === null ) {
324
			throw new \RuntimeException( sprintf( 'No supplier item for code "%1$s"', $code ) );
325
		}
326
327
		return $item;
328
	}
329
}
330