Passed
Push — master ( 938ca6...9526de )
by Aimeos
03:43
created

SelectTest::testUpdateProduct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2020
6
 */
7
8
9
namespace Aimeos\Controller\Frontend\Basket\Decorator;
10
11
12
class SelectTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $object;
15
	private $context;
16
	private $testItem;
17
18
19
	protected function setUp() : void
20
	{
21
		$this->context = \TestHelperFrontend::getContext();
22
23
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
24
		$this->testItem = $manager->find( 'U:TESTP', ['attribute', 'media', 'price', 'product', 'text'] );
25
26
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
27
		$this->object = new \Aimeos\Controller\Frontend\Basket\Decorator\Select( $object, $this->context );
28
	}
29
30
31
	protected function tearDown() : void
32
	{
33
		$this->object->clear();
34
		$this->context->getSession()->set( 'aimeos', [] );
35
36
		unset( $this->object, $this->testItem, $this->context );
37
	}
38
39
40
	public function testAddDeleteProduct()
41
	{
42
		$basket = $this->object->get();
43
44
		$this->assertSame( $this->object, $this->object->addProduct( $this->testItem, 2 ) );
45
		$this->assertEquals( 1, count( $basket->getProducts() ) );
46
		$this->assertEquals( 2, $basket->getProduct( 0 )->getQuantity() );
47
		$this->assertEquals( 'U:TESTPSUB01', $basket->getProduct( 0 )->getProductCode() );
48
	}
49
50
51
	public function testAddProductNoSelection()
52
	{
53
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
54
		$item = $manager->find( 'CNC', ['attribute', 'media', 'price', 'product', 'text'] );
55
56
		$this->assertSame( $this->object, $this->object->addProduct( $item ) );
57
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
58
		$this->assertEquals( 'CNC', $this->object->get()->getProduct( 0 )->getProductCode() );
59
		$this->assertEquals( 0, count( $this->object->get()->getProduct( 0 )->getProducts() ) );
60
	}
61
62
63
	public function testAddProductVariant()
64
	{
65
		$manager = \Aimeos\MShop::create( \TestHelperFrontend::getContext(), 'attribute' );
66
67
		$search = $manager->filter();
68
		$search->setConditions( $search->compare( '==', 'attribute.code', array( 'xs', 'white' ) ) );
69
70
		$attrIds = $manager->search( $search )->keys();
71
72
		if( $attrIds->isEmpty() ) {
73
			throw new \RuntimeException( 'Attributes not found' );
74
		}
75
76
77
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
78
		$item = $manager->find( 'CNC', ['attribute', 'media', 'price', 'product', 'text'] );
79
80
		$result = $this->object->addProduct( $item, 1, $attrIds->toArray(), [], [], 'default', 'unitsupplier' );
81
82
		$this->assertSame( $this->object, $result );
83
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
84
		$this->assertEquals( 'CNC', $this->object->get()->getProduct( 0 )->getProductCode() );
85
		$this->assertEquals( 'default', $this->object->get()->getProduct( 0 )->getStockType() );
86
		$this->assertEquals( 'unitsupplier', $this->object->get()->getProduct( 0 )->getSupplierCode() );
87
	}
88
89
90
	public function testAddProductVariantIncomplete()
91
	{
92
		$id = \Aimeos\MShop::create( $this->context, 'attribute' )->find( '30', [], 'product', 'length' )->getId();
93
94
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
95
		$item = $manager->find( 'U:TEST', ['attribute', 'media', 'price', 'product', 'text'] );
96
97
		$this->assertSame( $this->object, $this->object->addProduct( $item, 1, [$id] ) );
98
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
99
		$this->assertEquals( 'U:TESTSUB02', $this->object->get()->getProduct( 0 )->getProductCode() );
100
		$this->assertEquals( 2, count( $this->object->get()->getProduct( 0 )->getAttributeItems() ) );
101
	}
102
103
104
	public function testAddProductVariantNonUnique()
