WIP Coupon

RemoveXPercent and RemoveXAmount implementation
This commit is contained in:
gmorel
2013-08-19 20:02:53 +02:00
parent 4e40555313
commit 2113e7a457
8 changed files with 533 additions and 9 deletions

View File

@@ -23,6 +23,8 @@
namespace Thelia\Coupon;
use Symfony\Component\Intl\Exception\NotImplementedException;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
@@ -36,9 +38,57 @@ namespace Thelia\Coupon;
*/
abstract class CouponAbstract implements CouponInterface
{
/** @var CouponAdapterInterface Provide necessary value from Thelia*/
protected $adapter;
/** @var RuleOrganizerInterface */
protected $organizer = null;
/** @var string Coupon code (ex: XMAS) */
protected $code = null;
/** @var string Coupon title (ex: Coupon for XMAS) */
protected $title = null;
/** @var string Coupon short description */
protected $shortDescription = null;
/** @var string Coupon description */
protected $description = null;
/** @var bool if Coupon is cumulative */
protected $isCumulative = false;
/** @var bool if Coupon is removing postage */
protected $isRemovingPostage = false;
/**
* Set Adapter containing all relevant data
*
* @param CouponAdapterInterface $adapter Adapter
*
* @return $this
*/
public function setAdapter($adapter)
{
$this->adapter = $adapter;
return $this;
}
/**
* Set Rule Organizer
*
* @param RuleOrganizerInterface $organizer Manage Rule groups (&& and ||)
*
* @return $this
*/
public function setOrganizer($organizer)
{
$this->organizer = $organizer;
return $this;
}
/**
* Return Coupon code (ex: XMAS)
@@ -47,7 +97,7 @@ abstract class CouponAbstract implements CouponInterface
*/
public function getCode()
{
// TODO: Implement getCode() method.
return $this->code;
}
/**
@@ -57,7 +107,7 @@ abstract class CouponAbstract implements CouponInterface
*/
public function getTitle()
{
// TODO: Implement getTitle() method.
return $this->title;
}
/**
@@ -67,7 +117,7 @@ abstract class CouponAbstract implements CouponInterface
*/
public function getShortDescription()
{
// TODO: Implement getShortDescription() method.
return $this->shortDescription;
}
/**
@@ -77,7 +127,7 @@ abstract class CouponAbstract implements CouponInterface
*/
public function getDescription()
{
// TODO: Implement getDescription() method.
return $this->description;
}
/**
@@ -85,11 +135,11 @@ abstract class CouponAbstract implements CouponInterface
* If is cumulative you can sum Coupon effects
* If not cancel all other Coupon and take the last given
*
* @return string
* @return bool
*/
public function isCumulative()
{
// TODO: Implement isCumulative() method.
return $this->isCumulative;
}
/**
@@ -99,17 +149,20 @@ abstract class CouponAbstract implements CouponInterface
*/
public function isRemovingPostage()
{
// TODO: Implement isRemovingPostage() method.
return $this->isRemovingPostage;
}
/**
* Return effects generated by the coupon
*
* @throws \Symfony\Component\Intl\Exception\NotImplementedException
* @return \Closure
*/
public function getEffect()
{
// TODO: Implement getEffect() method.
throw new NotImplementedException(
'Abstract method to implement (CouponAbstract->getEffect)'
);
}
}

View File

@@ -57,4 +57,26 @@ interface CouponAdapterInterface
* @return \Thelia\Model\Customer
*/
public function getCustomer();
/**
* Return Checkout total price
*
* @return float
*/
public function getCheckoutTotalPrice();
/**
* Return Products total price
*
* @return float
*/
public function getCheckoutTotalPriceWithoutDiscountAndPostagePrice();
/**
* Return Checkout total postage (only) price
*
* @return float
*/
public function getCheckoutPostagePrice();
}

View File

@@ -64,4 +64,24 @@ class CouponBaseAdapter implements CouponAdapterInterface
// TODO: Implement getCustomer() method.
}
/**
* Return Checkout total price
*
* @return float
*/
public function getCheckoutTotalPrice()
{
// TODO: Implement getCheckoutTotalPrice() method.
}
/**
* Return Checkout total postage (only) price
*
* @return float
*/
public function getCheckoutPostagePrice()
{
// TODO: Implement getCheckoutPostagePrice() method.
}
}

View File

@@ -30,6 +30,8 @@ use Thelia\Coupon\CouponAbstract;
* Date: 8/19/13
* Time: 3:24 PM
*
* Allow to remove an amount from the checkout total
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
@@ -37,4 +39,42 @@ use Thelia\Coupon\CouponAbstract;
class RemoveXAmount extends CouponAbstract
{
protected $amount = 0;
/**
* Constructor
*
* @param string $code Coupon code (ex: XMAS)
* @param string $title Coupon title (ex: Coupon for XMAS)
* @param string $shortDescription Coupon short description
* @param string $description Coupon description
* @param float $amount Coupon amount to deduce
* @param bool $isCumulative if Coupon is cumulative
* @param bool $isRemovingPostage if Coupon is removing postage
*/
function __construct($code, $title, $shortDescription, $description, $amount, $isCumulative, $isRemovingPostage)
{
$this->code = $code;
$this->title = $title;
$this->shortDescription = $shortDescription;
$this->description = $description;
$this->isCumulative = $isCumulative;
$this->isRemovingPostage = $isRemovingPostage;
$this->amount = $amount;
}
/**
* Return effects generated by the coupon
* A negative value
*
* @return float
*/
public function getEffect()
{
return -$this->amount;
}
}

