Adding/Refactoring unit test for ConditionFactory/ConditionManagerAbstract
This commit is contained in:
@@ -54,7 +54,7 @@ abstract class ConditionManagerAbstract implements ConditionManagerInterface
|
||||
protected $validators = array();
|
||||
|
||||
/** @var FacadeInterface Provide necessary value from Thelia */
|
||||
protected $adapter = null;
|
||||
protected $facade = null;
|
||||
|
||||
/** @var Translator Service Translator */
|
||||
protected $translator = null;
|
||||
@@ -71,13 +71,13 @@ abstract class ConditionManagerAbstract implements ConditionManagerInterface
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param FacadeInterface $adapter Service adapter
|
||||
* @param FacadeInterface $facade Service Facade
|
||||
*/
|
||||
public function __construct(FacadeInterface $adapter)
|
||||
public function __construct(FacadeInterface $facade)
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
$this->translator = $adapter->getTranslator();
|
||||
$this->conditionValidator = $adapter->getConditionEvaluator();
|
||||
$this->facade = $facade;
|
||||
$this->translator = $facade->getTranslator();
|
||||
$this->conditionValidator = $facade->getConditionEvaluator();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,9 +181,9 @@ abstract class ConditionManagerAbstract implements ConditionManagerInterface
|
||||
* @return bool
|
||||
* @throws \Thelia\Exception\InvalidConditionValueException
|
||||
*/
|
||||
protected function IsCurrencyValid($currencyValue)
|
||||
protected function isCurrencyValid($currencyValue)
|
||||
{
|
||||
$availableCurrencies = $this->adapter->getAvailableCurrencies();
|
||||
$availableCurrencies = $this->facade->getAvailableCurrencies();
|
||||
/** @var Currency $currency */
|
||||
$currencyFound = false;
|
||||
foreach ($availableCurrencies as $currency) {
|
||||
|
||||
@@ -124,7 +124,7 @@ class MatchForTotalAmountManager extends ConditionManagerAbstract
|
||||
$this->isPriceValid($priceValue);
|
||||
|
||||
|
||||
$this->IsCurrencyValid($currencyValue);
|
||||
$this->isCurrencyValid($currencyValue);
|
||||
|
||||
|
||||
$this->operators = array(
|
||||
@@ -160,12 +160,12 @@ class MatchForTotalAmountManager extends ConditionManagerAbstract
|
||||
}
|
||||
|
||||
$condition1 = $this->conditionValidator->variableOpComparison(
|
||||
$this->adapter->getCartTotalPrice(),
|
||||
$this->facade->getCartTotalPrice(),
|
||||
$this->operators[self::INPUT1],
|
||||
$this->values[self::INPUT1]
|
||||
);
|
||||
$condition2 = $this->conditionValidator->variableOpComparison(
|
||||
$this->adapter->getCheckoutCurrency(),
|
||||
$this->facade->getCheckoutCurrency(),
|
||||
$this->operators[self::INPUT2],
|
||||
$this->values[self::INPUT2]
|
||||
);
|
||||
|
||||
@@ -124,7 +124,7 @@ class MatchForXArticlesManager extends ConditionManagerAbstract
|
||||
public function isMatching()
|
||||
{
|
||||
$condition1 = $this->conditionValidator->variableOpComparison(
|
||||
$this->adapter->getNbArticlesInCart(),
|
||||
$this->facade->getNbArticlesInCart(),
|
||||
$this->operators[self::INPUT1],
|
||||
$this->values[self::INPUT1]
|
||||
);
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
|
||||
<!-- Coupon module -->
|
||||
<service id="thelia.adapter" class="Thelia\Coupon\BaseFacade">
|
||||
<service id="thelia.facade" class="Thelia\Coupon\BaseFacade">
|
||||
<argument type="service" id="service_container" />
|
||||
</service>
|
||||
<service id="thelia.coupon.manager" class="Thelia\Coupon\CouponManager">
|
||||
@@ -20,11 +20,11 @@
|
||||
</service>
|
||||
|
||||
<service id="thelia.coupon.type.remove_x_amount" class="Thelia\Coupon\Type\RemoveXAmountManager">
|
||||
<argument type="service" id="thelia.adapter" />
|
||||
<argument type="service" id="thelia.facade" />
|
||||
<tag name="thelia.coupon.addCoupon"/>
|
||||
</service>
|
||||
<service id="thelia.coupon.type.remove_x_percent" class="Thelia\Coupon\Type\RemoveXPercentManager">
|
||||
<argument type="service" id="thelia.adapter" />
|
||||
<argument type="service" id="thelia.facade" />
|
||||
<tag name="thelia.coupon.addCoupon"/>
|
||||
</service>
|
||||
|
||||
@@ -36,17 +36,18 @@
|
||||
<service id="thelia.condition.validator" class="Thelia\Condition\ConditionEvaluator">
|
||||
</service>
|
||||
<service id="thelia.condition.match_for_everyone" class="Thelia\Condition\Implementation\MatchForEveryoneManager">
|
||||
<argument type="service" id="thelia.adapter" />
|
||||
<tag name="thelia.coupon.addCondition"/>
|
||||
</service>
|
||||
<service id="thelia.condition.match_for_x_articles" class="Thelia\Condition\Implementation\MatchForXArticlesManager">
|
||||
<argument type="service" id="thelia.adapter" />
|
||||
<argument type="service" id="thelia.facade" />
|
||||
<tag name="thelia.coupon.addCondition"/>
|
||||
</service>
|
||||
<service id="thelia.condition.match_for_total_amount" class="Thelia\Condition\Implementation\MatchForTotalAmountManager">
|
||||
<argument type="service" id="thelia.adapter" />
|
||||
<argument type="service" id="thelia.facade" />
|
||||
<tag name="thelia.coupon.addCondition"/>
|
||||
</service>
|
||||
<service id="thelia.condition.match_for_x_articles" class="Thelia\Condition\Implementation\MatchForXArticlesManager">
|
||||
<argument type="service" id="thelia.facade" />
|
||||
<tag name="thelia.coupon.addCondition"/>
|
||||
</service>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -387,6 +387,66 @@ class ConditionFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test condition serialization if collection is empty
|
||||
*
|
||||
* @covers Thelia\Condition\ConditionFactory::serializeConditionCollection
|
||||
*/
|
||||
public function testSerializeConditionCollectionEmpty()
|
||||
{
|
||||
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
/** @var FacadeInterface $stubFacade */
|
||||
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$stubFacade->expects($this->any())
|
||||
->method('getTranslator')
|
||||
->will($this->returnValue($stubTranslator));
|
||||
$stubFacade->expects($this->any())
|
||||
->method('getConditionEvaluator')
|
||||
->will($this->returnValue(new ConditionEvaluator()));
|
||||
|
||||
$currencies = CurrencyQuery::create();
|
||||
$currencies = $currencies->find();
|
||||
$stubFacade->expects($this->any())
|
||||
->method('getAvailableCurrencies')
|
||||
->will($this->returnValue($currencies));
|
||||
|
||||
$stubContainer = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$stubContainer->expects($this->any())
|
||||
->method('get')
|
||||
->will($this->returnValue(new MatchForEveryoneManager($stubFacade)));
|
||||
|
||||
$stubContainer->expects($this->any())
|
||||
->method('has')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$stubFacade->expects($this->any())
|
||||
->method('getContainer')
|
||||
->will($this->returnValue($stubContainer));
|
||||
|
||||
|
||||
$conditions = new ConditionCollection();
|
||||
|
||||
$conditionFactory = new ConditionFactory($stubContainer);
|
||||
|
||||
|
||||
$conditionNone = new MatchForEveryoneManager($stubFacade);
|
||||
$expectedCollection = new ConditionCollection();
|
||||
$expectedCollection->add($conditionNone);
|
||||
|
||||
$expected = $conditionFactory->serializeConditionCollection($expectedCollection);
|
||||
$actual = $conditionFactory->serializeConditionCollection($conditions);
|
||||
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the fixture, for example, closes a network connection.
|
||||
* This method is called after a test is executed.
|
||||
|
||||
@@ -24,10 +24,13 @@
|
||||
namespace Thelia\Condition\Implementation;
|
||||
|
||||
use Thelia\Condition\ConditionEvaluator;
|
||||
use Thelia\Condition\ConditionFactory;
|
||||
use Thelia\Condition\Operators;
|
||||
use Thelia\Coupon\ConditionCollection;
|
||||
use Thelia\Coupon\FacadeInterface;
|
||||
use Thelia\Exception\InvalidConditionValueException;
|
||||
use Thelia\Model\Currency;
|
||||
use Thelia\Model\CurrencyQuery;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
@@ -599,6 +602,73 @@ class MatchForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check unknown currency
|
||||
*
|
||||
* @covers Thelia\Condition\ConditionManagerAbstract::isCurrencyValid
|
||||
* @expectedException \Thelia\Exception\InvalidConditionValueException
|
||||
*
|
||||
*/
|
||||
public function testUnknownCurrency()
|
||||
{
|
||||
$stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
/** @var FacadeInterface $stubFacade */
|
||||
$stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$stubFacade->expects($this->any())
|
||||
->method('getTranslator')
|
||||
->will($this->returnValue($stubTranslator));
|
||||
$stubFacade->expects($this->any())
|
||||
->method('getConditionEvaluator')
|
||||
->will($this->returnValue(new ConditionEvaluator()));
|
||||
|
||||
$currencies = CurrencyQuery::create();
|
||||
$currencies = $currencies->find();
|
||||
$stubFacade->expects($this->any())
|
||||
->method('getAvailableCurrencies')
|
||||
->will($this->returnValue($currencies));
|
||||
|
||||
|
||||
$condition1 = new MatchForTotalAmountManager($stubFacade);
|
||||
$operators = array(
|
||||
MatchForTotalAmountManager::INPUT1 => Operators::EQUAL,
|
||||
MatchForTotalAmountManager::INPUT2 => Operators::EQUAL
|
||||
);
|
||||
$values = array(
|
||||
MatchForTotalAmountManager::INPUT1 => 400.00,
|
||||
MatchForTotalAmountManager::INPUT2 => 'UNK');
|
||||
$condition1->setValidatorsFromForm($operators, $values);
|
||||
|
||||
|
||||
$stubContainer = $this->getMockBuilder('\Symfony\Component\DependencyInjection\Container')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$stubContainer->expects($this->any())
|
||||
->method('get')
|
||||
->will($this->returnValue($condition1));
|
||||
|
||||
$stubContainer->expects($this->any())
|
||||
->method('has')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$stubFacade->expects($this->any())
|
||||
->method('getContainer')
|
||||
->will($this->returnValue($stubContainer));
|
||||
|
||||
$conditionFactory = new ConditionFactory($stubContainer);
|
||||
|
||||
$collection = new ConditionCollection();
|
||||
$collection->add($condition1);
|
||||
|
||||
$serialized = $conditionFactory->serializeConditionCollection($collection);
|
||||
$unserialized = $conditionFactory->unserializeConditionCollection($serialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the fixture, for example, closes a network connection.
|
||||
* This method is called after a test is executed.
|
||||
|
||||
Reference in New Issue
Block a user