Improve test coverage.

This commit is contained in:
James Cole
2018-03-04 07:56:30 +01:00
parent 7542175258
commit 2ab44fb33a
10 changed files with 603 additions and 55 deletions

View File

@@ -29,6 +29,7 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\Note;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;
use FireflyIII\Services\Internal\Update\AccountUpdateService;
use Tests\TestCase;
@@ -109,6 +110,62 @@ class AccountUpdateServiceTest extends TestCase
$this->assertEquals($data['name'], $account->name);
}
/**
* @covers \FireflyIII\Services\Internal\Update\AccountUpdateService
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
public function testUpdateBasicEmptyNote()
{
/** @var Account $account */
$account = $this->user()->accounts()->first();
$data = [
'name' => 'Some new name #' . rand(1, 1000),
'active' => true,
'virtualBalance' => '0',
'iban' => null,
'accountRole' => 'defaultAsset',
'notes' => '',
];
/** @var AccountUpdateService $service */
$service = app(AccountUpdateService::class);
$account = $service->update($account, $data);
$this->assertEquals($data['name'], $account->name);
$this->assertEquals(0, $account->notes()->count());
}
/**
* @covers \FireflyIII\Services\Internal\Update\AccountUpdateService
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
public function testUpdateBasicExistingNote()
{
/** @var Account $account */
$account = $this->user()->accounts()->first();
$note = new Note;
$note->noteable()->associate($account);
$note->text = 'Hi there';
$note->save();
$data = [
'name' => 'Some new name #' . rand(1, 1000),
'active' => true,
'virtualBalance' => '0',
'iban' => null,
'accountRole' => 'defaultAsset',
'notes' => '',
];
/** @var AccountUpdateService $service */
$service = app(AccountUpdateService::class);
$account = $service->update($account, $data);
$this->assertEquals($data['name'], $account->name);
$this->assertEquals(0, $account->notes()->count());
}
/**
* @covers \FireflyIII\Services\Internal\Update\AccountUpdateService
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
@@ -161,6 +218,61 @@ class AccountUpdateServiceTest extends TestCase
$this->assertEquals($data['notes'], $note->text);
}
/**
* @covers \FireflyIII\Services\Internal\Update\AccountUpdateService
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
public function testUpdateExistingIBZero()
{
$deleteService = $this->mock(JournalDestroyService::class);
$deleteService->shouldReceive('destroy')->once();
/** @var Account $account */
$account = Account::create(
['user_id' => $this->user()->id, 'account_type_id' => 1, 'name' => 'Some name #' . rand(1, 1000),
'virtual_balance' => '0', 'iban' => null, 'active' => true]
);
$opposing = $this->user()->accounts()->first();
$journal = TransactionJournal::create(
['user_id' => $this->user()->id, 'transaction_type_id' => 4, 'transaction_currency_id' => 1, 'description' => 'IB',
'date' => '2018-01-01', 'completed' => true,
]
);
// transactions:
Transaction::create(
['account_id' => $account->id, 'transaction_journal_id' => $journal->id,
'transaction_currency_id' => 1, 'amount' => '100', 'identifier' => 0,]
);
Transaction::create(
['account_id' => $opposing->id, 'transaction_journal_id' => $journal->id,
'transaction_currency_id' => 1, 'amount' => '-100', 'identifier' => 0,]
);
$data = [
'name' => 'Some new name #' . rand(1, 1000),
'active' => true,
'virtualBalance' => '0',
'iban' => null,
'accountRole' => 'defaultAsset',
'openingBalance' => '0',
'openingBalanceDate' => new Carbon('2018-01-01'),
'notes' => 'Hello',
'currency_id' => 1,
];
/** @var AccountUpdateService $service */
$service = app(AccountUpdateService::class);
$account = $service->update($account, $data);
$this->assertEquals($data['name'], $account->name);
$this->assertEquals(1, $account->transactions()->count());
$this->assertEquals(100, $account->transactions()->first()->amount);
/** @var Note $note */
$note = $account->notes()->first();
$this->assertEquals($data['notes'], $note->text);
}
/**
* @covers \FireflyIII\Services\Internal\Update\AccountUpdateService
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait

View File

@@ -32,6 +32,7 @@ use FireflyIII\Factory\TransactionJournalMetaFactory;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Services\Internal\Update\JournalUpdateService;
use FireflyIII\Services\Internal\Update\TransactionUpdateService;
use Mockery;
use Tests\TestCase;
/**
@@ -81,6 +82,95 @@ class JournalUpdateServiceTest extends TestCase
$this->assertEquals(0, $result->transactions()->count());
}
/**
* @covers \FireflyIII\Services\Internal\Update\JournalUpdateService
* @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
*/
public function testUpdateBasicEmptyNote()
{
// mock other stuff:
$transactionFactory = $this->mock(TransactionFactory::class);
$transactionService = $this->mock(TransactionUpdateService::class);
$billFactory = $this->mock(BillFactory::class);
$tagFactory = $this->mock(TagFactory::class);
$metaFactory = $this->mock(TransactionJournalMetaFactory::class);
// mock calls
$billFactory->shouldReceive('setUser');
$billFactory->shouldReceive('find')->andReturn(null);
$transactionService->shouldReceive('setUser');
$transactionFactory->shouldReceive('setUser');
$tagFactory->shouldReceive('setUser');
$metaFactory->shouldReceive('setUser');
/** @var TransactionJournal $journal */
$journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 2)->first();
$data = [
'description' => 'Updated journal #' . rand(1, 1000),
'date' => new Carbon('2018-01-01'),
'bill_id' => null,
'bill_name' => null,
'tags' => [],
'notes' => '',
'transactions' => [],
];
/** @var JournalUpdateService $service */
$service = app(JournalUpdateService::class);
$result = $service->update($journal, $data);
$this->assertEquals($data['description'], $result->description);
$this->assertEquals(0, $result->transactions()->count());
$this->assertEquals(0, $result->notes()->count());
}
/**
* @covers \FireflyIII\Services\Internal\Update\JournalUpdateService
*/
public function testUpdateBudget()
{
$budget = $this->user()->budgets()->first();
$service = $this->mock(TransactionUpdateService::class);
$service->shouldReceive('setUser');
$service->shouldReceive('updateBudget')->withArgs([Mockery::any(), $budget->id])->twice();
do {
/** @var TransactionJournal $journal */
$journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 1)->first();
$count = $journal->transactions()->count();
} while ($count !== 2);
// call update service to update budget. Should call transaction service twice.
/** @var JournalUpdateService $service */
$service = app(JournalUpdateService::class);
$service->updateBudget($journal, $budget->id);
}
/**
* @covers \FireflyIII\Services\Internal\Update\JournalUpdateService
*/
public function testUpdateCategory()
{
$service = $this->mock(TransactionUpdateService::class);
$service->shouldReceive('setUser');
$service->shouldReceive('updateCategory')->withArgs([Mockery::any(), 'New category'])->twice();
do {
/** @var TransactionJournal $journal */
$journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 1)->first();
$count = $journal->transactions()->count();
} while ($count !== 2);
// call update service to update budget. Should call transaction service twice.
/** @var JournalUpdateService $service */
$service = app(JournalUpdateService::class);
$service->updateCategory($journal, 'New category');
}
/**
* @covers \FireflyIII\Services\Internal\Update\JournalUpdateService
* @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait

View File

@@ -0,0 +1,222 @@
<?php
/**
* TransactionUpdateServiceTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Unit\Services\Internal\Update;
use FireflyIII\Factory\BudgetFactory;
use FireflyIII\Factory\CategoryFactory;
use FireflyIII\Models\Transaction;
use FireflyIII\Services\Internal\Update\TransactionUpdateService;
use Tests\TestCase;
/**
* Class TransactionUpdateServiceTest
*/
class TransactionUpdateServiceTest extends TestCase
{
/**
* @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService
*/
public function testReconcile()
{
$transaction = $this->user()->transactions()->inRandomOrder()->first();
/** @var TransactionUpdateService $service */
$service = app(TransactionUpdateService::class);
$service->setUser($this->user());
$result = $service->reconcile($transaction->id);
$this->assertEquals($result->id, $transaction->id);
$this->assertEquals(true, $result->reconciled);
}
/**
* @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
*/
public function testReconcileNull()
{
/** @var TransactionUpdateService $service */
$service = app(TransactionUpdateService::class);
$service->setUser($this->user());
$result = $service->reconcile(-1);
$this->assertNull($result);
}
/**
* @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
*/
public function testUpdateBudget()
{
/** @var Transaction $source */
$source = $this->user()->transactions()->where('amount', '>', 0)->inRandomOrder()->first();
$budget = $this->user()->budgets()->inRandomOrder()->first();
$factory = $this->mock(BudgetFactory::class);
$factory->shouldReceive('setUser');
$factory->shouldReceive('find')->andReturn($budget);
/** @var TransactionUpdateService $service */
$service = app(TransactionUpdateService::class);
$service->setUser($this->user());
$result = $service->updateBudget($source, $budget->id);
$this->assertEquals(1, $result->budgets()->count());
$this->assertEquals($budget->name, $result->budgets()->first()->name);
}
/**
* @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
*/
public function testUpdateCategory()
{
/** @var Transaction $source */
$source = $this->user()->transactions()->where('amount', '>', 0)->inRandomOrder()->first();
$category = $this->user()->categories()->inRandomOrder()->first();
$factory = $this->mock(CategoryFactory::class);
$factory->shouldReceive('setUser');
$factory->shouldReceive('findOrCreate')->andReturn($category);
/** @var TransactionUpdateService $service */
$service = app(TransactionUpdateService::class);
$service->setUser($this->user());
$result = $service->updateCategory($source, $category->name);
$this->assertEquals(1, $result->categories()->count());
$this->assertEquals($category->name, $result->categories()->first()->name);
}
/**
* @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
*/
public function testUpdateDestinationBasic()
{
/** @var Transaction $source */
$source = $this->user()->transactions()->where('amount', '>', 0)->inRandomOrder()->first();
$data = [
'currency_id' => 1,
'currency_code' => null,
'description' => 'Some new description',
'reconciled' => false,
'foreign_amount' => null,
'budget_id' => null,
'budget_name' => null,
'destination_id' => intval($source->account_id),
'destination_name' => null,
'category_id' => null,
'category_name' => null,
'amount' => $source->amount,
'foreign_currency_id' => null,
'foreign_currency_code' => null,
];
/** @var TransactionUpdateService $service */
$service = app(TransactionUpdateService::class);
$service->setUser($this->user());
$result = $service->update($source, $data);
$this->assertEquals($source->id, $result->id);
$this->assertEquals($result->description, $data['description']);
}
/**
* @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
*/
public function testUpdateDestinationForeign()
{
/** @var Transaction $source */
$source = $this->user()->transactions()->where('amount', '>', 0)->inRandomOrder()->first();
$data = [
'currency_id' => 1,
'currency_code' => null,
'description' => 'Some new description',
'reconciled' => false,
'foreign_amount' => '12.34',
'budget_id' => null,
'budget_name' => null,
'destination_id' => intval($source->account_id),
'destination_name' => null,
'category_id' => null,
'category_name' => null,
'amount' => $source->amount,
'foreign_currency_id' => 2,
'foreign_currency_code' => null,
];
/** @var TransactionUpdateService $service */
$service = app(TransactionUpdateService::class);
$service->setUser($this->user());
$result = $service->update($source, $data);
$this->assertEquals($source->id, $result->id);
$this->assertEquals($result->description, $data['description']);
$this->assertEquals($data['foreign_amount'], $result->foreign_amount);
$this->assertEquals($data['foreign_currency_id'], $result->foreign_currency_id);
}
/**
* @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService
* @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait
*/
public function testUpdateSourceBasic()
{
/** @var Transaction $source */
$source = $this->user()->transactions()->where('amount', '<', 0)->inRandomOrder()->first();
$data = [
'currency_id' => 1,
'currency_code' => null,
'description' => 'Some new description',
'reconciled' => false,
'foreign_amount' => null,
'budget_id' => null,
'budget_name' => null,
'source_id' => intval($source->account_id),
'source_name' => null,
'category_id' => null,
'category_name' => null,
'amount' => $source->amount,
'foreign_currency_id' => null,
'foreign_currency_code' => null,
];
/** @var TransactionUpdateService $service */
$service = app(TransactionUpdateService::class);
$service->setUser($this->user());
$result = $service->update($source, $data);
$this->assertEquals($source->id, $result->id);
$this->assertEquals($result->description, $data['description']);
}
}