Passed
Push — master ( b2da90...4d83d5 )
by Aimeos
06:05
created

ProductPriceTest::testGetConfigBE()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2018
7
 */
8
9
10
namespace Aimeos\MShop\Plugin\Provider\Order;
11
12
13
class ProductPriceTest extends \PHPUnit\Framework\TestCase
14
{
15
	private $context;
16
	private $object;
17
	private $order;
18
	private $plugin;
19
20
21
	protected function setUp()
22
	{
23
		$this->context = \TestHelperMShop::getContext();
24
		$this->plugin = \Aimeos\MShop::create( $this->context, 'plugin' )->createItem();
25
		$this->order = \Aimeos\MShop::create( $this->context, 'order/base' )->createItem()->off(); // remove event listeners
0 ignored issues
show
Bug introduced by
The method off() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
		$this->order = \Aimeos\MShop::create( $this->context, 'order/base' )->createItem()->/** @scrutinizer ignore-call */ off(); // remove event listeners

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
26
27
		$orderBaseProductManager = \Aimeos\MShop::create( $this->context, 'order/base/product' );
28
		$search = $orderBaseProductManager->createSearch();
29
		$search->setConditions( $search->compare( '==', 'order.base.product.prodcode', 'CNC' ) );
30
		$orderProducts = $orderBaseProductManager->searchItems( $search );
31
32
		if( ( $orderProduct = reset( $orderProducts ) ) === false ) {
33
			throw new \RuntimeException( 'No order base product item found.' );
34
		}
35
36
		$price = $orderProduct->getPrice();
37
		$price = $price->setValue( 600.00 )->setCosts( 30.00 )->setRebate( 0.00 )->setTaxrate( 19.00 );
38
39
		$orderProduct = $orderProduct->setPrice( $price );
40
		$this->order->addProduct( $orderProduct );
41
42
		$this->object = new \Aimeos\MShop\Plugin\Provider\Order\ProductPrice( $this->context, $this->plugin );
0 ignored issues
show
Bug introduced by
$this->plugin of type Aimeos\MShop\Attribute\Item\Iface is incompatible with the type Aimeos\MShop\Plugin\Item\Iface expected by parameter $item of Aimeos\MShop\Plugin\Prov...uctPrice::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
		$this->object = new \Aimeos\MShop\Plugin\Provider\Order\ProductPrice( $this->context, /** @scrutinizer ignore-type */ $this->plugin );
Loading history...
43
	}
44
45
46
	protected function tearDown()
47
	{
48
		unset( $this->object, $this->plugin, $this->order, $this->context );
49
	}
50
51
52
	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...
53
	{
54
		$attributes = array(
55
			'ignore-modified' => '0',
56
		);
57
58
		$result = $this->object->checkConfigBE( $attributes );
59
60
		$this->assertEquals( 1, count( $result ) );
61
		$this->assertEquals( null, $result['ignore-modified'] );
62
	}
63
64
65
	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...
66
	{
67
		$list = $this->object->getConfigBE();
68
69
		$this->assertEquals( 1, count( $list ) );
70
		$this->assertArrayHasKey( 'ignore-modified', $list );
71
72
		foreach( $list as $entry ) {
73
			$this->assertInstanceOf( \Aimeos\MW\Criteria\Attribute\Iface::class, $entry );
74
		}
75
	}
76
77
	public function testRegister()
78
	{
79
		$this->object->register( $this->order );
80
	}
81
82
83
	public function testUpdateArticlePriceCorrect()
84
	{
85
		$this->plugin->setConfig( ['update' => true] );
86
		$part = \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT;
87
88
		$this->assertEquals( $part, $this->object->update( $this->order, 'check.after', $part ) );
89
	}
90
91
92
	public function testUpdateSelectionPriceCorrect()
93
	{
94
		$productItem = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'U:TEST', ['price'] );
95
		$refPrices = $productItem->getRefItems( 'price', 'default', 'default' );
96
97
		if( ( $productPrice = reset( $refPrices ) ) === false ) {
98
			throw new \RuntimeException( 'No product price available' );
99
		}
100
101
102
		$orderProduct = $this->order->getProduct( 0 )->setProductId( $productItem->getId() )
103
			->setProductCode( 'U:TESTSUB02' )->setPrice( $productPrice );
104
105
		$this->order->addProduct( $orderProduct, 0 );
106
		$this->plugin->setConfig( array( 'update' => true ) );
107
		$part = \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT;
108
109
		$this->assertEquals( $part, $this->object->update( $this->order, 'check.after', $part ) );
110
	}
111
112
113
	public function testUpdateArticlePriceUpdated()
