Completed
Push — master ( 207311...06e599 )
by Aimeos
09:29
created

FixedRebateTest::testCheckConfigBEFailure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2017
7
 */
8
9
10
namespace Aimeos\MShop\Coupon\Provider;
11
12
13
class FixedRebateTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $orderBase;
17
18
19
	protected function setUp()
20
	{
21
		$context = \TestHelperMShop::getContext();
22
23
		$priceManager = \Aimeos\MShop\Price\Manager\Factory::createManager( $context );
24
		$couponItem = \Aimeos\MShop\Coupon\Manager\Factory::createManager( $context )->createItem();
25
		$couponItem->setConfig( array( 'fixedrebate.productcode' => 'U:MD', 'fixedrebate.rebate' => '2.50' ) );
26
27
		// Don't create order base item by createItem() as this would already register the plugins
28
		$this->orderBase = new \Aimeos\MShop\Order\Item\Base\Standard( $priceManager->createItem(), $context->getLocale() );
29
		$this->object = new \Aimeos\MShop\Coupon\Provider\FixedRebate( $context, $couponItem, 'zyxw' );
30
	}
31
32
33
	protected function tearDown()
34
	{
35
		unset( $this->object );
36
		unset( $this->orderBase );
37
	}
38
39
40
	public function testAddCoupon()
41
	{
42
		$this->object->addCoupon( $this->orderBase );
43
44
		$coupons = $this->orderBase->getCoupons();
45
		$products = $this->orderBase->getProducts();
46
47
		if( ( $product = reset( $coupons['zyxw'] ) ) === false ) {
48
			throw new \RuntimeException( 'No coupon available' );
49
		}
50
51
		$this->assertEquals( 1, count( $products ) );
52
		$this->assertEquals( '-2.50', $product->getPrice()->getValue() );
53
		$this->assertEquals( '2.50', $product->getPrice()->getRebate() );
54
		$this->assertEquals( 'U:MD', $product->getProductCode() );
55
		$this->assertNotEquals( '', $product->getProductId() );
56
		$this->assertEquals( '', $product->getSupplierCode() );
57
		$this->assertEquals( '', $product->getMediaUrl() );
58
		$this->assertEquals( 'Geldwerter Nachlass', $product->getName() );
59
	}
60
61
62
	public function testAddCouponMultipleCurrencies()
63
	{
64
		$context = \TestHelperMShop::getContext();
65
		$config = array(
66
			'fixedrebate.productcode' => 'U:MD',
67
			'fixedrebate.rebate' => array(
68
				'EUR' => '1.25',
69
				'USD' => '1.50',
70
			),
71
		);
72
73
		$couponItem = \Aimeos\MShop\Coupon\Manager\Factory::createManager( $context )->createItem();
74
		$couponItem->setConfig( $config );
75
76
		$object = new \Aimeos\MShop\Coupon\Provider\FixedRebate( $context, $couponItem, 'zyxw' );
77
78
		$object->addCoupon( $this->orderBase );
79
80
		$coupons = $this->orderBase->getCoupons();
81
		$products = $this->orderBase->getProducts();
82
83
		if( ( $product = reset( $coupons['zyxw'] ) ) === false ) {
84
			throw new \RuntimeException( 'No coupon available' );
85
		}
86
87
		$this->assertEquals( 1, count( $products ) );
88
		$this->assertEquals( '-1.25', $product->getPrice()->getValue() );
89
		$this->assertEquals( '1.25', $product->getPrice()->getRebate() );
90
		$this->assertEquals( 'U:MD', $product->getProductCode() );
91
	}
92
93
94
	public function testAddCouponMultipleTaxRates()
