mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-18 18:40:12 +00:00
make sure randomly selected journals match prerequisites.
This commit is contained in:
199
tests/Unit/Services/Internal/Update/AccountUpdateServiceTest.php
Normal file
199
tests/Unit/Services/Internal/Update/AccountUpdateServiceTest.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
/**
|
||||
* AccountUpdateServiceTest.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 Carbon\Carbon;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Services\Internal\Update\AccountUpdateService;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class AccountUpdateServiceTest
|
||||
*/
|
||||
class AccountUpdateServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \FireflyIII\Services\Internal\Update\AccountUpdateService
|
||||
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
|
||||
*/
|
||||
public function testDeleteExistingIB()
|
||||
{
|
||||
/** @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',
|
||||
'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(0, $account->transactions()->count());
|
||||
/** @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
|
||||
*/
|
||||
public function testUpdateBasic()
|
||||
{
|
||||
/** @var Account $account */
|
||||
$account = $this->user()->accounts()->first();
|
||||
$data = [
|
||||
'name' => 'Some new name #' . rand(1, 1000),
|
||||
'active' => true,
|
||||
'virtualBalance' => '0',
|
||||
'iban' => null,
|
||||
'accountRole' => 'defaultAsset',
|
||||
];
|
||||
|
||||
/** @var AccountUpdateService $service */
|
||||
$service = app(AccountUpdateService::class);
|
||||
$account = $service->update($account, $data);
|
||||
|
||||
$this->assertEquals($data['name'], $account->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Services\Internal\Update\AccountUpdateService
|
||||
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
|
||||
*/
|
||||
public function testUpdateExistingIB()
|
||||
{
|
||||
/** @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' => '105',
|
||||
'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(105, $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
|
||||
*/
|
||||
public function testUpdateNewIB()
|
||||
{
|
||||
/** @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]
|
||||
);
|
||||
$data = [
|
||||
'name' => 'Some new name #' . rand(1, 1000),
|
||||
'active' => true,
|
||||
'virtualBalance' => '0',
|
||||
'iban' => null,
|
||||
'accountRole' => 'defaultAsset',
|
||||
'openingBalance' => '100',
|
||||
'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);
|
||||
}
|
||||
|
||||
}
|
133
tests/Unit/Services/Internal/Update/JournalUpdateServiceTest.php
Normal file
133
tests/Unit/Services/Internal/Update/JournalUpdateServiceTest.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
/**
|
||||
* JournalUpdateServiceTest.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 Carbon\Carbon;
|
||||
use FireflyIII\Factory\BillFactory;
|
||||
use FireflyIII\Factory\TagFactory;
|
||||
use FireflyIII\Factory\TransactionFactory;
|
||||
use FireflyIII\Factory\TransactionJournalMetaFactory;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Services\Internal\Update\JournalUpdateService;
|
||||
use FireflyIII\Services\Internal\Update\TransactionUpdateService;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class JournalUpdateServiceTest
|
||||
*/
|
||||
class JournalUpdateServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \FireflyIII\Services\Internal\Update\JournalUpdateService
|
||||
* @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
|
||||
*/
|
||||
public function testUpdateBasic()
|
||||
{
|
||||
// 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()->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' => 'Hello',
|
||||
'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());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \FireflyIII\Services\Internal\Update\JournalUpdateService
|
||||
* @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
|
||||
*/
|
||||
public function testUpdateLotsOfTransactions()
|
||||
{
|
||||
// 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');
|
||||
$transactionService->shouldReceive('update')->times(2);
|
||||
$transactionFactory->shouldReceive('createPair')->times(2);
|
||||
$tagFactory->shouldReceive('setUser');
|
||||
$metaFactory->shouldReceive('setUser');
|
||||
|
||||
|
||||
/** @var TransactionJournal $journal */
|
||||
$journal = $this->user()->transactionJournals()->skip(4)->where('transaction_type_id', 1)->first();
|
||||
$data = [
|
||||
'description' => 'Updated journal #' . rand(1, 1000),
|
||||
'date' => new Carbon('2018-01-01'),
|
||||
'bill_id' => null,
|
||||
'bill_name' => null,
|
||||
'tags' => [],
|
||||
'notes' => 'Hello',
|
||||
'transactions' => [
|
||||
['identifier' => 0],
|
||||
['identifier' => 1],
|
||||
['identifier' => 2],
|
||||
],
|
||||
];
|
||||
|
||||
/** @var JournalUpdateService $service */
|
||||
$service = app(JournalUpdateService::class);
|
||||
$result = $service->update($journal, $data);
|
||||
|
||||
$this->assertEquals($data['description'], $result->description);
|
||||
$this->assertEquals(2, $result->transactions()->count());
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user