SelectTest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 90
c 0
b 0
f 0
dl 0
loc 188
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddDeleteProduct() 0 8 1
A tearDown() 0 6 1
A setUp() 0 9 1
A testAddProductNoSelection() 0 9 1
A testAddProductHiddenAttribute() 0 16 1
A testUpdateProduct() 0 11 1
A testAddProductVariantIncomplete() 0 11 1
A testAddProductEmptySelectionException() 0 7 1
A testAddProductVariant() 0 23 2
A testAddProductVariantNotRequired() 0 9 1
A testUpdateProductSelect() 0 9 1
A testAddProductConfigAttribute() 0 11 1
A testAddProductVariantNonUnique() 0 9 1
A testAddProductSelectionWithPricelessItem() 0 4 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
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 = \TestHelper::context();
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->session()->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( \TestHelper::context(), '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' );
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
	}
87
88
89
	public function testAddProductVariantIncomplete()
90
	{
91
		$id = \Aimeos\MShop::create( $this->context, 'attribute' )->find( '30', [], 'product', 'length' )->getId();
92
93
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
94
		$item = $manager->find( 'U:TEST', ['attribute', 'media', 'price', 'product', 'text'] );
95
96
		$this->assertSame( $this->object, $this->object->addProduct( $item, 1, [$id] ) );
97
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
98
		$this->assertEquals( 'U:TESTSUB02', $this->object->get()->getProduct( 0 )->getProductCode() );
99
		$this->assertEquals( 2, count( $this->object->get()->getProduct( 0 )->getAttributeItems() ) );
100
	}
101
102
103
	public function testAddProductVariantNonUnique()
104
	{
105
		$id = \Aimeos\MShop::create( $this->context, 'attribute' )->find( '30', [], 'product', 'width' )->getId();
106
107
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
108
		$item = $manager->find( 'U:TEST', ['attribute', 'media', 'price', 'product', 'text'] );
109
110
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
111
		$this->object->addProduct( $item, 1, [$id] );
112
	}
113
114
115
	public function testAddProductVariantNotRequired()
116
	{
117
		$this->context->config()->set( 'controller/frontend/basket/require-variant', false );
118
		$id = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'xs', [], 'product', 'size' )->getId();
119
120
		$this->object->addProduct( $this->testItem, 1, [$id] );
121
122
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
123
		$this->assertEquals( 'U:TESTP', $this->object->get()->getProduct( 0 )->getProductCode() );
124
	}
125
126
127
	public function testAddProductEmptySelectionException()
128
	{
129
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
130
		$item = $manager->find( 'U:noSel', ['attribute', 'media', 'price', 'product', 'text'] );
131
132
		$this->expectException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
133
		$this->object->addProduct( $item );
134
	}
135
136
137
	public function testAddProductSelectionWithPricelessItem()
138
	{
139
		$this->assertSame( $this->object, $this->object->addProduct( $this->testItem ) );
140
		$this->assertEquals( 'U:TESTPSUB01', $this->object->get()->getProduct( 0 )->getProductCode() );
141
	}
142
143
144
	public function testAddProductConfigAttribute()
145
	{
146
		$id = \Aimeos\MShop::create( $this->context, 'attribute' )->find( 'xs', [], 'product', 'size' )->getId();
147
148
		$result = $this->object->addProduct( $this->testItem, 1, [], [$id => 1] );
149
		$basket = $this->object->get();
150
151
		$this->assertSame( $this->object, $result );
152
		$this->assertEquals( 1, count( $basket->getProducts() ) );
153
		$this->assertEquals( 'U:TESTPSUB01', $basket->getProduct( 0 )->getProductCode() );
154
		$this->assertEquals( 'xs', $basket->getProduct( 0 )->getAttribute( 'size', 'config' ) );
155
	}
156
157
158
	public function testAddProductHiddenAttribute()
159
	{
160
		$result = $this->object->addProduct( $this->testItem );
161
162
		$basket = $this->object->get();
163
		$this->assertEquals( 1, count( $basket->getProducts() ) );
164
165
		$product = $basket->getProduct( 0 );
166
		$this->assertEquals( 'U:TESTPSUB01', $product->getProductCode() );
167
168
		$attributes = $product->getAttributeItems();
169
		$this->assertEquals( 1, count( $attributes ) );
170
171
		$this->assertSame( $this->object, $result );
172
		$this->assertEquals( 'hidden', $attributes->first()->getType() );
173
		$this->assertEquals( '29', $product->getAttribute( 'width', 'hidden' ) );
174
	}
175
176
177
	public function testUpdateProduct()
178
	{
179
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
180
		$item = $manager->find( 'CNC', ['attribute', 'media', 'price', 'product', 'text'] );
181
		$this->object->addProduct( $item );
182
183
		$this->assertSame( $this->object, $this->object->updateProduct( 0, 2 ) );
184
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
185
		$this->assertEquals( 2, $this->object->get()->getProduct( 0 )->getQuantity() );
186
		$this->assertEquals( 'CNC', $this->object->get()->getProduct( 0 )->getProductCode() );
187
		$this->assertEquals( '600.00', $this->object->get()->getProduct( 0 )->getPrice()->getValue() );
188
	}
189
190
191
	public function testUpdateProductSelect()
192
	{
193
		$this->object->addProduct( $this->testItem, 1 );
194
195
		$this->assertSame( $this->object, $this->object->updateProduct( 0, 2 ) );
196
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
197
		$this->assertEquals( 2, $this->object->get()->getProduct( 0 )->getQuantity() );
198
		$this->assertEquals( 'U:TESTPSUB01', $this->object->get()->getProduct( 0 )->getProductCode() );
199
		$this->assertEquals( '18.00', $this->object->get()->getProduct( 0 )->getPrice()->getValue() );
200
	}
201
}
202