View File

@@ -23,6 +23,9 @@
namespace Thelia\Coupon\Type;
use Thelia\Coupon\CouponAbstract;
use Thelia\Exception\MissingAdapterException;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
@@ -34,5 +37,57 @@ namespace Thelia\Coupon\Type;
*/
class RemoveXPercent extends CouponAbstract
{
protected $percent = 0;
/**
* Constructor
*
* @param string $code Coupon code (ex: XMAS)
* @param string $title Coupon title (ex: Coupon for XMAS)
* @param string $shortDescription Coupon short description
* @param string $description Coupon description
* @param float $percent Coupon % to deduce (ex:3.14 for 3.14%)
* @param bool $isCumulative if Coupon is cumulative
* @param bool $isRemovingPostage if Coupon is removing postage
*/
function __construct($code, $title, $shortDescription, $description, $percent, $isCumulative, $isRemovingPostage)
{
$this->code = $code;
$this->title = $title;
$this->shortDescription = $shortDescription;
$this->description = $description;
$this->isCumulative = $isCumulative;
$this->isRemovingPostage = $isRemovingPostage;
$this->percent = $percent;
}
/**
* Return effects generated by the coupon
* A negative value
*
* @throws \Thelia\Exception\MissingAdapterException
* @throws \InvalidArgumentException
* @return float
*/
public function getEffect()
{
if ($this->adapter === null) {
throw new MissingAdapterException(
'Cant calculate effect : CouponAdapterInterface is missing.'
);
}
if ($this->percent >= 100) {
throw new \InvalidArgumentException(
'Percentage must be inferior to 100'
);
}
$basePrice = $this->adapter
->getCheckoutTotalPriceWithoutDiscountAndPostagePrice();
return $basePrice * (( 100 - $this->percent ) / 100);
}
}

View File

@@ -0,0 +1,50 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Exception;
use Thelia\Log\Tlog;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Thrown when the Adapter is not set
*
* @package Coupon
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class MissingAdapterException extends \RuntimeException
{
/**
* {@inheritdoc}
*/
public function __construct($message, $code = null, $previous = null) {
Tlog::getInstance()->addError($message);
parent::__construct($message, $code, $previous);
}
}

View File

