diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php
index c881b6ce83..49f52942af 100644
--- a/app/Http/Controllers/AccountController.php
+++ b/app/Http/Controllers/AccountController.php
@@ -270,8 +270,8 @@ class AccountController extends Controller
// prep for current period
if (strlen($moment) === 0) {
- $start = session('start', Navigation::startOfPeriod(new Carbon, $range));
- $end = session('end', Navigation::endOfPeriod(new Carbon, $range));
+ $start = clone session('start', Navigation::startOfPeriod(new Carbon, $range));
+ $end = clone session('end', Navigation::endOfPeriod(new Carbon, $range));
$periods = $this->periodEntries($account);
}
diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php
index 2235b32e67..b238ad4bf8 100644
--- a/database/factories/ModelFactory.php
+++ b/database/factories/ModelFactory.php
@@ -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(
FireflyIII\Models\Transaction::class, function (Faker\Generator $faker) {
return [
- 'transaction_amount' => strval($faker->randomFloat(2, -100, 100)),
- 'destination_amount' => strval($faker->randomFloat(2, -100, 100)),
- 'opposing_account_id' => $faker->numberBetween(1, 10),
- 'source_account_id' => $faker->numberBetween(1, 10),
- 'opposing_account_name' => $faker->words(3, true),
- 'description' => $faker->words(3, true),
- 'source_account_name' => $faker->words(3, true),
- 'destination_account_id' => $faker->numberBetween(1, 10),
- 'destination_account_name' => $faker->words(3, true),
- 'amount' => strval($faker->randomFloat(2, -100, 100)),
- 'budget_id' => 0,
- 'category' => $faker->words(3, true),
+ 'transaction_amount' => strval($faker->randomFloat(2, -100, 100)),
+ 'destination_amount' => strval($faker->randomFloat(2, -100, 100)),
+ 'opposing_account_id' => $faker->numberBetween(1, 10),
+ 'source_account_id' => $faker->numberBetween(1, 10),
+ 'opposing_account_name' => $faker->words(3, true),
+ 'description' => $faker->words(3, true),
+ 'source_account_name' => $faker->words(3, true),
+ 'destination_account_id' => $faker->numberBetween(1, 10),
+ 'destination_account_name' => $faker->words(3, true),
+ 'amount' => strval($faker->randomFloat(2, -100, 100)),
+ 'budget_id' => 0,
+ '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',
];
}
);
\ No newline at end of file
diff --git a/tests/Feature/Controllers/AccountControllerTest.php b/tests/Feature/Controllers/AccountControllerTest.php
index f8a79adfed..e15ca29237 100644
--- a/tests/Feature/Controllers/AccountControllerTest.php
+++ b/tests/Feature/Controllers/AccountControllerTest.php
@@ -14,10 +14,14 @@ namespace Tests\Feature\Controllers;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Account;
+use FireflyIII\Models\AccountType;
+use FireflyIII\Models\Transaction;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Account\AccountTaskerInterface;
+use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
+use Steam;
use Tests\TestCase;
class AccountControllerTest extends TestCase
@@ -27,6 +31,11 @@ class AccountControllerTest extends TestCase
*/
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());
$response = $this->get(route('accounts.create', ['asset']));
$response->assertStatus(200);
@@ -39,6 +48,12 @@ class AccountControllerTest extends TestCase
*/
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());
$account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.delete', [$account->id]));
@@ -52,13 +67,15 @@ class AccountControllerTest extends TestCase
*/
public function testDestroy()
{
- $this->session(['accounts.delete.url' => 'http://localhost/accounts/show/1']);
-
- $account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first();
+ // mock stuff
+ $collector = $this->mock(JournalCollectorInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('find')->withArgs([0])->once()->andReturn(new Account);
$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());
$response = $this->post(route('accounts.destroy', [$account->id]));
$response->assertStatus(302);
@@ -70,6 +87,11 @@ class AccountControllerTest extends TestCase
*/
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());
$account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first();
$response = $this->get(route('accounts.edit', [$account->id]));
@@ -88,6 +110,13 @@ class AccountControllerTest extends TestCase
*/
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->changeDateRange($this->user(), $range);
$response = $this->get(route('accounts.index', ['asset']));
@@ -96,37 +125,6 @@ class AccountControllerTest extends TestCase
$response->assertSee('
');
}
- /**
- * @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::periodEntries
@@ -139,22 +137,22 @@ class AccountControllerTest extends TestCase
$date = new Carbon;
$this->session(['start' => $date, 'end' => clone $date]);
+ // mock stuff:
$tasker = $this->mock(AccountTaskerInterface::class);
$tasker->shouldReceive('amountOutInPeriod')->withAnyArgs()->andReturn('-1');
$tasker->shouldReceive('amountInInPeriod')->withAnyArgs()->andReturn('1');
- // mock repository:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('oldestJournalDate')->andReturn(clone $date);
$repository->shouldReceive('getAccountsByType')->andReturn(new Collection);
-
- $collector = $this->mock(JournalCollectorInterface::class);
+ $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([], 0, 10));
+ $collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([$transaction], 0, 10));
$this->be($this->user());
@@ -173,6 +171,24 @@ class AccountControllerTest extends TestCase
*/
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->changeDateRange($this->user(), $range);
$response = $this->get(route('accounts.show', [1, 'all']));
@@ -181,14 +197,77 @@ class AccountControllerTest extends TestCase
$response->assertSee('');
}
+ /**
+ * @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('');
+ }
+
/**
* @covers \FireflyIII\Http\Controllers\AccountController::show
* @dataProvider dateRangeProvider
*
* @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->changeDateRange($this->user(), $range);
$response = $this->get(route('accounts.show.date', [1, '2016-01-01']));
@@ -197,11 +276,30 @@ class AccountControllerTest extends TestCase
$response->assertSee('');
}
+ /**
+ * @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
*/
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->be($this->user());
$data = [
@@ -212,13 +310,6 @@ class AccountControllerTest extends TestCase
$response = $this->post(route('accounts.store', ['asset']), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
-
- // list should have this new account.
- $response = $this->get(route('accounts.index', ['asset']));
- $response->assertStatus(200);
- // has bread crumb
- $response->assertSee('');
- $response->assertSee($data['name']);
}
/**
@@ -226,6 +317,10 @@ class AccountControllerTest extends TestCase
*/
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->be($this->user());
$data = [
@@ -237,12 +332,5 @@ class AccountControllerTest extends TestCase
$response = $this->post(route('accounts.update', [1]), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
-
- // list should have this new account.
- $response = $this->get(route('accounts.index', ['asset']));
- $response->assertStatus(200);
- // has bread crumb
- $response->assertSee('');
- $response->assertSee($data['name']);
}
}