<?php
/*************************************************************************************/
/*      This file is part of the Thelia package.                                     */
/*                                                                                   */
/*      Copyright (c) OpenStudio                                                     */
/*      email : dev@thelia.net                                                       */
/*      web : http://www.thelia.net                                                  */
/*                                                                                   */
/*      For the full copyright and license information, please view the LICENSE.txt  */
/*      file that was distributed with this source code.                             */
/*************************************************************************************/

namespace Thelia\Condition;

use Thelia\Condition\Implementation\MatchForTotalAmount;
use Thelia\Model\CurrencyQuery;

/**
 * Unit Test ConditionCollection Class
 * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-11-17 at 18:59:24.
 *
 * @package Condition
 * @author  Guillaume MOREL <gmorel@openstudio.fr>
 *
 */
class ConditionCollectionTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var ConditionCollection
     */
    protected $object;

    /**
     * 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;
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     */
    protected function tearDown()
    {
    }

    /**
     *
     */
    public function testGetConditions()
    {
        $stubFacade = $this->generateFacadeStub();

        $condition1 = new MatchForTotalAmount($stubFacade);
        $operators = array(
            MatchForTotalAmount::CART_TOTAL => Operators::EQUAL,
            MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
        );
        $values = array(
            MatchForTotalAmount::CART_TOTAL => 400,
            MatchForTotalAmount::CART_CURRENCY => 'EUR');
        $condition1->setValidatorsFromForm($operators, $values);

        $collection = new ConditionCollection();
        $collection[] = $condition1;

        $expected = $condition1;
        $actual = $collection[0];

        $this->assertEquals($expected, $actual);

        $this->assertFalse($collection->count() == 0);
    }

    /**
     * @covers Thelia\Condition\ConditionCollection::count
     */
    public function testIsEmpty()
    {
        $collection = new ConditionCollection();
        $this->assertTrue($collection->count() == 0);
    }

    /**
     * @covers Thelia\Condition\ConditionCollection::__toString
     */
    public function test__toString()
    {
        $stubFacade = $this->generateFacadeStub();

        $condition1 = new MatchForTotalAmount($stubFacade);
        $operators1 = array(
            MatchForTotalAmount::CART_TOTAL => Operators::SUPERIOR,
            MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
        );
        $values1 = array(
            MatchForTotalAmount::CART_TOTAL => 400,
            MatchForTotalAmount::CART_CURRENCY => 'EUR');
        $condition1->setValidatorsFromForm($operators1, $values1);

        $condition2 = new MatchForTotalAmount($stubFacade);
        $operators2 = array(
            MatchForTotalAmount::CART_TOTAL => Operators::INFERIOR,
            MatchForTotalAmount::CART_CURRENCY => Operators::EQUAL
        );
        $values2 = array(
            MatchForTotalAmount::CART_TOTAL => 600,
            MatchForTotalAmount::CART_CURRENCY => 'EUR');
        $condition2->setValidatorsFromForm($operators2, $values2);

        $collection = new ConditionCollection();
        $collection[] = $condition1;
        $collection[] = $condition2;

        $expected = '[{"conditionServiceId":"thelia.condition.match_for_total_amount","operators":{"price":">","currency":"=="},"values":{"price":400,"currency":"EUR"}},{"conditionServiceId":"thelia.condition.match_for_total_amount","operators":{"price":"<","currency":"=="},"values":{"price":600,"currency":"EUR"}}]';
        $actual = $collection->__toString();
        ;

        $this->assertEquals($expected, $actual);
    }
}
