StandardTest::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 */
7
8
9
namespace Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Stock;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $endpoint;
16
17
18
	protected function setUp() : void
19
	{
20
		\Aimeos\MShop::cache( true );
21
22
		$this->context = \TestHelper::context();
23
		$this->endpoint = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Done( $this->context, [] );
24
	}
25
26
27
	protected function tearDown() : void
28
	{
29
		\Aimeos\MShop::cache( false );
30
	}
31
32
33
	public function testProcess()
34
	{
35
		$mapping = array(
36
			0 => 'stock.stocklevel',
37
			1 => 'stock.dateback',
38
		);
39
40
		$data = array(
41
			0 => '100',
42
			1 => '2000-01-01 00:00:00',
43
		);
44
45
		$product = $this->create( 'job_csv_test' );
46
47
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Stock\Standard( $this->context, $mapping, $this->endpoint );
48
		$object->process( $product, $data );
0 ignored issues
show
Bug introduced by
It seems like $product can also be of type Aimeos\Map; however, parameter $product of Aimeos\Controller\Jobs\C...ock\Standard::process() does only seem to accept Aimeos\MShop\Product\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

48
		$object->process( /** @scrutinizer ignore-type */ $product, $data );
Loading history...
49
50
		$items = $this->getStockItems( $product->getId() );
51
		$this->delete( $product );
52
53
54
		$this->assertEquals( 1, count( $items ) );
55
56
		foreach( $items as $item )
57
		{
58
			$this->assertEquals( 100, $item->getStockLevel() );
59
			$this->assertEquals( '2000-01-01 00:00:00', $item->getDateBack() );
60
		}
61
	}
62
63
64
	public function testProcessMultiple()
65
	{
66
		$mapping = array(
67
			0 => 'stock.type',
68
			1 => 'stock.stocklevel',
69
			2 => 'stock.type',
70
			3 => 'stock.stocklevel',
71
		);
72
73
		$data = array(
74
			0 => 'unit_type1',
75
			1 => '200',
76
			2 => 'unit_type2',
77
			3 => '200',
78
		);
79
80
		$product = $this->create( 'job_csv_test' );
81
82
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Stock\Standard( $this->context, $mapping, $this->endpoint );
83
		$object->process( $product, $data );
0 ignored issues
show
Bug introduced by
It seems like $product can also be of type Aimeos\Map; however, parameter $product of Aimeos\Controller\Jobs\C...ock\Standard::process() does only seem to accept Aimeos\MShop\Product\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

83
		$object->process( /** @scrutinizer ignore-type */ $product, $data );
Loading history...
84
85
		$items = $this->getStockItems( $product->getId() );
86
		$this->delete( $product );
87
88
89
		$this->assertEquals( 2, count( $items ) );
90
91
		foreach( $items as $item ) {
92
			$this->assertEquals( 200, $item->getStockLevel() );
93
		}
94
	}
95
96
97
	public function testProcessUpdate()
98
	{
99
		$mapping = array(
100
			0 => 'stock.stocklevel',
101
		);
102
103
		$data = array(
104
			0 => '10',
105
		);
106
107
		$dataUpdate = array(
108
			0 => '20',
109
		);
110
111
		$product = $this->create( 'job_csv_test' );
112
113
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Stock\Standard( $this->context, $mapping, $this->endpoint );
114
115
		$object->process( $product, $data );
0 ignored issues
show
Bug introduced by
It seems like $product can also be of type Aimeos\Map; however, parameter $product of Aimeos\Controller\Jobs\C...ock\Standard::process() does only seem to accept Aimeos\MShop\Product\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

115
		$object->process( /** @scrutinizer ignore-type */ $product, $data );
Loading history...
116
		$object->process( $product, $dataUpdate );
117
118
		$items = $this->getStockItems( $product->getId() );
119
		$this->delete( $product );
120
121
122
		$this->assertEquals( 1, count( $items ) );
123
		$this->assertEquals( 20, current( $items )->getStockLevel() );
124
	}
125
126
127
	public function testProcessDelete()
128
	{
129
		$mapping = array(
130
			0 => 'stock.stocklevel',
131
		);
132
133
		$data = array(
134
			0 => 50,
135
		);
136
137
		$product = $this->create( 'job_csv_test' );
138
139
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Stock\Standard( $this->context, $mapping, $this->endpoint );
140
		$object->process( $product, $data );
0 ignored issues
show
Bug introduced by
It seems like $product can also be of type Aimeos\Map; however, parameter $product of Aimeos\Controller\Jobs\C...ock\Standard::process() does only seem to accept Aimeos\MShop\Product\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

140
		$object->process( /** @scrutinizer ignore-type */ $product, $data );
Loading history...
141
142
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Stock\Standard( $this->context, [], $this->endpoint );
143
		$object->process( $product, [] );
144
145
		$items = $this->getStockItems( $product->getId() );
146
		$this->delete( $product );
147
148
149
		$this->assertEquals( 0, count( $items ) );
150
	}
151
152
153
	public function testProcessEmpty()
154
	{
155
		$mapping = array(
156
			0 => 'stock.type',
157
			1 => 'stock.stocklevel',
158
			2 => 'stock.dateback',
159
		);
160
161
		$data = array(
162
			0 => 'unit_type1',
163
			1 => '',
164
			2 => '',
165
		);
166
167
		$product = $this->create( 'job_csv_test' );
168
169
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Stock\Standard( $this->context, $mapping, $this->endpoint );
170
		$object->process( $product, $data );
0 ignored issues
show
Bug introduced by
It seems like $product can also be of type Aimeos\Map; however, parameter $product of Aimeos\Controller\Jobs\C...ock\Standard::process() does only seem to accept Aimeos\MShop\Product\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

170
		$object->process( /** @scrutinizer ignore-type */ $product, $data );
Loading history...
171
172
		$items = $this->getStockItems( $product->getId() );
173
		$this->delete( $product );
174
175
		$this->assertEquals( 1, count( $items ) );
176
177
		foreach( $items as $item )
178
		{
179
			$this->assertEquals( null, $item->getStockLevel() );
180
			$this->assertEquals( null, $item->getDateBack() );
181
		}
182
	}
183
184
185
	/**
186
	 * @param string $code
187
	 */
188
	protected function create( $code )
189
	{
190
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
191
		return $manager->save( $manager->create()->setCode( $code ) );
192
	}
193
194
195
	protected function delete( \Aimeos\MShop\Product\Item\Iface $product )
196
	{
197
		$manager = \Aimeos\MShop::create( $this->context, 'stock' );
198
		$filter = $manager->filter()->add( ['stock.productid' => $product->getId()] );
199
		$manager->delete( $manager->search( $filter )->toArray() );
200
201
		\Aimeos\MShop::create( $this->context, 'product' )->delete( $product->getId() );
202
	}
203
204
205
	protected function getStockItems( $prodid ) : array
206
	{
207
		$manager = \Aimeos\MShop::create( $this->context, 'stock' );
208
		return $manager->search( $manager->filter()->add( ['stock.productid' => $prodid] ) )->all();
209
	}
210
}
211