Expand some tests.

This commit is contained in:
James Cole 2017-03-04 19:14:36 +01:00
parent c206a95d55
commit 884d6c59a2
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 170 additions and 67 deletions

View File

@ -270,8 +270,8 @@ class AccountController extends Controller
// prep for current period // prep for current period
if (strlen($moment) === 0) { if (strlen($moment) === 0) {
$start = session('start', Navigation::startOfPeriod(new Carbon, $range)); $start = clone session('start', Navigation::startOfPeriod(new Carbon, $range));
$end = session('end', Navigation::endOfPeriod(new Carbon, $range)); $end = clone session('end', Navigation::endOfPeriod(new Carbon, $range));
$periods = $this->periodEntries($account); $periods = $this->periodEntries($account);
} }

View File

@ -33,21 +33,36 @@ $factory->define(
} }
); );
$factory->define(
FireflyIII\Models\Account::class, function (Faker\Generator $faker) {
return [
'id' => $faker->numberBetween(1, 10),
'name' => $faker->words(3, true),
];
}
);
$factory->define( $factory->define(
FireflyIII\Models\Transaction::class, function (Faker\Generator $faker) { FireflyIII\Models\Transaction::class, function (Faker\Generator $faker) {
return [ return [
'transaction_amount' => strval($faker->randomFloat(2, -100, 100)), 'transaction_amount' => strval($faker->randomFloat(2, -100, 100)),
'destination_amount' => strval($faker->randomFloat(2, -100, 100)), 'destination_amount' => strval($faker->randomFloat(2, -100, 100)),
'opposing_account_id' => $faker->numberBetween(1, 10), 'opposing_account_id' => $faker->numberBetween(1, 10),
'source_account_id' => $faker->numberBetween(1, 10), 'source_account_id' => $faker->numberBetween(1, 10),
'opposing_account_name' => $faker->words(3, true), 'opposing_account_name' => $faker->words(3, true),
'description' => $faker->words(3, true), 'description' => $faker->words(3, true),
'source_account_name' => $faker->words(3, true), 'source_account_name' => $faker->words(3, true),
'destination_account_id' => $faker->numberBetween(1, 10), 'destination_account_id' => $faker->numberBetween(1, 10),
'destination_account_name' => $faker->words(3, true), 'destination_account_name' => $faker->words(3, true),
'amount' => strval($faker->randomFloat(2, -100, 100)), 'amount' => strval($faker->randomFloat(2, -100, 100)),
'budget_id' => 0, 'budget_id' => 0,
'category' => $faker->words(3, true), 'category' => $faker->words(3, true),
'transaction_journal_id' => $faker->numberBetween(1, 10),
'journal_id' => $faker->numberBetween(1, 10),
'transaction_currency_code' => 'EUR',
'transaction_type_type' => 'Withdrawal',
'account_encrypted' => 0,
'account_name' => 'Some name',
]; ];
} }
); );

View File

