Completed
Pull Request — master (#25)
by
unknown
04:29
created

StandardTest   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 312
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 155
c 1
b 0
f 0
dl 0
loc 312
rs 10
wmc 21

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testProcessMultiple() 0 29 3
A testProcessDelete() 0 27 1
A testProcessEmpty() 0 28 1
A get() 0 12 2
A tearDownAfterClass() 0 4 1
A createListType() 0 15 1
A deleteListType() 0 7 1
A delete() 0 7 1
A testProcess() 0 31 3
A create() 0 10 1
A testProcessUpdate() 0 36 1
A tearDown() 0 6 2
A setUpBeforeClass() 0 10 1
A testProcessListtypes() 0 24 1
A setUp() 0 6 1
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
		\Aimeos\MShop::cache( false );
57
	}
58
59
60
	public function testProcess()
61
	{
62
		$mapping = array(
63
			0 => 'supplier.lists.type',
64
			1 => 'supplier.code',
65
			2 => 'supplier.lists.type',
66
			3 => 'supplier.code',
67
		);
68
69
		$data = array(
70
			0 => 'default',
71
			1 => 'job_csv_test',
72
			2 => 'default',
73
			3 => 'job_csv_test2',
74
		);
75
76
		$suppliersCodes = ['job_csv_test', 'job_csv_test2'];
77
78
		foreach( $suppliersCodes as $code )
79
			$this->create( $code );
80
81
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
82
		$object->process( self::$product, $data );
83
84
		foreach( $suppliersCodes as $code ) {
85
			$supplier = $this->get( $code );
86
			$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

86
			$this->delete( /** @scrutinizer ignore-type */ $supplier );
Loading history...
87
88
			$actualProductItem = $supplier->getRefItems()->first()[self::$product->getId()];
89
90
			$this->assertEquals( self::$product->getId(), $actualProductItem->getId() );
91
		}
92
93
	}
94
95
96
	public function testProcessMultiple()
97
	{
98
		$mapping = array(
99
			0 => 'supplier.lists.type',
100
			1 => 'supplier.code',
101
			2 => 'supplier.lists.type',
102
			3 => 'supplier.code',
103
		);
104
105
		$data = array(
106
			0 => 'default',
107
			1 => "job_csv_test\njob_csv_test2",
108
		);
109
110
		$suppliersCodes = ['job_csv_test', 'job_csv_test2'];
111
112
		foreach( $suppliersCodes as $code )
113
			$this->create( $code );
114
115
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
116
		$object->process( self::$product, $data );
117
118
		foreach( $suppliersCodes as $code ) {
119
			$supplier = $this->get( $code );
120
			$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

120
			$this->delete( /** @scrutinizer ignore-type */ $supplier );
Loading history...
121
122
			$actualProductItem = $supplier->getRefItems()->first()[self::$product->getId()];
123
124
			$this->assertEquals( self::$product->getId(), $actualProductItem->getId() );
125
		}
126
	}
127
128
129
	public function testProcessUpdate()
130
	{
131
		$mapping = array(
132
			0 => 'supplier.lists.type',
133
			1 => 'supplier.code',
134
		);
135
136
		$data = array(
137
			0 => 'default',
138
			1 => 'job_csv_test',
139
		);
140
141
		$dataUpdate = array(
142
			0 => 'promotion',
143
			1 => 'job_csv_test',
144
		);
145
146
		$listType = $this->createListType( 'promotion' );
147
		$this->create( 'job_csv_test' );
148
149
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
150
		$object->process( self::$product, $data );
151
		$object->process( self::$product, $dataUpdate );
152
153
		$supplier = $this->get( 'job_csv_test' );
154
		$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

154
		$this->delete( /** @scrutinizer ignore-type */ $supplier );
Loading history...
155
		$this->deleteListType( $listType );
156
157
158
		$listItems = $supplier->getListItems();
159
		$listItem = $listItems->first();
160
161
		$this->assertEquals( 1, count( $listItems ) );
162
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Lists\\Iface', $listItem );
163
164
		$this->assertEquals( 'job_csv_prod', $listItem->getRefItem()->getCode() );
165
	}
166
167
168
	public function testProcessDelete()
169
	{
170
		$mapping = array(
171
			0 => 'supplier.lists.type',
172
			1 => 'supplier.code',
173
		);
174
175
		$data = array(
176
			0 => 'default',
177
			1 => 'job_csv_test',
178
		);
179
180
		$this->create( 'job_csv_test' );
181
182
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
183
		$object->process( self::$product, $data );
184
185
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, [], $this->endpoint );
186
		$object->process( self::$product, [] );
187
188
		$supplier = $this->get( 'job_csv_test' );
189
		$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

189
		$this->delete( /** @scrutinizer ignore-type */ $supplier );
Loading history...
190
191
192
		$listItems = $supplier->getListItems();
193
194
		$this->assertEquals( 0, count( $listItems ) );
195
	}
196
197
198
	public function testProcessEmpty()
199
	{
200
		$mapping = array(
201
			0 => 'supplier.lists.type',
202
			1 => 'supplier.code',
203
			2 => 'supplier.lists.type',
204
			3 => 'supplier.code',
205
		);
206
207
		$data = array(
208
			0 => '',
209
			1 => '',
210
			2 => 'default',
211
			3 => 'job_csv_test',
212
		);
213
214
		$this->create( 'job_csv_test' );
215
216
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
217
		$object->process( self::$product, $data );
218
219
		$supplier = $this->get( 'job_csv_test' );
220
		$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

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