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

419 lines
16 KiB
PHP
Raw Normal View History

2017-02-12 05:00:11 -06:00
<?php
/**
* PiggyBankControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:42:21 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-02-12 05:00:11 -06:00
*/
2017-07-07 23:28:44 -05:00
declare(strict_types=1);
2017-02-12 05:00:11 -06:00
namespace Tests\Feature\Controllers;
2018-03-07 03:18:22 -06:00
use Amount;
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;
2018-03-07 03:18:22 -06:00
use FireflyIII\Models\TransactionCurrency;
2017-03-05 11:15:38 -06:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-03-07 03:18:22 -06:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-03-05 11:15:38 -06:00
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;
2018-03-24 00:08:50 -05:00
use Log;
2018-06-01 15:04:52 -05:00
use Mockery;
2017-03-17 10:34:57 -05:00
use Steam;
2017-02-12 05:00:11 -06:00
use Tests\TestCase;
2017-03-05 11:15:38 -06:00
/**
* Class PiggyBankControllerTest
*
2017-08-12 03:27:45 -05:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-03-05 11:15:38 -06:00
*/
2017-02-12 05:00:11 -06:00
class PiggyBankControllerTest extends TestCase
{
2018-03-24 00:08:50 -05:00
/**
*
*/
public function setUp()
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
2018-03-24 00:08:50 -05:00
}
2017-02-12 05:21:44 -06:00
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testAdd(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
2018-02-28 08:50:00 -06:00
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
2017-03-05 11:15:38 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-02-28 08:50:00 -06:00
$piggyRepos->shouldReceive('getCurrentAmount')->andReturn('0');
2018-04-27 04:29:09 -05:00
$piggyRepos->shouldReceive('leftOnAccount')->andReturn('0');
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('piggy-banks.add', [1]));
$response->assertStatus(200);
}
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testAddMobile(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
2018-02-28 08:50:00 -06:00
$piggyRepos = $this->mock(PiggyBankRepositoryInterface::class);
2017-03-05 11:15:38 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-02-28 08:50:00 -06:00
$piggyRepos->shouldReceive('getCurrentAmount')->andReturn('0');
2018-04-27 04:29:09 -05:00
$piggyRepos->shouldReceive('leftOnAccount')->andReturn('0');
2017-03-05 11:15:38 -06:00
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">');
}
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testCreate(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
2018-03-07 03:18:22 -06:00
2017-03-05 11:15:38 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2018-03-07 03:18:22 -06:00
// new account list thing.
$currency = TransactionCurrency::first();
$account = factory(Account::class)->make();
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$currencyRepos->shouldReceive('findNull')->andReturn($currency);
2017-02-12 05:21:44 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-03-07 03:18:22 -06:00
Amount::shouldReceive('getDefaultCurrency')->andReturn($currency);
Amount::shouldReceive('balance')->andReturn('0');
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-03-07 03:18:22 -06:00
$this->be($this->user());
$response = $this->get(route('piggy-banks.create'));
2018-03-07 03:18:22 -06:00
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
2017-02-12 05:21:44 -06:00
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testDelete(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-05 11:15:38 -06:00
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">');
}
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testDestroy(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-05 11:15:38 -06:00
2017-02-12 05:21:44 -06:00
$repository->shouldReceive('destroy')->andReturn(true);
2017-03-18 14:53:44 -05:00
$this->session(['piggy-banks.delete.uri' => 'http://localhost']);
2017-02-12 05:21:44 -06:00
$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
}
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testEdit(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
$account = factory(Account::class)->make();
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-03-07 03:18:22 -06:00
// mock stuff for new account list thing.
$currency = TransactionCurrency::first();
$account = factory(Account::class)->make();
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$currencyRepos->shouldReceive('findNull')->andReturn($currency);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2017-03-05 11:15:38 -06:00
$accountRepos->shouldReceive('getAccountsByType')
2018-03-07 03:18:22 -06:00
->withArgs([[AccountType::ASSET, AccountType::DEFAULT]])->andReturn(new Collection([$account]))->once();
Amount::shouldReceive('getDefaultCurrency')->andReturn($currency);
Amount::shouldReceive('balance')->andReturn('0');
2017-03-05 11:15:38 -06:00
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">');
}
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testIndex(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
2018-07-01 02:27:22 -05:00
$first = $this->user()->transactionJournals()->inRandomOrder()->first();
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$piggies = $this->user()->piggyBanks()->take(2)->get();
$journalRepos->shouldReceive('firstNull')->once()->andReturn($first);
$repository->shouldReceive('getPiggyBanks')->andReturn($piggies);
2018-02-17 07:14:26 -06:00
$repository->shouldReceive('getCurrentAmount')->andReturn('10');
2018-04-27 04:29:09 -05:00
$repository->shouldReceive('setUser');
$repository->shouldReceive('correctOrder');
2018-05-31 13:04:19 -05:00
$repository->shouldReceive('getSuggestedMonthlyAmount')->andReturn('1');
2017-03-17 10:34:57 -05:00
2018-04-27 04:29:09 -05:00
Steam::shouldReceive('balance')->twice()->andReturn('1');
2017-03-05 11:15:38 -06:00
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">');
}
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
public function testPostAdd(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('canAddAmount')->once()->andReturn(true);
$repository->shouldReceive('addAmount')->once()->andReturn(true);
2017-03-05 11:15:38 -06:00
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 way too much
2017-02-12 05:21:44 -06:00
*
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
public function testPostAddTooMuch(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('canAddAmount')->once()->andReturn(false);
2017-03-05 11:15:38 -06:00
$data = ['amount' => '1000'];
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->post(route('piggy-banks.add', [1]), $data);
2017-02-12 05:21:44 -06:00
$response->assertStatus(302);
2017-02-12 05:32:13 -06:00
$response->assertRedirect(route('piggy-banks.index'));
$response->assertSessionHas('error');
2017-02-12 05:21:44 -06:00
}
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
public function testPostRemove(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('canRemoveAmount')->once()->andReturn(true);
$repository->shouldReceive('removeAmount')->once()->andReturn(true);
2017-03-05 11:15:38 -06:00
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');
}
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
*/
public function testPostRemoveTooMuch(): void
{
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('canRemoveAmount')->once()->andReturn(false);
$data = ['amount' => '1.123'];
$this->be($this->user());
$response = $this->post(route('piggy-banks.remove', [1]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('piggy-banks.index'));
$response->assertSessionHas('error');
}
2017-02-12 05:21:44 -06:00
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
public function testRemove(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-05 11:15:38 -06:00
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('piggy-banks.remove', [1]));
$response->assertStatus(200);
}
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
public function testRemoveMobile(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-05 11:15:38 -06:00
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">');
}
/**
2018-06-01 15:04:52 -05:00
* Test setting of order/
*
* @covers \FireflyIII\Http\Controllers\PiggyBankController
*/
public function testSetOrder(): void
{
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$repository->shouldReceive('setOrder')->once()->withArgs([Mockery::any(), 3])->andReturn(false);
$data = ['order' => '3'];
$this->be($this->user());
$response = $this->post(route('piggy-banks.set-order', [1]), $data);
$response->assertStatus(200);
$response->assertExactJson(['data' => 'OK']);
}
/**
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2017-02-12 05:21:44 -06:00
*/
public function testShow(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
2018-07-01 02:27:22 -05:00
$first = $this->user()->transactionJournals()->inRandomOrder()->first();
2017-03-05 11:15:38 -06:00
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2018-05-31 13:04:19 -05:00
$repository->shouldReceive('setUser')->once();
2018-07-01 02:27:22 -05:00
$journalRepos->shouldReceive('firstNull')->once()->andReturn($first);
2017-03-05 11:15:38 -06:00
$repository->shouldReceive('getEvents')->andReturn(new Collection);
2018-05-31 13:04:19 -05:00
$repository->shouldReceive('getSuggestedMonthlyAmount')->andReturn('1');
$repository->shouldReceive('getCurrentAmount')->andReturn('1');
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">');
}
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2018-03-03 10:16:47 -06:00
* @covers \FireflyIII\Http\Requests\PiggyBankFormRequest
2017-02-12 05:21:44 -06:00
*/
public function testStore(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-05 11:15:38 -06:00
$repository->shouldReceive('store')->andReturn(new PiggyBank);
2017-03-18 14:53:44 -05:00
$this->session(['piggy-banks.create.uri' => 'http://localhost']);
2017-02-12 05:21:44 -06:00
$data = [
2018-03-28 12:38:20 -05:00
'name' => 'Piggy ' . random_int(999, 10000),
2017-02-12 05:21:44 -06:00
'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
}
/**
2018-06-01 15:04:52 -05:00
* @covers \FireflyIII\Http\Controllers\PiggyBankController
2018-03-03 10:16:47 -06:00
* @covers \FireflyIII\Http\Requests\PiggyBankFormRequest
2017-02-12 05:21:44 -06:00
*/
public function testUpdate(): void
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
$repository = $this->mock(PiggyBankRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-05 11:15:38 -06:00
$repository->shouldReceive('update')->andReturn(new PiggyBank);
2017-03-18 14:53:44 -05:00
$this->session(['piggy-banks.edit.uri' => 'http://localhost']);
2017-02-12 05:21:44 -06:00
$data = [
2018-03-03 10:16:47 -06:00
'id' => 3,
2018-03-28 12:38:20 -05:00
'name' => 'Updated Piggy ' . random_int(999, 10000),
2017-02-12 05:21:44 -06:00
'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
}