<?php
use FireflyIII\Repositories\Currency\CurrencyRepository;
use League\FactoryMuffin\Facade as FactoryMuffin;

/**
 * Generated by PHPUnit_SkeletonGenerator on 2015-05-05 at 19:19:32.
 */
class CurrencyRepositoryTest extends TestCase
{
    /**
     * @var CurrencyRepository
     */
    protected $object;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    public function setUp()
    {
        $this->object = new CurrencyRepository;
        parent::setUp();
    }

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

    /**
     * @covers FireflyIII\Repositories\Currency\CurrencyRepository::countJournals
     */
    public function testCountJournals()
    {
        $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
        $count    = $this->object->countJournals($currency);
        $this->assertEquals(0, $count);
    }

    /**
     * @covers FireflyIII\Repositories\Currency\CurrencyRepository::get
     */
    public function testGet()
    {
        FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
        FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');

        $set = $this->object->get();
        $this->assertCount(3, $set); // EUR is already present.
    }

    /**
     * @covers FireflyIII\Repositories\Currency\CurrencyRepository::getCurrencyByPreference
     */
    public function testGetCurrencyByPreference()
    {
        $currency         = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
        $preference       = FactoryMuffin::create('FireflyIII\Models\Preference');
        $preference->data = $currency->code;
        $preference->save();
        $found = $this->object->getCurrencyByPreference($preference);
        $this->assertEquals($currency->id, $found->id);
    }

    /**
     * @covers FireflyIII\Repositories\Currency\CurrencyRepository::getCurrencyByPreference
     */
    public function testGetCurrencyByPreferenceNull()
    {
        FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
        $preference       = FactoryMuffin::create('FireflyIII\Models\Preference');
        $preference->data = 'ABC';
        $preference->save();
        $found = $this->object->getCurrencyByPreference($preference);
        $this->assertEquals(1, $found->id); // EUR is first and will be set.
    }

    /**
     * @covers FireflyIII\Repositories\Currency\CurrencyRepository::store
     */
    public function testStore()
    {
        $data = [
            'name'   => 'Some Currency',
            'code'   => 'ABC',
            'symbol' => 'S'
        ];

        $currency = $this->object->store($data);
        $this->assertEquals($data['name'], $currency->name);
    }

    /**
     * @covers FireflyIII\Repositories\Currency\CurrencyRepository::update
     */
    public function testUpdate()
    {
        $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');

        $data = [
            'name'   => 'Some Currency',
            'code'   => 'ABC',
            'symbol' => 'S'
        ];

        $newCurrency = $this->object->update($currency, $data);
        $this->assertEquals($data['name'], $newCurrency->name);
        $this->assertEquals($currency->id, $newCurrency->id);
    }
}
