Skip to content

Commit

Permalink
Implemented Nocosts service decorator to avoid costs per item when us…
Browse files Browse the repository at this point in the history
…ing click&collect
  • Loading branch information
aimeos committed Jun 4, 2020
1 parent 22ec298 commit d89274b
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/mshoplib/src/MShop/Service/Provider/Decorator/Nocosts.php
@@ -0,0 +1,41 @@
<?php

/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2020
* @package MShop
* @subpackage Service
*/


namespace Aimeos\MShop\Service\Provider\Decorator;


/**
* Decorator for service providers setting costs to zero.
*
* @package MShop
* @subpackage Service
*/
class Nocosts
extends \Aimeos\MShop\Service\Provider\Decorator\Base
implements \Aimeos\MShop\Service\Provider\Decorator\Iface
{
/**
* Returns the costs per item as negative value to get no costs at all.
*
* @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
* @return \Aimeos\MShop\Price\Item\Iface Price item containing the price, shipping, rebate
*/
public function calcPrice( \Aimeos\MShop\Order\Item\Base\Iface $basket ) : \Aimeos\MShop\Price\Item\Iface
{
$costs = 0;
$price = $this->getProvider()->calcPrice( $basket );

foreach( $basket->getProducts() as $product ) {
$costs += $product->getPrice()->getCosts() * $product->getQuantity();
}

return $price->setCosts( -$costs );
}
}
@@ -0,0 +1,80 @@
<?php

/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2020
*/


namespace Aimeos\MShop\Service\Provider\Decorator;


class NocostsTest extends \PHPUnit\Framework\TestCase
{
private $object;
private $basket;
private $context;
private $servItem;
private $mockProvider;


protected function setUp() : void
{
$this->context = \TestHelperMShop::getContext();

$servManager = \Aimeos\MShop::create( $this->context, 'service' );
$this->servItem = $servManager->createItem()->setId( -1 );

$this->mockProvider = $this->getMockBuilder( \Aimeos\MShop\Service\Provider\Decorator\Nocosts::class )
->disableOriginalConstructor()->getMock();

$orderManager = \Aimeos\MShop\Order\Manager\Factory::create( $this->context );
$this->basket = $orderManager->getSubManager( 'base' )->createItem()->off(); // remove plugins

$this->object = new \Aimeos\MShop\Service\Provider\Decorator\Nocosts( $this->mockProvider, $this->context, $this->servItem );
}


protected function tearDown() : void
{
unset( $this->object, $this->basket, $this->mockProvider, $this->servItem, $this->context );
}


public function testCalcPrice()
{
$this->basket->addProduct( $this->getOrderProduct() );
$priceItem = \Aimeos\MShop::create( $this->context, 'price' )->createItem();

$this->mockProvider->expects( $this->once() )->method( 'calcPrice' )->will( $this->returnValue( $priceItem ) );

$price = $this->object->calcPrice( $this->basket );
$this->assertEquals( '0.00', $price->getValue() );
$this->assertEquals( '-5.00', $price->getCosts() );
}


/**
* Returns an order product item
*
* @return \Aimeos\MShop\Order\Item\Base\Product\Iface Order product item
*/
protected function getOrderProduct()
{
$priceManager = \Aimeos\MShop::create( $this->context, 'price' );
$productManager = \Aimeos\MShop::create( $this->context, 'product' );
$orderProductManager = \Aimeos\MShop::create( $this->context, 'order/base/product' );

$price = $priceManager->createItem();
$price->setValue( '20.00' )->setCosts( '5.00' );

$product = $productManager->createItem()->setId( '-1' );
$product->setCode( 'test' )->setType( 'test' );

$orderProduct = $orderProductManager->createItem();
$orderProduct->copyFrom( $product );
$orderProduct->setPrice( $price );

return $orderProduct;
}
}

0 comments on commit d89274b

Please sign in to comment.