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

PercentRebateTest::testCheckConfigBE()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 9.6666
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 PercentRebateTest 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( 'percentrebate.productcode' => 'U:MD', 'percentrebate.rebate' => '10' ) );
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\PercentRebate( $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
		$orderProducts = $this->getOrderProducts();
43
44
		$this->orderBase->addProduct( $orderProducts['CNE'] );
45
		$this->orderBase->addProduct( $orderProducts['CNC'] );
46
47
		$this->object->addCoupon( $this->orderBase );
48
49
		$coupons = $this->orderBase->getCoupons();
50
		$products = $this->orderBase->getProducts();
51
52
		if( ( $product = reset( $coupons['zyxw'] ) ) === false ) {
53
			throw new \RuntimeException( 'No coupon available' );
54
		}
55
56
		$this->assertEquals( 3, count( $products ) );
57
		$this->assertEquals( 1, count( $coupons['zyxw'] ) );
58
		$this->assertEquals( '-70.40', $product->getPrice()->getValue() );
59
		$this->assertEquals( '70.40', $product->getPrice()->getRebate() );
60
		$this->assertEquals( 'U:MD', $product->getProductCode() );
61
		$this->assertNotEquals( '', $product->getProductId() );
62
		$this->assertEquals( '', $product->getSupplierCode() );
63
		$this->assertEquals( '', $product->getMediaUrl() );
64
		$this->assertEquals( 'Geldwerter Nachlass', $product->getName() );
65
	}
66
67
68
	public function testAddCouponMultipleTaxRates()
69
	{
70
		$products = $this->getOrderProducts();
71
72
		$products['CNC']->getPrice()->setTaxRate( '10.00' );
73
		$products['CNE']->getPrice()->setTaxRate( '20.00' );
74
75
		$products['CNC']->setQuantity( 1 );
76
		$products['CNE']->setQuantity( 1 );
77
78
		$this->orderBase->addProduct( $products['CNE'] );
79
		$this->orderBase->addProduct( $products['CNC'] );
80
81
		$this->object->addCoupon( $this->orderBase );
82
83
		$coupons = $this->orderBase->getCoupons();
84
		$products = $this->orderBase->getProducts();
85
86
		if( ( $couponProduct20 = reset( $coupons['zyxw'] ) ) === false ) {
87
			throw new \RuntimeException( 'No coupon available' );
88
		}
89
90
		if( ( $couponProduct10 = end( $coupons['zyxw'] ) ) === false ) {
91
			throw new \RuntimeException( 'No coupon available' );
92
		}
93
94
		$this->assertEquals( 4, count( $products ) );
95
		$this->assertEquals( '-37.00', $couponProduct20->getPrice()->getValue() );
96
		$this->assertEquals( '37.00', $couponProduct20->getPrice()->getRebate() );
97
		$this->assertEquals( '-29.70', $couponProduct10->getPrice()->getValue() );
98
		$this->assertEquals( '29.70', $couponProduct10->getPrice()->getRebate() );
99
	}
100
101
102
	public function testDeleteCoupon()
103
	{
104
		$orderProducts = $this->getOrderProducts();
105
		$this->orderBase->addProduct( $orderProducts['CNE'] );
106
107
		$this->object->addCoupon( $this->orderBase );
108
		$this->object->deleteCoupon( $this->orderBase );
109
110
		$products = $this->orderBase->getProducts();
111
		$coupons = $this->orderBase->getCoupons();
112
113
		$this->assertEquals( 1, count( $products ) );
114
		$this->assertArrayNotHasKey( 'zyxw', $coupons );
115
	}
116
117
118
	public function testAddCouponInvalidConfig()
119
	{
120
		$context = \TestHelperMShop::getContext();
121
		$couponItem = \Aimeos\MShop\Coupon\Manager\Factory::createManager( \TestHelperMShop::getContext() )->createItem();
122
123
		$object = new \Aimeos\MShop\Coupon\Provider\PercentRebate( $context, $couponItem, 'zyxw' );
124
125
		$this->setExpectedException( '\\Aimeos\\MShop\\Coupon\\Exception' );
126
		$object->addCoupon( $this->orderBase );
127
	}
128
129
130
	public function testGetConfigBE()
131
	{
132
		$result = $this->object->getConfigBE();
133
134
		$this->assertArrayHasKey( 'percentrebate.productcode', $result );
135
		$this->assertArrayHasKey( 'percentrebate.rebate', $result );
136
	}
137
138
139
	public function testCheckConfigBE()
140
	{
141
		$attributes = ['percentrebate.productcode' => 'test', 'percentrebate.rebate' => 5];
142
		$result = $this->object->checkConfigBE( $attributes );
143
144
		$this->assertEquals( 2, count( $result ) );
145
		$this->assertInternalType( 'null', $result['percentrebate.productcode'] );
146
		$this->assertInternalType( 'null', $result['percentrebate.rebate'] );
147
	}
148
149
150
	public function testCheckConfigBEFailure()
151
	{
152
		$result = $this->object->checkConfigBE( [] );
153
154
		$this->assertEquals( 2, count( $result ) );
155
		$this->assertInternalType( 'string', $result['percentrebate.productcode'] );
156
		$this->assertInternalType( 'string', $result['percentrebate.rebate'] );
157
	}
158
159
160
	public function testIsAvailable()
161
	{
162
		$this->assertTrue( $this->object->isAvailable( $this->orderBase ) );
163
	}
164
165
166
	/**
167
	 * Return the order products.
168
	 *
169
	 * @return \Aimeos\MShop\Order\Item\Base\Product\Iface[]
170
	 * @throws \Exception
171
	 */
172
	protected function getOrderProducts()
173
	{
174
		$products = [];
175
		$manager = \Aimeos\MShop\Factory::createManager( \TestHelperMShop::getContext(), 'order/base/product' );
176
177
		$search = $manager->createSearch();
178
		$search->setConditions( $search->combine( '&&', array(
179
			$search->compare( '==', 'order.base.product.prodcode', array( 'CNE', 'CNC' ) ),
180
			$search->compare( '==', 'order.base.product.price', array( '600.00', '36.00' ) )
181
		) ) );
182
		$items = $manager->searchItems( $search );
183
184
		if( count( $items ) < 2 ) {
185
			throw new \RuntimeException( 'Please fix the test data in your database.' );
186
		}
187
188
		foreach( $items as $item ) {
189
			$products[$item->getProductCode()] = $item;
190
		}
191
192
		return $products;
193
	}
194
}
195