Passed
Push — master ( c858ef...566d79 )
by Aimeos
05:46
created

RequiredTest::testCalcPriceOnly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2017-2020
7
 */
8
9
10
namespace Aimeos\MShop\Coupon\Provider\Decorator;
11
12
13
class RequiredTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $object;
16
	private $orderBase;
17
	private $couponItem;
18
19
20
	protected function setUp() : void
21
	{
22
		$orderProducts = [];
23
		$context = \TestHelperMShop::getContext();
24
		$this->couponItem = \Aimeos\MShop\Coupon\Manager\Factory::create( $context )->createItem();
25
26
		$provider = new \Aimeos\MShop\Coupon\Provider\Example( $context, $this->couponItem, 'abcd' );
27
		$this->object = new \Aimeos\MShop\Coupon\Provider\Decorator\Required( $provider, $context, $this->couponItem, 'abcd' );
28
		$this->object->setObject( $this->object );
29
30
		$orderManager = \Aimeos\MShop\Order\Manager\Factory::create( $context );
31
		$orderBaseManager = $orderManager->getSubManager( 'base' );
32
		$orderProductManager = $orderBaseManager->getSubManager( 'product' );
33
34
		$productManager = \Aimeos\MShop\Product\Manager\Factory::create( $context );
35
		$search = $productManager->createSearch();
36
		$search->setConditions( $search->compare( '==', 'product.code', ['CNC', 'CNE'] ) );
37
		$products = $productManager->searchItems( $search )->toArray();
38
39
		$priceManager = \Aimeos\MShop\Price\Manager\Factory::create( $context );
40
		$price = $priceManager->createItem();
41
42
		foreach( $products as $product ) {
43
			$orderProducts[$product->getCode()] = $orderProductManager->createItem()->copyFrom( $product );
44
		}
45
46
		$orderProducts['CNC']->setPrice( clone $price->setValue( 321 ) );
47
		$orderProducts['CNE']->setPrice( clone $price->setValue( 123 ) );
48
49
		$this->orderBase = new \Aimeos\MShop\Order\Item\Base\Standard( $priceManager->createItem(), $context->getLocale() );
50
		$this->orderBase->addProduct( $orderProducts['CNC'] );
51
		$this->orderBase->addProduct( $orderProducts['CNE'] );
52
	}
53
54
55
	protected function tearDown() : void
56
	{
57
		unset( $this->object );
58
		unset( $this->orderBase );
59
		unset( $this->couponItem );
60
	}
61
62
63
	public function testCalcPrice()
64
	{
65
		$this->couponItem->setConfig( ['required.productcode' => 'CNC'] );
66
67
		$price = $this->object->calcPrice( $this->orderBase );
68
		$this->assertEquals( 444, $price->getValue() + $price->getCosts() );
69
	}
70
71
72
	public function testCalcPriceOnly()
73
	{
74
		$this->couponItem->setConfig( ['required.productcode' => 'CNC', 'required.only' => 1] );
75
76
		$price = $this->object->calcPrice( $this->orderBase );
77
		$this->assertEquals( 321, $price->getValue() + $price->getCosts() );
78
	}
79
80
81
	public function testGetConfigBE()
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
82
	{
83
		$result = $this->object->getConfigBE();
84
85
		$this->assertArrayHasKey( 'required.productcode', $result );
86
	}
87
88
89
	public function testCheckConfigBE()
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
90
	{
91
		$attributes = ['required.productcode' => 'test'];
92
		$result = $this->object->checkConfigBE( $attributes );
93
94
		$this->assertEquals( 2, count( $result ) );
95
		$this->assertNull( $result['required.productcode'] );
96
		$this->assertNull( $result['required.only'] );
97
	}
98
99
100
	public function testCheckConfigBEFailure()
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
101
	{
102
		$result = $this->object->checkConfigBE( [] );
103
104
		$this->assertEquals( 2, count( $result ) );
105
		$this->assertIsString( $result['required.productcode'] );
106
		$this->assertNull( $result['required.only'] );
107
	}
108
109
110
	public function testIsAvailable()
111
	{
112
		$this->assertTrue( $this->object->isAvailable( $this->orderBase ) );
113
	}
114
115
116
	public function testIsAvailableWithProduct()
117
	{
118
		$this->couponItem->setConfig( array( 'required.productcode' => 'CNC' ) );
119
120
		$this->assertTrue( $this->object->isAvailable( $this->orderBase ) );
121
	}
122
123
124
	public function testIsAvailableWithoutProduct()
125
	{
126
		$this->couponItem->setConfig( array( 'required.productcode' => 'ABCD' ) );
127
128
		$this->assertFalse( $this->object->isAvailable( $this->orderBase ) );
129
	}
130
131
}
132