@@ -1,20 +1,165 @@
<?php
namespace Thelia\Coupon;
use PHPUnit_Framework_TestCase;
use Thelia\Coupon\Type\RemoveXAmount;
/**
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-08-19 at 18:26:01.
*/
class RemoveXAmountTest extends \PHPUnit_Framework_TestCase
class RemoveXAmountTest extends PHPUnit_Framework_TestCase
{
CONST VALID_COUPON_CODE = 'XMAS';
CONST VALID_COUPON_TITLE = 'XMAS Coupon';
CONST VALID_COUPON_SHORT_DESCRIPTION = 'Coupon for christmas';
CONST VALID_COUPON_DESCRIPTION = '<h1>Lorem</h1><span>ipsum</span>';
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
}
protected function generateValidCumulativeRemovingPostageCoupon()
{
$coupon = new RemoveXAmount(
self::VALID_COUPON_CODE,
self::VALID_COUPON_TITLE,
self::VALID_COUPON_SHORT_DESCRIPTION,
self::VALID_COUPON_DESCRIPTION,
30.00,
true,
true
);
return $coupon;
}
protected function generateValidNonCumulativeNonRemovingPostageCoupon()
{
$coupon = new RemoveXAmount(
self::VALID_COUPON_CODE,
self::VALID_COUPON_TITLE,
self::VALID_COUPON_SHORT_DESCRIPTION,
self::VALID_COUPON_DESCRIPTION,
30.00,
false,
false
);
return $coupon;
}
/**
*
* @covers Thelia\Coupon\type\RemoveXAmount::getCode
* @covers Thelia\Coupon\type\RemoveXAmount::getTitle
* @covers Thelia\Coupon\type\RemoveXAmount::getShortDescription
* @covers Thelia\Coupon\type\RemoveXAmount::getDescription
*
*/
public function testDisplay()
{
$coupon = $this->generateValidCumulativeRemovingPostageCoupon();
$expected = self::VALID_COUPON_CODE;
$actual = $coupon->getCode();
$this->assertEquals($expected, $actual);
$expected = self::VALID_COUPON_TITLE;
$actual = $coupon->getTitle();
$this->assertEquals($expected, $actual);
$expected = self::VALID_COUPON_SHORT_DESCRIPTION;
$actual = $coupon->getShortDescription();
$this->assertEquals($expected, $actual);
$expected = self::VALID_COUPON_DESCRIPTION;
$actual = $coupon->getDescription();
$this->assertEquals($expected, $actual);
}
/**
*
* @covers Thelia\Coupon\type\RemoveXAmount::isCumulative
*
*/
public function testIsCumulative()
{
$coupon = $this->generateValidCumulativeRemovingPostageCoupon();
$actual = $coupon->isCumulative();
$this->assertTrue($actual);
}
/**
*
* @covers Thelia\Coupon\type\RemoveXAmount::isCumulative
*
*/
public function testIsNotCumulative()
{
$coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon();
$actual = $coupon->isCumulative();
$this->assertFalse($actual);
}
/**
*
* @covers Thelia\Coupon\type\RemoveXAmount::isRemovingPostage
*
*/
public function testIsRemovingPostage()
{
$coupon = $this->generateValidCumulativeRemovingPostageCoupon();
$actual = $coupon->isRemovingPostage();
$this->assertTrue($actual);
}
/**
*
* @covers Thelia\Coupon\type\RemoveXAmount::isRemovingPostage
*
*/
public function testIsNotRemovingPostage()
{
$coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon();
$actual = $coupon->isRemovingPostage();
$this->assertFalse($actual);
}
/**
*
* @covers Thelia\Coupon\type\RemoveXAmount::getEffect
*
*/
public function testGetEffect()
{
$coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon();
$expected = -30.00;
$actual = $coupon->getEffect();
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.

View File

@@ -1,6 +1,9 @@
<?php
namespace Thelia\Coupon;
use PHPUnit_Framework_TestCase;
use Thelia\Coupon\Type\RemoveXPercent;
/**
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-08-19 at 18:26:01.
*/
@@ -15,6 +18,142 @@ class RemoveXPercentTest extends \PHPUnit_Framework_TestCase
{
}
protected function generateValidCumulativeRemovingPostageCoupon()
{
$coupon = new RemoveXPercent(
self::VALID_COUPON_CODE,
self::VALID_COUPON_TITLE,
self::VALID_COUPON_SHORT_DESCRIPTION,
self::VALID_COUPON_DESCRIPTION,
30.00,
true,
true
);
return $coupon;
}
protected function generateValidNonCumulativeNonRemovingPostageCoupon()
{
$coupon = new RemoveXPercent(
self::VALID_COUPON_CODE,
self::VALID_COUPON_TITLE,
self::VALID_COUPON_SHORT_DESCRIPTION,
self::VALID_COUPON_DESCRIPTION,
30.00,
false,
false
);
return $coupon;
}
/**
*
* @covers Thelia\Coupon\Type\RemoveXPercent::getCode
* @covers Thelia\Coupon\Type\RemoveXPercent::getTitle
* @covers Thelia\Coupon\Type\RemoveXPercent::getShortDescription
* @covers Thelia\Coupon\Type\RemoveXPercent::getDescription
*
*/
public function testDisplay()
{
$coupon = $this->generateValidCumulativeRemovingPostageCoupon();
$expected = self::VALID_COUPON_CODE;
$actual = $coupon->getCode();
$this->assertEquals($expected, $actual);
$expected = self::VALID_COUPON_TITLE;
$actual = $coupon->getTitle();
$this->assertEquals($expected, $actual);
$expected = self::VALID_COUPON_SHORT_DESCRIPTION;
$actual = $coupon->getShortDescription();
$this->assertEquals($expected, $actual);
$expected = self::VALID_COUPON_DESCRIPTION;
$actual = $coupon->getDescription();
$this->assertEquals($expected, $actual);
}
/**
*
* @covers Thelia\Coupon\Type\RemoveXPercent::isCumulative
*
*/
public function testIsCumulative()
{
$coupon = $this->generateValidCumulativeRemovingPostageCoupon();
$actual = $coupon->isCumulative();
$this->assertTrue($actual);
}
/**
*
* @covers Thelia\Coupon\Type\RemoveXPercent::isCumulative
*
*/
public function testIsNotCumulative()
{
$coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon();
$actual = $coupon->isCumulative();
$this->assertFalse($actual);
}
/**
*
* @covers Thelia\Coupon\Type\RemoveXPercent::isRemovingPostage
*
*/
public function testIsRemovingPostage()
{
$coupon = $this->generateValidCumulativeRemovingPostageCoupon();
$actual = $coupon->isRemovingPostage();
$this->assertTrue($actual);
}
/**
*
* @covers Thelia\Coupon\Type\RemoveXPercent::isRemovingPostage
*
*/
public function testIsNotRemovingPostage()
{
$coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon();
$actual = $coupon->isRemovingPostage();
$this->assertFalse($actual);
}
/**
*
* @covers Thelia\Coupon\Type\RemoveXPercent::getEffect
*
*/
public function testGetEffect()
{
$coupon = $this->generateValidNonCumulativeNonRemovingPostageCoupon();
$expected = -30.00;
$actual = $coupon->getEffect();
$this->assertEquals($expected, $actual);
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.