@ -14,10 +14,14 @@ namespace Tests\Feature\Controllers;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface; use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Account\AccountTaskerInterface; use FireflyIII\Repositories\Account\AccountTaskerInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Steam;
use Tests\TestCase; use Tests\TestCase;
class AccountControllerTest extends TestCase class AccountControllerTest extends TestCase
@ -27,6 +31,11 @@ class AccountControllerTest extends TestCase
*/ */
public function testCreate() public function testCreate()
{ {
// mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('get')->andReturn(new Collection);
$this->be($this->user()); $this->be($this->user());
$response = $this->get(route('accounts.create', ['asset'])); $response = $this->get(route('accounts.create', ['asset']));
$response->assertStatus(200); $response->assertStatus(200);
@ -39,6 +48,12 @@ class AccountControllerTest extends TestCase
*/ */
public function testDelete() public function testDelete()
{ {
// mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getAccountsByType')->withArgs([[AccountType::ASSET]])->andReturn(new Collection);
$this->be($this->user()); $this->be($this->user());
$account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first(); $account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.delete', [$account->id])); $response = $this->get(route('accounts.delete', [$account->id]));
@ -52,13 +67,15 @@ class AccountControllerTest extends TestCase
*/ */
public function testDestroy() public function testDestroy()
{ {
$this->session(['accounts.delete.url' => 'http://localhost/accounts/show/1']); // mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first();
$repository = $this->mock(AccountRepositoryInterface::class); $repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('find')->withArgs([0])->once()->andReturn(new Account); $repository->shouldReceive('find')->withArgs([0])->once()->andReturn(new Account);
$repository->shouldReceive('destroy')->andReturn(true); $repository->shouldReceive('destroy')->andReturn(true);
$this->session(['accounts.delete.url' => 'http://localhost/accounts/show/1']);
$account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first();
$this->be($this->user()); $this->be($this->user());
$response = $this->post(route('accounts.destroy', [$account->id])); $response = $this->post(route('accounts.destroy', [$account->id]));
$response->assertStatus(302); $response->assertStatus(302);
@ -70,6 +87,11 @@ class AccountControllerTest extends TestCase
*/ */
public function testEdit() public function testEdit()
{ {
// mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('get')->andReturn(new Collection);
$this->be($this->user()); $this->be($this->user());
$account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first(); $account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.edit', [$account->id])); $response = $this->get(route('accounts.edit', [$account->id]));
@ -88,6 +110,13 @@ class AccountControllerTest extends TestCase
*/ */
public function testIndex(string $range) public function testIndex(string $range)
{ {
// mock stuff
$account = factory(Account::class)->make();
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getAccountsByType')->andReturn(new Collection([$account]));
Steam::shouldReceive('balancesById')->andReturn([]);
Steam::shouldReceive('getLastActivities')->andReturn([]);
$this->be($this->user()); $this->be($this->user());
$this->changeDateRange($this->user(), $range); $this->changeDateRange($this->user(), $range);
$response = $this->get(route('accounts.index', ['asset'])); $response = $this->get(route('accounts.index', ['asset']));
@ -96,37 +125,6 @@ class AccountControllerTest extends TestCase
$response->assertSee('<ol class="breadcrumb">'); $response->assertSee('<ol class="breadcrumb">');
} }
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @covers \FireflyIII\Http\Controllers\AccountController::redirectToOriginalAccount
*/
public function testShowInitial()
{
$date = new Carbon;
$this->session(['start' => $date, 'end' => clone $date]);
$this->be($this->user());
$account = $this->user()->accounts()->where('account_type_id', 6)->orderBy('id','DESC')->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.show', [$account->id]));
$response->assertStatus(302);
}
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @covers \FireflyIII\Http\Controllers\AccountController::redirectToOriginalAccount
* @expectedExceptionMessage Expected a transaction
*/
public function testShowBrokenInitial()
{
$date = new Carbon;
$this->session(['start' => $date, 'end' => clone $date]);
$this->be($this->user());
$account = $this->user()->accounts()->where('account_type_id', 6)->orderBy('id','ASC')->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.show', [$account->id]));
$response->assertStatus(500);
}
/** /**
* @covers \FireflyIII\Http\Controllers\AccountController::show * @covers \FireflyIII\Http\Controllers\AccountController::show
* @covers \FireflyIII\Http\Controllers\AccountController::periodEntries * @covers \FireflyIII\Http\Controllers\AccountController::periodEntries
@ -139,22 +137,22 @@ class AccountControllerTest extends TestCase
$date = new Carbon; $date = new Carbon;
$this->session(['start' => $date, 'end' => clone $date]); $this->session(['start' => $date, 'end' => clone $date]);
// mock stuff:
$tasker = $this->mock(AccountTaskerInterface::class); $tasker = $this->mock(AccountTaskerInterface::class);
$tasker->shouldReceive('amountOutInPeriod')->withAnyArgs()->andReturn('-1'); $tasker->shouldReceive('amountOutInPeriod')->withAnyArgs()->andReturn('-1');
$tasker->shouldReceive('amountInInPeriod')->withAnyArgs()->andReturn('1'); $tasker->shouldReceive('amountInInPeriod')->withAnyArgs()->andReturn('1');
// mock repository:
$repository = $this->mock(AccountRepositoryInterface::class); $repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('oldestJournalDate')->andReturn(clone $date); $repository->shouldReceive('oldestJournalDate')->andReturn(clone $date);
$repository->shouldReceive('getAccountsByType')->andReturn(new Collection); $repository->shouldReceive('getAccountsByType')->andReturn(new Collection);
$transaction = factory(Transaction::class)->make();
$collector = $this->mock(JournalCollectorInterface::class); $collector = $this->mock(JournalCollectorInterface::class);
$collector->shouldReceive('setAccounts')->andReturnSelf(); $collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf(); $collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf(); $collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf(); $collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10)); $collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([$transaction], 0, 10));
$this->be($this->user()); $this->be($this->user());
@ -173,6 +171,24 @@ class AccountControllerTest extends TestCase
*/ */
public function testShowAll(string $range) public function testShowAll(string $range)
{ {
// mock stuff
$transaction = factory(Transaction::class)->make();
$collector = $this->mock(JournalCollectorInterface::class);
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([$transaction], 0, 10));
$tasker = $this->mock(AccountTaskerInterface::class);
$tasker->shouldReceive('amountOutInPeriod')->withAnyArgs()->andReturn('-1');
$tasker->shouldReceive('amountInInPeriod')->withAnyArgs()->andReturn('1');
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('oldestJournalDate')->andReturn(new Carbon)->once();
$repository->shouldReceive('getAccountsByType')->withArgs([[AccountType::ASSET, AccountType::DEFAULT]])->once()->andReturn(new Collection);
$this->be($this->user()); $this->be($this->user());
$this->changeDateRange($this->user(), $range); $this->changeDateRange($this->user(), $range);
$response = $this->get(route('accounts.show', [1, 'all'])); $response = $this->get(route('accounts.show', [1, 'all']));
@ -181,14 +197,77 @@ class AccountControllerTest extends TestCase
$response->assertSee('<ol class="breadcrumb">'); $response->assertSee('<ol class="breadcrumb">');
} }
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @covers \FireflyIII\Http\Controllers\AccountController::redirectToOriginalAccount
* @expectedExceptionMessage Expected a transaction
*/
public function testShowBrokenInitial()
{
$date = new Carbon;
$this->session(['start' => $date, 'end' => clone $date]);
$this->be($this->user());
$account = $this->user()->accounts()->where('account_type_id', 6)->orderBy('id', 'ASC')->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.show', [$account->id]));
$response->assertStatus(500);
}
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testShowByDate(string $range)
{
// mock stuff
$transaction = factory(Transaction::class)->make();
$collector = $this->mock(JournalCollectorInterface::class);
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([$transaction], 0, 10));
$tasker = $this->mock(AccountTaskerInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('oldestJournalDate')->andReturn(new Carbon);
$repository->shouldReceive('getAccountsByType')->withArgs([[AccountType::ASSET, AccountType::DEFAULT]])->once()->andReturn(new Collection);
$tasker->shouldReceive('amountOutInPeriod')->withAnyArgs()->andReturn('-1');
$tasker->shouldReceive('amountInInPeriod')->withAnyArgs()->andReturn('1');
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('accounts.show.date', [1, '2016-01-01']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/** /**
* @covers \FireflyIII\Http\Controllers\AccountController::show * @covers \FireflyIII\Http\Controllers\AccountController::show
* @dataProvider dateRangeProvider * @dataProvider dateRangeProvider
* *
* @param string $range * @param string $range
*/ */
public function testShowByDate(string $range) public function testShowByDateEmpty(string $range)
{ {
// mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10));
$tasker = $this->mock(AccountTaskerInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('oldestJournalDate')->andReturn(new Carbon);
$repository->shouldReceive('getAccountsByType')->withArgs([[AccountType::ASSET, AccountType::DEFAULT]])->once()->andReturn(new Collection);
$tasker->shouldReceive('amountOutInPeriod')->withAnyArgs()->andReturn('-1');
$tasker->shouldReceive('amountInInPeriod')->withAnyArgs()->andReturn('1');
$this->be($this->user()); $this->be($this->user());
$this->changeDateRange($this->user(), $range); $this->changeDateRange($this->user(), $range);
$response = $this->get(route('accounts.show.date', [1, '2016-01-01'])); $response = $this->get(route('accounts.show.date', [1, '2016-01-01']));
@ -197,11 +276,30 @@ class AccountControllerTest extends TestCase
$response->assertSee('<ol class="breadcrumb">'); $response->assertSee('<ol class="breadcrumb">');
} }
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @covers \FireflyIII\Http\Controllers\AccountController::redirectToOriginalAccount
*/
public function testShowInitial()
{
$date = new Carbon;
$this->session(['start' => $date, 'end' => clone $date]);
$this->be($this->user());
$account = $this->user()->accounts()->where('account_type_id', 6)->orderBy('id', 'DESC')->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.show', [$account->id]));
$response->assertStatus(302);
}
/** /**
* @covers \FireflyIII\Http\Controllers\AccountController::store * @covers \FireflyIII\Http\Controllers\AccountController::store
*/ */
public function testStore() public function testStore()
{ {
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('find')->andReturn(new Account)->once();
$repository->shouldReceive('store')->once()->andReturn(factory(Account::class)->make());
$this->session(['accounts.create.url' => 'http://localhost']); $this->session(['accounts.create.url' => 'http://localhost']);
$this->be($this->user()); $this->be($this->user());
$data = [ $data = [
@ -212,13 +310,6 @@ class AccountControllerTest extends TestCase
$response = $this->post(route('accounts.store', ['asset']), $data); $response = $this->post(route('accounts.store', ['asset']), $data);
$response->assertStatus(302); $response->assertStatus(302);
$response->assertSessionHas('success'); $response->assertSessionHas('success');
// list should have this new account.
$response = $this->get(route('accounts.index', ['asset']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
$response->assertSee($data['name']);
} }
/** /**
@ -226,6 +317,10 @@ class AccountControllerTest extends TestCase
*/ */
public function testUpdate() public function testUpdate()
{ {
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('find')->andReturn(new Account)->once();
$repository->shouldReceive('update')->once();
$this->session(['accounts.edit.url' => 'http://localhost']); $this->session(['accounts.edit.url' => 'http://localhost']);
$this->be($this->user()); $this->be($this->user());
$data = [ $data = [
@ -237,12 +332,5 @@ class AccountControllerTest extends TestCase
$response = $this->post(route('accounts.update', [1]), $data); $response = $this->post(route('accounts.update', [1]), $data);
$response->assertStatus(302); $response->assertStatus(302);
$response->assertSessionHas('success'); $response->assertSessionHas('success');
// list should have this new account.
$response = $this->get(route('accounts.index', ['asset']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
$response->assertSee($data['name']);
} }
} }