From 5abe350b65e62a4327258dc19cf4a0164fc6389f Mon Sep 17 00:00:00 2001 From: gmorel Date: Fri, 27 Sep 2013 14:52:38 +0200 Subject: [PATCH 01/11] WIP : Coupon : Fix condition selection JS + Dependency Injection register --- .../Thelia/Controller/Admin/CouponController.php | 15 ++++++--------- core/lib/Thelia/Core/Bundle/TheliaBundle.php | 6 ++---- ...lePass.php => RegisterCouponConditionPass.php} | 10 ++++++---- core/lib/Thelia/Coupon/CouponManager.php | 2 +- core/lib/Thelia/Form/CouponCreationForm.php | 9 +++++---- templates/admin/default/assets/js/coupon.js | 12 ++++++------ templates/admin/default/coupon-update.html | 2 +- templates/admin/default/coupon/form.html | 2 -- 8 files changed, 27 insertions(+), 31 deletions(-) rename core/lib/Thelia/Core/DependencyInjection/Compiler/{RegisterRulePass.php => RegisterCouponConditionPass.php} (92%) diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index 0ba2e961b..0bf2a4a39 100755 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -208,10 +208,7 @@ class CouponController extends BaseAdminController $conditions = $conditionFactory->unserializeConditionCollection( $coupon->getSerializedConditions() ); -var_dump($coupon->getIsEnabled());; -var_dump($coupon->getIsAvailableOnSpecialOffers());; -var_dump($coupon->getIsCumulative());; -var_dump($coupon->getIsRemovingPostage());; + $data = array( 'code' => $coupon->getCode(), 'title' => $coupon->getTitle(), @@ -219,11 +216,11 @@ var_dump($coupon->getIsRemovingPostage());; 'type' => $coupon->getType(), 'shortDescription' => $coupon->getShortDescription(), 'description' => $coupon->getDescription(), - 'isEnabled' => ($coupon->getIsEnabled() == 1), + 'isEnabled' => $coupon->getIsEnabled(), 'expirationDate' => $coupon->getExpirationDate('Y-m-d'), - 'isAvailableOnSpecialOffers' => ($coupon->getIsAvailableOnSpecialOffers() == 1), - 'isCumulative' => ($coupon->getIsCumulative() == 1), - 'isRemovingPostage' => ($coupon->getIsRemovingPostage() == 1), + 'isAvailableOnSpecialOffers' => $coupon->getIsAvailableOnSpecialOffers(), + 'isCumulative' => $coupon->getIsCumulative(), + 'isRemovingPostage' => $coupon->getIsRemovingPostage(), 'maxUsage' => $coupon->getMaxUsage(), 'conditions' => $conditions, 'locale' => $coupon->getLocale(), @@ -264,7 +261,7 @@ var_dump($coupon->getIsRemovingPostage());; Router::ABSOLUTE_URL ); - $args['formAction'] = 'admin/coupon/update' . $couponId; + $args['formAction'] = 'admin/coupon/update/' . $couponId; return $this->render('coupon-update', $args); } diff --git a/core/lib/Thelia/Core/Bundle/TheliaBundle.php b/core/lib/Thelia/Core/Bundle/TheliaBundle.php index a9704b350..dcdcdd5d2 100755 --- a/core/lib/Thelia/Core/Bundle/TheliaBundle.php +++ b/core/lib/Thelia/Core/Bundle/TheliaBundle.php @@ -30,7 +30,7 @@ use Thelia\Core\DependencyInjection\Compiler\RegisterCouponPass; use Thelia\Core\DependencyInjection\Compiler\RegisterListenersPass; use Thelia\Core\DependencyInjection\Compiler\RegisterParserPluginPass; use Thelia\Core\DependencyInjection\Compiler\RegisterRouterPass; -use Thelia\Core\DependencyInjection\Compiler\RegisterRulePass; +use Thelia\Core\DependencyInjection\Compiler\RegisterCouponConditionPass; /** * First Bundle use in Thelia @@ -63,8 +63,6 @@ class TheliaBundle extends Bundle ->addCompilerPass(new RegisterParserPluginPass()) ->addCompilerPass(new RegisterRouterPass()) ->addCompilerPass(new RegisterCouponPass()) - ->addCompilerPass(new RegisterRulePass()) - ; - + ->addCompilerPass(new RegisterCouponConditionPass()); } } diff --git a/core/lib/Thelia/Core/DependencyInjection/Compiler/RegisterRulePass.php b/core/lib/Thelia/Core/DependencyInjection/Compiler/RegisterCouponConditionPass.php similarity index 92% rename from core/lib/Thelia/Core/DependencyInjection/Compiler/RegisterRulePass.php rename to core/lib/Thelia/Core/DependencyInjection/Compiler/RegisterCouponConditionPass.php index dcc54cf8e..7caf79c5e 100755 --- a/core/lib/Thelia/Core/DependencyInjection/Compiler/RegisterRulePass.php +++ b/core/lib/Thelia/Core/DependencyInjection/Compiler/RegisterCouponConditionPass.php @@ -35,11 +35,13 @@ use Symfony\Component\DependencyInjection\Reference; * Class RegisterListenersPass * Source code come from Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RegisterKernelListenersPass class * + * Register all available Conditions for the coupon module + * * @package Thelia\Core\DependencyInjection\Compiler * @author Guillaume MOREL * */ -class RegisterRulePass implements CompilerPassInterface +class RegisterCouponConditionPass implements CompilerPassInterface { /** * You can modify the container here before it is dumped to PHP code. @@ -55,11 +57,11 @@ class RegisterRulePass implements CompilerPassInterface } $couponManager = $container->getDefinition('thelia.coupon.manager'); - $services = $container->findTaggedServiceIds("thelia.coupon.addRule"); + $services = $container->findTaggedServiceIds("thelia.coupon.addCondition"); - foreach ($services as $id => $rule) { + foreach ($services as $id => $condition) { $couponManager->addMethodCall( - 'addAvailableRule', + 'addAvailableCondition', array( new Reference($id) ) diff --git a/core/lib/Thelia/Coupon/CouponManager.php b/core/lib/Thelia/Coupon/CouponManager.php index c4ba661be..09e5a30e8 100644 --- a/core/lib/Thelia/Coupon/CouponManager.php +++ b/core/lib/Thelia/Coupon/CouponManager.php @@ -241,7 +241,7 @@ class CouponManager * * @param ConditionManagerInterface $condition ConditionManagerInterface */ - public function addAvailableRule(ConditionManagerInterface $condition) + public function addAvailableCondition(ConditionManagerInterface $condition) { $this->availableConditions[] = $condition; } diff --git a/core/lib/Thelia/Form/CouponCreationForm.php b/core/lib/Thelia/Form/CouponCreationForm.php index f3ee0df63..fcb2e793c 100755 --- a/core/lib/Thelia/Form/CouponCreationForm.php +++ b/core/lib/Thelia/Form/CouponCreationForm.php @@ -28,6 +28,7 @@ use Symfony\Component\Validator\Constraints\DateTime; use Symfony\Component\Validator\Constraints\GreaterThanOrEqual; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotEqualTo; +use Symfony\Component\Validator\Constraints\Range; /** * Created by JetBrains PhpStorm. @@ -110,7 +111,7 @@ class CouponCreationForm extends BaseForm ) ->add( 'isEnabled', - 'checkbox', + 'text', array() ) ->add( @@ -125,17 +126,17 @@ class CouponCreationForm extends BaseForm ) ->add( 'isCumulative', - 'checkbox', + 'text', array() ) ->add( 'isRemovingPostage', - 'checkbox', + 'text', array() ) ->add( 'isAvailableOnSpecialOffers', - 'checkbox', + 'text', array() ) ->add( diff --git a/templates/admin/default/assets/js/coupon.js b/templates/admin/default/assets/js/coupon.js index 68cfb42e9..de36414bd 100644 --- a/templates/admin/default/assets/js/coupon.js +++ b/templates/admin/default/assets/js/coupon.js @@ -55,7 +55,7 @@ $(function($){ $.couponManager.conditionToSave = $.couponManager.conditionsToSave[id]; // Set the condition selector - $("#category-rule option").filter(function() { + $("#category-condition option").filter(function() { return $(this).val() == $.couponManager.conditionToSave.serviceId; }).prop('selected', true); @@ -90,7 +90,7 @@ $(function($){ // Save conditions on click $.couponManager.onClickSaveCondition = function() { $('#constraint-save-btn').on('click', function () { - if($('#category-rule').val() == 'thelia.condition.match_for_everyone') { + if($('#category-condition').val() == 'thelia.condition.match_for_everyone') { // // @todo translate message + put it in modal var r = confirm("Do you really want to set this coupon available to everyone ?"); if (r == true) { @@ -140,7 +140,7 @@ $(function($){ // Reload condition inputs when changing effect $.couponManager.onConditionChange = function() { - $('#category-rule').on('change', function () { + $('#category-condition').on('change', function () { $.couponManager.loadConditionInputs($(this).val(), function() {}); }); }; @@ -152,9 +152,9 @@ $(function($){ // Set max usage to unlimited or not $.couponManager.onUsageUnlimitedChange = function() { - var isUnlimited = $('#is-unlimited'); + var $isUnlimited = $('#is-unlimited'); if ($('#max-usage').val() == -1) { - isUnlimited.prop('checked', true); + $isUnlimited.prop('checked', true); $('#max-usage').hide(); $('#max-usage-label').hide(); } else { @@ -163,7 +163,7 @@ $(function($){ $('#max-usage-label').show(); } - isUnlimited.change(function(){ + $isUnlimited.change(function(){ var $this = $(this); if ($this.is(':checked')) { $('#max-usage').hide().val('-1'); diff --git a/templates/admin/default/coupon-update.html b/templates/admin/default/coupon-update.html index be9354362..97a10f122 100755 --- a/templates/admin/default/coupon-update.html +++ b/templates/admin/default/coupon-update.html @@ -43,7 +43,7 @@ {/javascripts} - {**} - {**} + {/javascripts} + -//Curabitur quis augue feugiat, ullamcorper mauris ac, interdum mi. Quisque aliquam lorem vitae felis lobortis, id interdum turpis mattis. Vestibulum diam massa, ornare congue blandit quis, facilisis at nisl. In tortor metus, venenatis non arcu nec, sollicitudin ornare nisl. Nunc erat risus, varius nec urna at, iaculis lacinia elit. Aenean ut felis tempus, tincidunt odio non, sagittis nisl. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec vitae hendrerit elit. Nunc sit amet gravida risus, euismod lobortis massa. Nam a erat mauris. Nam a malesuada lorem. Nulla id accumsan dolor, sed rhoncus tellus. Quisque dictum felis sed leo auctor, at volutpat lectus viverra. Morbi rutrum, est ac aliquam imperdiet, nibh sem sagittis justo, ac mattis magna lacus eu nulla. -// -//Duis interdum lectus nulla, nec pellentesque sapien condimentum at. Suspendisse potenti. Sed eu purus tellus. Nunc quis rhoncus metus. Fusce vitae tellus enim. Interdum et malesuada fames ac ante ipsum primis in faucibus. Etiam tempor porttitor erat vitae iaculis. Sed est elit, consequat non ornare vitae, vehicula eget lectus. Etiam consequat sapien mauris, eget consectetur magna imperdiet eget. Nunc sollicitudin luctus velit, in commodo nulla adipiscing fermentum. Fusce nisi sapien, posuere vitae metus sit amet, facilisis sollicitudin dui. Fusce ultricies auctor enim sit amet iaculis. Morbi at vestibulum enim, eget adipiscing eros. -// -//Praesent ligula lorem, faucibus ut metus quis, fermentum iaculis erat. Pellentesque elit erat, lacinia sed semper ac, sagittis vel elit. Nam eu convallis est. Curabitur rhoncus odio vitae consectetur pellentesque. Nam vitae arcu nec ante scelerisque dignissim vel nec neque. Suspendisse augue nulla, mollis eget dui et, tempor facilisis erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ac diam ipsum. Donec convallis dui ultricies velit auctor, non lobortis nulla ultrices. Morbi vitae dignissim ante, sit amet lobortis tortor. Nunc dapibus condimentum augue, in molestie neque congue non. -// -//Sed facilisis pellentesque nisl, eu tincidunt erat scelerisque a. Nullam malesuada tortor vel erat volutpat tincidunt. In vehicula diam est, a convallis eros scelerisque ut. Donec aliquet venenatis iaculis. Ut a arcu gravida, placerat dui eu, iaculis nisl. Quisque adipiscing orci sit amet dui dignissim lacinia. Sed vulputate lorem non dolor adipiscing ornare. Morbi ornare id nisl id aliquam. Ut fringilla elit ante, nec lacinia enim fermentum sit amet. Aenean rutrum lorem eu convallis pharetra. Cras malesuada varius metus, vitae gravida velit. Nam a varius ipsum, ac commodo dolor. Phasellus nec elementum elit. Etiam vel adipiscing leo.'; -// -// /** -// * Sets up the fixture, for example, opens a network connection. -// * This method is called before a test is executed. -// */ -// protected function setUp() -// { -// } -// -// /** -// * Test getDiscount() behaviour -// * Entering : 1 valid Coupon (If 40 < total amount 400) - 10euros -// * -// * @covers Thelia\Coupon\CouponManager::getDiscount -// */ -// public function testGetDiscountOneCoupon() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// /** @var CouponInterface $coupon */ -// $coupon = self::generateValidCoupon(); -// -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter(array($coupon), $cartTotalPrice, $checkoutTotalPrice); -// -// $couponManager = new CouponManager($stubCouponBaseAdapter); -// $discount = $couponManager->getDiscount(); -// -// $expected = 10.00; -// $actual = $discount; -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test getDiscount() behaviour -// * Entering : 1 valid Coupon (If 40 < total amount 400) - 10euros -// * 1 valid Coupon (If total amount > 20) - 15euros -// * -// * @covers Thelia\Coupon\CouponManager::getDiscount -// */ -// public function testGetDiscountTwoCoupon() -// { -// $adapter = new BaseFacade(); -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// /** @var CouponInterface $coupon1 */ -// $coupon1 = self::generateValidCoupon(); -// $rule1 = new AvailableForTotalAmount( -// $adapter, array( -// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( -// Operators::SUPERIOR, -// new PriceParam( -// $adapter, 40.00, 'EUR' -// ) -// ) -// ) -// ); -// $rules = new ConditionCollection(array($rule1)); -// /** @var CouponInterface $coupon2 */ -// $coupon2 = $this->generateValidCoupon('XMAS2', null, null, null, 15.00, null, null, $rules); -// -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter(array($coupon1, $coupon2), $cartTotalPrice, $checkoutTotalPrice); -// -// $couponManager = new CouponManager($stubCouponBaseAdapter); -// $discount = $couponManager->getDiscount(); -// -// $expected = 25.00; -// $actual = $discount; -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test getDiscount() behaviour -// * For a Cart of 21euros -// * Entering : 1 valid Coupon (If total amount > 20) - 30euros -// * -// * @covers Thelia\Coupon\CouponManager::getDiscount -// */ -// public function testGetDiscountAlwaysInferiorToPrice() -// { -// $adapter = new BaseFacade(); -// $cartTotalPrice = 21.00; -// $checkoutTotalPrice = 26.00; -// -// $rule1 = new AvailableForTotalAmount( -// $adapter, array( -// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( -// Operators::SUPERIOR, -// new PriceParam( -// $adapter, 20.00, 'EUR' -// ) -// ) -// ) -// ); -// $rules = new ConditionCollection(array($rule1)); -// /** @var CouponInterface $coupon */ -// $coupon = $this->generateValidCoupon('XMAS2', null, null, null, 30.00, null, null, $rules); -// -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter(array($coupon), $cartTotalPrice, $checkoutTotalPrice); -// -// $couponManager = new CouponManager($stubCouponBaseAdapter); -// $discount = $couponManager->getDiscount(); -// -// $expected = 21.00; -// $actual = $discount; -// $this->assertEquals($expected, $actual); -// } -// -// -// /** -// * Check if removing postage on discout is working -// * @covers Thelia\Coupon\CouponManager::isCouponRemovingPostage -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testIsCouponRemovingPostage() -// { -// $adapter = new BaseFacade(); -// $cartTotalPrice = 21.00; -// $checkoutTotalPrice = 27.00; -// -// $rule1 = new AvailableForTotalAmount( -// $adapter, array( -// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( -// Operators::SUPERIOR, -// new PriceParam( -// $adapter, 20.00, 'EUR' -// ) -// ) -// ) -// ); -// $rules = new ConditionCollection(array($rule1)); -// /** @var CouponInterface $coupon */ -// $coupon = $this->generateValidCoupon('XMAS2', null, null, null, 30.00, null, null, $rules, null, true); -// -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter(array($coupon), $cartTotalPrice, $checkoutTotalPrice); -// -// $couponManager = new CouponManager($stubCouponBaseAdapter); -// $discount = $couponManager->getDiscount(); -// -// $expected = 21.00; -// $actual = $discount; -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Testing how multiple Coupon behaviour -// * Entering 1 Coupon not cumulative -// * -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testCouponCumulationOneCouponNotCumulative() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// // Given -// /** @var CouponInterface $coupon */ -// $couponCumulative1 = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false); -// -// $coupons = array($couponCumulative1); -// -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); -// -// // When -// $sortedCoupons = PhpUnitUtils::callMethod( -// new CouponManager($stubCouponBaseAdapter), -// 'sortCoupons', -// array($coupons) -// ); -// -// // Then -// $expected = $coupons; -// $actual = $sortedCoupons; -// -// $this->assertSame($expected, $actual, 'Array Sorted despite there is only once'); -// } -// -// /** -// * Testing how multiple Coupon behaviour -// * Entering 1 Coupon cumulative -// * -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testCouponCumulationOneCouponCumulative() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// // Given -// /** @var CouponInterface $coupon */ -// $couponCumulative1 = $this->generateValidCoupon(null, null, null, null, null, null, null, null, true); -// -// $coupons = array($couponCumulative1); -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); -// -// // When -// $sortedCoupons = PhpUnitUtils::callMethod( -// new CouponManager($stubCouponBaseAdapter), -// 'sortCoupons', -// array($coupons) -// ); -// -// // Then -// $expected = $coupons; -// $actual = $sortedCoupons; -// -// $this->assertSame($expected, $actual, 'Array Sorted despite there is only once'); -// } -// -// /** -// * Testing how multiple Coupon behaviour -// * Entering 1 Coupon cumulative -// * 1 Coupon cumulative -// * -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testCouponCumulationTwoCouponCumulative() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// // Given -// /** @var CouponInterface $coupon */ -// $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, true); -// $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, true); -// -// $coupons = array($couponCumulative1, $couponCumulative2); -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); -// -// // When -// $sortedCoupons = PhpUnitUtils::callMethod( -// new CouponManager($stubCouponBaseAdapter), -// 'sortCoupons', -// array($coupons) -// ); -// -// // Then -// $expected = $coupons; -// $actual = $sortedCoupons; -// -// $this->assertSame($expected, $actual, 'Array Sorted despite both Coupon can be accumulated'); -// } -// -// /** -// * Testing how multiple Coupon behaviour -// * Entering 1 Coupon cumulative -// * 1 Coupon non cumulative -// * -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testCouponCumulationOneCouponCumulativeOneNonCumulative() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// // Given -// /** @var CouponInterface $coupon */ -// $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, true); -// $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, false); -// -// $coupons = array($couponCumulative1, $couponCumulative2); -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); -// -// // When -// $sortedCoupons = PhpUnitUtils::callMethod( -// new CouponManager($stubCouponBaseAdapter), -// 'sortCoupons', -// array($coupons) -// ); -// -// // Then -// $expected = array($couponCumulative2); -// $actual = $sortedCoupons; -// -// $this->assertSame($expected, $actual, 'Array Sorted despite both Coupon can be accumulated'); -// } -// -// /** -// * Testing how multiple Coupon behaviour -// * Entering 1 Coupon non cumulative -// * 1 Coupon cumulative -// * -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testCouponCumulationOneCouponNonCumulativeOneCumulative() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// // Given -// /** @var CouponInterface $coupon */ -// $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, false); -// $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, true); -// -// $coupons = array($couponCumulative1, $couponCumulative2); -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); -// -// // When -// $sortedCoupons = PhpUnitUtils::callMethod( -// new CouponManager($stubCouponBaseAdapter), -// 'sortCoupons', -// array($coupons) -// ); -// -// // Then -// $expected = array($couponCumulative2); -// $actual = $sortedCoupons; -// -// $this->assertSame($expected, $actual, 'Array Sorted despite both Coupon can be accumulated'); -// } -// -// /** -// * Testing how multiple Coupon behaviour -// * Entering 1 Coupon non cumulative -// * 1 Coupon non cumulative -// * -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testCouponCumulationTwoCouponNonCumulative() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// // Given -// /** @var CouponInterface $coupon */ -// $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, false); -// $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, false); -// -// $coupons = array($couponCumulative1, $couponCumulative2); -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); -// -// // When -// $sortedCoupons = PhpUnitUtils::callMethod( -// new CouponManager($stubCouponBaseAdapter), -// 'sortCoupons', -// array($coupons) -// ); -// -// // Then -// $expected = array($couponCumulative2); -// $actual = $sortedCoupons; -// -// $this->assertSame($expected, $actual, 'Array Sorted despite both Coupon can be accumulated'); -// } -// -// /** -// * Testing how multiple Coupon behaviour -// * Entering 1 Coupon cumulative expired -// * -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testCouponCumulationOneCouponCumulativeExpired() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// // Given -// /** @var CouponInterface $coupon */ -// $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, new \DateTime(), null, true); -// -// $coupons = array($couponCumulative1); -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); -// -// // When -// $sortedCoupons = PhpUnitUtils::callMethod( -// new CouponManager($stubCouponBaseAdapter), -// 'sortCoupons', -// array($coupons) -// ); -// -// // Then -// $expected = array(); -// $actual = $sortedCoupons; -// -// $this->assertSame($expected, $actual, 'Coupon expired ignored'); -// } -// -// /** -// * Testing how multiple Coupon behaviour -// * Entering 1 Coupon cumulative expired -// * 1 Coupon cumulative expired -// * -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testCouponCumulationTwoCouponCumulativeExpired() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// // Given -// /** @var CouponInterface $coupon */ -// $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, new \DateTime(), null, true); -// $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, new \DateTime(), null, true); -// -// $coupons = array($couponCumulative1, $couponCumulative2); -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); -// -// // When -// $sortedCoupons = PhpUnitUtils::callMethod( -// new CouponManager($stubCouponBaseAdapter), -// 'sortCoupons', -// array($coupons) -// ); -// -// // Then -// $expected = array(); -// $actual = $sortedCoupons; -// -// $this->assertSame($expected, $actual, 'Coupon expired ignored'); -// } -// -// /** -// * Testing how multiple Coupon behaviour -// * Entering 1 Coupon cumulative expired -// * 1 Coupon cumulative valid -// * -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testCouponCumulationOneCouponCumulativeExpiredOneNonExpired() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// // Given -// /** @var CouponInterface $coupon */ -// $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, new \DateTime(), null, true); -// $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, true); -// -// $coupons = array($couponCumulative1, $couponCumulative2); -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); -// -// // When -// $sortedCoupons = PhpUnitUtils::callMethod( -// new CouponManager($stubCouponBaseAdapter), -// 'sortCoupons', -// array($coupons) -// ); -// -// // Then -// $expected = array($couponCumulative2); -// $actual = $sortedCoupons; -// -// $this->assertSame($expected, $actual, 'Coupon expired ignored'); -// } -// -// /** -// * Testing how multiple Coupon behaviour -// * Entering 1 Coupon cumulative valid -// * 1 Coupon cumulative expired -// * -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testCouponCumulationOneCouponCumulativeNonExpiredOneExpired() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// // Given -// /** @var CouponInterface $coupon */ -// $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, true); -// $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, new \DateTime(), null, true); -// -// $coupons = array($couponCumulative1, $couponCumulative2); -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); -// -// // When -// $sortedCoupons = PhpUnitUtils::callMethod( -// new CouponManager($stubCouponBaseAdapter), -// 'sortCoupons', -// array($coupons) -// ); -// -// // Then -// $expected = array($couponCumulative1); -// $actual = $sortedCoupons; -// -// $this->assertSame($expected, $actual, 'Coupon expired ignored'); -// } -// -// /** -// * Testing how multiple Coupon behaviour -// * Entering 1 Coupon cumulative valid -// * 1 Coupon cumulative valid -// * 1 Coupon cumulative valid -// * 1 Coupon cumulative valid -// * -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testCouponCumulationFourCouponCumulative() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// // Given -// /** @var CouponInterface $coupon */ -// $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, true); -// $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, true); -// $couponCumulative3 = $this->generateValidCoupon('XMAS3', null, null, null, null, null, null, null, true); -// $couponCumulative4 = $this->generateValidCoupon('XMAS4', null, null, null, null, null, null, null, true); -// -// $coupons = array($couponCumulative1, $couponCumulative2, $couponCumulative3, $couponCumulative4); -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); -// -// // When -// $sortedCoupons = PhpUnitUtils::callMethod( -// new CouponManager($stubCouponBaseAdapter), -// 'sortCoupons', -// array($coupons) -// ); -// -// // Then -// $expected = $coupons; -// $actual = $sortedCoupons; -// -// $this->assertSame($expected, $actual, 'Coupon cumulative ignored'); -// } -// -// /** -// * Testing how multiple Coupon behaviour -// * Entering 1 Coupon cumulative valid -// * 1 Coupon cumulative valid -// * 1 Coupon cumulative valid -// * 1 Coupon non cumulative valid -// * -// * @covers Thelia\Coupon\CouponManager::sortCoupons -// */ -// public function testCouponCumulationThreeCouponCumulativeOneNonCumulative() -// { -// $cartTotalPrice = 100.00; -// $checkoutTotalPrice = 120.00; -// -// // Given -// /** @var CouponInterface $coupon */ -// $couponCumulative1 = $this->generateValidCoupon('XMAS1', null, null, null, null, null, null, null, true); -// $couponCumulative2 = $this->generateValidCoupon('XMAS2', null, null, null, null, null, null, null, true); -// $couponCumulative3 = $this->generateValidCoupon('XMAS3', null, null, null, null, null, null, null, true); -// $couponCumulative4 = $this->generateValidCoupon('XMAS4', null, null, null, null, null, null, null, false); -// -// $coupons = array($couponCumulative1, $couponCumulative2, $couponCumulative3, $couponCumulative4); -// /** @var FacadeInterface $stubCouponBaseAdapter */ -// $stubCouponBaseAdapter = $this->generateFakeAdapter($coupons, $cartTotalPrice, $checkoutTotalPrice); -// -// // When -// $sortedCoupons = PhpUnitUtils::callMethod( -// new CouponManager($stubCouponBaseAdapter), -// 'sortCoupons', -// array($coupons) -// ); -// -// // Then -// $expected = array($couponCumulative4); -// $actual = $sortedCoupons; -// -// $this->assertSame($expected, $actual, 'Coupon cumulative ignored'); -// } -// -// -// /** -// * Generate valid CouponRuleInterfaces -// * -// * @return array Array of ConditionManagerInterface -// */ -// public static function generateValidRules() -// { -// $adapter = new BaseFacade(); -// $rule1 = new AvailableForTotalAmount( -// $adapter, array( -// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( -// Operators::SUPERIOR, -// new PriceParam( -// $adapter, 40.00, 'EUR' -// ) -// ) -// ) -// ); -// $rule2 = new AvailableForTotalAmount( -// $adapter, array( -// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( -// Operators::INFERIOR, -// new PriceParam( -// $adapter, 400.00, 'EUR' -// ) -// ) -// ) -// ); -// $rules = new ConditionCollection(array($rule1, $rule2)); -// -// return $rules; -// } -// -// /** -// * Tears down the fixture, for example, closes a network connection. -// * This method is called after a test is executed. -// */ -// protected function tearDown() -// { -// } -// -// /** -// * Generate a fake Adapter -// * -// * @param array $coupons Coupons -// * @param float $cartTotalPrice Cart total price -// * @param float $checkoutTotalPrice Checkout total price -// * @param float $postagePrice Checkout postage price -// * -// * @return \PHPUnit_Framework_MockObject_MockObject -// */ -// public function generateFakeAdapter(array $coupons, $cartTotalPrice, $checkoutTotalPrice, $postagePrice = 6.00) -// { -// $stubCouponBaseAdapter = $this->getMock( -// 'Thelia\Coupon\BaseFacade', -// array( -// 'getCurrentCoupons', -// 'getCartTotalPrice', -// 'getCheckoutTotalPrice', -// 'getCheckoutPostagePrice' -// ), -// array() -// ); -// -// $stubCouponBaseAdapter->expects($this->any()) -// ->method('getCurrentCoupons') -// ->will($this->returnValue(($coupons))); -// -// // Return Cart product amount = $cartTotalPrice euros -// $stubCouponBaseAdapter->expects($this->any()) -// ->method('getCartTotalPrice') -// ->will($this->returnValue($cartTotalPrice)); -// -// // Return Checkout amount = $checkoutTotalPrice euros -// $stubCouponBaseAdapter->expects($this->any()) -// ->method('getCheckoutTotalPrice') -// ->will($this->returnValue($checkoutTotalPrice)); -// -// $stubCouponBaseAdapter->expects($this->any()) -// ->method('getCheckoutPostagePrice') -// ->will($this->returnValue($postagePrice)); -// -// return $stubCouponBaseAdapter; -// } -// -// /** -// * Generate valid CouponInterface -// * -// * @param string $code Coupon Code -// * @param string $title Coupon Title -// * @param string $shortDescription Coupon short -// * description -// * @param string $description Coupon description -// * @param float $amount Coupon discount -// * @param bool $isEnabled Is Coupon enabled -// * @param \DateTime $expirationDate Coupon expiration date -// * @param ConditionCollection $rules Coupon rules -// * @param bool $isCumulative If is cumulative -// * @param bool $isRemovingPostage If is removing postage -// * @param bool $isAvailableOnSpecialOffers If is available on -// * special offers or not -// * @param int $maxUsage How many time a Coupon -// * can be used -// * -// * @return CouponInterface -// */ -// public static function generateValidCoupon( -// $code = null, -// $title = null, -// $shortDescription = null, -// $description = null, -// $amount = null, -// $isEnabled = null, -// $expirationDate = null, -// $rules = null, -// $isCumulative = null, -// $isRemovingPostage = null, -// $isAvailableOnSpecialOffers = null, -// $maxUsage = null -// ) { -// $adapter = new BaseFacade(); -// if ($code === null) { -// $code = self::VALID_CODE; -// } -// if ($title === null) { -// $title = self::VALID_TITLE; -// } -// if ($shortDescription === null) { -// $shortDescription = self::VALID_SHORT_DESCRIPTION; -// } -// if ($description === null) { -// $description = self::VALID_DESCRIPTION; -// } -// if ($amount === null) { -// $amount = 10.00; -// } -// if ($isEnabled === null) { -// $isEnabled = true; -// } -// if ($isCumulative === null) { -// $isCumulative = true; -// } -// if ($isRemovingPostage === null) { -// $isRemovingPostage = false; -// } -// if ($isAvailableOnSpecialOffers === null) { -// $isAvailableOnSpecialOffers = true; -// } -// if ($maxUsage === null) { -// $maxUsage = 40; -// } -// -// if ($expirationDate === null) { -// $expirationDate = new \DateTime(); -// $expirationDate->setTimestamp(strtotime("today + 2 months")); -// } -// -// $coupon = new RemoveXAmount($adapter, $code, $title, $shortDescription, $description, $amount, $isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate); -// -// if ($rules === null) { -// $rules = self::generateValidRules(); -// } -// -// $coupon->setRules($rules); -// -// return $coupon; -// } } diff --git a/core/lib/Thelia/Tests/Coupon/CouponRuleCollectionTest.php b/core/lib/Thelia/Tests/Coupon/CouponRuleCollectionTest.php index 2b19ba130..0745e9b98 100644 --- a/core/lib/Thelia/Tests/Coupon/CouponRuleCollectionTest.php +++ b/core/lib/Thelia/Tests/Coupon/CouponRuleCollectionTest.php @@ -23,11 +23,6 @@ namespace Thelia\Coupon; -use Thelia\Constraint\Validator\PriceParam; -use Thelia\Constraint\Validator\RuleValidator; -use Thelia\Constraint\Rule\AvailableForTotalAmount; -use Thelia\Constraint\Rule\Operators; - /** * Created by JetBrains PhpStorm. * Date: 8/19/13 @@ -48,39 +43,4 @@ class CouponRuleCollectionTest extends \PHPUnit_Framework_TestCase 'This test has not been implemented yet.' ); } -// /** -// * -// */ -// public function testRuleSerialisation() -// { -//// $rule1 = new AvailableForTotalAmount( -//// , array( -//// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( -//// Operators::SUPERIOR, -//// new PriceParam( -//// , 40.00, 'EUR' -//// ) -//// ) -//// ) -//// ); -//// $rule2 = new AvailableForTotalAmount( -//// , array( -//// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( -//// Operators::INFERIOR, -//// new PriceParam( -//// , 400.00, 'EUR' -//// ) -//// ) -//// ) -//// ); -//// $rules = new ConditionCollection(array($rule1, $rule2)); -//// -//// $serializedRules = base64_encode(serialize($rules)); -//// $unserializedRules = unserialize(base64_decode($serializedRules)); -//// -//// $expected = $rules; -//// $actual = $unserializedRules; -//// -//// $this->assertEquals($expected, $actual); -// } } diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php index 80d6d459e..ee8ed3068 100644 --- a/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php +++ b/core/lib/Thelia/Tests/Coupon/Type/RemoveXAmountTest.php @@ -39,341 +39,27 @@ namespace Thelia\Coupon; */ class RemoveXAmountTest extends \PHPUnit_Framework_TestCase { + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + } + public function testSomething() { // Stop here and mark this test as incomplete. $this->markTestIncomplete( - 'This test has not been implemented yet.' + 'This coupon has not been implemented yet.' ); } -// /** -// * Sets up the fixture, for example, opens a network connection. -// * This method is called before a test is executed. -// */ -// protected function setUp() -// { -// -// } -// -// /** -// * Test if a Coupon is well displayed -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::getCode -// * @covers Thelia\Coupon\type\RemoveXAmountManager::getTitle -// * @covers Thelia\Coupon\type\RemoveXAmountManager::getShortDescription -// * @covers Thelia\Coupon\type\RemoveXAmountManager::getDescription -// * -// */ -// public function testDisplay() -// { -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, true, true); -// -// $expected = CouponManagerTest::VALID_CODE; -// $actual = $coupon->getCode(); -// $this->assertEquals($expected, $actual); -// -// $expected = CouponManagerTest::VALID_TITLE; -// $actual = $coupon->getTitle(); -// $this->assertEquals($expected, $actual); -// -// $expected = CouponManagerTest::VALID_SHORT_DESCRIPTION; -// $actual = $coupon->getShortDescription(); -// $this->assertEquals($expected, $actual); -// -// $expected = CouponManagerTest::VALID_DESCRIPTION; -// $actual = $coupon->getDescription(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test if a Coupon can be Cumulative -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::isCumulative -// * -// */ -// public function testIsCumulative() -// { -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, true, true); -// -// $actual = $coupon->isCumulative(); -// $this->assertTrue($actual); -// } -// -// /** -// * Test if a Coupon can be non cumulative -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::isCumulative -// * -// */ -// public function testIsNotCumulative() -// { -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// $actual = $coupon->isCumulative(); -// $this->assertFalse($actual); -// } -// -// -// /** -// * Test if a Coupon can remove postage -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::isRemovingPostage -// * -// */ -// public function testIsRemovingPostage() -// { -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, true, true); -// -// $actual = $coupon->isRemovingPostage(); -// $this->assertTrue($actual); -// } -// -// /** -// * Test if a Coupon won't remove postage if not set to -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::isRemovingPostage -// */ -// public function testIsNotRemovingPostage() -// { -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// $actual = $coupon->isRemovingPostage(); -// $this->assertFalse($actual); -// } -// -// -// /** -// * Test if a Coupon has the effect expected (discount 10euros) -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::getEffect -// */ -// public function testGetEffect() -// { -// $adapter = new BaseFacade(); -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// $expected = 10; -// $actual = $coupon->getDiscount(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test Coupon rule setter -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::setRules -// * @covers Thelia\Coupon\type\RemoveXAmountManager::getRules -// */ -// public function testSetRulesValid() -// { -// // Given -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::EQUAL, -// 20.00 -// ); -// $rule1 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::INFERIOR, -// 100.23 -// ); -// $rule2 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::SUPERIOR, -// 421.23 -// ); -// -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0, $rule1, $rule2))); -// -// // Then -// $expected = 3; -// $this->assertCount($expected, $coupon->getRules()->getRules()); -// } -// -// /** -// * Test Coupon rule setter -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::setRules -// * @expectedException \Thelia\Exception\InvalidConditionException -// * -// */ -// public function testSetRulesInvalid() -// { -// // Given -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::EQUAL, -// 20.00 -// ); -// $rule1 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::INFERIOR, -// 100.23 -// ); -// $rule2 = $this; -// -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0, $rule1, $rule2))); -// } -// -// /** -// * Test Coupon effect for rule Total Amount < 400 -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::getEffect -// * -// */ -// public function testGetEffectIfTotalAmountInferiorTo400Valid() -// { -// // Given -// $adapter = new BaseFacade(); -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::INFERIOR, -// 400.00 -// ); -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0))); -// -// // Then -// $expected = 10.00; -// $actual = $coupon->getDiscount(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test Coupon effect for rule Total Amount <= 400 -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::getEffect -// * -// */ -// public function testGetEffectIfTotalAmountInferiorOrEqualTo400Valid() -// { -// // Given -// $adapter = new BaseFacade(); -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::INFERIOR_OR_EQUAL, -// 400.00 -// ); -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0))); -// -// // Then -// $expected = 10.00; -// $actual = $coupon->getDiscount(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test Coupon effect for rule Total Amount == 400 -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::getEffect -// * -// */ -// public function testGetEffectIfTotalAmountEqualTo400Valid() -// { -// // Given -// $adapter = new BaseFacade(); -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::EQUAL, -// 400.00 -// ); -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0))); -// -// // Then -// $expected = 10.00; -// $actual = $coupon->getDiscount(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test Coupon effect for rule Total Amount >= 400 -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::getEffect -// * -// */ -// public function testGetEffectIfTotalAmountSuperiorOrEqualTo400Valid() -// { -// // Given -// $adapter = new BaseFacade(); -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::SUPERIOR_OR_EQUAL, -// 400.00 -// ); -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0))); -// -// // Then -// $expected = 10.00; -// $actual = $coupon->getDiscount(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test Coupon effect for rule Total Amount > 400 -// * -// * @covers Thelia\Coupon\type\RemoveXAmountManager::getEffect -// * -// */ -// public function testGetEffectIfTotalAmountSuperiorTo400Valid() -// { -// // Given -// $adapter = new BaseFacade(); -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::SUPERIOR, -// 400.00 -// ); -// $coupon = CouponManagerTest::generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0))); -// -// // Then -// $expected = 10.00; -// $actual = $coupon->getDiscount(); -// $this->assertEquals($expected, $actual); -// } -// -// -// -// /** -// * Tears down the fixture, for example, closes a network connection. -// * This method is called after a test is executed. -// */ -// protected function tearDown() -// { -// } -// -// /** -// * Generate valid rule AvailableForTotalAmount -// * according to given operator and amount -// * -// * @param string $operator Operators::CONST -// * @param float $amount Amount with 2 decimals -// * -// * @return AvailableForTotalAmount -// */ -// protected function generateValidRuleAvailableForTotalAmountOperatorTo($operator, $amount) -// { -// $adapter = new BaseFacade(); -// $validators = array( -// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( -// $operator, -// new PriceParam( -// $adapter, -// $amount, -// 'EUR' -// ) -// ) -// ); -// -// return new AvailableForTotalAmount($adapter, $validators); -// } + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } } diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php index c217326ed..bbb0c24a3 100644 --- a/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php +++ b/core/lib/Thelia/Tests/Coupon/Type/RemoveXPercentTest.php @@ -41,412 +41,27 @@ use PHPUnit_Framework_TestCase; class RemoveXPercentTest extends \PHPUnit_Framework_TestCase { + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + } + public function testSomething() { - // Stop here and mark this test as incomplete. $this->markTestIncomplete( - 'This test has not been implemented yet.' + 'This coupon has not been implemented yet.' ); } -// /** -// * Sets up the fixture, for example, opens a network connection. -// * This method is called before a test is executed. -// */ -// protected function setUp() -// { -// } -// -// /** -// * Test if a Coupon can be Cumulative -// * -// * @covers Thelia\Coupon\type\RemoveXPercentManager::isCumulative -// * -// */ -// public function testIsCumulative() -// { -// $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, true, true); -// -// $actual = $coupon->isCumulative(); -// $this->assertTrue($actual); -// } -// -// /** -// * Test if a Coupon can be non cumulative -// * -// * @covers Thelia\Coupon\type\RemoveXPercentManager::isCumulative -// * -// */ -// public function testIsNotCumulative() -// { -// $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// $actual = $coupon->isCumulative(); -// $this->assertFalse($actual); -// } -// -// -// /** -// * Test if a Coupon can remove postage -// * -// * @covers Thelia\Coupon\type\RemoveXPercentManager::isRemovingPostage -// * -// */ -// public function testIsRemovingPostage() -// { -// $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, true, true); -// -// $actual = $coupon->isRemovingPostage(); -// $this->assertTrue($actual); -// } -// -// /** -// * Test if a Coupon won't remove postage if not set to -// * -// * @covers Thelia\Coupon\type\RemoveXPercentManager::isRemovingPostage -// */ -// public function testIsNotRemovingPostage() -// { -// $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// $actual = $coupon->isRemovingPostage(); -// $this->assertFalse($actual); -// } -// -// -// /** -// * Test if a Coupon has the effect expected (discount 10euros) -// * -// * @covers Thelia\Coupon\type\RemoveXPercentManager::getEffect -// */ -// public function testGetEffect() -// { -// $adapter = $this->generateFakeAdapter(245); -// $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// $expected = 24.50; -// $actual = $coupon->getDiscount($adapter); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test Coupon rule setter -// * -// * @covers Thelia\Coupon\type\RemoveXPercentManager::setRules -// * @covers Thelia\Coupon\type\RemoveXPercentManager::getRules -// */ -// public function testSetRulesValid() -// { -// // Given -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::EQUAL, -// 20.00 -// ); -// $rule1 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::INFERIOR, -// 100.23 -// ); -// $rule2 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::SUPERIOR, -// 421.23 -// ); -// -// $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0, $rule1, $rule2))); -// -// // Then -// $expected = 3; -// $this->assertCount($expected, $coupon->getRules()->getRules()); -// } -// -// /** -// * Test Coupon rule setter -// * -// * @covers Thelia\Coupon\type\RemoveXPercentManager::setRules -// * @expectedException \Thelia\Exception\InvalidConditionException -// * -// */ -// public function testSetRulesInvalid() -// { -// // Given -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::EQUAL, -// 20.00 -// ); -// $rule1 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::INFERIOR, -// 100.23 -// ); -// $rule2 = $this; -// -// $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0, $rule1, $rule2))); -// } -// -// /** -// * Test Coupon effect for rule Total Amount < 400 -// * -// * @covers Thelia\Coupon\type\RemoveXPercentManager::getEffect -// * -// */ -// public function testGetEffectIfTotalAmountInferiorTo400Valid() -// { -// // Given -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::INFERIOR, -// 400.00 -// ); -// $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0))); -// -// // Then -// $expected = 24.50; -// $actual = $coupon->getDiscount(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test Coupon effect for rule Total Amount <= 400 -// * -// * @covers Thelia\Coupon\type\RemoveXPercentManager::getEffect -// * -// */ -// public function testGetEffectIfTotalAmountInferiorOrEqualTo400Valid() -// { -// // Given -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::INFERIOR_OR_EQUAL, -// 400.00 -// ); -// $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0))); -// -// // Then -// $expected = 24.50; -// $actual = $coupon->getDiscount(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test Coupon effect for rule Total Amount == 400 -// * -// * @covers Thelia\Coupon\type\RemoveXPercentManager::getEffect -// * -// */ -// public function testGetEffectIfTotalAmountEqualTo400Valid() -// { -// // Given -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::EQUAL, -// 400.00 -// ); -// $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0))); -// -// // Then -// $expected = 24.50; -// $actual = $coupon->getDiscount(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test Coupon effect for rule Total Amount >= 400 -// * -// * @covers Thelia\Coupon\type\RemoveXPercentManager::getEffect -// * -// */ -// public function testGetEffectIfTotalAmountSuperiorOrEqualTo400Valid() -// { -// // Given -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::SUPERIOR_OR_EQUAL, -// 400.00 -// ); -// $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0))); -// -// // Then -// $expected = 24.50; -// $actual = $coupon->getDiscount(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Test Coupon effect for rule Total Amount > 400 -// * -// * @covers Thelia\Coupon\type\RemoveXPercentManager::getEffect -// * -// */ -// public function testGetEffectIfTotalAmountSuperiorTo400Valid() -// { -// // Given -// $rule0 = $this->generateValidRuleAvailableForTotalAmountOperatorTo( -// Operators::SUPERIOR, -// 400.00 -// ); -// $coupon = $this->generateValidCoupon(null, null, null, null, null, null, null, null, false, false); -// -// // When -// $coupon->setRules(new ConditionCollection(array($rule0))); -// -// // Then -// $expected = 24.50; -// $actual = $coupon->getDiscount(); -// $this->assertEquals($expected, $actual); -// } -// -// /** -// * Generate valid CouponInterface -// * -// * @param string $code Coupon Code -// * @param string $title Coupon Title -// * @param string $shortDescription Coupon short -// * description -// * @param string $description Coupon description -// * @param float $amount Coupon discount -// * @param bool $isEnabled Is Coupon enabled -// * @param \DateTime $expirationDate Coupon expiration date -// * @param ConditionCollection $rules Coupon rules -// * @param bool $isCumulative If is cumulative -// * @param bool $isRemovingPostage If is removing postage -// * @param bool $isAvailableOnSpecialOffers If is available on -// * special offers or not -// * @param int $maxUsage How many time a Coupon -// * can be used -// * -// * @return CouponInterface -// */ -// public function generateValidCoupon( -// $code = null, -// $title = null, -// $shortDescription = null, -// $description = null, -// $percent = null, -// $isEnabled = null, -// $expirationDate = null, -// $rules = null, -// $isCumulative = null, -// $isRemovingPostage = null, -// $isAvailableOnSpecialOffers = null, -// $maxUsage = null -// ) { -// $adapter = $this->generateFakeAdapter(245); -// -// if ($code === null) { -// $code = CouponManagerTest::VALID_CODE; -// } -// if ($title === null) { -// $title = CouponManagerTest::VALID_TITLE; -// } -// if ($shortDescription === null) { -// $shortDescription = CouponManagerTest::VALID_SHORT_DESCRIPTION; -// } -// if ($description === null) { -// $description = CouponManagerTest::VALID_DESCRIPTION; -// } -// if ($percent === null) { -// $percent = 10.00; -// } -// if ($isEnabled === null) { -// $isEnabled = true; -// } -// if ($isCumulative === null) { -// $isCumulative = true; -// } -// if ($isRemovingPostage === null) { -// $isRemovingPostage = false; -// } -// if ($isAvailableOnSpecialOffers === null) { -// $isAvailableOnSpecialOffers = true; -// } -// if ($maxUsage === null) { -// $maxUsage = 40; -// } -// -// if ($expirationDate === null) { -// $expirationDate = new \DateTime(); -// $expirationDate->setTimestamp(strtotime("today + 2 months")); -// } -// -// $coupon = new RemoveXPercent($adapter, $code, $title, $shortDescription, $description, $percent, $isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, $expirationDate); -// -// if ($rules === null) { -// $rules = CouponManagerTest::generateValidRules(); -// } -// -// $coupon->setRules($rules); -// -// return $coupon; -// } -// -// -// /** -// * Generate valid rule AvailableForTotalAmount -// * according to given operator and amount -// * -// * @param string $operator Operators::CONST -// * @param float $amount Amount with 2 decimals -// * -// * @return AvailableForTotalAmount -// */ -// protected function generateValidRuleAvailableForTotalAmountOperatorTo($operator, $amount) -// { -// $adapter = new BaseFacade(); -// $validators = array( -// AvailableForTotalAmount::PARAM1_PRICE => new RuleValidator( -// $operator, -// new PriceParam( -// $adapter, -// $amount, -// 'EUR' -// ) -// ) -// ); -// -// return new AvailableForTotalAmount($adapter, $validators); -// } -// -// /** -// * Generate a fake Adapter -// * -// * @param float $cartTotalPrice Cart total price -// * -// * @return \PHPUnit_Framework_MockObject_MockObject -// */ -// public function generateFakeAdapter($cartTotalPrice) -// { -// $stubCouponBaseAdapter = $this->getMock( -// 'Thelia\Coupon\BaseFacade', -// array( -// 'getCartTotalPrice' -// ), -// array() -// ); -// -// $stubCouponBaseAdapter->expects($this->any()) -// ->method('getCartTotalPrice') -// ->will($this->returnValue(($cartTotalPrice))); -// -// return $stubCouponBaseAdapter; -// } -// -// /** -// * Tears down the fixture, for example, closes a network connection. -// * This method is called after a test is executed. -// */ -// protected function tearDown() -// { -// } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + protected function tearDown() + { + } } From 52e471ab1bdfd81783e8c3724231d48646dab4c9 Mon Sep 17 00:00:00 2001 From: gmorel Date: Tue, 22 Oct 2013 23:04:08 +0200 Subject: [PATCH 11/11] Working adding decrementation when coupon is consumed --- core/lib/Thelia/Action/Coupon.php | 7 ++++- .../Controller/Admin/CouponController.php | 3 -- core/lib/Thelia/Coupon/CouponManager.php | 31 +++++++++++++++++++ .../lib/Thelia/Coupon/Type/CouponAbstract.php | 1 - 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/core/lib/Thelia/Action/Coupon.php b/core/lib/Thelia/Action/Coupon.php index b476af6b4..e3a8defbf 100755 --- a/core/lib/Thelia/Action/Coupon.php +++ b/core/lib/Thelia/Action/Coupon.php @@ -35,6 +35,7 @@ use Thelia\Coupon\CouponManager; use Thelia\Coupon\ConditionCollection; use Thelia\Coupon\Type\CouponInterface; use Thelia\Model\Coupon as CouponModel; +use Thelia\Model\CouponQuery; /** * Created by JetBrains PhpStorm. @@ -120,8 +121,12 @@ class Coupon extends BaseAction implements EventSubscriberInterface $request->getSession()->setConsumedCoupons($consumedCoupons); $totalDiscount = $couponManager->getDiscount(); + // @todo insert false product in cart with the name of the coupon and the discount as negative price - // @todo decrement coupon quantity + // Decrement coupon quantity + $couponQuery = CouponQuery::create(); + $couponModel = $couponQuery->findOneByCode($coupon->getCode()); + $couponManager->decrementeQuantity($couponModel); $request->getSession()->getCart()->setDiscount($totalDiscount); diff --git a/core/lib/Thelia/Controller/Admin/CouponController.php b/core/lib/Thelia/Controller/Admin/CouponController.php index 63e9abaef..59ef312d5 100755 --- a/core/lib/Thelia/Controller/Admin/CouponController.php +++ b/core/lib/Thelia/Controller/Admin/CouponController.php @@ -28,9 +28,6 @@ use Symfony\Component\Routing\Router; use Thelia\Condition\ConditionFactory; use Thelia\Condition\ConditionManagerInterface; use Thelia\Core\Security\Resource\AdminResources; -use Thelia\Core\Event\Condition\ConditionCreateOrUpdateEvent; -use Thelia\Core\Event\AdminResources; -use Thelia\Core\Event\Coupon\CouponConsumeEvent; use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Security\AccessManager; diff --git a/core/lib/Thelia/Coupon/CouponManager.php b/core/lib/Thelia/Coupon/CouponManager.php index 0b4a9e9e8..1b446f1e0 100644 --- a/core/lib/Thelia/Coupon/CouponManager.php +++ b/core/lib/Thelia/Coupon/CouponManager.php @@ -26,6 +26,7 @@ namespace Thelia\Coupon; use Symfony\Component\DependencyInjection\ContainerInterface; use Thelia\Condition\ConditionManagerInterface; use Thelia\Coupon\Type\CouponInterface; +use Thelia\Model\Coupon; /** * Created by JetBrains PhpStorm. @@ -228,4 +229,34 @@ class CouponManager { return $this->availableConditions; } + + /** + * Decrement this coupon quantity + * + * To call when a coupon is consumed + * + * @param \Thelia\Model\Coupon $coupon Coupon consumed + * + * @return bool + */ + public function decrementeQuantity(Coupon $coupon) + { + $ret = true; + try { + + $oldMaxUsage = $coupon->getMaxUsage(); + + if ($oldMaxUsage > 0) { + $oldMaxUsage--; + $coupon->setMaxUsage($$oldMaxUsage); + + $coupon->save(); + } + + } catch(\Exception $e) { + $ret = false; + } + + return $ret; + } } \ No newline at end of file diff --git a/core/lib/Thelia/Coupon/Type/CouponAbstract.php b/core/lib/Thelia/Coupon/Type/CouponAbstract.php index 35ce3db4e..d3107faa7 100644 --- a/core/lib/Thelia/Coupon/Type/CouponAbstract.php +++ b/core/lib/Thelia/Coupon/Type/CouponAbstract.php @@ -307,5 +307,4 @@ abstract class CouponAbstract implements CouponInterface return $this->conditionEvaluator->isMatching($this->conditions); } - }