StandardTest::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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\Property;
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 => 'product.property.type',
37
			1 => 'product.property.value',
38
			2 => 'product.property.languageid',
39
			3 => 'product.property.type',
40
			4 => 'product.property.value',
41
		);
42
43
		$data = array(
44
			0 => 'package-weight',
45
			1 => '3.00',
46
			2 => 'de',
47
			3 => 'package-width',
48
			4 => '50',
49
		);
50
51
		$product = $this->create( 'job_csv_test' );
52
53
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Property\Standard( $this->context, $mapping, $this->endpoint );
54
		$object->process( $product, $data );
55
56
57
		$pos = 0;
58
		$expected = array(
59
			array( 'package-weight', '3.00', 'de' ),
60
			array( 'package-width', '50', null ),
61
		);
62
63
		$items = $product->getPropertyItems();
64
		$this->assertEquals( 2, count( $items ) );
65
66
		foreach( $items as $item )
67
		{
68
			$this->assertEquals( $expected[$pos][0], $item->getType() );
69
			$this->assertEquals( $expected[$pos][1], $item->getValue() );
70
			$this->assertEquals( $expected[$pos][2], $item->getLanguageId() );
71
			$pos++;
72
		}
73
	}
74
75
76
	public function testProcessUpdate()
77
	{
78
		$mapping = array(
79
			0 => 'product.property.type',
80
			1 => 'product.property.value',
81
		);
82
83
		$data = array(
84
			0 => 'package-weight',
85
			1 => '3.00',
86
		);
87
88
		$dataUpdate = array(
89
			0 => 'package-size',
90
			1 => 'S',
91
		);
92
93
		$product = $this->create( 'job_csv_test' );
94
95
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Property\Standard( $this->context, $mapping, $this->endpoint );
96
		$object->process( $product, $data );
97
		$object->process( $product, $dataUpdate );
98
99
100
		$object->finish(); // test if new type is created
101
		$manager = \Aimeos\MShop::create( $this->context, 'product/property/type' );
102
		$manager->delete( $manager->find( 'package-size' )->getId() );
103
104
		$items = $product->getPropertyItems();
105
		$item = $items->first();
106
107
		$this->assertEquals( 1, count( $items ) );
108
		$this->assertInstanceOf( '\\Aimeos\\MShop\\Common\\Item\\Property\\Iface', $item );
109
110
		$this->assertEquals( 'package-size', $item->getType() );
111
		$this->assertEquals( 'S', $item->getValue() );
112
	}
113
114
115
	public function testProcessDelete()
116
	{
117
		$mapping = array(
118
			0 => 'product.property.type',
119
			1 => 'product.property.value',
120
		);
121
122
		$data = array(
123
			0 => 'package-weight',
124
			1 => '3.00',
125
		);
126
127
		$product = $this->create( 'job_csv_test' );
128
129
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Property\Standard( $this->context, $mapping, $this->endpoint );
130
		$object->process( $product, $data );
131
132
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Property\Standard( $this->context, [], $this->endpoint );
133
		$object->process( $product, [] );
134
135
136
		$items = $product->getPropertyItems();
137
138
		$this->assertEquals( 0, count( $items ) );
139
	}
140
141
142
	public function testProcessEmpty()
143
	{
144
		$mapping = array(
145
			0 => 'product.property.type',
146
			1 => 'product.property.value',
147
			2 => 'product.property.type',
148
			3 => 'product.property.value',
149
		);
150
151
		$data = array(
152
			0 => '',
153
			1 => '',
154
			2 => 'package-weight',
155
			3 => '3.00',
156
		);
157
158
		$product = $this->create( 'job_csv_test' );
159
160
		$object = new \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Property\Standard( $this->context, $mapping, $this->endpoint );
161
		$object->process( $product, $data );
162
163
		$items = $product->getPropertyItems();
164
165
		$this->assertEquals( 1, count( $items ) );
166
	}
167
168
169
	/**
170
	 * @param string $code
171
	 */
172
	protected function create( $code )
173
	{
174
		return \Aimeos\MShop::create( $this->context, 'product' )->create()->setCode( $code );
175
	}
176
}
177