Adding/Refactoring unit test for ConditionFactory/ConditionManagerAbstract

This commit is contained in:
gmorel
2013-11-16 23:58:49 +01:00
parent 3a4bff634e
commit 5679f54ec0
6 changed files with 152 additions and 21 deletions

View File

@@ -54,7 +54,7 @@ abstract class ConditionManagerAbstract implements ConditionManagerInterface
protected $validators = array(); protected $validators = array();
/** @var FacadeInterface Provide necessary value from Thelia */ /** @var FacadeInterface Provide necessary value from Thelia */
protected $adapter = null; protected $facade = null;
/** @var Translator Service Translator */ /** @var Translator Service Translator */
protected $translator = null; protected $translator = null;
@@ -71,13 +71,13 @@ abstract class ConditionManagerAbstract implements ConditionManagerInterface
/** /**
* Constructor * 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->facade = $facade;
$this->translator = $adapter->getTranslator(); $this->translator = $facade->getTranslator();
$this->conditionValidator = $adapter->getConditionEvaluator(); $this->conditionValidator = $facade->getConditionEvaluator();
} }
/** /**
@@ -181,9 +181,9 @@ abstract class ConditionManagerAbstract implements ConditionManagerInterface
* @return bool * @return bool
* @throws \Thelia\Exception\InvalidConditionValueException * @throws \Thelia\Exception\InvalidConditionValueException
*/ */
protected function IsCurrencyValid($currencyValue) protected function isCurrencyValid($currencyValue)
{ {
$availableCurrencies = $this->adapter->getAvailableCurrencies(); $availableCurrencies = $this->facade->getAvailableCurrencies();
/** @var Currency $currency */ /** @var Currency $currency */
$currencyFound = false; $currencyFound = false;
foreach ($availableCurrencies as $currency) { foreach ($availableCurrencies as $currency) {

View File

@@ -124,7 +124,7 @@ class MatchForTotalAmountManager extends ConditionManagerAbstract
$this->isPriceValid($priceValue); $this->isPriceValid($priceValue);
$this->IsCurrencyValid($currencyValue); $this->isCurrencyValid($currencyValue);
$this->operators = array( $this->operators = array(
@@ -160,12 +160,12 @@ class MatchForTotalAmountManager extends ConditionManagerAbstract
} }
$condition1 = $this->conditionValidator->variableOpComparison( $condition1 = $this->conditionValidator->variableOpComparison(
$this->adapter->getCartTotalPrice(), $this->facade->getCartTotalPrice(),
$this->operators[self::INPUT1], $this->operators[self::INPUT1],
$this->values[self::INPUT1] $this->values[self::INPUT1]
); );
$condition2 = $this->conditionValidator->variableOpComparison( $condition2 = $this->conditionValidator->variableOpComparison(
$this->adapter->getCheckoutCurrency(), $this->facade->getCheckoutCurrency(),
$this->operators[self::INPUT2], $this->operators[self::INPUT2],
$this->values[self::INPUT2] $this->values[self::INPUT2]
); );

View File

@@ -124,7 +124,7 @@ class MatchForXArticlesManager extends ConditionManagerAbstract
public function isMatching() public function isMatching()
{ {
$condition1 = $this->conditionValidator->variableOpComparison( $condition1 = $this->conditionValidator->variableOpComparison(
$this->adapter->getNbArticlesInCart(), $this->facade->getNbArticlesInCart(),
$this->operators[self::INPUT1], $this->operators[self::INPUT1],
$this->values[self::INPUT1] $this->values[self::INPUT1]
); );

View File

@@ -9,7 +9,7 @@
<!-- Coupon module --> <!-- Coupon module -->
<service id="thelia.adapter" class="Thelia\Coupon\BaseFacade"> <service id="thelia.facade" class="Thelia\Coupon\BaseFacade">
<argument type="service" id="service_container" /> <argument type="service" id="service_container" />
</service> </service>
<service id="thelia.coupon.manager" class="Thelia\Coupon\CouponManager"> <service id="thelia.coupon.manager" class="Thelia\Coupon\CouponManager">
@@ -20,11 +20,11 @@
</service> </service>
<service id="thelia.coupon.type.remove_x_amount" class="Thelia\Coupon\Type\RemoveXAmountManager"> <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"/> <tag name="thelia.coupon.addCoupon"/>
</service> </service>
<service id="thelia.coupon.type.remove_x_percent" class="Thelia\Coupon\Type\RemoveXPercentManager"> <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"/> <tag name="thelia.coupon.addCoupon"/>
</service> </service>
@@ -36,17 +36,18 @@
<service id="thelia.condition.validator" class="Thelia\Condition\ConditionEvaluator"> <service id="thelia.condition.validator" class="Thelia\Condition\ConditionEvaluator">
</service> </service>
<service id="thelia.condition.match_for_everyone" class="Thelia\Condition\Implementation\MatchForEveryoneManager"> <service id="thelia.condition.match_for_everyone" class="Thelia\Condition\Implementation\MatchForEveryoneManager">
<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.adapter" />
<tag name="thelia.coupon.addCondition"/> <tag name="thelia.coupon.addCondition"/>
</service> </service>
<service id="thelia.condition.match_for_total_amount" class="Thelia\Condition\Implementation\MatchForTotalAmountManager"> <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"/> <tag name="thelia.coupon.addCondition"/>
</service> </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>

View File

@@ -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. * Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed. * This method is called after a test is executed.

View File

@@ -24,10 +24,13 @@
namespace Thelia\Condition\Implementation; namespace Thelia\Condition\Implementation;
use Thelia\Condition\ConditionEvaluator; use Thelia\Condition\ConditionEvaluator;
use Thelia\Condition\ConditionFactory;
use Thelia\Condition\Operators; use Thelia\Condition\Operators;
use Thelia\Coupon\ConditionCollection;
use Thelia\Coupon\FacadeInterface; use Thelia\Coupon\FacadeInterface;
use Thelia\Exception\InvalidConditionValueException; use Thelia\Exception\InvalidConditionValueException;
use Thelia\Model\Currency; use Thelia\Model\Currency;
use Thelia\Model\CurrencyQuery;
/** /**
* Created by JetBrains PhpStorm. * Created by JetBrains PhpStorm.
@@ -599,6 +602,73 @@ class MatchForTotalAmountManagerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $actual); $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. * Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed. * This method is called after a test is executed.