114
	{
115
		$this->plugin->setConfig( array( 'update' => true ) );
116
117
		$orderProduct = $this->order->getProduct( 0 );
118
		$orderProduct->setPrice( $orderProduct->getPrice()->setValue( 13.13 ) );
119
		$this->order->addProduct( $orderProduct, 0 );
120
121
		try
122
		{
123
			$part = \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT;
124
			$this->object->update( $this->order, 'check.after', $part );
125
126
			$this->fail( 'Price changes not recognized' );
127
		}
128
		catch( \Aimeos\MShop\Plugin\Provider\Exception $mppe )
129
		{
130
			$this->assertEquals( '600.00', $this->order->getProduct( 0 )->getPrice()->getValue() );
131
			$this->assertEquals( ['product' => ['0' => 'price.changed']], $mppe->getErrorCodes() );
132
		}
133
	}
134
135
136
	public function testUpdateSelectionPriceUpdated()
137
	{
138
		$productItem = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'U:TEST' );
139
140
		$orderProduct = $this->order->getProduct( 0 );
141
		$orderProduct = $orderProduct->setProductCode( 'U:TESTSUB02' )
142
			->setProductId( $productItem->getId() )->setPrice( $orderProduct->getPrice() );
143
144
		$this->order->addProduct( $orderProduct, 0 );
145
146
		try
147
		{
148
			$part = \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT;
149
			$this->object->update( $this->order, 'check.after', $part );
150
151
			$this->fail( 'Price changes not recognized' );
152
		}
153
		catch( \Aimeos\MShop\Plugin\Provider\Exception $mppe )
154
		{
155
			$this->assertEquals( '18.00', $this->order->getProduct( 0 )->getPrice()->getValue() );
156
			$this->assertEquals( ['product' => ['0' => 'price.changed']], $mppe->getErrorCodes() );
157
		}
158
	}
159
160
161
	public function testUpdateAttributePriceUpdated()
162
	{
163
		$attribute = \Aimeos\MShop::create( $this->context, 'attribute' )
164
			->findItem( 'xs', ['price'], 'product', 'size' );
165
166
		$ordAttr = \Aimeos\MShop::create( $this->context, 'order/base/product/attribute' )->createItem()
167
			->copyFrom( $attribute )->setQuantity( 2 );
0 ignored issues
show
Bug introduced by
The method copyFrom() does not exist on Aimeos\MShop\Attribute\Item\Iface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
			->/** @scrutinizer ignore-call */ copyFrom( $attribute )->setQuantity( 2 );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
168
169
		$orderProduct = $this->order->getProduct( 0 )->setAttributeItems( [$ordAttr] );
170
		$this->order->addProduct( $orderProduct, 0 );
171
172
		try
173
		{
174
			$part = \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT;
175
			$this->object->update( $this->order, 'check.after', $part );
176
177
			$this->fail( 'Price changes not recognized' );
178
		}
179
		catch( \Aimeos\MShop\Plugin\Provider\Exception $mppe )
180
		{
181
			$this->assertEquals( '625.90', $this->order->getProduct( 0 )->getPrice()->getValue() );
182
			$this->assertEquals( ['product' => ['0' => 'price.changed']], $mppe->getErrorCodes() );
183
		}
184
	}
185
186
187
	public function testUpdateNoPriceChange()
188
	{
189
		$orderProduct = $this->order->getProduct( 0 );
190
191
		$refPrice = $orderProduct->getPrice()->getValue();
192
		$orderProduct->setPrice( $orderProduct->getPrice()->setValue( 13.13 ) );
193
194
		$this->order->addProduct( $orderProduct, 0 );
195
196
		try
197
		{
198
			$part = \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT;
199
			$this->object->update( $this->order, 'check.after', $part );
200
201
			$this->fail( 'Price changes not recognized' );
202
		}
203
		catch( \Aimeos\MShop\Plugin\Provider\Exception $mppe )
204
		{
205
			$product = $this->order->getProduct( 0 );
206
207
			$this->assertEquals( $refPrice, $product->getPrice()->getValue() );
208
			$this->assertEquals( ['product' => ['0' => 'price.changed']], $mppe->getErrorCodes() );
209
		}
210
	}
211
212
213
	public function testUpdatePriceImmutable()
214
	{
215
		$orderProduct = $this->order->getProduct( 0 );
216
		$orderProduct = $orderProduct->setPrice( $orderProduct->getPrice()->setValue( 13.13 ) )
217
			->setFlags( \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE );
218
219
		$part = \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT;
220
		$oldPrice = clone $this->order->getProduct( 0 )->getPrice();
221
222
		$this->assertEquals( $part, $this->object->update( $this->order, 'check.after', $part ) );
223
		$this->assertEquals( $oldPrice, $orderProduct->getPrice() );
224
	}
225
226
227
	public function testIgnoreModified()
228
	{
229
		$this->plugin->setConfig( array( 'ignore-modified' => true ) );
230
231
		$orderProduct = $this->order->getProduct( 0 );
232
		$orderProduct->setPrice( $orderProduct->getPrice()->setValue( 13.13 ) );
233
234
		$part = \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT;
235
		$oldPrice = clone $this->order->getProduct( 0 )->getPrice();
236
237
		$this->assertEquals( $part, $this->object->update( $this->order, 'check.after', $part ) );
238
		$this->assertEquals( $oldPrice, $orderProduct->getPrice() );
239
	}
240
}
241