95
	{
96
		$products = $this->getOrderProducts();
97
98
		$products['CNC']->getPrice()->setTaxRate( '10.00' );
99
		$products['CNE']->getPrice()->setTaxRate( '20.00' );
100
101
		$products['CNC']->setQuantity( 1 );
102
		$products['CNE']->setQuantity( 1 );
103
104
		$this->orderBase->addProduct( $products['CNE'] );
105
		$this->orderBase->addProduct( $products['CNC'] );
106
107
		$context = \TestHelperMShop::getContext();
108
		$config = array(
109
			'fixedrebate.productcode' => 'U:MD',
110
			'fixedrebate.rebate' => array(
111
				'EUR' => '50.00',
112
			),
113
		);
114
115
		$couponItem = \Aimeos\MShop\Coupon\Manager\Factory::createManager( $context )->createItem();
116
		$couponItem->setConfig( $config );
117
118
		$object = new \Aimeos\MShop\Coupon\Provider\FixedRebate( $context, $couponItem, 'zyxw' );
119
120
		$object->addCoupon( $this->orderBase );
121
122
		$coupons = $this->orderBase->getCoupons();
123
		$products = $this->orderBase->getProducts();
124
125
		if( ( $couponProduct20 = reset( $coupons['zyxw'] ) ) === false ) {
126
			throw new \RuntimeException( 'No coupon available' );
127
		}
128
129
		if( ( $couponProduct10 = end( $coupons['zyxw'] ) ) === false ) {
130
			throw new \RuntimeException( 'No coupon available' );
131
		}
132
133
		$this->assertEquals( 4, count( $products ) );
134
		$this->assertEquals( '-37.00', $couponProduct20->getPrice()->getValue() );
135
		$this->assertEquals( '37.00', $couponProduct20->getPrice()->getRebate() );
136
		$this->assertEquals( '-13.00', $couponProduct10->getPrice()->getValue() );
137
		$this->assertEquals( '13.00', $couponProduct10->getPrice()->getRebate() );
138
	}
139
140
141
	public function testDeleteCoupon()
142
	{
143
		$this->object->addCoupon( $this->orderBase );
144
		$this->object->deleteCoupon( $this->orderBase );
145
146
		$products = $this->orderBase->getProducts();
147
		$coupons = $this->orderBase->getCoupons();
148
149
		$this->assertEquals( 0, count( $products ) );
150
		$this->assertArrayNotHasKey( 'zyxw', $coupons );
151
	}
152
153
154
	public function testAddCouponInvalidConfig()
155
	{
156
		$context = \TestHelperMShop::getContext();
157
		$couponItem = \Aimeos\MShop\Coupon\Manager\Factory::createManager( \TestHelperMShop::getContext() )->createItem();
158
		$couponItem->setConfig( array( 'fixedrebate.rebate' => '2.50' ) );
159
160
		$object = new \Aimeos\MShop\Coupon\Provider\FixedRebate( $context, $couponItem, 'zyxw' );
161
162
		$this->setExpectedException( '\\Aimeos\\MShop\\Coupon\\Exception' );
163
		$object->addCoupon( $this->orderBase );
164
	}
165
166
167
	public function testGetConfigBE()
168
	{
169
		$result = $this->object->getConfigBE();
170
171
		$this->assertArrayHasKey( 'fixedrebate.productcode', $result );
172
		$this->assertArrayHasKey( 'fixedrebate.rebate', $result );
173
	}
174
175
176
	public function testCheckConfigBE()
177
	{
178
		$attributes = ['fixedrebate.productcode' => 'test', 'fixedrebate.rebate' => ['EUR' => '10.00']];
179
		$result = $this->object->checkConfigBE( $attributes );
180
181
		$this->assertEquals( 2, count( $result ) );
182
		$this->assertInternalType( 'null', $result['fixedrebate.productcode'] );
183
		$this->assertInternalType( 'null', $result['fixedrebate.rebate'] );
184
	}
185
186
187
	public function testCheckConfigBEFailure()
188
	{
189
		$result = $this->object->checkConfigBE( [] );
190
191
		$this->assertEquals( 2, count( $result ) );
192
		$this->assertInternalType( 'string', $result['fixedrebate.productcode'] );
193
		$this->assertInternalType( 'string', $result['fixedrebate.rebate'] );
194
	}
195
196
197
	public function testIsAvailable()
198
	{
199
		$this->assertTrue( $this->object->isAvailable( $this->orderBase ) );
200
	}
201
202
203
	protected function getOrderProducts()
204
	{
205
		$products = [];
206
		$manager = \Aimeos\MShop\Factory::createManager( \TestHelperMShop::getContext(), 'order/base/product' );
207
208
		$search = $manager->createSearch();
209
		$search->setConditions( $search->combine( '&&', array(
210
			$search->compare( '==', 'order.base.product.prodcode', array( 'CNE', 'CNC' ) ),
211
			$search->compare( '==', 'order.base.product.price', array( '600.00', '36.00' ) )
212
		) ) );
213
		$items = $manager->searchItems( $search );
214
215
		if( count( $items ) < 2 ) {
216
			throw new \RuntimeException( 'Please fix the test data in your database.' );
217
		}
218
219
		foreach( $items as $item ) {
220
			$products[$item->getProductCode()] = $item;
221
		}
222
223
		return $products;
224
	}
225
}
226