Completed
Push — master ( b46c0d...fe698d )
by Aimeos
06:27
created

SelectTest::testAddProductHiddenAttribute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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