Added status change helper methods (setPaid / isPaid, etc)

This commit is contained in:
Franck Allimant
2014-03-01 22:01:11 +01:00
parent 73baab5283
commit defd66af27
2 changed files with 209 additions and 11 deletions

View File

@@ -45,13 +45,14 @@ class Order extends BaseOrder
return uniqid('ORD', true);
}
/**
* calculate the total amount
* Compute this order amount.
*
* @param int $tax
* @param bool $includePostage
*
* @return float|int|string
* @param float $tax (output only) returns the tax amount for this order
* @param bool $includePostage if true, the postage cost is included to the total
* @param bool $includeDiscount if true, the discount will be included to the total
* @return float
*/
public function getTotalAmount(&$tax = 0, $includePostage = true, $includeDiscount = true)
{
@@ -93,4 +94,106 @@ class Order extends BaseOrder
return $total;
}
/**
* Set the status of the current order to NOT PAID
*/
public function setNotPaid() {
$this->setStatusHelper(OrderStatus::CODE_NOT_PAID);
}
/**
* Check if the current status of this order is NOT PAID
*
* @return bool true if this order is NOT PAID, false otherwise.
*/
public function isNotPaid() {
return $this->hasStatusHelper(OrderStatus::CODE_NOT_PAID);
}
/**
* Set the status of the current order to PAID
*/
public function setPaid() {
$this->setStatusHelper(OrderStatus::CODE_PAID);
}
/**
* Check if the current status of this order is PAID
*
* @return bool true if this order is PAID, false otherwise.
*/
public function isPaid() {
return $this->hasStatusHelper(OrderStatus::CODE_PAID);
}
/**
* Set the status of the current order to PROCESSING
*/
public function setProcessing() {
$this->setStatusHelper(OrderStatus::CODE_PROCESSING);
}
/**
* Check if the current status of this order is PROCESSING
*
* @return bool true if this order is PROCESSING, false otherwise.
*/
public function isProcessing() {
return $this->hasStatusHelper(OrderStatus::CODE_PROCESSING);
}
/**
* Set the status of the current order to SENT
*/
public function setSent() {
$this->setStatusHelper(OrderStatus::CODE_SENT);
}
/**
* Check if the current status of this order is SENT
*
* @return bool true if this order is SENT, false otherwise.
*/
public function isSent() {
return $this->hasStatusHelper(OrderStatus::CODE_SENT);
}
/**
* Set the status of the current order to CANCELED
*/
public function setCancelled() {
$this->setStatusHelper(OrderStatus::CODE_CANCELED);
}
/**
* Check if the current status of this order is CANCELED
*
* @return bool true if this order is CANCELED, false otherwise.
*/
public function isCancelled() {
return $this->hasStatusHelper(OrderStatus::CODE_CANCELED);
}
/**
* Set the status of the current order to the provided status
*
* @param string $statusCode the status code, one of OrderStatus::CODE_xxx constants.
*/
public function setStatusHelper($statusCode) {
if (null !== $ordeStatus = OrderStatusQuery::create()->findOneByCode($statusCode)) {
$this->setOrderStatus($ordeStatus)->save();
}
}
/**
* Check if the current status of this order is $statusCode
*
* @param string $statusCode the status code, one of OrderStatus::CODE_xxx constants.
* @return bool true if this order have the provided status, false otherwise.
*/
public function hasStatusHelper($statusCode) {
return $this->getOrderStatus()->getCode() == $statusCode;
}
}

View File

@@ -25,6 +25,7 @@ namespace Thelia\Tests\Action;
use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Thelia\Action\Order;
use Thelia\Core\Event\Order\OrderAddressEvent;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\HttpFoundation\Request;
@@ -37,18 +38,17 @@ use Thelia\Model\AddressQuery;
use Thelia\Model\Base\OrderAddressQuery;
use Thelia\Model\Base\OrderProductQuery;
use Thelia\Model\Base\OrderQuery;
use Thelia\Model\OrderStatus;
use Thelia\Model\ProductSaleElementsQuery;
use Thelia\Model\Cart;
use Thelia\Model\CartItem;
use Thelia\Model\CurrencyQuery;
use Thelia\Model\Customer as CustomerModel;
use Thelia\Model\CustomerQuery;
use Thelia\Model\ModuleQuery;
use Thelia\Model\Order as OrderModel;
use Thelia\Model\Customer as CustomerModel;
use Thelia\Action\Order;
use Thelia\Model\OrderStatus;
use Thelia\Model\OrderStatusQuery;
use Thelia\Model\ProductQuery;
use Thelia\Model\ProductSaleElementsQuery;
use Thelia\Module\BaseModule;
/**
@@ -404,6 +404,101 @@ class OrderTest extends \PHPUnit_Framework_TestCase
);
}
/**
* @depends testCreate
*
* @param OrderModel $order
*/
public function testModelUpdateStatusPaidWithHelpers(OrderModel $order)
{
$order->setPaid();
$this->assertEquals(
$order->getStatusId(),
OrderStatusQuery::create()->findOneByCode(OrderStatus::CODE_PAID)->getId()
);
$this->assertTrue(
$order->isPaid()
);
}
/**
* @depends testCreate
*
* @param OrderModel $order
*/
public function testModelUpdateStatusNotPaidWithHelpers(OrderModel $order)
{
$order->setNotPaid();
$this->assertEquals(
$order->getStatusId(),
OrderStatusQuery::create()->findOneByCode(OrderStatus::CODE_NOT_PAID)->getId()
);
$this->assertTrue(
$order->isNotPaid()
);
}
/**
* @depends testCreate
*
* @param OrderModel $order
*/
public function testModelUpdateStatusProcessedWithHelpers(OrderModel $order)
{
$order->setProcessing();
$this->assertEquals(
$order->getStatusId(),
OrderStatusQuery::create()->findOneByCode(OrderStatus::CODE_PROCESSING)->getId()
);
$this->assertTrue(
$order->isProcessing()
);
}
/**
* @depends testCreate
*
* @param OrderModel $order
*/
public function testModelUpdateStatusSentWithHelpers(OrderModel $order)
{
$order->setSent();
$this->assertEquals(
$order->getStatusId(),
OrderStatusQuery::create()->findOneByCode(OrderStatus::CODE_SENT)->getId()
);
$this->assertTrue(
$order->isSent()
);
}
/**
* @depends testCreate
*
* @param OrderModel $order
*/
public function testModelUpdateStatusCanceledWithHelpers(OrderModel $order)
{
$order->setCancelled();
$this->assertEquals(
$order->getStatusId(),
OrderStatusQuery::create()->findOneByCode(OrderStatus::CODE_CANCELED)->getId()
);
$this->assertTrue(
$order->isCancelled()
);
}
/**
* @depends testCreate
*
@@ -472,4 +567,4 @@ class OrderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('J', $newOrderAddress->getPhone());
$this->assertEquals('K', $newOrderAddress->getCompany());
}
}
}