Completed
Push — master ( 4ffab1...137f58 )
by Aimeos
07:59
created

StockTest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 1
cbo 8
dl 0
loc 198
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A tearDown() 0 7 1
B testAddProductNotEnoughStockException() 0 24 3
A testAddProductNoStockException() 0 23 3
A testAddProductNoStockRequired() 0 15 2
A testAddProductNoStockItem() 0 16 2
B testEditProductNotEnoughStock() 0 28 3
B testEditProductNoStock() 0 34 3
A testEditProductStockNotChecked() 0 22 2
1
<?php
2
3
namespace Aimeos\Controller\Frontend\Basket\Decorator;
4
5
6
class StockTest extends \PHPUnit_Framework_TestCase
7
{
8
	private $object;
9
	private $context;
10
11
12
	protected function setUp()
13
	{
14
		$this->context = \TestHelperFrontend::getContext();
15
		$object = new \Aimeos\Controller\Frontend\Basket\Standard( $this->context );
16
		$this->object = new \Aimeos\Controller\Frontend\Basket\Decorator\Stock( $object, $this->context );
17
	}
18
19
20
	protected function tearDown()
21
	{
22
		$this->object->clear();
23
		$this->context->getSession()->set( 'aimeos', array() );
24
25
		unset( $this->object );
26
	}
27
28
29
	public function testAddProductNotEnoughStockException()
30
	{
31
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
32
33
		$search = $productManager->createSearch();
34
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
35
36
		$items = $productManager->searchItems( $search );
37
38
		if( ( $item = reset( $items ) ) === false ) {
39
			throw new \Exception( 'Product not found' );
40
		}
41
42
		try
43
		{
44
			$this->object->addProduct( $item->getId(), 5, array(), array(), array(), array(), array(), 'unit_warehouse3' );
45
			throw new \Exception( 'Expected exception not thrown' );
46
		}
47
		catch( \Aimeos\Controller\Frontend\Basket\Exception $e )
48
		{
49
			$item = $this->object->get()->getProduct( 0 );
50
			$this->assertEquals( 3, $item->getQuantity() );
51
		}
52
	}
53
54
55
	public function testAddProductNoStockException()
56
	{
57
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
58
59
		$search = $productManager->createSearch();
60
		$search->setConditions( $search->compare( '==', 'product.code', 'EFGH' ) );
61
62
		$items = $productManager->searchItems( $search );
63
64
		if( ( $item = reset( $items ) ) === false ) {
65
			throw new \Exception( 'Product not found' );
66
		}
67
68
		try
69
		{
70
			$this->object->addProduct( $item->getId(), 5, array(), array(), array(), array(), array(), 'unit_warehouse2' );
71
			throw new \Exception( 'Expected exception not thrown' );
72
		}
73
		catch( \Aimeos\Controller\Frontend\Basket\Exception $e )
74
		{
75
			$this->assertEquals( array(), $this->object->get()->getProducts() );
76
		}
77
	}
78
79
80
	public function testAddProductNoStockRequired()
81
	{
82
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
83
84
		$search = $productManager->createSearch();
85
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
86
87
		$items = $productManager->searchItems( $search );
88
89
		if( ( $item = reset( $items ) ) === false ) {
90
			throw new \Exception( 'Product not found' );
91
		}
92
93
		$this->object->addProduct( $item->getId(), 5, array( 'stock' => false ) );
94
	}
95
96
97
	public function testAddProductNoStockItem()
98
	{
99
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
100
101
		$search = $productManager->createSearch();
102
		$search->setConditions( $search->compare( '==', 'product.code', 'QRST' ) );
103
104
		$items = $productManager->searchItems( $search );
105
106
		if( ( $item = reset( $items ) ) === false ) {
107
			throw new \Exception( 'Product not found' );
108
		}
109
110
		$this->setExpectedException( '\\Aimeos\\Controller\\Frontend\\Basket\\Exception' );
111
		$this->object->addProduct( $item->getId(), 1 );
112
	}
113
114
115
	public function testEditProductNotEnoughStock()
116
	{
117
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
118
		$search = $productManager->createSearch();
119
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
120
		$items = $productManager->searchItems( $search );
121
122
		if( ( $item = reset( $items ) ) === false ) {
123
			throw new \Exception( 'Product not found' );
124
		}
125
126
		$this->object->addProduct( $item->getId(), 2, array(), array(), array(), array(), array(), 'unit_warehouse3' );
127
128
		$item = $this->object->get()->getProduct( 0 );
129
		$this->assertEquals( 2, $item->getQuantity() );
130
131
		try
132
		{
133
			$this->object->editProduct( 0, 5 );
134
			throw new \Exception( 'Expected exception not thrown' );
135
		}
136
		catch( \Aimeos\Controller\Frontend\Basket\Exception $e )
137
		{
138
			$item = $this->object->get()->getProduct( 0 );
139
			$this->assertEquals( 3, $item->getQuantity() );
140
			$this->assertEquals( 'IJKL', $item->getProductCode() );
141
		}
142
	}
143
144
145
	public function testEditProductNoStock()
146
	{
147
		$context = \TestHelperFrontend::getContext();
148
149
		$productManager = \Aimeos\MShop\Factory::createManager( $context, 'product' );
150
		$search = $productManager->createSearch();
151
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
152
		$items = $productManager->searchItems( $search );
153
154
		if( ( $item = reset( $items ) ) === false ) {
155
			throw new \Exception( 'Product not found' );
156
		}
157
158
		$orderProductManager = \Aimeos\MShop\Factory::createManager( $context, 'order/base/product' );
159
		$orderProductItem = $orderProductManager->createItem();
160
		$orderProductItem->copyFrom( $item );
161
		$orderProductItem->setQuantity( 2 );
162
		$orderProductItem->setWarehouseCode( 'unit_warehouse3' );
163
164
		$pos = $this->object->get()->addProduct( $orderProductItem, 1 );
1 ignored issue
show
Compatibility introduced by
$orderProductItem of type object<Aimeos\MShop\Common\Item\Iface> is not a sub-type of object<Aimeos\MShop\Orde...tem\Base\Product\Iface>. It seems like you assume a child interface of the interface Aimeos\MShop\Common\Item\Iface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
165
166
		$item = $this->object->get()->getProduct( $pos );
167
		$this->assertEquals( 2, $item->getQuantity() );
168
169
		try
170
		{
171
			$this->object->editProduct( $pos, 5 );
172
			throw new \Exception( 'Expected exception not thrown' );
173
		}
174
		catch( \Aimeos\Controller\Frontend\Basket\Exception $e )
175
		{
176
			$this->assertEquals( 3, $this->object->get()->getProduct( $pos )->getQuantity() );
177
		}
178
	}
179
180
181
	public function testEditProductStockNotChecked()
182
	{
183
		$productManager = \Aimeos\MShop\Product\Manager\Factory::createManager( \TestHelperFrontend::getContext() );
184
		$search = $productManager->createSearch();
185
		$search->setConditions( $search->compare( '==', 'product.code', 'IJKL' ) );
186
		$items = $productManager->searchItems( $search );
187
188
		if( ( $item = reset( $items ) ) === false ) {
189
			throw new \Exception( 'Product not found' );
190
		}
191
192
		$this->object->addProduct( $item->getId(), 2, array(), array(), array(), array(), array(), 'unit_warehouse3' );
193
194
		$item = $this->object->get()->getProduct( 0 );
195
		$this->assertEquals( 2, $item->getQuantity() );
196
197
		$this->object->editProduct( 0, 5, array( 'stock' => false ) );
198
199
		$item = $this->object->get()->getProduct( 0 );
200
		$this->assertEquals( 5, $item->getQuantity() );
201
		$this->assertEquals( 'IJKL', $item->getProductCode() );
202
	}
203
}
204