Completed
Push — master ( 937f3b...5e44e2 )
by Aimeos
16:51 queued 07:27
created

SelectTest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 8
dl 0
loc 224
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A tearDown() 0 7 1
A testAddDeleteProduct() 0 9 1
A testAddProductNoSelection() 0 10 1
A testAddProductVariant() 0 21 2
B testAddProductVariantIncomplete() 0 27 2
B testAddProductVariantNonUnique() 0 24 2
A testAddProductVariantNotRequired() 0 20 2
A testAddProductEmptySelectionException() 0 7 1
A testAddProductSelectionWithPricelessItem() 0 6 1
A testAddProductConfigAttribute() 0 20 2
B testAddProductHiddenAttribute() 0 35 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
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()
20
	{
21
		$this->context = \TestHelperFrontend::getContext();
22
		$this->testItem = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'U:TESTP' );
23
24
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
25
		$this->object = new \Aimeos\Controller\Frontend\Basket\Decorator\Select( $object, $this->context );
26
	}
27
28
29
	protected function tearDown()
30
	{
31
		$this->object->clear();
32
		$this->context->getSession()->set( 'aimeos', array() );
33
34
		unset( $this->object );
35
	}
36
37
38
	public function testAddDeleteProduct()
39
	{
40
		$basket = $this->object->get();
41
		$this->object->addProduct( $this->testItem->getId(), 2 );
42
43
		$this->assertEquals( 1, count( $basket->getProducts() ) );
44
		$this->assertEquals( 2, $basket->getProduct( 0 )->getQuantity() );
45
		$this->assertEquals( 'U:TESTPSUB01', $basket->getProduct( 0 )->getProductCode() );
46
	}
47
48
49
	public function testAddProductNoSelection()
50
	{
51
		$item = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' );
52
53
		$this->object->addProduct( $item->getId(), 1 );
54
55
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
56
		$this->assertEquals( 'CNC', $this->object->get()->getProduct( 0 )->getProductCode() );
57
		$this->assertEquals( 0, count( $this->object->get()->getProduct( 0 )->getProducts() ) );
58
	}
59
60
61
	public function testAddProductVariant()
62
	{
63
		$attributeManager = \Aimeos\MShop\Attribute\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
64
65
		$search = $attributeManager->createSearch();
66
		$search->setConditions( $search->compare( '==', 'attribute.code', array( 'xs', 'white' ) ) );
67
68
		$attributes = $attributeManager->searchItems( $search );
69
70
		if( count( $attributes ) === 0 ) {
71
			throw new \RuntimeException( 'Attributes not found' );
72
		}
73
74
75
		$item = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' );
76
77
		$this->object->addProduct( $item->getId(), 1, array(), array_keys( $attributes ), array(), array(), array(), 'default' );
78
79
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
80
		$this->assertEquals( 'CNC', $this->object->get()->getProduct( 0 )->getProductCode() );
81
	}
82
83
84
	public function testAddProductVariantIncomplete()
85
	{
86
		$attributeManager = \Aimeos\MShop\Factory::createManager( $this->context, 'attribute' );
87
88
		$search = $attributeManager->createSearch();
89
		$expr = array(
90
			$search->compare( '==', 'attribute.domain', 'product' ),
91
			$search->compare( '==', 'attribute.code', '30' ),
92
			$search->compare( '==', 'attribute.type.code', 'length' ),
93
		);
94
		$search->setConditions( $search->combine( '&&', $expr ) );
95
96
		$attributes = $attributeManager->searchItems( $search );
97
98
		if( count( $attributes ) === 0 ) {
99
			throw new \RuntimeException( 'Attributes not found' );
100
		}
101
102
103
		$item = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'U:TEST' );
104
105
		$this->object->addProduct( $item->getId(), 1, array(), array_keys( $attributes ) );
106
107
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
108
		$this->assertEquals( 'U:TESTSUB02', $this->object->get()->getProduct( 0 )->getProductCode() );
109
		$this->assertEquals( 2, count( $this->object->get()->getProduct( 0 )->getAttributes() ) );
110
	}
111
112
113
	public function testAddProductVariantNonUnique()
