Completed
Push — master ( 57abee...452e12 )
by Aimeos
30s queued 14s
created

StandardTest::tearDown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
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
		{
55
			$this->delete( self::$supplier );
56
		}
57
58
		\Aimeos\MShop::cache( false );
59
	}
60
61
62
	public function testProcess()
63
	{
64
		$mapping = array(
65
			0 => 'supplier.lists.type',
66
			1 => 'supplier.code',
67
			2 => 'supplier.lists.type',
68
			3 => 'supplier.code',
69
		);
70
71
		$data = array(
72
			0 => 'default',
73
			1 => 'job_csv_test',
74
			2 => 'default',
75
			3 => 'job_csv_test2',
76
		);
77
78
		$suppliersCodes = [ 'job_csv_test', 'job_csv_test2' ];
79
80
		foreach( $suppliersCodes as $code )
81
		{
82
			$this->create( $code );
83
		}
84
85
		$object = new \Aimeos\Controller\Common\Product\Import\Csv\Processor\Supplier\Standard( $this->context, $mapping, $this->endpoint );
86
		$object->process( self::$product, $data );
87
88
		foreach( $suppliersCodes as $code )
89
		{
90
			$supplier = $this->get( $code );
91
			$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

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

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

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

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

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