<?php
use Carbon\Carbon;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Models\Reminder;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepository;
use League\FactoryMuffin\Facade as FactoryMuffin;

/**
 * @SuppressWarnings(PHPMD.TooManyMethods)
 * Generated by PHPUnit_SkeletonGenerator on 2015-05-05 at 19:19:32.
 */
class PiggyBankRepositoryTest extends TestCase
{
    /**
     * @var PiggyBankRepository
     */
    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 PiggyBankRepository;
        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\PiggyBank\PiggyBankRepository::createEvent
     */
    public function testCreateEvent()
    {
        $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
        $this->object->createEvent($piggyBank, 100);

        $this->assertCount(1, $piggyBank->piggybankevents()->get());
    }


    /**
     * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::destroy
     * @covers FireflyIII\Providers\EventServiceProvider::boot
     * @covers FireflyIII\Providers\EventServiceProvider::registerDeleteEvents
     */
    public function testDestroy()
    {
        $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
        $reminder  = FactoryMuffin::create('FireflyIII\Models\Reminder');

        // attach reminders to the piggy bank:
        $reminder->remindersable_id   = $piggyBank->id;
        $reminder->remindersable_type = 'FireflyIII\Models\PiggyBank';
        $reminder->save();

        $this->object->destroy($piggyBank);

        $this->assertCount(0, PiggyBank::where('id', $piggyBank->id)->whereNull('deleted_at')->get());
        $this->assertCount(0, Reminder::where('id', $reminder->id)->whereNull('deleted_at')->get());

    }

    /**
     * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::getEventSummarySet
     */
    public function testGetEventSummarySet()
    {
        $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
        $set       = $this->object->getEventSummarySet($piggyBank);

        $this->assertCount(0, $set);
    }

    /**
     * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::getEvents
     */
    public function testGetEvents()
    {
        $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
        $set       = $this->object->getEvents($piggyBank);

        $this->assertCount(0, $set);
    }

    /**
     * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::getPiggyBanks
     */
    public function testGetPiggyBanks()
    {
        $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
        $this->be($piggyBank->account->user);
        $set = $this->object->getPiggyBanks();

        $this->assertCount(1, $set);
    }

    /**
     * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::reset
     */
    public function testReset()
    {
        $piggyBank        = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
        $piggyBank->order = 4;
        $piggyBank->save();
        $this->be($piggyBank->account->user);
        $this->object->reset();

        $this->assertCount(1, PiggyBank::where('order', 0)->get());
    }

    /**
     * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::setOrder
     */
    public function testSetOrder()
    {
        $piggyBank        = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
        $piggyBank->order = 4;
        $this->be($piggyBank->account->user);
        $piggyBank->save();

        $this->object->setOrder($piggyBank->id, 3);
        $newPiggy = PiggyBank::find($piggyBank->id);
        $this->assertEquals(3, $newPiggy->order);
    }

    /**
     * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::store
     * @covers FireflyIII\Providers\EventServiceProvider::boot
     * @covers FireflyIII\Providers\EventServiceProvider::registerCreateEvents
     */
    public function testStore()
    {
        $account = FactoryMuffin::create('FireflyIII\Models\Account');

        $data = [
            'remind_me'     => 1,
            'account_id'    => $account->id,
            'name'          => 'Some piggy',
            'targetamount'  => 100,
            'reminder_skip' => 0,
            'order'         => 1,

        ];

        $piggyBank = $this->object->store($data);

        $this->assertEquals(1, $piggyBank->id);
    }

    /**
     * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::store
     * @covers FireflyIII\Providers\EventServiceProvider::boot
     * @covers FireflyIII\Providers\EventServiceProvider::registerCreateEvents
     */
    public function testStoreRedirect()
    {
        $account = FactoryMuffin::create('FireflyIII\Models\Account');

        $data = [
            'remind_me'      => 1,
            'account_id'     => $account->id,
            'name'           => 'Some piggy',
            'targetamount'   => 100,
            'create_another' => 1,
            'reminder_skip'  => 0,
            'order'          => 1,

        ];

        $piggyBank = $this->object->store($data);

        $this->assertEquals(1, $piggyBank->id);
    }

    /**
     * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::update
     */
    public function testUpdate()
    {
        $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');

        $data = [
            'name'         => 'Update piggy ' . rand(1, 100),
            'account_id'   => $piggyBank->account_id,
            'targetamount' => 101,
            'targetdate'   => new Carbon,
            'reminder'     => null,
            'startdate'    => new Carbon,
            'remind_me'    => '1'
        ];

        $new = $this->object->update($piggyBank, $data);

        $this->assertEquals($data['name'], $new->name);
        $this->assertEquals($piggyBank->id, $new->id);
    }
}
