firefly-iii/tests/Feature/Controllers/PiggyBankControllerTest.php

336 lines
12 KiB
PHP
Raw Normal View History

2017-02-12 05:00:11 -06:00
<?php
/**
* PiggyBankControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace Tests\Feature\Controllers;
2017-03-05 11:15:38 -06:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2017-02-12 05:32:13 -06:00
use FireflyIII\Models\PiggyBank;
2017-03-05 11:15:38 -06:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2017-02-17 13:14:38 -06:00
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
2017-03-05 11:15:38 -06:00
use Illuminate\Support\Collection;
2017-02-12 05:00:11 -06:00
use Tests\TestCase;
2017-03-05 11:15:38 -06:00
/**
* Class PiggyBankControllerTest
*
* @package Tests\Feature\Controllers
*/
2017-02-12 05:00:11 -06:00
class PiggyBankControllerTest extends TestCase
{
2017-02-12 05:21:44 -06:00
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::add
*/
public function testAdd()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('piggy-banks.add', [1]));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::addMobile
*/
public function testAddMobile()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('piggy-banks.add-money-mobile', [1]));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::create
*/
public function testCreate()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$account = factory(Account::class)->make();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')
->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection([$account]))->once();
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('piggy-banks.create'));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::delete
*/
public function testDelete()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('piggy-banks.delete', [1]));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::destroy
*/
public function testDestroy()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$repository->shouldReceive('destroy')->andReturn(true);
2017-03-05 11:15:38 -06:00
2017-02-12 05:21:44 -06:00
$this->session(['piggy-banks.delete.url' => 'http://localhost']);
$this->be($this->user());
$response = $this->post(route('piggy-banks.destroy', [2]));
$response->assertStatus(302);
$response->assertSessionHas('success');
2017-02-12 05:32:13 -06:00
$response->assertRedirect(route('index'));
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::edit
*/
public function testEdit()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$account = factory(Account::class)->make();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')
->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection([$account]))->once();
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('piggy-banks.edit', [1]));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::index
2017-02-17 13:14:38 -06:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController::__construct
2017-02-12 05:21:44 -06:00
*/
public function testIndex()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('getPiggyBanks')->andReturn(new Collection);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('piggy-banks.index'));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::order
*/
public function testOrder()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('reset');
$repository->shouldReceive('setOrder')->times(2);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
2017-03-05 11:15:38 -06:00
$response = $this->post(route('piggy-banks.order'), ['order' => [1, 2]]);
2017-02-12 05:21:44 -06:00
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::postAdd
*/
public function testPostAdd()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('createEvent')->once();
2017-02-12 05:21:44 -06:00
$data = ['amount' => '1.123'];
$this->be($this->user());
$response = $this->post(route('piggy-banks.add', [1]), $data);
$response->assertStatus(302);
2017-02-12 05:32:13 -06:00
$response->assertRedirect(route('piggy-banks.index'));
2017-02-12 05:21:44 -06:00
$response->assertSessionHas('success');
}
/**
* Add the exact amount to fill a piggy bank
*
* @covers \FireflyIII\Http\Controllers\PiggyBankController::postAdd
*/
public function testPostAddExact()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('createEvent')->once();
2017-02-12 05:21:44 -06:00
// find a piggy with current amount = 0.
$piggy = PiggyBank::leftJoin('piggy_bank_repetitions', 'piggy_bank_repetitions.piggy_bank_id', '=', 'piggy_banks.id')
->where('currentamount', 0)
->first(['piggy_banks.id', 'targetamount']);
$data = ['amount' => strval($piggy->targetamount)];
$this->be($this->user());
$response = $this->post(route('piggy-banks.add', [$piggy->id]), $data);
$response->assertStatus(302);
2017-02-12 05:32:13 -06:00
$response->assertRedirect(route('piggy-banks.index'));
2017-02-12 05:21:44 -06:00
$response->assertSessionHas('success');
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::postRemove
*/
public function testPostRemove()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('createEvent')->once();
2017-02-12 05:21:44 -06:00
$data = ['amount' => '1.123'];
$this->be($this->user());
$response = $this->post(route('piggy-banks.remove', [1]), $data);
$response->assertStatus(302);
2017-02-12 05:32:13 -06:00
$response->assertRedirect(route('piggy-banks.index'));
2017-02-12 05:21:44 -06:00
$response->assertSessionHas('success');
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::remove
*/
public function testRemove()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('piggy-banks.remove', [1]));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::removeMobile
*/
public function testRemoveMobile()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('piggy-banks.remove-money-mobile', [1]));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::show
*/
public function testShow()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('getEvents')->andReturn(new Collection);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('piggy-banks.show', [1]));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::store
*/
public function testStore()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('store')->andReturn(new PiggyBank);
2017-02-12 05:21:44 -06:00
$this->session(['piggy-banks.create.url' => 'http://localhost']);
$data = [
'name' => 'Piggy ' . rand(999, 10000),
'targetamount' => '100.123',
'account_id' => 2,
'amount_currency_id_targetamount' => 1,
];
$this->be($this->user());
$response = $this->post(route('piggy-banks.store'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
2017-02-12 05:32:13 -06:00
$response->assertRedirect(route('index'));
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController::update
*/
public function testUpdate()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('update')->andReturn(new PiggyBank);
2017-02-12 05:21:44 -06:00
$this->session(['piggy-banks.edit.url' => 'http://localhost']);
$data = [
'name' => 'Updated Piggy ' . rand(999, 10000),
'targetamount' => '100.123',
'account_id' => 2,
'amount_currency_id_targetamount' => 1,
];
$this->be($this->user());
$response = $this->post(route('piggy-banks.update', [3]), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
2017-02-12 05:32:13 -06:00
$response->assertRedirect(route('index'));
2017-02-12 05:21:44 -06:00
}
2017-02-16 15:33:32 -06:00
}