diff --git a/core/lib/Thelia/Condition/Implementation/StartDate.php b/core/lib/Thelia/Condition/Implementation/StartDate.php index 251ba395b..2524e1b6d 100644 --- a/core/lib/Thelia/Condition/Implementation/StartDate.php +++ b/core/lib/Thelia/Condition/Implementation/StartDate.php @@ -55,6 +55,8 @@ class StartDate extends ConditionAbstract */ public function setValidatorsFromForm(array $operators, array $values) { + $this->checkComparisonOperatorValue($operators, self::START_DATE); + if (! isset($values[self::START_DATE])) { $values[self::START_DATE] = time(); } diff --git a/core/lib/Thelia/Tests/Condition/Implementation/CartContainsCategoriesTest.php b/core/lib/Thelia/Tests/Condition/Implementation/CartContainsCategoriesTest.php new file mode 100644 index 000000000..1a5259b5e --- /dev/null +++ b/core/lib/Thelia/Tests/Condition/Implementation/CartContainsCategoriesTest.php @@ -0,0 +1,365 @@ + + */ +class CartContainsCategoriesTest 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() + { + + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $stubFacade->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue($cartTotalPrice)); + + $stubFacade->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue($checkoutCurrency)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + $category1 = new Category(); + $category1->setId(10); + + $category2 = new Category(); + $category2->setId(20); + + $category3 = new Category(); + $category3->setId(30); + + $product1 = new Product(); + $product1->addCategory($category1)->addCategory($category2); + + $product2 = new Product(); + $product2->addCategory($category3); + + $cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem1Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product1)) + ; + + $cartItem1Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(1)) + ; + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem2Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product2)); + + $cartItem2Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)) + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue([$cartItem1Stub, $cartItem2Stub])); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + + return $stubFacade; + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Condition\Implementation\CartContainsCategories::setValidators + * @expectedException \Thelia\Exception\InvalidConditionOperatorException + */ + public function testInValidBackOfficeInputOperator() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new CartContainsCategories($stubFacade); + $operators = array( + CartContainsCategories::CATEGORIES_LIST => Operators::INFERIOR_OR_EQUAL + ); + $values = array( + CartContainsCategories::CATEGORIES_LIST => array() + ); + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Condition\Implementation\CartContainsCategories::setValidators + * @expectedException \Thelia\Exception\InvalidConditionValueException + */ + public function testInValidBackOfficeInputValue() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new CartContainsCategories($stubFacade); + $operators = array( + CartContainsCategories::CATEGORIES_LIST => Operators::IN + ); + $values = array( + CartContainsCategories::CATEGORIES_LIST => array() + ); + + $condition1->setValidatorsFromForm($operators, $values); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Condition\Implementation\CartContainsCategories::isMatching + * + */ + public function testMatchingRule() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new CartContainsCategories($stubFacade); + $operators = array( + CartContainsCategories::CATEGORIES_LIST => Operators::IN + ); + $values = array( + CartContainsCategories::CATEGORIES_LIST => array(10, 20) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Condition\Implementation\CartContainsCategories::isMatching + * + */ + public function testNotMatching() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new CartContainsCategories($stubFacade); + + $operators = array( + CartContainsCategories::CATEGORIES_LIST => Operators::IN + ); + $values = array( + CartContainsCategories::CATEGORIES_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + public function testGetSerializableRule() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new CartContainsCategories($stubFacade); + + $operators = array( + CartContainsCategories::CATEGORIES_LIST => Operators::IN + ); + $values = array( + CartContainsCategories::CATEGORIES_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $serializableRule = $condition1->getSerializableCondition(); + + $expected = new SerializableCondition(); + $expected->conditionServiceId = $condition1->getServiceId(); + $expected->operators = $operators; + $expected->values = $values; + + $actual = $serializableRule; + + $this->assertEquals($expected, $actual); + + } + + /** + * Check getName i18n + * + * @covers Thelia\Condition\Implementation\CartContainsCategories::getName + * + */ + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Number of articles in cart'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new CartContainsCategories($stubFacade); + + $actual = $condition1->getName(); + $expected = 'Number of articles in cart'; + $this->assertEquals($expected, $actual); + } + + /** + * Check tooltip i18n + * + * @covers Thelia\Condition\Implementation\CartContainsCategories::getToolTip + * + */ + public function testGetToolTip() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Sample coupon condition'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new CartContainsCategories($stubFacade); + + $actual = $condition1->getToolTip(); + $expected = 'Sample coupon condition'; + $this->assertEquals($expected, $actual); + } + + /** + * Check validator + * + * @covers Thelia\Condition\Implementation\CartContainsCategories::generateInputs + * + */ + public function testGetValidator() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Price'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new CartContainsCategories($stubFacade); + + $operators = array( + CartContainsCategories::CATEGORIES_LIST => Operators::IN + ); + $values = array( + CartContainsCategories::CATEGORIES_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $actual = $condition1->getValidators(); + + $validators = array( + 'inputs' => array( + CartContainsCategories::CATEGORIES_LIST => array( + 'availableOperators' => array( + 'in' => 'Price', + 'out' => 'Price', + ), + 'value' => '', + 'selectedOperator' => 'in' + ) + ), + 'setOperators' => array( + 'categories' => 'in' + ), + 'setValues' => array( + 'categories' => array(50, 60) + ) + ); + $expected = $validators; + + $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() + { + } + +} diff --git a/core/lib/Thelia/Tests/Condition/Implementation/CartContainsProductsTest.php b/core/lib/Thelia/Tests/Condition/Implementation/CartContainsProductsTest.php new file mode 100644 index 000000000..f24dea73a --- /dev/null +++ b/core/lib/Thelia/Tests/Condition/Implementation/CartContainsProductsTest.php @@ -0,0 +1,365 @@ + + */ +class CartContainsProductsTest 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() + { + + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $stubFacade->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue($cartTotalPrice)); + + $stubFacade->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue($checkoutCurrency)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + $category1 = new Category(); + $category1->setId(10); + + $category2 = new Category(); + $category2->setId(20); + + $category3 = new Category(); + $category3->setId(30); + + $product1 = new Product(); + $product1->setId(10)->addCategory($category1)->addCategory($category2); + + $product2 = new Product(); + $product2->setId(20)->addCategory($category3); + + $cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem1Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product1)) + ; + + $cartItem1Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(1)) + ; + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem2Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product2)); + + $cartItem2Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)) + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue([$cartItem1Stub, $cartItem2Stub])); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + + return $stubFacade; + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Condition\Implementation\CartContainsProducts::setValidators + * @expectedException \Thelia\Exception\InvalidConditionOperatorException + */ + public function testInValidBackOfficeInputOperator() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new CartContainsProducts($stubFacade); + $operators = array( + CartContainsProducts::PRODUCTS_LIST => Operators::INFERIOR_OR_EQUAL + ); + $values = array( + CartContainsProducts::PRODUCTS_LIST => array() + ); + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Condition\Implementation\CartContainsProducts::setValidators + * @expectedException \Thelia\Exception\InvalidConditionValueException + */ + public function testInValidBackOfficeInputValue() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new CartContainsProducts($stubFacade); + $operators = array( + CartContainsProducts::PRODUCTS_LIST => Operators::IN + ); + $values = array( + CartContainsProducts::PRODUCTS_LIST => array() + ); + + $condition1->setValidatorsFromForm($operators, $values); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Condition\Implementation\CartContainsProducts::isMatching + * + */ + public function testMatchingRule() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new CartContainsProducts($stubFacade); + $operators = array( + CartContainsProducts::PRODUCTS_LIST => Operators::IN + ); + $values = array( + CartContainsProducts::PRODUCTS_LIST => array(10, 20) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Condition\Implementation\CartContainsProducts::isMatching + * + */ + public function testNotMatching() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new CartContainsProducts($stubFacade); + + $operators = array( + CartContainsProducts::PRODUCTS_LIST => Operators::IN + ); + $values = array( + CartContainsProducts::PRODUCTS_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + public function testGetSerializableRule() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new CartContainsProducts($stubFacade); + + $operators = array( + CartContainsProducts::PRODUCTS_LIST => Operators::IN + ); + $values = array( + CartContainsProducts::PRODUCTS_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $serializableRule = $condition1->getSerializableCondition(); + + $expected = new SerializableCondition(); + $expected->conditionServiceId = $condition1->getServiceId(); + $expected->operators = $operators; + $expected->values = $values; + + $actual = $serializableRule; + + $this->assertEquals($expected, $actual); + + } + + /** + * Check getName i18n + * + * @covers Thelia\Condition\Implementation\CartContainsProducts::getName + * + */ + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Number of articles in cart'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new CartContainsProducts($stubFacade); + + $actual = $condition1->getName(); + $expected = 'Number of articles in cart'; + $this->assertEquals($expected, $actual); + } + + /** + * Check tooltip i18n + * + * @covers Thelia\Condition\Implementation\CartContainsProducts::getToolTip + * + */ + public function testGetToolTip() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Sample coupon condition'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new CartContainsProducts($stubFacade); + + $actual = $condition1->getToolTip(); + $expected = 'Sample coupon condition'; + $this->assertEquals($expected, $actual); + } + + /** + * Check validator + * + * @covers Thelia\Condition\Implementation\CartContainsProducts::generateInputs + * + */ + public function testGetValidator() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Price'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new CartContainsProducts($stubFacade); + + $operators = array( + CartContainsProducts::PRODUCTS_LIST => Operators::IN + ); + $values = array( + CartContainsProducts::PRODUCTS_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $actual = $condition1->getValidators(); + + $validators = array( + 'inputs' => array( + CartContainsProducts::PRODUCTS_LIST => array( + 'availableOperators' => array( + 'in' => 'Price', + 'out' => 'Price', + ), + 'value' => '', + 'selectedOperator' => 'in' + ) + ), + 'setOperators' => array( + 'products' => 'in' + ), + 'setValues' => array( + 'products' => array(50, 60) + ) + ); + $expected = $validators; + + $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() + { + } + +} diff --git a/core/lib/Thelia/Tests/Condition/Implementation/ForSomeCustomersTest.php b/core/lib/Thelia/Tests/Condition/Implementation/ForSomeCustomersTest.php new file mode 100644 index 000000000..018d7210d --- /dev/null +++ b/core/lib/Thelia/Tests/Condition/Implementation/ForSomeCustomersTest.php @@ -0,0 +1,304 @@ + + */ +class ForSomeCustomersTest 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() + { + + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $customer = new Customer(); + $customer->setId(10); + + $stubFacade->expects($this->any()) + ->method('getCustomer') + ->will($this->returnValue($customer)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + return $stubFacade; + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Condition\Implementation\ForSomeCustomers::setValidators + * @expectedException \Thelia\Exception\InvalidConditionOperatorException + */ + public function testInValidBackOfficeInputOperator() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new ForSomeCustomers($stubFacade); + $operators = array( + ForSomeCustomers::CUSTOMERS_LIST => Operators::INFERIOR_OR_EQUAL + ); + $values = array( + ForSomeCustomers::CUSTOMERS_LIST => array() + ); + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Condition\Implementation\ForSomeCustomers::setValidators + * @expectedException \Thelia\Exception\InvalidConditionValueException + */ + public function testInValidBackOfficeInputValue() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new ForSomeCustomers($stubFacade); + $operators = array( + ForSomeCustomers::CUSTOMERS_LIST => Operators::IN + ); + $values = array( + ForSomeCustomers::CUSTOMERS_LIST => array() + ); + + $condition1->setValidatorsFromForm($operators, $values); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Condition\Implementation\ForSomeCustomers::isMatching + * + */ + public function testMatchingRule() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new ForSomeCustomers($stubFacade); + $operators = array( + ForSomeCustomers::CUSTOMERS_LIST => Operators::IN + ); + $values = array( + ForSomeCustomers::CUSTOMERS_LIST => array(10, 20) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Condition\Implementation\ForSomeCustomers::isMatching + * + */ + public function testNotMatching() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new ForSomeCustomers($stubFacade); + + $operators = array( + ForSomeCustomers::CUSTOMERS_LIST => Operators::IN + ); + $values = array( + ForSomeCustomers::CUSTOMERS_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + public function testGetSerializableRule() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new ForSomeCustomers($stubFacade); + + $operators = array( + ForSomeCustomers::CUSTOMERS_LIST => Operators::IN + ); + $values = array( + ForSomeCustomers::CUSTOMERS_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $serializableRule = $condition1->getSerializableCondition(); + + $expected = new SerializableCondition(); + $expected->conditionServiceId = $condition1->getServiceId(); + $expected->operators = $operators; + $expected->values = $values; + + $actual = $serializableRule; + + $this->assertEquals($expected, $actual); + + } + + /** + * Check getName i18n + * + * @covers Thelia\Condition\Implementation\ForSomeCustomers::getName + * + */ + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Number of articles in cart'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new ForSomeCustomers($stubFacade); + + $actual = $condition1->getName(); + $expected = 'Number of articles in cart'; + $this->assertEquals($expected, $actual); + } + + /** + * Check tooltip i18n + * + * @covers Thelia\Condition\Implementation\ForSomeCustomers::getToolTip + * + */ + public function testGetToolTip() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Sample coupon condition'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new ForSomeCustomers($stubFacade); + + $actual = $condition1->getToolTip(); + $expected = 'Sample coupon condition'; + $this->assertEquals($expected, $actual); + } + + /** + * Check validator + * + * @covers Thelia\Condition\Implementation\ForSomeCustomers::generateInputs + * + */ + public function testGetValidator() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Price'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new ForSomeCustomers($stubFacade); + + $operators = array( + ForSomeCustomers::CUSTOMERS_LIST => Operators::IN + ); + $values = array( + ForSomeCustomers::CUSTOMERS_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $actual = $condition1->getValidators(); + + $validators = array( + 'inputs' => array( + ForSomeCustomers::CUSTOMERS_LIST => array( + 'availableOperators' => array( + 'in' => 'Price', + 'out' => 'Price', + ), + 'value' => '', + 'selectedOperator' => 'in' + ) + ), + 'setOperators' => array( + 'customers' => 'in' + ), + 'setValues' => array( + 'customers' => array(50, 60) + ) + ); + $expected = $validators; + + $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() + { + } + +} diff --git a/core/lib/Thelia/Tests/Condition/Implementation/MatchBillingCountriesTest.php b/core/lib/Thelia/Tests/Condition/Implementation/MatchBillingCountriesTest.php new file mode 100644 index 000000000..61ac53c2a --- /dev/null +++ b/core/lib/Thelia/Tests/Condition/Implementation/MatchBillingCountriesTest.php @@ -0,0 +1,312 @@ + + */ +class MatchBillingCountriesTest 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() + { + + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $address = new Address(); + $address->setCountryId(10); + + $stubCustomer = $this->getMockBuilder('\Thelia\Model\Customer') + ->disableOriginalConstructor() + ->getMock(); + + $stubCustomer->expects($this->any()) + ->method('getDefaultAddress') + ->will($this->returnValue($address)); + + $stubFacade->expects($this->any()) + ->method('getCustomer') + ->will($this->returnValue($stubCustomer)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + return $stubFacade; + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Condition\Implementation\MatchBillingCountries::setValidators + * @expectedException \Thelia\Exception\InvalidConditionOperatorException + */ + public function testInValidBackOfficeInputOperator() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new MatchBillingCountries($stubFacade); + $operators = array( + MatchBillingCountries::COUNTRIES_LIST => Operators::INFERIOR_OR_EQUAL + ); + $values = array( + MatchBillingCountries::COUNTRIES_LIST => array() + ); + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Condition\Implementation\MatchBillingCountries::setValidators + * @expectedException \Thelia\Exception\InvalidConditionValueException + */ + public function testInValidBackOfficeInputValue() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new MatchBillingCountries($stubFacade); + $operators = array( + MatchBillingCountries::COUNTRIES_LIST => Operators::IN + ); + $values = array( + MatchBillingCountries::COUNTRIES_LIST => array() + ); + + $condition1->setValidatorsFromForm($operators, $values); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Condition\Implementation\MatchBillingCountries::isMatching + * + */ + public function testMatchingRule() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new MatchBillingCountries($stubFacade); + $operators = array( + MatchBillingCountries::COUNTRIES_LIST => Operators::IN + ); + $values = array( + MatchBillingCountries::COUNTRIES_LIST => array(10, 20) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Condition\Implementation\MatchBillingCountries::isMatching + * + */ + public function testNotMatching() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new MatchBillingCountries($stubFacade); + + $operators = array( + MatchBillingCountries::COUNTRIES_LIST => Operators::IN + ); + $values = array( + MatchBillingCountries::COUNTRIES_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + public function testGetSerializableRule() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new MatchBillingCountries($stubFacade); + + $operators = array( + MatchBillingCountries::COUNTRIES_LIST => Operators::IN + ); + $values = array( + MatchBillingCountries::COUNTRIES_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $serializableRule = $condition1->getSerializableCondition(); + + $expected = new SerializableCondition(); + $expected->conditionServiceId = $condition1->getServiceId(); + $expected->operators = $operators; + $expected->values = $values; + + $actual = $serializableRule; + + $this->assertEquals($expected, $actual); + + } + + /** + * Check getName i18n + * + * @covers Thelia\Condition\Implementation\MatchBillingCountries::getName + * + */ + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Number of articles in cart'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new MatchBillingCountries($stubFacade); + + $actual = $condition1->getName(); + $expected = 'Number of articles in cart'; + $this->assertEquals($expected, $actual); + } + + /** + * Check tooltip i18n + * + * @covers Thelia\Condition\Implementation\MatchBillingCountries::getToolTip + * + */ + public function testGetToolTip() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Sample coupon condition'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new MatchBillingCountries($stubFacade); + + $actual = $condition1->getToolTip(); + $expected = 'Sample coupon condition'; + $this->assertEquals($expected, $actual); + } + + /** + * Check validator + * + * @covers Thelia\Condition\Implementation\MatchBillingCountries::generateInputs + * + */ + public function testGetValidator() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Price'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new MatchBillingCountries($stubFacade); + + $operators = array( + MatchBillingCountries::COUNTRIES_LIST => Operators::IN + ); + $values = array( + MatchBillingCountries::COUNTRIES_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $actual = $condition1->getValidators(); + + $validators = array( + 'inputs' => array( + MatchBillingCountries::COUNTRIES_LIST => array( + 'availableOperators' => array( + 'in' => 'Price', + 'out' => 'Price', + ), + 'value' => '', + 'selectedOperator' => 'in' + ) + ), + 'setOperators' => array( + 'countries' => 'in' + ), + 'setValues' => array( + 'countries' => array(50, 60) + ) + ); + $expected = $validators; + + $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() + { + } + +} diff --git a/core/lib/Thelia/Tests/Condition/Implementation/MatchDeliveryCountriesTest.php b/core/lib/Thelia/Tests/Condition/Implementation/MatchDeliveryCountriesTest.php new file mode 100644 index 000000000..623a1aeef --- /dev/null +++ b/core/lib/Thelia/Tests/Condition/Implementation/MatchDeliveryCountriesTest.php @@ -0,0 +1,304 @@ + + */ +class MatchDeliveryCountriesTest 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() + { + + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $address = new Address(); + $address->setCountryId(10); + + $stubFacade->expects($this->any()) + ->method('getDeliveryAddress') + ->will($this->returnValue($address)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + return $stubFacade; + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Condition\Implementation\MatchDeliveryCountries::setValidators + * @expectedException \Thelia\Exception\InvalidConditionOperatorException + */ + public function testInValidBackOfficeInputOperator() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new MatchDeliveryCountries($stubFacade); + $operators = array( + MatchDeliveryCountries::COUNTRIES_LIST => Operators::INFERIOR_OR_EQUAL + ); + $values = array( + MatchDeliveryCountries::COUNTRIES_LIST => array() + ); + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Condition\Implementation\MatchDeliveryCountries::setValidators + * @expectedException \Thelia\Exception\InvalidConditionValueException + */ + public function testInValidBackOfficeInputValue() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new MatchDeliveryCountries($stubFacade); + $operators = array( + MatchDeliveryCountries::COUNTRIES_LIST => Operators::IN + ); + $values = array( + MatchDeliveryCountries::COUNTRIES_LIST => array() + ); + + $condition1->setValidatorsFromForm($operators, $values); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Condition\Implementation\MatchDeliveryCountries::isMatching + * + */ + public function testMatchingRule() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new MatchDeliveryCountries($stubFacade); + $operators = array( + MatchDeliveryCountries::COUNTRIES_LIST => Operators::IN + ); + $values = array( + MatchDeliveryCountries::COUNTRIES_LIST => array(10, 20) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Condition\Implementation\MatchDeliveryCountries::isMatching + * + */ + public function testNotMatching() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new MatchDeliveryCountries($stubFacade); + + $operators = array( + MatchDeliveryCountries::COUNTRIES_LIST => Operators::IN + ); + $values = array( + MatchDeliveryCountries::COUNTRIES_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + public function testGetSerializableRule() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new MatchDeliveryCountries($stubFacade); + + $operators = array( + MatchDeliveryCountries::COUNTRIES_LIST => Operators::IN + ); + $values = array( + MatchDeliveryCountries::COUNTRIES_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $serializableRule = $condition1->getSerializableCondition(); + + $expected = new SerializableCondition(); + $expected->conditionServiceId = $condition1->getServiceId(); + $expected->operators = $operators; + $expected->values = $values; + + $actual = $serializableRule; + + $this->assertEquals($expected, $actual); + + } + + /** + * Check getName i18n + * + * @covers Thelia\Condition\Implementation\MatchDeliveryCountries::getName + * + */ + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Number of articles in cart'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new MatchDeliveryCountries($stubFacade); + + $actual = $condition1->getName(); + $expected = 'Number of articles in cart'; + $this->assertEquals($expected, $actual); + } + + /** + * Check tooltip i18n + * + * @covers Thelia\Condition\Implementation\MatchDeliveryCountries::getToolTip + * + */ + public function testGetToolTip() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Sample coupon condition'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new MatchDeliveryCountries($stubFacade); + + $actual = $condition1->getToolTip(); + $expected = 'Sample coupon condition'; + $this->assertEquals($expected, $actual); + } + + /** + * Check validator + * + * @covers Thelia\Condition\Implementation\MatchDeliveryCountries::generateInputs + * + */ + public function testGetValidator() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Price'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new MatchDeliveryCountries($stubFacade); + + $operators = array( + MatchDeliveryCountries::COUNTRIES_LIST => Operators::IN + ); + $values = array( + MatchDeliveryCountries::COUNTRIES_LIST => array(50, 60) + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $actual = $condition1->getValidators(); + + $validators = array( + 'inputs' => array( + MatchDeliveryCountries::COUNTRIES_LIST => array( + 'availableOperators' => array( + 'in' => 'Price', + 'out' => 'Price', + ), + 'value' => '', + 'selectedOperator' => 'in' + ) + ), + 'setOperators' => array( + 'countries' => 'in' + ), + 'setValues' => array( + 'countries' => array(50, 60) + ) + ); + $expected = $validators; + + $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() + { + } + +} diff --git a/core/lib/Thelia/Tests/Condition/Implementation/MatchForEveryoneTest.php b/core/lib/Thelia/Tests/Condition/Implementation/MatchForEveryoneTest.php index ddf3d25ae..84b57afb1 100644 --- a/core/lib/Thelia/Tests/Condition/Implementation/MatchForEveryoneTest.php +++ b/core/lib/Thelia/Tests/Condition/Implementation/MatchForEveryoneTest.php @@ -13,7 +13,6 @@ namespace Thelia\Condition\Implementation; use Thelia\Condition\ConditionEvaluator; - use Thelia\Coupon\FacadeInterface; use Thelia\Model\Currency; diff --git a/core/lib/Thelia/Tests/Condition/Implementation/MatchForTotalAmountTest.php b/core/lib/Thelia/Tests/Condition/Implementation/MatchForTotalAmountTest.php index bffbda092..fa599a866 100644 --- a/core/lib/Thelia/Tests/Condition/Implementation/MatchForTotalAmountTest.php +++ b/core/lib/Thelia/Tests/Condition/Implementation/MatchForTotalAmountTest.php @@ -12,13 +12,12 @@ namespace Thelia\Tests\Condition\Implementation; +use Thelia\Condition\ConditionCollection; use Thelia\Condition\ConditionEvaluator; use Thelia\Condition\ConditionFactory; use Thelia\Condition\Implementation\MatchForTotalAmount; use Thelia\Condition\Operators; -use Thelia\Condition\ConditionCollection; use Thelia\Coupon\FacadeInterface; - use Thelia\Model\Currency; use Thelia\Model\CurrencyQuery; diff --git a/core/lib/Thelia/Tests/Condition/Implementation/StartDateTest.php b/core/lib/Thelia/Tests/Condition/Implementation/StartDateTest.php new file mode 100644 index 000000000..6df0fddbb --- /dev/null +++ b/core/lib/Thelia/Tests/Condition/Implementation/StartDateTest.php @@ -0,0 +1,329 @@ + + */ +class StartDateTest extends \PHPUnit_Framework_TestCase +{ + + var $startDate; + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + $this->startDate = time() - 2000; + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $address = new Address(); + $address->setCountryId(10); + + $stubFacade->expects($this->any()) + ->method('getDeliveryAddress') + ->will($this->returnValue($address)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + $lang = new Lang(); + $lang->setDateFormat("d/m/Y"); + + $stubSession = $this->getMockBuilder('\Thelia\Core\HttpFoundation\Session\Session') + ->disableOriginalConstructor() + ->getMock(); + + $stubSession->expects($this->any()) + ->method('getLang') + ->will($this->returnValue($lang)); + + $stubRequest = $this->getMockBuilder('\Thelia\Core\HttpFoundation\Request') + ->disableOriginalConstructor() + ->getMock(); + + $stubRequest->expects($this->any()) + ->method('getSession') + ->will($this->returnValue($stubSession)); + + $stubFacade->expects($this->any()) + ->method('getRequest') + ->will($this->returnValue($stubRequest)); + + return $stubFacade; + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Condition\Implementation\StartDate::setValidators + * @expectedException \Thelia\Exception\InvalidConditionOperatorException + */ + public function testInValidBackOfficeInputOperator() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new StartDate($stubFacade); + + $operators = array( + StartDate::START_DATE => 'petite licorne' + ); + $values = array( + StartDate::START_DATE => $this->startDate + ); + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if validity test on BackOffice inputs are working + * + * @covers Thelia\Condition\Implementation\StartDate::setValidators + * @expectedException \Thelia\Exception\InvalidConditionValueException + */ + public function testInValidBackOfficeInputValue() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new StartDate($stubFacade); + $operators = array( + StartDate::START_DATE => Operators::SUPERIOR_OR_EQUAL + ); + $values = array( + StartDate::START_DATE => 'petit poney' + ); + + $condition1->setValidatorsFromForm($operators, $values); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Condition\Implementation\StartDate::isMatching + * + */ + public function testMatchingRule() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new StartDate($stubFacade); + $operators = array( + StartDate::START_DATE => Operators::SUPERIOR_OR_EQUAL + ); + $values = array( + StartDate::START_DATE => $this->startDate + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = true; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + /** + * Check if test inferior operator is working + * + * @covers Thelia\Condition\Implementation\StartDate::isMatching + * + */ + public function testNotMatching() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new StartDate($stubFacade); + + $operators = array( + StartDate::START_DATE => Operators::SUPERIOR_OR_EQUAL + ); + $values = array( + StartDate::START_DATE => time() + 2000 + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $isValid = $condition1->isMatching(); + + $expected = false; + $actual =$isValid; + $this->assertEquals($expected, $actual); + } + + public function testGetSerializableRule() + { + /** @var FacadeInterface $stubFacade */ + $stubFacade = $this->generateFacadeStub(); + + $condition1 = new StartDate($stubFacade); + + $operators = array( + StartDate::START_DATE => Operators::SUPERIOR_OR_EQUAL + ); + $values = array( + StartDate::START_DATE => $this->startDate + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $serializableRule = $condition1->getSerializableCondition(); + + $expected = new SerializableCondition(); + $expected->conditionServiceId = $condition1->getServiceId(); + $expected->operators = $operators; + $expected->values = $values; + + $actual = $serializableRule; + + $this->assertEquals($expected, $actual); + + } + + /** + * Check getName i18n + * + * @covers Thelia\Condition\Implementation\StartDate::getName + * + */ + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Number of articles in cart'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new StartDate($stubFacade); + + $actual = $condition1->getName(); + $expected = 'Number of articles in cart'; + $this->assertEquals($expected, $actual); + } + + /** + * Check tooltip i18n + * + * @covers Thelia\Condition\Implementation\StartDate::getToolTip + * + */ + public function testGetToolTip() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Sample coupon condition'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new StartDate($stubFacade); + + $actual = $condition1->getToolTip(); + $expected = 'Sample coupon condition'; + $this->assertEquals($expected, $actual); + } + + /** + * Check validator + * + * @covers Thelia\Condition\Implementation\StartDate::generateInputs + * + */ + public function testGetValidator() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Price'); + + /** @var FacadeInterface $stubFacade */ + $condition1 = new StartDate($stubFacade); + + $operators = array( + StartDate::START_DATE => Operators::SUPERIOR_OR_EQUAL + ); + $values = array( + StartDate::START_DATE => $this->startDate + ); + + $condition1->setValidatorsFromForm($operators, $values); + + $actual = $condition1->getValidators(); + + $validators = array( + 'inputs' => array( + StartDate::START_DATE => array( + 'availableOperators' => array( + '>=' => 'Price', + ), + 'value' => '', + 'selectedOperator' => '>=' + ) + ), + 'setOperators' => array( + 'start_date' => '>=' + ), + 'setValues' => array( + 'start_date' => $this->startDate + ) + ); + $expected = $validators; + + $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() + { + } + +} diff --git a/core/lib/Thelia/Tests/Coupon/Type/FreeProductTest.php b/core/lib/Thelia/Tests/Coupon/Type/FreeProductTest.php new file mode 100644 index 000000000..72be02204 --- /dev/null +++ b/core/lib/Thelia/Tests/Coupon/Type/FreeProductTest.php @@ -0,0 +1,412 @@ + + */ +class FreeProductTest extends \PHPUnit_Framework_TestCase +{ + + /** @var Product $freeProduct */ + var $freeProduct; + var $originalPrice; + var $originalPromo; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + protected function setUp() + { + $currency = CurrencyQuery::create()->filterByCode('EUR')->findOne(); + + // Find a product + $this->freeProduct = ProductQuery::create()->findOne(); + + $this->originalPrice = $this->freeProduct->getDefaultSaleElements()->getPricesByCurrency($currency)->getPrice(); + $this->originalPromo = $this->freeProduct->getDefaultSaleElements()->getPromo(); + + $this->freeProduct->getDefaultSaleElements()->setPromo(false)->save(); + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $currencies = CurrencyQuery::create(); + $currencies = $currencies->find(); + $stubFacade->expects($this->any()) + ->method('getAvailableCurrencies') + ->will($this->returnValue($currencies)); + + $stubFacade->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue($cartTotalPrice)); + + $stubFacade->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue($checkoutCurrency)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + $stubDispatcher = $this->getMockBuilder('\Symfony\Component\EventDispatcher\EventDispatcher') + ->disableOriginalConstructor() + ->getMock(); + + $stubDispatcher->expects($this->any()) + ->method('dispatch') + ->will($this->returnCallback(function($dummy, $cartEvent) { + $ci = new CartItem(); + $ci->setId(3)->setPrice(123)->setPromo(0); + + $cartEvent->setCartItem($ci); + })); + + $stubFacade->expects($this->any()) + ->method('getDispatcher') + ->will($this->returnValue($stubDispatcher)); + + $stubSession = $this->getMockBuilder('\Thelia\Core\HttpFoundation\Session\Session') + ->disableOriginalConstructor() + ->getMock(); + + $stubSession->expects($this->any()) + ->method('get') + ->will($this->onConsecutiveCalls(-1, 3)); + + $stubRequest = $this->getMockBuilder('\Thelia\Core\HttpFoundation\Request') + ->disableOriginalConstructor() + ->getMock(); + + $stubRequest->expects($this->any()) + ->method('getSession') + ->will($this->returnValue($stubSession)); + + $stubFacade->expects($this->any()) + ->method('getRequest') + ->will($this->returnValue($stubRequest)); + + return $stubFacade; + } + + public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade, $count) { + + $product1 = new Product(); + $product1->setId(10); + + $product2 = new Product(); + $product2->setId(20); + + $cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem1Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product1)) + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(1)) + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getPrice') + ->will($this->returnValue(100)) + ; + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem2Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product2)); + + $cartItem2Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)) + ; + $cartItem2Stub + ->expects($this->any()) + ->method('getPrice') + ->will($this->returnValue(150)) + + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + if ($count == 1) + $ret = [$cartItem1Stub]; + else + $ret = [$cartItem1Stub, $cartItem2Stub]; + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue($ret)); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) { + + $product2 = new Product(); + $product2->setId(30); + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem2Stub->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product2)) + ; + $cartItem2Stub->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)) + ; + $cartItem2Stub + ->expects($this->any()) + ->method('getPrice') + ->will($this->returnValue(11000)) + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue([$cartItem2Stub])); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function testSet() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new FreeProduct($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10.00, 'products' => [10, 20], 'offered_product_id' => $this->freeProduct->getId(), 'offered_category_id' => 1), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $condition1 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 40.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition1->setValidatorsFromForm($operators, $values); + + $condition2 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 400.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition2->setValidatorsFromForm($operators, $values); + + $conditions = new ConditionCollection(); + $conditions[] = $condition1; + $conditions[] = $condition2; + $coupon->setConditions($conditions); + + $this->assertEquals('TEST', $coupon->getCode()); + $this->assertEquals('TEST Coupon', $coupon->getTitle()); + $this->assertEquals('This is a test coupon title', $coupon->getShortDescription()); + $this->assertEquals('This is a test coupon description', $coupon->getDescription()); + + $this->assertEquals(true, $coupon->isCumulative()); + $this->assertEquals(true, $coupon->isRemovingPostage()); + $this->assertEquals(true, $coupon->isAvailableOnSpecialOffers()); + $this->assertEquals(true, $coupon->isEnabled()); + + $this->assertEquals(254, $coupon->getMaxUsage()); + $this->assertEquals($date, $coupon->getExpirationDate()); + } + + public function testMatchOne() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new FreeProduct($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10.00, 'products' => [10, 20], 'offered_product_id' => $this->freeProduct->getId(), 'offered_category_id' => 1), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateMatchingCart($stubFacade, 1); + + $this->assertEquals(123.00, $coupon->exec()); + } + + public function testMatchSeveral() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new FreeProduct($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10.00, 'products' => [10, 20], 'offered_product_id' => $this->freeProduct->getId(), 'offered_category_id' => 1), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateMatchingCart($stubFacade, 2); + + $this->assertEquals(123.00, $coupon->exec()); + } + + public function testNoMatch() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new FreeProduct($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10.00, 'products' => [10, 20], 'offered_product_id' => $this->freeProduct->getId(), 'offered_category_id' => 1), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateNoMatchingCart($stubFacade); + + $this->assertEquals(0.00, $coupon->exec()); + } + + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name'); + + /** @var FacadeInterface $stubFacade */ + $coupon = new FreeProduct($stubFacade); + + $actual = $coupon->getName(); + $expected = 'Coupon test name'; + $this->assertEquals($expected, $actual); + } + + public function testGetToolTip() + { + $tooltip = 'Coupon test tooltip'; + $stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip); + + /** @var FacadeInterface $stubFacade */ + $coupon = new FreeProduct($stubFacade); + + $actual = $coupon->getToolTip(); + $expected = $tooltip; + $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() + { + $this->freeProduct->getDefaultSaleElements()->setPromo($this->originalPromo)->save(); + } +} diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemoveAmountOnAttributeValuesTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemoveAmountOnAttributeValuesTest.php new file mode 100644 index 000000000..67bd1ce60 --- /dev/null +++ b/core/lib/Thelia/Tests/Coupon/Type/RemoveAmountOnAttributeValuesTest.php @@ -0,0 +1,398 @@ + + */ +class RemoveAmountOnAttributeValuesTest 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() + { + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $currencies = CurrencyQuery::create(); + $currencies = $currencies->find(); + $stubFacade->expects($this->any()) + ->method('getAvailableCurrencies') + ->will($this->returnValue($currencies)); + + $stubFacade->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue($cartTotalPrice)); + + $stubFacade->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue($checkoutCurrency)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + return $stubFacade; + } + + public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade, $count) { + + $attrCombination1 = $this->getMockBuilder('\Thelia\Model\AttributeCombination') + ->disableOriginalConstructor() + ->getMock() + ; + + $attrCombination1 + ->expects($this->any()) + ->method('getAttributeAvId') + ->will($this->returnValue(10)) + ; + + $attrCombination2 = $this->getMockBuilder('\Thelia\Model\AttributeCombination') + ->disableOriginalConstructor() + ->getMock() + ; + + $attrCombination2 + ->expects($this->any()) + ->method('getAttributeAvId') + ->will($this->returnValue(20)) + ; + + $pse1 = $this->getMockBuilder('\Thelia\Model\ProductSaleElements') + ->disableOriginalConstructor() + ->getMock() + ; + $pse1 + ->expects($this->any()) + ->method('getAttributeCombinations') + ->will($this->returnValue([$attrCombination1])) + ; + + $pse2 = $this->getMockBuilder('\Thelia\Model\ProductSaleElements') + ->disableOriginalConstructor() + ->getMock(); + ; + $pse2 + ->expects($this->any()) + ->method('getAttributeCombinations') + ->will($this->returnValue([$attrCombination1, $attrCombination2])) + ; + + $cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock() + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getProductSaleElements') + ->will($this->returnValue($pse1)) + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(1)) + ; + + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock() + ; + $cartItem2Stub + ->expects($this->any()) + ->method('getProductSaleElements') + ->will($this->returnValue($pse2)) + ; + $cartItem2Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)) + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + if ($count == 1) + $ret = [$cartItem1Stub]; + else + $ret = [$cartItem1Stub, $cartItem2Stub]; + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue($ret)); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) { + + $attrCombination1 = $this->getMockBuilder('\Thelia\Model\AttributeCombination') + ->disableOriginalConstructor() + ->getMock() + ; + + $attrCombination1 + ->expects($this->any()) + ->method('getAttributeAvId') + ->will($this->returnValue(30)) + ; + + $pse1 = $this->getMockBuilder('\Thelia\Model\ProductSaleElements') + ->disableOriginalConstructor() + ->getMock() + ; + $pse1 + ->expects($this->any()) + ->method('getAttributeCombinations') + ->will($this->returnValue([$attrCombination1])) + ; + + $cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock() + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getProductSaleElements') + ->will($this->returnValue($pse1)) + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(1)) + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue([$cartItem1Stub])); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function testSet() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemoveAmountOnAttributeValues($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('amount' => 10.00, 'attribute_avs' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $condition1 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 40.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition1->setValidatorsFromForm($operators, $values); + + $condition2 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 400.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition2->setValidatorsFromForm($operators, $values); + + $conditions = new ConditionCollection(); + $conditions[] = $condition1; + $conditions[] = $condition2; + $coupon->setConditions($conditions); + + $this->assertEquals('TEST', $coupon->getCode()); + $this->assertEquals('TEST Coupon', $coupon->getTitle()); + $this->assertEquals('This is a test coupon title', $coupon->getShortDescription()); + $this->assertEquals('This is a test coupon description', $coupon->getDescription()); + + $this->assertEquals(true, $coupon->isCumulative()); + $this->assertEquals(true, $coupon->isRemovingPostage()); + $this->assertEquals(true, $coupon->isAvailableOnSpecialOffers()); + $this->assertEquals(true, $coupon->isEnabled()); + + $this->assertEquals(254, $coupon->getMaxUsage()); + $this->assertEquals($date, $coupon->getExpirationDate()); + } + + public function testMatchOne() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemoveAmountOnAttributeValues($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('amount' => 10.00, 'attribute_avs' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateMatchingCart($stubFacade, 1); + + $this->assertEquals(10.00, $coupon->exec()); + } + + public function testMatchSeveral() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemoveAmountOnAttributeValues($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('amount' => 10.00, 'attribute_avs' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateMatchingCart($stubFacade, 2); + + $this->assertEquals(30.00, $coupon->exec()); + } + + public function testNoMatch() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemoveAmountOnAttributeValues($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('amount' => 10.00, 'attribute_avs' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateNoMatchingCart($stubFacade); + + $this->assertEquals(0.00, $coupon->exec()); + } + + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name'); + + /** @var FacadeInterface $stubFacade */ + $coupon = new RemoveAmountOnAttributeValues($stubFacade); + + $actual = $coupon->getName(); + $expected = 'Coupon test name'; + $this->assertEquals($expected, $actual); + } + + public function testGetToolTip() + { + $tooltip = 'Coupon test tooltip'; + $stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip); + + /** @var FacadeInterface $stubFacade */ + $coupon = new RemoveAmountOnAttributeValues($stubFacade); + + $actual = $coupon->getToolTip(); + $expected = $tooltip; + $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() + { + } +} diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemoveAmountOnCategoriesTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemoveAmountOnCategoriesTest.php new file mode 100644 index 000000000..01be96bde --- /dev/null +++ b/core/lib/Thelia/Tests/Coupon/Type/RemoveAmountOnCategoriesTest.php @@ -0,0 +1,330 @@ + + */ +class RemoveAmountOnCategoriesTest 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() + { + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $currencies = CurrencyQuery::create(); + $currencies = $currencies->find(); + $stubFacade->expects($this->any()) + ->method('getAvailableCurrencies') + ->will($this->returnValue($currencies)); + + $stubFacade->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue($cartTotalPrice)); + + $stubFacade->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue($checkoutCurrency)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + return $stubFacade; + } + + public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) { + + $category1 = new Category(); + $category1->setId(10); + + $category2 = new Category(); + $category2->setId(20); + + $category3 = new Category(); + $category3->setId(30); + + $product1 = new Product(); + $product1->addCategory($category1)->addCategory($category2); + + $product2 = new Product(); + $product2->addCategory($category3); + + $cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem1Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product1)) + ; + + $cartItem1Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(1)) + ; + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem2Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product2)); + + $cartItem2Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)) + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue([$cartItem1Stub, $cartItem2Stub])); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) { + + $category3 = new Category(); + $category3->setId(30); + + $product2 = new Product(); + $product2->addCategory($category3); + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem2Stub->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product2)); + + $cartItem2Stub->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)); + + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue([$cartItem2Stub])); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function testSet() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemoveAmountOnCategories($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('amount' => 10.00, 'categories' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $condition1 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 40.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition1->setValidatorsFromForm($operators, $values); + + $condition2 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 400.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition2->setValidatorsFromForm($operators, $values); + + $conditions = new ConditionCollection(); + $conditions[] = $condition1; + $conditions[] = $condition2; + $coupon->setConditions($conditions); + + $this->assertEquals('TEST', $coupon->getCode()); + $this->assertEquals('TEST Coupon', $coupon->getTitle()); + $this->assertEquals('This is a test coupon title', $coupon->getShortDescription()); + $this->assertEquals('This is a test coupon description', $coupon->getDescription()); + + $this->assertEquals(true, $coupon->isCumulative()); + $this->assertEquals(true, $coupon->isRemovingPostage()); + $this->assertEquals(true, $coupon->isAvailableOnSpecialOffers()); + $this->assertEquals(true, $coupon->isEnabled()); + + $this->assertEquals(254, $coupon->getMaxUsage()); + $this->assertEquals($date, $coupon->getExpirationDate()); + + $this->generateMatchingCart($stubFacade); + + $this->assertEquals(10.00, $coupon->exec()); + } + + public function testMatch() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemoveAmountOnCategories($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('amount' => 10.00, 'categories' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateMatchingCart($stubFacade); + + $this->assertEquals(10.00, $coupon->exec()); + } + + public function testNoMatch() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemoveAmountOnCategories($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('amount' => 10.00, 'categories' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateNoMatchingCart($stubFacade); + + $this->assertEquals(0.00, $coupon->exec()); + } + + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name'); + + /** @var FacadeInterface $stubFacade */ + $coupon = new RemoveAmountOnCategories($stubFacade); + + $actual = $coupon->getName(); + $expected = 'Coupon test name'; + $this->assertEquals($expected, $actual); + } + + public function testGetToolTip() + { + $tooltip = 'Coupon test tooltip'; + $stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip); + + /** @var FacadeInterface $stubFacade */ + $coupon = new RemoveAmountOnCategories($stubFacade); + + $actual = $coupon->getToolTip(); + $expected = $tooltip; + $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() + { + } +} diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemoveAmountOnProductsTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemoveAmountOnProductsTest.php new file mode 100644 index 000000000..55e64f861 --- /dev/null +++ b/core/lib/Thelia/Tests/Coupon/Type/RemoveAmountOnProductsTest.php @@ -0,0 +1,343 @@ + + */ +class RemoveAmountOnProductsTest 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() + { + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $currencies = CurrencyQuery::create(); + $currencies = $currencies->find(); + $stubFacade->expects($this->any()) + ->method('getAvailableCurrencies') + ->will($this->returnValue($currencies)); + + $stubFacade->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue($cartTotalPrice)); + + $stubFacade->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue($checkoutCurrency)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + return $stubFacade; + } + + public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade, $count) { + + $product1 = new Product(); + $product1->setId(10); + + $product2 = new Product(); + $product2->setId(20); + + $cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem1Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product1)) + ; + + $cartItem1Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(1)) + ; + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem2Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product2)); + + $cartItem2Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)) + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + if ($count == 1) + $ret = [$cartItem1Stub]; + else + $ret = [$cartItem1Stub, $cartItem2Stub]; + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue($ret)); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) { + + $product2 = new Product(); + $product2->setId(30); + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem2Stub->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product2)); + + $cartItem2Stub->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)); + + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue([$cartItem2Stub])); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function testSet() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemoveAmountOnProducts($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('amount' => 10.00, 'products' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $condition1 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 40.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition1->setValidatorsFromForm($operators, $values); + + $condition2 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 400.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition2->setValidatorsFromForm($operators, $values); + + $conditions = new ConditionCollection(); + $conditions[] = $condition1; + $conditions[] = $condition2; + $coupon->setConditions($conditions); + + $this->assertEquals('TEST', $coupon->getCode()); + $this->assertEquals('TEST Coupon', $coupon->getTitle()); + $this->assertEquals('This is a test coupon title', $coupon->getShortDescription()); + $this->assertEquals('This is a test coupon description', $coupon->getDescription()); + + $this->assertEquals(true, $coupon->isCumulative()); + $this->assertEquals(true, $coupon->isRemovingPostage()); + $this->assertEquals(true, $coupon->isAvailableOnSpecialOffers()); + $this->assertEquals(true, $coupon->isEnabled()); + + $this->assertEquals(254, $coupon->getMaxUsage()); + $this->assertEquals($date, $coupon->getExpirationDate()); + } + + public function testMatchOne() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemoveAmountOnProducts($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('amount' => 10.00, 'products' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateMatchingCart($stubFacade, 1); + + $this->assertEquals(10.00, $coupon->exec()); + } + + public function testMatchSeveral() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemoveAmountOnProducts($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('amount' => 10.00, 'products' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateMatchingCart($stubFacade, 2); + + $this->assertEquals(30.00, $coupon->exec()); + } + + public function testNoMatch() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemoveAmountOnProducts($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('amount' => 10.00, 'products' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateNoMatchingCart($stubFacade); + + $this->assertEquals(0.00, $coupon->exec()); + } + + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name'); + + /** @var FacadeInterface $stubFacade */ + $coupon = new RemoveAmountOnProducts($stubFacade); + + $actual = $coupon->getName(); + $expected = 'Coupon test name'; + $this->assertEquals($expected, $actual); + } + + public function testGetToolTip() + { + $tooltip = 'Coupon test tooltip'; + $stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip); + + /** @var FacadeInterface $stubFacade */ + $coupon = new RemoveAmountOnProducts($stubFacade); + + $actual = $coupon->getToolTip(); + $expected = $tooltip; + $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() + { + } +} diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemovePercentageOnAttributeValuesTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemovePercentageOnAttributeValuesTest.php new file mode 100644 index 000000000..eaa5e4de9 --- /dev/null +++ b/core/lib/Thelia/Tests/Coupon/Type/RemovePercentageOnAttributeValuesTest.php @@ -0,0 +1,412 @@ + + */ +class RemovePercentageOnAttributeValuesTest 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() + { + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $currencies = CurrencyQuery::create(); + $currencies = $currencies->find(); + $stubFacade->expects($this->any()) + ->method('getAvailableCurrencies') + ->will($this->returnValue($currencies)); + + $stubFacade->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue($cartTotalPrice)); + + $stubFacade->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue($checkoutCurrency)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + return $stubFacade; + } + + public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade, $count) { + + $attrCombination1 = $this->getMockBuilder('\Thelia\Model\AttributeCombination') + ->disableOriginalConstructor() + ->getMock() + ; + + $attrCombination1 + ->expects($this->any()) + ->method('getAttributeAvId') + ->will($this->returnValue(10)) + ; + + $attrCombination2 = $this->getMockBuilder('\Thelia\Model\AttributeCombination') + ->disableOriginalConstructor() + ->getMock() + ; + + $attrCombination2 + ->expects($this->any()) + ->method('getAttributeAvId') + ->will($this->returnValue(20)) + ; + + $pse1 = $this->getMockBuilder('\Thelia\Model\ProductSaleElements') + ->disableOriginalConstructor() + ->getMock() + ; + $pse1 + ->expects($this->any()) + ->method('getAttributeCombinations') + ->will($this->returnValue([$attrCombination1])) + ; + + $pse2 = $this->getMockBuilder('\Thelia\Model\ProductSaleElements') + ->disableOriginalConstructor() + ->getMock(); + ; + $pse2 + ->expects($this->any()) + ->method('getAttributeCombinations') + ->will($this->returnValue([$attrCombination1, $attrCombination2])) + ; + + $cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock() + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getProductSaleElements') + ->will($this->returnValue($pse1)) + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(1)) + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getPrice') + ->will($this->returnValue(100)) + ; + + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock() + ; + $cartItem2Stub + ->expects($this->any()) + ->method('getProductSaleElements') + ->will($this->returnValue($pse2)) + ; + $cartItem2Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)) + ; + $cartItem2Stub + ->expects($this->any()) + ->method('getPrice') + ->will($this->returnValue(150)) + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + if ($count == 1) + $ret = [$cartItem1Stub]; + else + $ret = [$cartItem1Stub, $cartItem2Stub]; + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue($ret)); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) { + + $attrCombination1 = $this->getMockBuilder('\Thelia\Model\AttributeCombination') + ->disableOriginalConstructor() + ->getMock() + ; + + $attrCombination1 + ->expects($this->any()) + ->method('getAttributeAvId') + ->will($this->returnValue(30)) + ; + + $pse1 = $this->getMockBuilder('\Thelia\Model\ProductSaleElements') + ->disableOriginalConstructor() + ->getMock() + ; + $pse1 + ->expects($this->any()) + ->method('getAttributeCombinations') + ->will($this->returnValue([$attrCombination1])) + ; + + $cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock() + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getProductSaleElements') + ->will($this->returnValue($pse1)) + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(1)) + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getPrice') + ->will($this->returnValue(100)) + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue([$cartItem1Stub])); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function testSet() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemovePercentageOnAttributeValues($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10.00, 'attribute_avs' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $condition1 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 40.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition1->setValidatorsFromForm($operators, $values); + + $condition2 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 400.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition2->setValidatorsFromForm($operators, $values); + + $conditions = new ConditionCollection(); + $conditions[] = $condition1; + $conditions[] = $condition2; + $coupon->setConditions($conditions); + + $this->assertEquals('TEST', $coupon->getCode()); + $this->assertEquals('TEST Coupon', $coupon->getTitle()); + $this->assertEquals('This is a test coupon title', $coupon->getShortDescription()); + $this->assertEquals('This is a test coupon description', $coupon->getDescription()); + + $this->assertEquals(true, $coupon->isCumulative()); + $this->assertEquals(true, $coupon->isRemovingPostage()); + $this->assertEquals(true, $coupon->isAvailableOnSpecialOffers()); + $this->assertEquals(true, $coupon->isEnabled()); + + $this->assertEquals(254, $coupon->getMaxUsage()); + $this->assertEquals($date, $coupon->getExpirationDate()); + } + + public function testMatchOne() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemovePercentageOnAttributeValues($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10.00, 'attribute_avs' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateMatchingCart($stubFacade, 1); + + $this->assertEquals(10.00, $coupon->exec()); + } + + public function testMatchSeveral() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemovePercentageOnAttributeValues($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10.00, 'attribute_avs' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateMatchingCart($stubFacade, 2); + + $this->assertEquals(40.00, $coupon->exec()); + } + + public function testNoMatch() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemovePercentageOnAttributeValues($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10.00, 'attribute_avs' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateNoMatchingCart($stubFacade); + + $this->assertEquals(0.00, $coupon->exec()); + } + + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name'); + + /** @var FacadeInterface $stubFacade */ + $coupon = new RemovePercentageOnAttributeValues($stubFacade); + + $actual = $coupon->getName(); + $expected = 'Coupon test name'; + $this->assertEquals($expected, $actual); + } + + public function testGetToolTip() + { + $tooltip = 'Coupon test tooltip'; + $stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip); + + /** @var FacadeInterface $stubFacade */ + $coupon = new RemovePercentageOnAttributeValues($stubFacade); + + $actual = $coupon->getToolTip(); + $expected = $tooltip; + $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() + { + } +} diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemovePercentageOnCategoriesTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemovePercentageOnCategoriesTest.php new file mode 100644 index 000000000..90b845531 --- /dev/null +++ b/core/lib/Thelia/Tests/Coupon/Type/RemovePercentageOnCategoriesTest.php @@ -0,0 +1,346 @@ + + */ +class RemovePercentageOnCategoriesTest 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() + { + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $currencies = CurrencyQuery::create(); + $currencies = $currencies->find(); + $stubFacade->expects($this->any()) + ->method('getAvailableCurrencies') + ->will($this->returnValue($currencies)); + + $stubFacade->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue($cartTotalPrice)); + + $stubFacade->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue($checkoutCurrency)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + return $stubFacade; + } + + public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) { + + $category1 = new Category(); + $category1->setId(10); + + $category2 = new Category(); + $category2->setId(20); + + $category3 = new Category(); + $category3->setId(30); + + $product1 = new Product(); + $product1->addCategory($category1)->addCategory($category2); + + $product2 = new Product(); + $product2->addCategory($category3); + + $cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem1Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product1)) + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(1)) + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getPrice') + ->will($this->returnValue(100)) + ; + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem2Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product2)) + ; + $cartItem2Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)) + ; + $cartItem2Stub + ->expects($this->any()) + ->method('getPrice') + ->will($this->returnValue(200)) + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue([$cartItem1Stub, $cartItem2Stub])); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) { + + $category3 = new Category(); + $category3->setId(30); + + $product2 = new Product(); + $product2->addCategory($category3); + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem2Stub->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product2)); + + $cartItem2Stub->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)); + + $cartItem2Stub + ->expects($this->any()) + ->method('getPrice') + ->will($this->returnValue(200000)) + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue([$cartItem2Stub])); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function testSet() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemovePercentageOnCategories($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10, 'categories' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $condition1 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 40.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition1->setValidatorsFromForm($operators, $values); + + $condition2 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 400.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition2->setValidatorsFromForm($operators, $values); + + $conditions = new ConditionCollection(); + $conditions[] = $condition1; + $conditions[] = $condition2; + $coupon->setConditions($conditions); + + $this->assertEquals('TEST', $coupon->getCode()); + $this->assertEquals('TEST Coupon', $coupon->getTitle()); + $this->assertEquals('This is a test coupon title', $coupon->getShortDescription()); + $this->assertEquals('This is a test coupon description', $coupon->getDescription()); + + $this->assertEquals(true, $coupon->isCumulative()); + $this->assertEquals(true, $coupon->isRemovingPostage()); + $this->assertEquals(true, $coupon->isAvailableOnSpecialOffers()); + $this->assertEquals(true, $coupon->isEnabled()); + + $this->assertEquals(254, $coupon->getMaxUsage()); + $this->assertEquals($date, $coupon->getExpirationDate()); + } + + public function testMatch() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemovePercentageOnCategories($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10, 'categories' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateMatchingCart($stubFacade); + + $this->assertEquals(10.00, $coupon->exec()); + } + + public function testNoMatch() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemovePercentageOnCategories($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10, 'categories' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateNoMatchingCart($stubFacade); + + $this->assertEquals(0.00, $coupon->exec()); + } + + /** + * @covers Thelia\Coupon\Type\RemoveXAmount::getName + */ + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name'); + + /** @var FacadeInterface $stubFacade */ + $coupon = new RemovePercentageOnCategories($stubFacade); + + $actual = $coupon->getName(); + $expected = 'Coupon test name'; + $this->assertEquals($expected, $actual); + } + + /** + * @covers Thelia\Coupon\Type\RemoveXAmount::getToolTip + */ + public function testGetToolTip() + { + $tooltip = 'Coupon test tooltip'; + $stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip); + + /** @var FacadeInterface $stubFacade */ + $coupon = new RemovePercentageOnCategories($stubFacade); + + $actual = $coupon->getToolTip(); + $expected = $tooltip; + $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() + { + } +} diff --git a/core/lib/Thelia/Tests/Coupon/Type/RemovePercentageOnProductsTest.php b/core/lib/Thelia/Tests/Coupon/Type/RemovePercentageOnProductsTest.php new file mode 100644 index 000000000..4c150b969 --- /dev/null +++ b/core/lib/Thelia/Tests/Coupon/Type/RemovePercentageOnProductsTest.php @@ -0,0 +1,360 @@ + + */ +class RemovePercentageOnProductsTest 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() + { + } + + /** + * Generate adapter stub + * + * @param int $cartTotalPrice Cart total price + * @param string $checkoutCurrency Checkout currency + * @param string $i18nOutput Output from each translation + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + public function generateFacadeStub($cartTotalPrice = 400, $checkoutCurrency = 'EUR', $i18nOutput = '') + { + $stubFacade = $this->getMockBuilder('\Thelia\Coupon\BaseFacade') + ->disableOriginalConstructor() + ->getMock(); + + $currencies = CurrencyQuery::create(); + $currencies = $currencies->find(); + $stubFacade->expects($this->any()) + ->method('getAvailableCurrencies') + ->will($this->returnValue($currencies)); + + $stubFacade->expects($this->any()) + ->method('getCartTotalPrice') + ->will($this->returnValue($cartTotalPrice)); + + $stubFacade->expects($this->any()) + ->method('getCheckoutCurrency') + ->will($this->returnValue($checkoutCurrency)); + + $stubFacade->expects($this->any()) + ->method('getConditionEvaluator') + ->will($this->returnValue(new ConditionEvaluator())); + + $stubTranslator = $this->getMockBuilder('\Thelia\Core\Translation\Translator') + ->disableOriginalConstructor() + ->getMock(); + $stubTranslator->expects($this->any()) + ->method('trans') + ->will($this->returnValue($i18nOutput)); + + $stubFacade->expects($this->any()) + ->method('getTranslator') + ->will($this->returnValue($stubTranslator)); + + return $stubFacade; + } + + public function generateMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade, $count) { + + $product1 = new Product(); + $product1->setId(10); + + $product2 = new Product(); + $product2->setId(20); + + $cartItem1Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem1Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product1)) + ; + + $cartItem1Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(1)) + ; + $cartItem1Stub + ->expects($this->any()) + ->method('getPrice') + ->will($this->returnValue(100)) + + ; + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem2Stub + ->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product2)); + + $cartItem2Stub + ->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)) + ; + $cartItem2Stub + ->expects($this->any()) + ->method('getPrice') + ->will($this->returnValue(150)) + + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + if ($count == 1) + $ret = [$cartItem1Stub]; + else + $ret = [$cartItem1Stub, $cartItem2Stub]; + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue($ret)); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function generateNoMatchingCart(\PHPUnit_Framework_MockObject_MockObject $stubFacade) { + + $product2 = new Product(); + $product2->setId(30); + + $cartItem2Stub = $this->getMockBuilder('\Thelia\Model\CartItem') + ->disableOriginalConstructor() + ->getMock(); + + $cartItem2Stub->expects($this->any()) + ->method('getProduct') + ->will($this->returnValue($product2)) + ; + $cartItem2Stub->expects($this->any()) + ->method('getQuantity') + ->will($this->returnValue(2)) + ; + $cartItem2Stub + ->expects($this->any()) + ->method('getPrice') + ->will($this->returnValue(11000)) + ; + + $cartStub = $this->getMockBuilder('\Thelia\Model\Cart') + ->disableOriginalConstructor() + ->getMock(); + + $cartStub + ->expects($this->any()) + ->method('getCartItems') + ->will($this->returnValue([$cartItem2Stub])); + + $stubFacade->expects($this->any()) + ->method('getCart') + ->will($this->returnValue($cartStub)); + } + + public function testSet() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemovePercentageOnProducts($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10.00, 'products' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $condition1 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 40.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition1->setValidatorsFromForm($operators, $values); + + $condition2 = new MatchForTotalAmount($stubFacade); + $operators = array( + MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR, + MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL + ); + $values = array( + MatchForTotalAmount::CART_TOTAL => 400.00, + MatchForTotalAmount::CART_CURRENCY => 'EUR' + ); + $condition2->setValidatorsFromForm($operators, $values); + + $conditions = new ConditionCollection(); + $conditions[] = $condition1; + $conditions[] = $condition2; + $coupon->setConditions($conditions); + + $this->assertEquals('TEST', $coupon->getCode()); + $this->assertEquals('TEST Coupon', $coupon->getTitle()); + $this->assertEquals('This is a test coupon title', $coupon->getShortDescription()); + $this->assertEquals('This is a test coupon description', $coupon->getDescription()); + + $this->assertEquals(true, $coupon->isCumulative()); + $this->assertEquals(true, $coupon->isRemovingPostage()); + $this->assertEquals(true, $coupon->isAvailableOnSpecialOffers()); + $this->assertEquals(true, $coupon->isEnabled()); + + $this->assertEquals(254, $coupon->getMaxUsage()); + $this->assertEquals($date, $coupon->getExpirationDate()); + } + + public function testMatchOne() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemovePercentageOnProducts($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10.00, 'products' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateMatchingCart($stubFacade, 1); + + $this->assertEquals(10.00, $coupon->exec()); + } + + public function testMatchSeveral() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemovePercentageOnProducts($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10.00, 'products' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateMatchingCart($stubFacade, 2); + + $this->assertEquals(40.00, $coupon->exec()); + } + + public function testNoMatch() + { + $stubFacade = $this->generateFacadeStub(); + + $coupon = new RemovePercentageOnProducts($stubFacade); + + $date = new \DateTime(); + + $coupon->set( + $stubFacade, + 'TEST', 'TEST Coupon', 'This is a test coupon title', 'This is a test coupon description', + array('percentage' => 10.00, 'products' => [10, 20]), + true, true, true, true, + 254, + $date->setTimestamp(strtotime("today + 3 months")), + new ObjectCollection(), + new ObjectCollection(), + false + ); + + $this->generateNoMatchingCart($stubFacade); + + $this->assertEquals(0.00, $coupon->exec()); + } + + public function testGetName() + { + $stubFacade = $this->generateFacadeStub(399, 'EUR', 'Coupon test name'); + + /** @var FacadeInterface $stubFacade */ + $coupon = new RemovePercentageOnProducts($stubFacade); + + $actual = $coupon->getName(); + $expected = 'Coupon test name'; + $this->assertEquals($expected, $actual); + } + + public function testGetToolTip() + { + $tooltip = 'Coupon test tooltip'; + $stubFacade = $this->generateFacadeStub(399, 'EUR', $tooltip); + + /** @var FacadeInterface $stubFacade */ + $coupon = new RemovePercentageOnProducts($stubFacade); + + $actual = $coupon->getToolTip(); + $expected = $tooltip; + $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() + { + } +}