firefly-iii/tests/TestCase.php

292 lines
8.1 KiB
PHP
Raw Normal View History

2015-07-02 02:44:56 -05:00
<?php
2017-08-10 22:36:05 -05:00
/**
* TestCase.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-08-10 22:36:05 -05:00
*/
2017-08-18 14:08:51 -05:00
declare(strict_types=1);
2017-02-05 08:42:00 -06:00
namespace Tests;
2017-02-12 05:21:44 -06:00
use Carbon\Carbon;
2018-10-28 13:19:39 -05:00
use Closure;
2018-09-02 13:13:25 -05:00
use DB;
2018-02-28 08:50:00 -06:00
use Exception;
2018-09-07 13:12:22 -05:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2017-02-12 05:21:44 -06:00
use FireflyIII\Models\Preference;
2018-02-28 08:50:00 -06:00
use FireflyIII\Models\TransactionJournal;
2018-09-02 13:13:25 -05:00
use FireflyIII\Models\TransactionType;
2018-02-28 08:50:00 -06:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\User;
2017-02-05 08:42:00 -06:00
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Log;
use Mockery;
2017-02-12 05:21:44 -06:00
2016-10-09 00:58:27 -05:00
/**
* Class TestCase
2017-02-05 08:42:00 -06:00
*
2017-08-12 03:27:45 -05:00
* @SuppressWarnings(PHPMD.NumberOfChildren)
2016-10-09 00:58:27 -05:00
*/
2017-02-05 08:42:00 -06:00
abstract class TestCase extends BaseTestCase
2015-07-02 02:44:56 -05:00
{
2018-03-03 01:12:18 -06:00
/**
* @param User $user
* @param string $range
*/
2018-05-11 12:58:10 -05:00
public function changeDateRange(User $user, $range): void
{
$valid = ['1D', '1W', '1M', '3M', '6M', '1Y', 'custom'];
if (\in_array($range, $valid)) {
2018-02-28 08:50:00 -06:00
try {
Preference::where('user_id', $user->id)->where('name', 'viewRange')->delete();
} catch (Exception $e) {
// don't care.
2018-05-11 12:58:10 -05:00
$e->getMessage();
2018-02-28 08:50:00 -06:00
}
Preference::create(
[
'user_id' => $user->id,
'name' => 'viewRange',
'data' => $range,
]
);
// set period to match?
}
2017-11-22 14:12:27 -06:00
if ('custom' === $range) {
$this->session(
[
'start' => Carbon::now()->subDays(20),
'end' => Carbon::now(),
]
);
}
}
/**
* @return array
*/
2018-05-11 12:58:10 -05:00
public function dateRangeProvider(): array
{
return [
'one day' => ['1D'],
'one week' => ['1W'],
'one month' => ['1M'],
'three months' => ['3M'],
'six months' => ['6M'],
'one year' => ['1Y'],
'custom range' => ['custom'],
];
}
2018-09-02 13:13:25 -05:00
use CreatesApplication;
2017-02-12 05:21:44 -06:00
/**
* @return User
*/
2017-12-26 10:33:53 -06:00
public function demoUser(): User
{
2018-04-02 07:17:11 -05:00
return User::find(4);
2017-12-26 10:33:53 -06:00
}
/**
* @return User
*/
public function emptyUser(): User
2017-02-12 05:21:44 -06:00
{
2018-04-02 07:17:11 -05:00
return User::find(2);
2017-02-12 05:21:44 -06:00
}
2018-09-07 13:12:22 -05:00
/**
* @return Account
*/
public function getRandomAsset(): Account
{
return $this->getRandomAccount(AccountType::ASSET);
}
2018-09-02 13:13:25 -05:00
/**
* @return TransactionJournal
*/
public function getRandomDeposit(): TransactionJournal
{
return $this->getRandomJournal(TransactionType::DEPOSIT);
}
2018-09-07 13:12:22 -05:00
/**
* @return Account
*/
public function getRandomExpense(): Account
{
return $this->getRandomAccount(AccountType::EXPENSE);
}
/**
* @return Account
*/
public function getRandomRevenue(): Account
{
return $this->getRandomAccount(AccountType::REVENUE);
}
/**
* @return TransactionJournal
*/
public function getRandomSplitWithdrawal(): TransactionJournal
{
return $this->getRandomSplitJournal(TransactionType::WITHDRAWAL);
}
2018-09-02 13:27:26 -05:00
/**
* @return TransactionJournal
*/
public function getRandomTransfer(): TransactionJournal
{
return $this->getRandomJournal(TransactionType::TRANSFER);
}
2018-09-02 13:13:25 -05:00
/**
* @return TransactionJournal
*/
public function getRandomWithdrawal(): TransactionJournal
{
return $this->getRandomJournal(TransactionType::WITHDRAWAL);
}
/**
2018-09-02 13:27:26 -05:00
*
2018-09-02 13:13:25 -05:00
*/
2018-09-02 13:27:26 -05:00
public function setUp(): void
2018-09-02 13:13:25 -05:00
{
2018-09-02 13:27:26 -05:00
parent::setUp();
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('firstNull')->andReturn(new TransactionJournal);
2018-09-02 13:13:25 -05:00
}
2017-02-12 05:21:44 -06:00
/**
* @return User
*/
2017-12-26 10:33:53 -06:00
public function user(): User
2017-02-12 05:21:44 -06:00
{
2018-04-02 07:17:11 -05:00
return User::find(1);
2017-02-12 05:21:44 -06:00
}
/**
2018-10-28 13:19:39 -05:00
* @param string $class
*
* @param Closure|null $closure
2017-02-12 05:21:44 -06:00
*
* @return \Mockery\MockInterface
*/
2018-10-28 13:19:39 -05:00
protected function mock($class, Closure $closure = null): \Mockery\MockInterface
2017-02-12 05:21:44 -06:00
{
Log::debug(sprintf('Will now mock %s', $class));
$object = Mockery::mock($class);
$this->app->instance($class, $object);
return $object;
}
2018-02-16 15:14:34 -06:00
/**
* @param string $class
*
* @return Mockery\MockInterface
*/
2018-05-11 12:58:10 -05:00
protected function overload(string $class): \Mockery\MockInterface
2018-02-16 15:14:34 -06:00
{
//$this->app->instance($class, $externalMock);
2018-04-02 07:17:11 -05:00
return Mockery::mock('overload:' . $class);
2018-02-16 15:14:34 -06:00
}
2018-02-28 08:50:00 -06:00
2018-09-07 13:12:22 -05:00
/**
* @param string $type
*
* @return Account
*/
private function getRandomAccount(string $type): 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);
2018-09-26 23:26:03 -05:00
$result = $query->first(['accounts.*']);
2018-09-07 13:12:22 -05:00
return $result;
}
2018-09-02 13:13:25 -05:00
/**
* @param string $type
*
* @return TransactionJournal
*/
private function getRandomJournal(string $type): TransactionJournal
{
$query = DB::table('transactions')
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->where('transaction_journals.user_id', $this->user()->id)
->whereNull('transaction_journals.deleted_at')
->whereNull('transactions.deleted_at')
->where('transaction_types.type', $type)
->groupBy('transactions.transaction_journal_id')
->having('ct', '=', 2)
->inRandomOrder()->take(1);
$result = $query->get(
[
'transactions.transaction_journal_id',
'transaction_journalstransaction_type_id',
DB::raw('COUNT(transaction_journal_id) as ct'),
]
)->first();
return TransactionJournal::find((int)$result->transaction_journal_id);
}
2018-09-07 13:12:22 -05:00
/**
* @param string $type
*
* @return TransactionJournal
*/
private function getRandomSplitJournal(string $type): TransactionJournal
{
$query = DB::table('transactions')
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->where('transaction_journals.user_id', $this->user()->id)
->whereNull('transaction_journals.deleted_at')
->whereNull('transactions.deleted_at')
->where('transaction_types.type', $type)
->groupBy('transactions.transaction_journal_id')
->having('ct', '>', 2)
->inRandomOrder()->take(1);
$result = $query->get(
[
'transactions.transaction_journal_id',
'transaction_journalstransaction_type_id',
DB::raw('COUNT(transaction_journal_id) as ct'),
]
)->first();
return TransactionJournal::find((int)$result->transaction_journal_id);
}
}