114
	{
115
		$attributeManager = \Aimeos\MShop\Factory::createManager( $this->context, 'attribute' );
116
117
		$search = $attributeManager->createSearch();
118
		$expr = array(
119
			$search->compare( '==', 'attribute.domain', 'product' ),
120
			$search->compare( '==', 'attribute.code', '30' ),
121
			$search->compare( '==', 'attribute.type.code', 'width' ),
122
		);
123
		$search->setConditions( $search->combine( '&&', $expr ) );
124
125
		$attributes = $attributeManager->searchItems( $search );
126
127
		if( count( $attributes ) === 0 ) {
128
			throw new \RuntimeException( 'Attributes not found' );
129
		}
130
131
132
		$item = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'U:TEST' );
133
134
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
135
		$this->object->addProduct( $item->getId(), 1, array(), array_keys( $attributes ) );
136
	}
137
138
139
	public function testAddProductVariantNotRequired()
140
	{
141
		$attributeManager = \Aimeos\MShop\Factory::createManager( $this->context, 'attribute' );
142
143
		$search = $attributeManager->createSearch();
144
		$search->setConditions( $search->compare( '==', 'attribute.code', 'xs' ) );
145
146
		$attributes = $attributeManager->searchItems( $search );
147
148
		if( count( $attributes ) === 0 ) {
149
			throw new \RuntimeException( 'Attribute not found' );
150
		}
151
152
		$options = array( 'variant' => false );
153
154
		$this->object->addProduct( $this->testItem->getId(), 1, $options, array_keys( $attributes ) );
155
156
		$this->assertEquals( 1, count( $this->object->get()->getProducts() ) );
157
		$this->assertEquals( 'U:TESTP', $this->object->get()->getProduct( 0 )->getProductCode() );
158
	}
159
160
161
	public function testAddProductEmptySelectionException()
162
	{
163
		$item = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'U:noSel' );
164
165
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
166
		$this->object->addProduct( $item->getId(), 1 );
167
	}
168
169
170
	public function testAddProductSelectionWithPricelessItem()
171
	{
172
		$this->object->addProduct( $this->testItem->getId(), 1 );
173
174
		$this->assertEquals( 'U:TESTPSUB01', $this->object->get()->getProduct( 0 )->getProductCode() );
175
	}
176
177
178
	public function testAddProductConfigAttribute()
179
	{
180
		$attributeManager = \Aimeos\MShop\Factory::createManager( $this->context, 'attribute' );
181
182
		$search = $attributeManager->createSearch();
183
		$search->setConditions( $search->compare( '==', 'attribute.code', 'xs' ) );
184
185
		$attributes = $attributeManager->searchItems( $search );
186
187
		if( empty( $attributes ) ) {
188
			throw new \RuntimeException( 'Attribute not found' );
189
		}
190
191
		$this->object->addProduct( $this->testItem->getId(), 1, array(), array(), array_keys( $attributes ) );
192
		$basket = $this->object->get();
193
194
		$this->assertEquals( 1, count( $basket->getProducts() ) );
195
		$this->assertEquals( 'U:TESTPSUB01', $basket->getProduct( 0 )->getProductCode() );
196
		$this->assertEquals( 'xs', $basket->getProduct( 0 )->getAttribute( 'size', 'config' ) );
197
	}
198
199
200
	public function testAddProductHiddenAttribute()
201
	{
202
		$attributeManager = \Aimeos\MShop\Factory::createManager( $this->context, 'attribute' );
203
204
		$search = $attributeManager->createSearch();
205
		$expr = array(
206
			$search->compare( '==', 'attribute.code', '29' ),
207
			$search->compare( '==', 'attribute.type.code', 'width' ),
208
		);
209
		$search->setConditions( $search->combine( '&&', $expr ) );
210
211
		$attributes = $attributeManager->searchItems( $search );
212
213
		if( empty( $attributes ) ) {
214
			throw new \RuntimeException( 'Attribute not found' );
215
		}
216
217
		$this->object->addProduct( $this->testItem->getId(), 1, array(), array(), array(), array_keys( $attributes ) );
218
219
		$basket = $this->object->get();
220
		$this->assertEquals( 1, count( $basket->getProducts() ) );
221
222
		$product = $basket->getProduct( 0 );
223
		$this->assertEquals( 'U:TESTPSUB01', $product->getProductCode() );
224
225
		$attributes = $product->getAttributes();
226
		$this->assertEquals( 1, count( $attributes ) );
227
228
		if( ( $attribute = reset( $attributes ) ) === false ) {
229
			throw new \RuntimeException( 'No attribute' );
230
		}
231
232
		$this->assertEquals( 'hidden', $attribute->getType() );
233
		$this->assertEquals( '29', $product->getAttribute( 'width', 'hidden' ) );
234
	}
235
}
236