105
	{
106
		$id = \Aimeos\MShop::create( $this->context, 'attribute' )->find( '30', [], 'product', 'width' )->getId();
107
108
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
109
		$item = $manager->find( 'U:TEST', ['attribute', 'media', 'price', 'product', 'text'] );
110
111
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
112
		$this->object->addProduct( $item, 1, [$id] );
113
	}
114
115
116
	public function testAddProductVariantNotRequired()
117
	{
118
		$this->context->getConfig()->set( 'controller/frontend/basket/require-variant', false );
119
		$id = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'xs', [], 'product', 'size' )->getId();
120
121
		$this->object->addProduct( $this->testItem, 1, [$id] );
122
123
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
124
		$this->assertEquals( 'U:TESTP', $this->object->get()->getProduct( 0 )->getProductCode() );
125
	}
126
127
128
	public function testAddProductEmptySelectionException()
129
	{
130
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
131
		$item = $manager->find( 'U:noSel', ['attribute', 'media', 'price', 'product', 'text'] );
132
133
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
134
		$this->object->addProduct( $item );
135
	}
136
137
138
	public function testAddProductSelectionWithPricelessItem()
139
	{
140
		$this->assertSame( $this->object, $this->object->addProduct( $this->testItem ) );
141
		$this->assertEquals( 'U:TESTPSUB01', $this->object->get()->getProduct( 0 )->getProductCode() );
142
	}
143
144
145
	public function testAddProductConfigAttribute()
146
	{
147
		$id = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'xs', [], 'product', 'size' )->getId();
148
149
		$result = $this->object->addProduct( $this->testItem, 1, [], [$id => 1] );
150
		$basket = $this->object->get();
151
152
		$this->assertSame( $this->object, $result );
153
		$this->assertEquals( 1, count( $basket->getProducts() ) );
154
		$this->assertEquals( 'U:TESTPSUB01', $basket->getProduct( 0 )->getProductCode() );
155
		$this->assertEquals( 'xs', $basket->getProduct( 0 )->getAttribute( 'size', 'config' ) );
156
	}
157
158
159
	public function testAddProductHiddenAttribute()
160
	{
161
		$result = $this->object->addProduct( $this->testItem );
162
163
		$basket = $this->object->get();
164
		$this->assertEquals( 1, count( $basket->getProducts() ) );
165
166
		$product = $basket->getProduct( 0 );
167
		$this->assertEquals( 'U:TESTPSUB01', $product->getProductCode() );
168
169
		$attributes = $product->getAttributeItems();
170
		$this->assertEquals( 1, count( $attributes ) );
171
172
		$this->assertSame( $this->object, $result );
173
		$this->assertEquals( 'hidden', $attributes->first()->getType() );
174
		$this->assertEquals( '29', $product->getAttribute( 'width', 'hidden' ) );
175
	}
176
177
178
	public function testUpdateProduct()
179
	{
180
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
181
		$item = $manager->find( 'CNC', ['attribute', 'media', 'price', 'product', 'text'] );
182
		$this->object->addProduct( $item );
183
184
		$this->assertSame( $this->object, $this->object->updateProduct( 0, 2 ) );
185
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
186
		$this->assertEquals( 2, $this->object->get()->getProduct( 0 )->getQuantity() );
187
		$this->assertEquals( 'CNC', $this->object->get()->getProduct( 0 )->getProductCode() );
188
		$this->assertEquals( '600.00', $this->object->get()->getProduct( 0 )->getPrice()->getValue() );
189
	}
190
191
192
	public function testUpdateProductSelect()
193
	{
194
		$this->object->addProduct( $this->testItem, 1 );
195
196
		$this->assertSame( $this->object, $this->object->updateProduct( 0, 2 ) );
197
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
198
		$this->assertEquals( 2, $this->object->get()->getProduct( 0 )->getQuantity() );
199
		$this->assertEquals( 'U:TESTPSUB01', $this->object->get()->getProduct( 0 )->getProductCode() );
200
		$this->assertEquals( '18.00', $this->object->get()->getProduct( 0 )->getPrice()->getValue() );
201
	}
202
}
203