Improved various tests.

This commit is contained in:
James Cole 2017-03-04 15:29:20 +01:00
parent 8b4ef4e2da
commit c206a95d55
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
9 changed files with 167 additions and 27 deletions

View File

@ -144,7 +144,7 @@ class MassController extends Controller
$filtered->push($journal);
}
if (count($messages)) {
if (count($messages) > 0) {
Session::flash('info', $messages);
}

View File

@ -45,8 +45,9 @@ class TriggerFactory
$triggerType = $trigger->trigger_type;
/** @var AbstractTrigger $class */
$class = self::getTriggerClass($triggerType);
$obj = $class::makeFromTriggerValue($trigger->trigger_value);
$class = self::getTriggerClass($triggerType);
$obj = $class::makeFromTriggerValue($trigger->trigger_value);
$obj->stopProcessing = $trigger->stop_processing;
Log::debug(sprintf('self::getTriggerClass("%s") = "%s"', $triggerType, $class));
Log::debug(sprintf('%s::makeFromTriggerValue(%s) = object of class "%s"', $class, $trigger->trigger_value, get_class($obj)));

View File

@ -69,6 +69,7 @@ final class Processor
$triggerSet = $rule->ruleTriggers()->orderBy('order', 'ASC')->get();
/** @var RuleTrigger $trigger */
foreach ($triggerSet as $trigger) {
Log::debug(sprintf('Push trigger %d', $trigger->id));
$self->triggers->push(TriggerFactory::getTrigger($trigger));
}
$self->actions = $rule->ruleActions()->orderBy('order', 'ASC')->get();

View File

@ -158,6 +158,7 @@ trait TransactionJournalTrait
foreach ($transactions as $t) {
$list->push($t->account);
}
$list = $list->unique('id');
$cache->store($list);
return $list;
@ -232,6 +233,7 @@ trait TransactionJournalTrait
foreach ($transactions as $t) {
$list->push($t->account);
}
$list = $list->unique('id');
$cache->store($list);
return $list;

View File

@ -36,9 +36,18 @@ $factory->define(
$factory->define(
FireflyIII\Models\Transaction::class, function (Faker\Generator $faker) {
return [
'transaction_amount' => strval($faker->randomFloat(2, -100, 100)),
'opposing_account_id' => $faker->numberBetween(1, 10),
'opposing_account_name' => $faker->words(3),
'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),
];
}
);

View File

@ -13,10 +13,12 @@ namespace Tests\Feature\Controllers\Transaction;
use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Support\Twig\Transaction;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
use Tests\TestCase;
@ -174,6 +176,9 @@ class ConvertControllerTest extends TestCase
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('find')->andReturn(new Account);
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$data = ['source_account_asset' => 1];
$this->be($this->user());
@ -194,6 +199,9 @@ class ConvertControllerTest extends TestCase
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('store')->andReturn(new Account);
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$data = ['destination_account_expense' => 'New expense name.',];
$this->be($this->user());
@ -288,6 +296,9 @@ class ConvertControllerTest extends TestCase
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('store')->andReturn(new Account);
$transfer = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->first();
$data = ['source_account_revenue' => 'New rev'];
$this->be($this->user());
@ -308,6 +319,9 @@ class ConvertControllerTest extends TestCase
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('store')->andReturn(new Account);
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
$data = ['source_account_revenue' => 'New revenue name.',];
$this->be($this->user());
@ -328,6 +342,9 @@ class ConvertControllerTest extends TestCase
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('find')->andReturn(new Account);
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
$data = [
'destination_account_asset' => 2,

View File

@ -12,9 +12,20 @@ declare(strict_types = 1);
namespace Tests\Feature\Controllers\Transaction;
use DB;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
use Tests\TestCase;
/**
* Class MassControllerTest
*
* @package Tests\Feature\Controllers\Transaction
*/
class MassControllerTest extends TestCase
{
/**
@ -37,21 +48,24 @@ class MassControllerTest extends TestCase
*/
public function testDestroy()
{
$deposits = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->take(2)->get();
$depositIds = $deposits->pluck('id')->toArray();
// mock deletion:
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('find')->andReturnValues([$deposits[0], $deposits[1]])->times(2);
$repository->shouldReceive('delete')->times(2);
$this->session(['transactions.mass-delete.url' => 'http://localhost']);
$deposits = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->take(2)->get()->pluck('id')->toArray();
$data = [
'confirm_mass_delete' => $deposits,
$data = [
'confirm_mass_delete' => $depositIds,
];
$this->be($this->user());
$response = $this->post(route('transactions.mass.destroy'), $data);
$response->assertSessionHas('success');
$response->assertStatus(302);
// visit them should give 404.
$response = $this->get(route('transactions.show', [$deposits[0]]));
$response->assertStatus(404);
}
/**
@ -59,9 +73,88 @@ class MassControllerTest extends TestCase
*/
public function testEdit()
{
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
$transfers = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->take(2)->get()->pluck('id')->toArray();
$this->be($this->user());
$response = $this->get(route('transactions.mass.delete', $transfers));
$response = $this->get(route('transactions.mass.edit', $transfers));
$response->assertStatus(200);
$response->assertSee('Edit a number of transactions');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\MassController::edit
*/
public function testEditMultipleNothingLeft()
{
Log::debug('Edit multiple.');
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
// default transactions
$collection = new Collection;
$collection->push(
TransactionJournal::where('transaction_type_id', 1)
->whereNull('transaction_journals.deleted_at')
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->groupBy('transaction_journals.id')
->orderBy('ct', 'DESC')
->where('user_id', $this->user()->id)->first(['transaction_journals.id', DB::raw('count(transactions.`id`) as ct')])
);
$allIds = $collection->pluck('id')->toArray();
Log::debug('Ids', $allIds);
$this->be($this->user());
$response = $this->get(route('transactions.mass.edit', join(',', $allIds)));
$response->assertStatus(200);
$response->assertSee('Edit a number of transactions');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\MassController::edit
*/
public function testEditMultiple()
{
Log::debug('Edit multiple.');
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
// default transactions
$collection = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->take(2)->get();
// add deposit (with multiple sources)
$collection->push(
TransactionJournal::where('transaction_type_id', 2)
->whereNull('transaction_journals.deleted_at')
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->groupBy('transaction_journals.id')
->orderBy('ct', 'DESC')
->where('user_id', $this->user()->id)->first(['transaction_journals.id', DB::raw('count(transactions.`id`) as ct')])
);
// add withdrawal (with multiple destinations)
$collection->push(
TransactionJournal::where('transaction_type_id', 1)
->whereNull('transaction_journals.deleted_at')
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->groupBy('transaction_journals.id')
->orderBy('ct', 'DESC')
->where('user_id', $this->user()->id)->first(['transaction_journals.id', DB::raw('count(transactions.`id`) as ct')])
);
$allIds = $collection->pluck('id')->toArray();
Log::debug('Ids', $allIds);
$this->be($this->user());
$response = $this->get(route('transactions.mass.edit', join(',', $allIds)));
$response->assertStatus(200);
$response->assertSee('Edit a number of transactions');
// has bread crumb
@ -76,6 +169,13 @@ class MassControllerTest extends TestCase
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)
->whereNull('deleted_at')
->first();
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('update')->once();
$repository->shouldReceive('find')->once()->andReturn($deposit);
$this->session(['transactions.mass-edit.url' => 'http://localhost']);
$data = [
@ -93,12 +193,5 @@ class MassControllerTest extends TestCase
$response = $this->post(route('transactions.mass.update', [$deposit->id]), $data);
$response->assertSessionHas('success');
$response->assertStatus(302);
// visit them should show updated content
$response = $this->get(route('transactions.show', [$deposit->id]));
$response->assertStatus(200);
$response->assertSee('Updated salary thing');
}
}

View File

@ -86,12 +86,13 @@ class SingleControllerTest extends TestCase
*/
public function testDestroy()
{
$this->session(['transactions.delete.url' => 'http://localhost']);
$this->be($this->user());
// mock
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('delete')->once();
$this->session(['transactions.delete.url' => 'http://localhost']);
$this->be($this->user());
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->whereNull('deleted_at')->where('user_id', $this->user()->id)->first();
$response = $this->post(route('transactions.destroy', [$withdrawal->id]));
$response->assertStatus(302);
@ -109,7 +110,6 @@ class SingleControllerTest extends TestCase
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection)->once();
$this->be($this->user());
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->whereNull('deleted_at')->where('user_id', $this->user()->id)->first();
$response = $this->get(route('transactions.edit', [$withdrawal->id]));

View File

@ -12,12 +12,17 @@ declare(strict_types = 1);
namespace Tests\Feature\Controllers\Transaction;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
use Tests\TestCase;
/**
@ -39,11 +44,14 @@ class SplitControllerTest extends TestCase
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$accountRepository = $this->mock(AccountRepositoryInterface::class);
$budgetRepository = $this->mock(BudgetRepositoryInterface::class);
$transactions = factory(Transaction::class, 3)->make();
$tasker = $this->mock(JournalTaskerInterface::class);
$currencyRepository->shouldReceive('get')->once()->andReturn(new Collection);
$accountRepository->shouldReceive('getAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])
->andReturn(new Collection)->once();
$budgetRepository->shouldReceive('getActiveBudgets')->andReturn(new Collection);
$tasker->shouldReceive('getTransactionsOverview')->andReturn($transactions->toArray());
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
@ -92,6 +100,15 @@ class SplitControllerTest extends TestCase
],
],
];
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('updateSplitJournal')->andReturn($deposit);
$repository->shouldReceive('first')->times(2)->andReturn(new TransactionJournal);
$attachmentRepos = $this->mock(AttachmentHelperInterface::class);
$attachmentRepos->shouldReceive('saveAttachmentsForModel');
$attachmentRepos->shouldReceive('getMessages')->andReturn(new MessageBag);
$this->be($this->user());
$response = $this->post(route('transactions.split.update', [$deposit->id]), $data);
$response->assertStatus(302);