Improve tests, models and views.

This commit is contained in:
James Cole
2019-04-16 16:20:46 +02:00
parent 5ac39dbdef
commit 66c55b7bbe
44 changed files with 13710 additions and 5067 deletions

View File

@@ -27,19 +27,21 @@ use Carbon\Carbon;
use Closure;
use DB;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Preference;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Transformers\TransactionTransformer;
use FireflyIII\User;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Log;
use Mockery;
use RuntimeException;
/**
* Class TestCase
*
@@ -55,7 +57,7 @@ abstract class TestCase extends BaseTestCase
public function changeDateRange(User $user, $range): void
{
$valid = ['1D', '1W', '1M', '3M', '6M', '1Y', 'custom'];
if (\in_array($range, $valid)) {
if (in_array($range, $valid, true)) {
try {
Preference::where('user_id', $user->id)->where('name', 'viewRange')->delete();
} catch (Exception $e) {
@@ -105,6 +107,8 @@ abstract class TestCase extends BaseTestCase
*/
public function demoUser(): User
{
throw new FireflyException('demoUser()-method is obsolete.');
return User::find(4);
}
@@ -113,23 +117,19 @@ abstract class TestCase extends BaseTestCase
*/
public function emptyUser(): User
{
throw new FireflyException('emptyUser()-method is obsolete.');
return User::find(2);
}
/**
* @param int|null $except
*
* @return Account
*/
public function getRandomAsset(): Account
public function getRandomAsset(?int $except = null): Account
{
return $this->getRandomAccount(AccountType::ASSET);
}
/**
* @return Account
*/
public function getRandomLoan(): Account
{
return $this->getRandomAccount(AccountType::LOAN);
return $this->getRandomAccount(AccountType::ASSET, $except);
}
/**
@@ -137,7 +137,7 @@ abstract class TestCase extends BaseTestCase
*/
public function getRandomDeposit(): TransactionJournal
{
return $this->getRandomJournal(TransactionType::DEPOSIT);
return $this->getRandomJournal(TransactionType::DEPOSIT, null);
}
/**
@@ -145,7 +145,15 @@ abstract class TestCase extends BaseTestCase
*/
public function getRandomExpense(): Account
{
return $this->getRandomAccount(AccountType::EXPENSE);
return $this->getRandomAccount(AccountType::EXPENSE, null);
}
/**
* @return Account
*/
public function getRandomLoan(): Account
{
return $this->getRandomAccount(AccountType::LOAN, null);
}
/**
@@ -153,7 +161,7 @@ abstract class TestCase extends BaseTestCase
*/
public function getRandomRevenue(): Account
{
return $this->getRandomAccount(AccountType::REVENUE);
return $this->getRandomAccount(AccountType::REVENUE, null);
}
/**
@@ -186,8 +194,8 @@ abstract class TestCase extends BaseTestCase
public function setUp(): void
{
parent::setUp();
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('firstNull')->andReturn(new TransactionJournal);
// $repository = $this->mock(JournalRepositoryInterface::class);
// $repository->shouldReceive('firstNull')->andReturn(new TransactionJournal);
}
/**
@@ -198,6 +206,14 @@ abstract class TestCase extends BaseTestCase
return User::find(1);
}
/**
* @return TransactionGroup
*/
protected function getRandomWithdrawalGroup(): TransactionGroup
{
return $this->getRandomGroup(TransactionType::WITHDRAWAL);
}
/**
* @param string $class
*
@@ -209,9 +225,9 @@ abstract class TestCase extends BaseTestCase
{
$deprecated = [
TransactionTransformer::class,
TransactionCollectorInterface::class
TransactionCollectorInterface::class,
];
if(in_array($class, $deprecated, true)) {
if (in_array($class, $deprecated, true)) {
throw new RuntimeException('Should not be mocking the transaction collector.');
}
Log::debug(sprintf('Will now mock %s', $class));
@@ -233,27 +249,55 @@ abstract class TestCase extends BaseTestCase
}
/**
* @param string $type
* @param string $type
*
* @param int|null $except
*
* @return Account
*/
private function getRandomAccount(string $type): Account
private function getRandomAccount(string $type, ?int $except): Account
{
$query = Account::
$query = Account::
leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->whereNull('accounts.deleted_at')
->where('accounts.user_id', $this->user()->id)
->where('account_types.type', $type)
->inRandomOrder()->take(1);
->whereNull('accounts.deleted_at')
->where('accounts.user_id', $this->user()->id)
->where('account_types.type', $type)
->inRandomOrder()->take(1);
if (null !== $except) {
$query->where('accounts.id', '!=', $except);
}
$result = $query->first(['accounts.*']);
return $result;
}
/**
* @param string $type
*
* @return TransactionGroup
*/
private function getRandomGroup(string $type): TransactionGroup
{
$transactionType = TransactionType::where('type', $type)->first();
// make sure it's a single count group
do {
$journal = $this->user()->transactionJournals()
->where('transaction_type_id', $transactionType->id)->inRandomOrder()->first();
/** @var TransactionGroup $group */
$group = $journal->transactionGroup;
$count = $group->transactionJournals()->count();
Log::debug(sprintf('Count is %d', $count));
} while (1 !== $count);
return $journal->transactionGroup;
}
/**
* @param string $type
*
* @return TransactionJournal
* @throws FireflyException
*/
private function getRandomJournal(string $type): TransactionJournal
{