WIP : Coupon implementation in front : Coupon : Fixing unit tests

This commit is contained in:
gmorel
2013-10-17 01:38:13 +02:00
parent 36dd7add79
commit d2851b1c93
5 changed files with 185 additions and 8 deletions

View File

@@ -93,6 +93,8 @@
<form name="thelia.order.payment" class="Thelia\Form\OrderPayment"/> <form name="thelia.order.payment" class="Thelia\Form\OrderPayment"/>
<form name="thelia.order.update.address" class="Thelia\Form\OrderUpdateAddress"/> <form name="thelia.order.update.address" class="Thelia\Form\OrderUpdateAddress"/>
<form name="thelia.order.coupon" class="Thelia\Form\CouponCode"/>
<form name="thelia.admin.config.creation" class="Thelia\Form\ConfigCreationForm"/> <form name="thelia.admin.config.creation" class="Thelia\Form\ConfigCreationForm"/>
<form name="thelia.admin.config.modification" class="Thelia\Form\ConfigModificationForm"/> <form name="thelia.admin.config.modification" class="Thelia\Form\ConfigModificationForm"/>

View File

@@ -135,6 +135,11 @@
<default key="_view">order-invoice</default> <default key="_view">order-invoice</default>
</route> </route>
<route id="order.coupon.process" path="/order/coupon" methods="post">
<default key="_controller">Thelia\Controller\Front\CouponController::consume</default>
<default key="_view">order-invoice</default>
</route>
<route id="order.payment.process" path="/order/pay"> <route id="order.payment.process" path="/order/pay">
<default key="_controller">Thelia\Controller\Front\OrderController::pay</default> <default key="_controller">Thelia\Controller\Front\OrderController::pay</default>
</route> </route>

View File

@@ -0,0 +1,94 @@
<?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\Controller\Front;
use Propel\Runtime\Exception\PropelException;
use Thelia\Core\Event\Coupon\CouponConsumeEvent;
use Thelia\Exception\TheliaProcessException;
use Thelia\Form\CouponCode;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Symfony\Component\HttpFoundation\Request;
use Thelia\Form\OrderDelivery;
use Thelia\Form\OrderPayment;
use Thelia\Log\Tlog;
use Thelia\Model\AddressQuery;
use Thelia\Model\AreaDeliveryModuleQuery;
use Thelia\Model\Base\OrderQuery;
use Thelia\Model\ModuleQuery;
use Thelia\Model\Order;
use Thelia\Tools\URL;
/**
* Class CouponController
* @package Thelia\Controller\Front
* @author Guillaume MOREL <gmorel@openstudio.fr>
*/
class CouponController extends BaseFrontController
{
/**
* Test Coupon consuming
*/
public function consumeAction()
{
$this->checkAuth();
$this->checkCartNotEmpty();
$message = false;
$couponCodeForm = new CouponCode($this->getRequest());
try {
$form = $this->validateForm($couponCodeForm, 'post');
$couponCode = $form->get('coupon-code')->getData();
if (null === $couponCode || empty($couponCode)) {
$message = true;
throw new \Exception('Coupon code can\'t be empty');
}
$couponConsumeEvent = new CouponConsumeEvent($couponCode);
// Dispatch Event to the Action
$this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent);
} catch (FormValidationException $e) {
$message = sprintf('Please check your coupon code: %s', $e->getMessage());
} catch (PropelException $e) {
$this->getParserContext()->setGeneralError($e->getMessage());
} catch (\Exception $e) {
$message = sprintf('Sorry, an error occurred: %s', $e->getMessage());
}
if ($message !== false) {
Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage()));
$couponCodeForm->setErrorMessage($message);
$this->getParserContext()
->addForm($couponCodeForm)
->setGeneralError($message);
}
}
}

View File

@@ -0,0 +1,63 @@
<?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\Form;
use Symfony\Component\Validator\Constraints;
use Thelia\Model\ModuleQuery;
use Thelia\Module\BaseModule;
/**
* Class CouponCode
*
* Manage how a coupon is entered by a customer
*
* @package Thelia\Form
* @author Guillaume MOREL <gmorel@openstudio.fr>
*/
class CouponCode extends BaseForm
{
/**
* Build form
*/
protected function buildForm()
{
$this->formBuilder
->add("coupon-code", "text", array(
"required" => true,
"constraints" => array(
new Constraints\NotBlank(),
)
)
);
}
/**
* Form name
*
* @return string
*/
public function getName()
{
return "thelia_coupon_code";
}
}

View File

@@ -30,9 +30,9 @@
<a href="#" role="button" class="btn btn-step disabled"><span class="step-nb">4</span> <span class="step-label">{intl l="Secure payment"}</span></a> <a href="#" role="button" class="btn btn-step disabled"><span class="step-nb">4</span> <span class="step-label">{intl l="Secure payment"}</span></a>
</div> </div>
{form name="thelia.order.payment"} {form name="thelia.order.coupon"}
<form id="form-cart-payment" action="{url path="/order/invoice"}" method="post" role="form" {form_enctype form=$form}> <form id="form-coupon" action="{url path="/order/coupon"}" method="post" role="form" {form_enctype form=$form}>
{form_hidden_fields form=$form} {form_hidden_fields form=$form}
@@ -137,12 +137,17 @@
<tr > <tr >
<th class="coupon"><label for="coupon">{intl l="You may have a coupon ?"}</label></th> <th class="coupon"><label for="coupon">{intl l="You may have a coupon ?"}</label></th>
<td class="coupon"> <td class="coupon">
<div class="input-group"> {form_field form=$form field='coupon-code'}
<input type="text" name="coupon" id="coupon" class="form-control"> <div class="input-group {if $error}has-error{/if}">
<span class="input-group-btn"> <label class="control-label" for="code">{intl l='Code :'}</label>
<button type="button" class="btn btn-coupon">{intl l="Ok"}</button> <input id="coupon" class="form-control" type="text" name="{$name}" value="{$value}" placeholder="{intl l='Coupon code'}">
</span> <span class="input-group-btn">
</div><!-- /input-group --> <button type="submit" class="btn btn-coupon">{intl l="Ok"}</button>
</span>
{if $error}{$message}{/if}
</div>
{/form_field}
<!-- /input-group -->
</td> </td>
</tr> </tr>
<tr > <tr >
@@ -155,7 +160,15 @@
</tr> </tr>
</tfoot> </tfoot>
</table> </table>
</form>
{/form}
{form name="thelia.order.payment"}
<form id="form-cart-payment" action="{url path="/order/invoice"}" method="post" role="form" {form_enctype form=$form}>
{form_hidden_fields form=$form}
{if $form_error}<div class="alert alert-danger">{$form_error_message}</div>{/if}
<div id="cart-address"> <div id="cart-address">
<div class="panel"> <div class="panel">
{loop type="address" name="delivery-address" id={order attr="delivery_address"}} {loop type="address" name="delivery-address" id={order attr="delivery_address"}}