diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 98d8915bff..46a093d1c4 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -98,6 +98,9 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * @property string transaction_journal_budget_encrypted * @property string type * @property string name + * @property Carbon created_at + * @property Carbon updated_at + * @property string foreign_currency_code * @SuppressWarnings(PHPMD.TooManyPublicMethods) */ class Transaction extends Model diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 37c2769833..244e786e99 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -31,9 +31,11 @@ use FireflyIII\Helpers\Collector\TransactionCollectorInterface; use FireflyIII\Helpers\Filter\InternalTransferFilter; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; +use FireflyIII\Models\Note; use FireflyIII\Models\PiggyBankEvent; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; +use FireflyIII\Models\TransactionJournalLink; use FireflyIII\Models\TransactionJournalMeta; use FireflyIII\Models\TransactionType; use FireflyIII\Services\Internal\Destroy\JournalDestroyService; @@ -455,6 +457,23 @@ class JournalRepository implements JournalRepositoryInterface return $amount; } + /** + * @param TransactionJournalLink $link + * + * @return string + */ + public function getLinkNoteText(TransactionJournalLink $link): string + { + $notes = null; + /** @var Note $note */ + $note = $link->notes()->first(); + if (null !== $note) { + return $note->text ?? ''; + } + + return ''; + } + /** * Return Carbon value of a meta field (or NULL). * diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php index c2e8bffc2a..22c5cebd37 100644 --- a/app/Repositories/Journal/JournalRepositoryInterface.php +++ b/app/Repositories/Journal/JournalRepositoryInterface.php @@ -27,6 +27,7 @@ use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Account; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; +use FireflyIII\Models\TransactionJournalLink; use FireflyIII\Models\TransactionJournalMeta; use FireflyIII\Models\TransactionType; use FireflyIII\User; @@ -38,6 +39,14 @@ use Illuminate\Support\MessageBag; */ interface JournalRepositoryInterface { + + /** + * @param TransactionJournalLink $link + * + * @return string + */ + public function getLinkNoteText(TransactionJournalLink $link): string; + /** * @param TransactionJournal $journal * @param TransactionType $type diff --git a/app/Repositories/User/UserRepository.php b/app/Repositories/User/UserRepository.php index cb7dd37c73..b6e9aa873d 100644 --- a/app/Repositories/User/UserRepository.php +++ b/app/Repositories/User/UserRepository.php @@ -217,6 +217,22 @@ class UserRepository implements UserRepositoryInterface return Role::where('name', $role)->first(); } + /** + * @param User $user + * + * @return string|null + */ + public function getRoleByUser(User $user): ?string + { + /** @var Role $role */ + $role = $user->roles()->first(); + if (null !== $role) { + return $role->name; + } + + return null; + } + /** * Return basic user information. * diff --git a/app/Repositories/User/UserRepositoryInterface.php b/app/Repositories/User/UserRepositoryInterface.php index c49c54df2d..33f36a4408 100644 --- a/app/Repositories/User/UserRepositoryInterface.php +++ b/app/Repositories/User/UserRepositoryInterface.php @@ -31,6 +31,13 @@ use Illuminate\Support\Collection; */ interface UserRepositoryInterface { + + /** + * @param User $user + * + * @return string|null + */ + public function getRoleByUser(User $user): ?string; /** * Returns a collection of all users. * diff --git a/app/Transformers/TransactionLinkTransformer.php b/app/Transformers/TransactionLinkTransformer.php index 0e5e91870f..766e83417e 100644 --- a/app/Transformers/TransactionLinkTransformer.php +++ b/app/Transformers/TransactionLinkTransformer.php @@ -24,11 +24,9 @@ declare(strict_types=1); namespace FireflyIII\Transformers; -use FireflyIII\Models\Note; use FireflyIII\Models\TransactionJournalLink; -use League\Fractal\TransformerAbstract; +use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use Log; -use Symfony\Component\HttpFoundation\ParameterBag; /** * @@ -36,6 +34,8 @@ use Symfony\Component\HttpFoundation\ParameterBag; */ class TransactionLinkTransformer extends AbstractTransformer { + /** @var JournalRepositoryInterface */ + private $repository; /** * Constructor. @@ -44,6 +44,8 @@ class TransactionLinkTransformer extends AbstractTransformer */ public function __construct() { + $this->repository = app(JournalRepositoryInterface::class); + if ('testing' === config('app.env')) { Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); } @@ -56,14 +58,8 @@ class TransactionLinkTransformer extends AbstractTransformer */ public function transform(TransactionJournalLink $link): array { - $notes = null; - /** @var Note $note */ - $note = $link->notes()->first(); - if (null !== $note) { - $notes = $note->text; - } - - $data = [ + $notes = $this->repository->getLinkNoteText($link); + $data = [ 'id' => (int)$link->id, 'created_at' => $link->created_at->toAtomString(), 'updated_at' => $link->updated_at->toAtomString(), diff --git a/app/Transformers/TransactionTransformer.php b/app/Transformers/TransactionTransformer.php index c676bbd3db..80e9ff8e9f 100644 --- a/app/Transformers/TransactionTransformer.php +++ b/app/Transformers/TransactionTransformer.php @@ -61,27 +61,21 @@ class TransactionTransformer extends AbstractTransformer */ public function transform(Transaction $transaction): array { - $categoryId = null; - $categoryName = null; - $budgetId = null; - $budgetName = null; - $categoryId = $transaction->transaction_category_id ?? $transaction->transaction_journal_category_id; - $categoryName = $transaction->transaction_category_name ?? $transaction->transaction_journal_category_name; - $journal = $transaction->transactionJournal; - $notes = $this->repository->getNoteText($journal); - if ($transaction->transaction_type_type === TransactionType::WITHDRAWAL) { - $budgetId = $transaction->transaction_budget_id ?? $transaction->transaction_journal_budget_id; - $budgetName = $transaction->transaction_budget_name ?? $transaction->transaction_journal_budget_name; - } - // get tags: - $tags = implode(',', $journal->tags->pluck('tag')->toArray()); + $journal = $transaction->transactionJournal; + $category = $this->getCategory($transaction); + $budget = $this->getBudget($transaction); + $this->repository->setUser($journal->user); + + $notes = $this->repository->getNoteText($journal); + $tags = implode(',', $this->repository->getTags($journal)); $data = [ 'id' => (int)$transaction->id, 'created_at' => $transaction->created_at->toAtomString(), 'updated_at' => $transaction->updated_at->toAtomString(), 'description' => $transaction->description, + 'journal_description' => $transaction->description, 'transaction_description' => $transaction->transaction_description, 'date' => $transaction->date->format('Y-m-d'), 'type' => $transaction->transaction_type_type, @@ -100,10 +94,10 @@ class TransactionTransformer extends AbstractTransformer 'foreign_currency_decimal_places' => $transaction->foreign_currency_dp, 'bill_id' => $transaction->bill_id, 'bill_name' => $transaction->bill_name, - 'category_id' => $categoryId, - 'category_name' => $categoryName, - 'budget_id' => $budgetId, - 'budget_name' => $budgetName, + 'category_id' => $category['category_id'], + 'category_name' => $category['category_name'], + 'budget_id' => $budget['budget_id'], + 'budget_name' => $budget['budget_name'], 'notes' => $notes, 'sepa_cc' => $this->repository->getMetaField($journal, 'sepa-cc'), 'sepa_ct_op' => $this->repository->getMetaField($journal, 'sepa-ct-op'), @@ -138,6 +132,7 @@ class TransactionTransformer extends AbstractTransformer if (null !== $transaction->transaction_foreign_amount) { $data['foreign_amount'] = round($transaction->transaction_foreign_amount, (int)$transaction->foreign_currency_dp); } + // switch on type for consistency switch ($transaction->transaction_type_type) { case TransactionType::WITHDRAWAL: @@ -176,11 +171,43 @@ class TransactionTransformer extends AbstractTransformer } // expand description. - if (\strlen((string)$transaction->transaction_description) > 0) { + if ('' !== (string)$transaction->transaction_description) { $data['description'] = $transaction->transaction_description . ' (' . $transaction->description . ')'; } - return $data; } + + /** + * @param Transaction $transaction + * + * @return array + */ + private function getBudget(Transaction $transaction): array + { + if ($transaction->transaction_type_type !== TransactionType::WITHDRAWAL) { + return [ + 'budget_id' => null, + 'budget_name' => null, + ]; + } + + return [ + 'budget_id' => $transaction->transaction_budget_id ?? $transaction->transaction_journal_budget_id, + 'budget_name' => $transaction->transaction_budget_name ?? $transaction->transaction_journal_budget_name, + ]; + } + + /** + * @param Transaction $transaction + * + * @return array + */ + private function getCategory(Transaction $transaction): array + { + return [ + 'category_id' => $transaction->transaction_category_id ?? $transaction->transaction_journal_category_id, + 'category_name' => $transaction->transaction_category_name ?? $transaction->transaction_journal_category_name, + ]; + } } diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php index 9ee916fa9f..f1d0e3c6fc 100644 --- a/app/Transformers/UserTransformer.php +++ b/app/Transformers/UserTransformer.php @@ -25,6 +25,7 @@ namespace FireflyIII\Transformers; use FireflyIII\Models\Role; +use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\User; use Log; @@ -33,6 +34,8 @@ use Log; */ class UserTransformer extends AbstractTransformer { + /** @var UserRepositoryInterface */ + private $repository; /** * UserTransformer constructor. * @@ -40,6 +43,7 @@ class UserTransformer extends AbstractTransformer */ public function __construct() { + $this->repository = app(UserRepositoryInterface::class); if ('testing' === config('app.env')) { Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this))); } @@ -54,12 +58,6 @@ class UserTransformer extends AbstractTransformer */ public function transform(User $user): array { - /** @var Role $role */ - $role = $user->roles()->first(); - if (null !== $role) { - $role = $role->name; - } - return [ 'id' => (int)$user->id, 'created_at' => $user->created_at->toAtomString(), @@ -67,7 +65,7 @@ class UserTransformer extends AbstractTransformer 'email' => $user->email, 'blocked' => 1 === (int)$user->blocked, 'blocked_code' => '' === $user->blocked_code ? null : $user->blocked_code, - 'role' => $role, + 'role' => $this->repository->getRoleByUser($user), 'links' => [ [ 'rel' => 'self', diff --git a/tests/Unit/Transformers/TagTransformerTest.php b/tests/Unit/Transformers/TagTransformerTest.php index 4ff7b32431..b38f7e555d 100644 --- a/tests/Unit/Transformers/TagTransformerTest.php +++ b/tests/Unit/Transformers/TagTransformerTest.php @@ -53,8 +53,10 @@ class TagTransformerTest extends TestCase 'zoomLevel' => 3, ] ); - $transformer = new TagTransformer(new ParameterBag); - $result = $transformer->transform($tag); + $transformer = app(TagTransformer::class); + $transformer->setParameters(new ParameterBag); + $result = $transformer->transform($tag); + $this->assertEquals($tag->tag, $result['tag']); $this->assertEquals(5.5, $result['latitude']); $this->assertEquals(6.6, $result['longitude']); diff --git a/tests/Unit/Transformers/TransactionLinkTransformerTest.php b/tests/Unit/Transformers/TransactionLinkTransformerTest.php new file mode 100644 index 0000000000..979e383ea3 --- /dev/null +++ b/tests/Unit/Transformers/TransactionLinkTransformerTest.php @@ -0,0 +1,66 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Transformers; + + +use Carbon\Carbon; +use FireflyIII\Models\Transaction; +use FireflyIII\Models\TransactionJournal; +use FireflyIII\Models\TransactionJournalLink; +use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use FireflyIII\Transformers\TransactionLinkTransformer; +use FireflyIII\Transformers\TransactionTransformer; +use Symfony\Component\HttpFoundation\ParameterBag; +use Tests\TestCase; + +/** + * Class TransactionLinkTransformerTest + */ +class TransactionLinkTransformerTest extends TestCase +{ + /** + * Test basic tag transformer + * + * @covers \FireflyIII\Transformers\TransactionLinkTransformer + */ + public function testBasic(): void + { + $repository = $this->mock(JournalRepositoryInterface::class); + + $repository->shouldReceive('getLinkNoteText')->atLeast()->once()->andReturn('abc'); + + /** @var TransactionJournalLink $link */ + $link = TransactionJournalLink::first(); + + $transformer = app(TransactionLinkTransformer::class); + $transformer->setParameters(new ParameterBag); + + $result = $transformer->transform($link); + + $this->assertEquals($link->source_id, $result['inward_id']); + $this->assertEquals('abc', $result['notes']); + + } + +} diff --git a/tests/Unit/Transformers/TransactionTransformerTest.php b/tests/Unit/Transformers/TransactionTransformerTest.php index 1848a0ac85..f71d158e1f 100644 --- a/tests/Unit/Transformers/TransactionTransformerTest.php +++ b/tests/Unit/Transformers/TransactionTransformerTest.php @@ -24,20 +24,12 @@ declare(strict_types=1); namespace Tests\Unit\Transformers; -use FireflyIII\Exceptions\FireflyException; -use FireflyIII\Helpers\Collector\TransactionCollector; -use FireflyIII\Helpers\Filter\NegativeAmountFilter; -use FireflyIII\Helpers\Filter\PositiveAmountFilter; -use FireflyIII\Models\Account; -use FireflyIII\Models\Budget; -use FireflyIII\Models\Category; +use Carbon\Carbon; use FireflyIII\Models\Transaction; -use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionJournal; -use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Transformers\TransactionTransformer; -use Illuminate\Support\Collection; +use Mockery; use Symfony\Component\HttpFoundation\ParameterBag; use Tests\TestCase; @@ -46,1506 +38,292 @@ use Tests\TestCase; */ class TransactionTransformerTest extends TestCase { + /** - * Basic journal (withdrawal) + * Test basic transaction transformer. * * @covers \FireflyIII\Transformers\TransactionTransformer */ public function testBasic(): void { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); + $repository = $this->mock(JournalRepositoryInterface::class); + $transformer = app(TransactionTransformer::class); + $transformer->setParameters(new ParameterBag()); - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); + $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('getNoteText')->once()->andReturn('Notes'); - // make new expense account: - $expense = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 4, // expense account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); + // all meta fields: + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-cc'])->andReturn('a')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-ct-op'])->andReturn('b')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-ct-ud'])->andReturn('c')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-db'])->andReturn('d')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-country'])->andReturn('e')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-ep'])->andReturn('f')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-ci'])->andReturn('g')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-batch-id'])->andReturn('h')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'internal_reference'])->andReturn('h')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'bunq_payment_id'])->andReturn('12345')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'importHashV2'])->andReturn('abcdef')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'recurrence_id'])->andReturn('5')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'external_id'])->andReturn('1')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'original-source'])->andReturn('test')->atLeast()->once(); - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 1, // withdrawal - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - // basic transactions - Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] - ); - Transaction::create( - ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] - ); + // all meta dates. + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'interest_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'book_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'process_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'due_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'payment_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'invoice_date'])->andReturn('2018-01-01')->atLeast()->once(); - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Withdrawal', $result['type']); - $this->assertEquals($journal->description, $result['description']); + // get tags + $repository->shouldReceive('getTags')->once()->andReturn(['a', 'b']); - // budget and category: - $this->assertNull($result['budget_id']); - $this->assertNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); + // create fake transaction object: + $transaction = new Transaction; + $journal = TransactionJournal::find(1); - // source: - $this->assertEquals($asset->name, $result['source_name']); - $this->assertEquals($asset->iban, $result['source_iban']); - $this->assertEquals($asset->id, $result['source_id']); - $this->assertEquals('Asset account', $result['source_type']); + // fill transaction with details + $transaction->transactionJournal = $journal; + $transaction->created_at = new Carbon; + $transaction->updated_at = new Carbon; + $transaction->description = ''; + $transaction->transaction_description = ''; + $transaction->date = new Carbon; + $transaction->identifier = 0; + $transaction->journal_id = 1; + $transaction->reconciled = false; + $transaction->transaction_amount = '123.456'; + $transaction->transaction_currency_id = 1; + $transaction->transaction_currency_code = 'EUR'; + $transaction->transaction_currency_symbol = 'x'; + $transaction->transaction_currency_dp = 2; + $transaction->bill_id = 1; + $transaction->bill_name = 'Bill'; + $transaction->transaction_type_type = 'Withdrawal'; + $transaction->transaction_budget_id = 1; + $transaction->transaction_budget_name = 'X'; + $transaction->transaction_category_id = 2; + $transaction->transaction_category_name = 'xab'; - // destination: - $this->assertEquals($expense->name, $result['destination_name']); - $this->assertEquals($expense->iban, $result['destination_iban']); - $this->assertEquals($expense->id, $result['destination_id']); - $this->assertEquals('Expense account', $result['destination_type']); + // account info (for a withdrawal): + $transaction->account_id = 1; + $transaction->account_name = 'Some source'; + $transaction->account_iban = 'IBAN'; + $transaction->account_type = 'Asset account'; + $transaction->opposing_account_id = 3; + $transaction->opposing_account_name = 'Some destination'; + $transaction->opposing_account_iban = 'IBAN2'; + $transaction->opposing_account_type = 'Expense'; + + + // next test: foreign currency info. + + + $transformer = app(TransactionTransformer::class); + $transformer->setParameters(new ParameterBag); + $result = $transformer->transform($transaction); + $this->assertEquals('Some source', $result['source_name']); } /** - * Basic journal (withdrawal) + * Test deposit. Budget should be null, despite the link. * * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException */ public function testDeposit(): void { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); + $repository = $this->mock(JournalRepositoryInterface::class); + $transformer = app(TransactionTransformer::class); + $transformer->setParameters(new ParameterBag()); - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); + $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('getNoteText')->once()->andReturn('Notes'); - // make new revenue account: - $revenue = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 5, // revenue account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); + // all meta fields: + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-cc'])->andReturn('a')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-ct-op'])->andReturn('b')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-ct-ud'])->andReturn('c')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-db'])->andReturn('d')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-country'])->andReturn('e')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-ep'])->andReturn('f')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-ci'])->andReturn('g')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-batch-id'])->andReturn('h')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'internal_reference'])->andReturn('h')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'bunq_payment_id'])->andReturn('12345')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'importHashV2'])->andReturn('abcdef')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'recurrence_id'])->andReturn('5')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'external_id'])->andReturn('1')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'original-source'])->andReturn('test')->atLeast()->once(); - // create deposit - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 2, // deposit - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - // basic transactions - Transaction::create( - ['account_id' => $revenue->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] - ); - Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] - ); + // all meta dates. + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'interest_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'book_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'process_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'due_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'payment_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'invoice_date'])->andReturn('2018-01-01')->atLeast()->once(); - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Deposit', $result['type']); + // get tags + $repository->shouldReceive('getTags')->once()->andReturn(['a', 'b']); - // budget and category: + // create fake transaction object: + $transaction = new Transaction; + $journal = TransactionJournal::find(1); + + // fill transaction with details + $transaction->transactionJournal = $journal; + $transaction->created_at = new Carbon; + $transaction->updated_at = new Carbon; + $transaction->description = 'Some description'; + $transaction->transaction_description = 'Some expanded description'; + $transaction->date = new Carbon; + $transaction->identifier = 0; + $transaction->journal_id = 1; + $transaction->reconciled = false; + $transaction->transaction_amount = '123.456'; + $transaction->transaction_currency_id = 1; + $transaction->transaction_currency_code = 'EUR'; + $transaction->transaction_currency_symbol = 'x'; + $transaction->transaction_currency_dp = 2; + $transaction->bill_id = 1; + $transaction->bill_name = 'Bill'; + $transaction->transaction_type_type = 'Deposit'; + $transaction->transaction_budget_id = 1; + $transaction->transaction_budget_name = 'X'; + $transaction->transaction_category_id = 2; + $transaction->transaction_category_name = 'xab'; + + // foreign amount info: + $transaction->transaction_foreign_amount = '456.789'; + $transaction->foreign_currency_dp = 2; + $transaction->foreign_currency_code = 'USD'; + $transaction->foreign_currency_symbol = 'x'; + + // account info (for a withdrawal): + $transaction->account_id = 1; + $transaction->account_name = 'Some source'; + $transaction->account_iban = 'IBAN'; + $transaction->account_type = 'Asset account'; + $transaction->opposing_account_id = 3; + $transaction->opposing_account_name = 'Some destination'; + $transaction->opposing_account_iban = 'IBAN2'; + $transaction->opposing_account_type = 'Expense'; + + + // next test: foreign currency info. + + + $transformer = app(TransactionTransformer::class); + $transformer->setParameters(new ParameterBag); + $result = $transformer->transform($transaction); + $this->assertEquals('Some destination', $result['source_name']); + $this->assertEquals(456.79, $result['foreign_amount']); + $this->assertEquals('Some expanded description', $result['transaction_description']); + $this->assertEquals('Some description', $result['journal_description']); + $this->assertEquals('Some expanded description (Some description)', $result['description']); $this->assertNull($result['budget_id']); $this->assertNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); - - // source: - $this->assertEquals($revenue->name, $result['source_name']); - $this->assertEquals($revenue->iban, $result['source_iban']); - $this->assertEquals($revenue->id, $result['source_id']); - $this->assertEquals('Revenue account', $result['source_type']); - - // destination: - $this->assertEquals($asset->name, $result['destination_name']); - $this->assertEquals($asset->iban, $result['destination_iban']); - $this->assertEquals($asset->id, $result['destination_id']); - $this->assertEquals('Asset account', $result['destination_type']); - } /** - * Deposit cannot have a budget + * Test transformer with foreign amount info. * * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException */ - public function testDepositBudget(): void + public function testForeign(): void { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new revenue account: - $revenue = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 5, // revenue account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create deposit - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 2, // deposit - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - // basic transactions - Transaction::create( - ['account_id' => $revenue->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] - ); - Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] - ); - - // create budget: - $budget = Budget::create( - [ - 'user_id' => $this->user()->id, - 'name' => 'Random budget #' . random_int(1, 10000), - 'active' => 1, - ] - ); - $journal->budgets()->save($budget); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Deposit', $result['type']); - - // budget and category: - $this->assertNull($result['budget_id']); - $this->assertNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); - - // source: - $this->assertEquals($revenue->name, $result['source_name']); - $this->assertEquals($revenue->iban, $result['source_iban']); - $this->assertEquals($revenue->id, $result['source_id']); - $this->assertEquals('Revenue account', $result['source_type']); - - // destination: - $this->assertEquals($asset->name, $result['destination_name']); - $this->assertEquals($asset->iban, $result['destination_iban']); - $this->assertEquals($asset->id, $result['destination_id']); - $this->assertEquals('Asset account', $result['destination_type']); - } - - /** - * Basic journal (withdrawal) with a foreign amount. - * - * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException - */ - public function testForeignAmount(): void - { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new expense account: - $expense = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 4, // expense account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 1, // withdrawal - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - // basic transactions - Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0, - 'foreign_amount' => -100, 'foreign_currency_id' => 2, - ] - ); - Transaction::create( - ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0, - 'foreign_amount' => 100, 'foreign_currency_id' => 2, - ] - ); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Withdrawal', $result['type']); - - // budget and category: - $this->assertNull($result['budget_id']); - $this->assertNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); - - // source: - $this->assertEquals($asset->name, $result['source_name']); - $this->assertEquals($asset->iban, $result['source_iban']); - $this->assertEquals($asset->id, $result['source_id']); - $this->assertEquals('Asset account', $result['source_type']); - - // destination: - $this->assertEquals($expense->name, $result['destination_name']); - $this->assertEquals($expense->iban, $result['destination_iban']); - $this->assertEquals($expense->id, $result['destination_id']); - $this->assertEquals('Expense account', $result['destination_type']); - - // foreign info: - $currency = TransactionCurrency::find(2); - $this->assertEquals(-100, $result['foreign_amount']); - $this->assertEquals($currency->code, $result['foreign_currency_code']); - } - - /** - * Basic journal (withdrawal) with a budget - * - * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException - */ - public function testJournalBudget(): void - { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new expense account: - $expense = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 4, // expense account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 1, // withdrawal - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - - // create budget: - $budget = Budget::create( - [ - 'user_id' => $this->user()->id, - 'name' => 'Random budget #' . random_int(1, 10000), - 'active' => 1, - ] - ); - $journal->budgets()->save($budget); - - // basic transactions - Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] - ); - Transaction::create( - ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] - ); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Withdrawal', $result['type']); - - // budget and category: - $this->assertNotNull($result['budget_id']); - $this->assertNotNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); - - // source: - $this->assertEquals($asset->name, $result['source_name']); - $this->assertEquals($asset->iban, $result['source_iban']); - $this->assertEquals($asset->id, $result['source_id']); - $this->assertEquals('Asset account', $result['source_type']); - - // destination: - $this->assertEquals($expense->name, $result['destination_name']); - $this->assertEquals($expense->iban, $result['destination_iban']); - $this->assertEquals($expense->id, $result['destination_id']); - $this->assertEquals('Expense account', $result['destination_type']); - - // budget ID and name: - $this->assertEquals($budget->id, $result['budget_id']); - $this->assertEquals($budget->name, $result['budget_name']); - } - - /** - * Basic journal (withdrawal) with a category - * - * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException - */ - public function testJournalCategory(): void - { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new expense account: - $expense = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 4, // expense account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 1, // withdrawal - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - - // create category: - $category = Category::create( - [ - 'user_id' => $this->user()->id, - 'name' => 'Random category #' . random_int(1, 10000), - 'active' => 1, - ] - ); - $journal->categories()->save($category); - - // basic transactions - Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] - ); - Transaction::create( - ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] - ); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Withdrawal', $result['type']); - - // budget and category: - $this->assertNull($result['budget_id']); - $this->assertNull($result['budget_name']); - $this->assertNotNull($result['category_id']); - $this->assertNotNull($result['category_name']); - - // source: - $this->assertEquals($asset->name, $result['source_name']); - $this->assertEquals($asset->iban, $result['source_iban']); - $this->assertEquals($asset->id, $result['source_id']); - $this->assertEquals('Asset account', $result['source_type']); - - // destination: - $this->assertEquals($expense->name, $result['destination_name']); - $this->assertEquals($expense->iban, $result['destination_iban']); - $this->assertEquals($expense->id, $result['destination_id']); - $this->assertEquals('Expense account', $result['destination_type']); - - // budget ID and name: - $this->assertEquals($category->id, $result['category_id']); - $this->assertEquals($category->name, $result['category_name']); - } - - /** - * Basic journal (opening balance) - * - * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException - */ - public function testOpeningBalanceNeg(): void - { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new initial balance account: - $initial = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 6, // initial balance account - 'name' => 'Initial balance for ' . $asset->name, - 'virtual_balance' => 0, - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 4, // opening balance - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - // basic transactions (negative opening balance). - Transaction::create( - ['account_id' => $initial->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 100, 'identifier' => 0,] - ); - - Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -100, 'identifier' => 0,] - ); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Opening balance', $result['type']); - $this->assertEquals($journal->description, $result['description']); - - // budget and category: - $this->assertNull($result['budget_id']); - $this->assertNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); - - // source: - $this->assertEquals($asset->name, $result['source_name']); - $this->assertEquals($asset->iban, $result['source_iban']); - $this->assertEquals($asset->id, $result['source_id']); - $this->assertEquals('Asset account', $result['source_type']); - - // destination: - $this->assertEquals($initial->name, $result['destination_name']); - $this->assertEquals($initial->iban, $result['destination_iban']); - $this->assertEquals($initial->id, $result['destination_id']); - $this->assertEquals('Initial balance account', $result['destination_type']); - - } - - /** - * Basic journal (opening balance) - * - * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException - */ - public function testOpeningBalancePos(): void - { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new initial balance account: - $initial = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 6, // initial balance account - 'name' => 'Initial balance for ' . $asset->name, - 'virtual_balance' => 0, - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 4, // opening balance - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - // basic transactions (positive opening balance). - - Transaction::create( - ['account_id' => $initial->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -100, 'identifier' => 0,] - ); - - Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 100, 'identifier' => 0,] - ); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Opening balance', $result['type']); - $this->assertEquals($journal->description, $result['description']); - - // budget and category: - $this->assertNull($result['budget_id']); - $this->assertNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); - - // source: - $this->assertEquals($initial->name, $result['source_name']); - $this->assertEquals($initial->iban, $result['source_iban']); - $this->assertEquals($initial->id, $result['source_id']); - $this->assertEquals('Initial balance account', $result['source_type']); - - // destination: - $this->assertEquals($asset->name, $result['destination_name']); - $this->assertEquals($asset->iban, $result['destination_iban']); - $this->assertEquals($asset->id, $result['destination_id']); - $this->assertEquals('Asset account', $result['destination_type']); - } - - /** - * Basic journal (reconciliation) - * - * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException - */ - public function testReconciliationNeg(): void - { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new initial balance account: - $recon = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 10, // Reconciliation account - 'name' => 'Reconciliation for something', - 'virtual_balance' => 0, - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 5, // reconciliation - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - // basic transactions (negative reconciliation). - - Transaction::create( - ['account_id' => $recon->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 100, 'identifier' => 0,] - ); - - Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -100, 'identifier' => 0,] - ); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Reconciliation', $result['type']); - $this->assertEquals($journal->description, $result['description']); - - // budget and category: - $this->assertNull($result['budget_id']); - $this->assertNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); - - // source: - $this->assertEquals($asset->name, $result['source_name']); - $this->assertEquals($asset->iban, $result['source_iban']); - $this->assertEquals($asset->id, $result['source_id']); - $this->assertEquals('Asset account', $result['source_type']); - - // destination: - $this->assertEquals($recon->name, $result['destination_name']); - $this->assertEquals($recon->iban, $result['destination_iban']); - $this->assertEquals($recon->id, $result['destination_id']); - $this->assertEquals('Reconciliation account', $result['destination_type']); - } - - /** - * Basic journal (reconciliation) - * - * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException - */ - public function testReconciliationPos(): void - { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new initial balance account: - $recon = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 10, // Reconciliation account - 'name' => 'Reconciliation for something', - 'virtual_balance' => 0, - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 5, // reconciliation - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - // basic transactions (positive reconciliation). - - Transaction::create( - ['account_id' => $recon->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -100, 'identifier' => 0,] - ); - - Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 100, 'identifier' => 0,] - ); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Reconciliation', $result['type']); - $this->assertEquals($journal->description, $result['description']); - - // budget and category: - $this->assertNull($result['budget_id']); - $this->assertNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); - - // source: - $this->assertEquals($recon->name, $result['source_name']); - $this->assertEquals($recon->iban, $result['source_iban']); - $this->assertEquals($recon->id, $result['source_id']); - $this->assertEquals('Reconciliation account', $result['source_type']); - - // destination: - $this->assertEquals($asset->name, $result['destination_name']); - $this->assertEquals($asset->iban, $result['destination_iban']); - $this->assertEquals($asset->id, $result['destination_id']); - $this->assertEquals('Asset account', $result['destination_type']); - } - - /** - * Basic journal (withdrawal) with budget on the transactions. - * - * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException - */ - public function testTransactionBudget(): void - { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new expense account: - $expense = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 4, // expense account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 1, // withdrawal - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - - // create budget: - $budget = Budget::create( - [ - 'user_id' => $this->user()->id, - 'name' => 'Random budget #' . random_int(1, 10000), - 'active' => 1, - ] - ); - - - // basic transactions - $one = Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] - ); - $two = Transaction::create( - ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] - ); - $one->budgets()->save($budget); - $two->budgets()->save($budget); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Withdrawal', $result['type']); - - // budget and category: - $this->assertNotNull($result['budget_id']); - $this->assertNotNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); - - // source: - $this->assertEquals($asset->name, $result['source_name']); - $this->assertEquals($asset->iban, $result['source_iban']); - $this->assertEquals($asset->id, $result['source_id']); - $this->assertEquals('Asset account', $result['source_type']); - - // destination: - $this->assertEquals($expense->name, $result['destination_name']); - $this->assertEquals($expense->iban, $result['destination_iban']); - $this->assertEquals($expense->id, $result['destination_id']); - $this->assertEquals('Expense account', $result['destination_type']); - - // budget ID and name: - $this->assertEquals($budget->id, $result['budget_id']); - $this->assertEquals($budget->name, $result['budget_name']); - } - - /** - * Basic journal (withdrawal) with a category on the transactions - * - * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException - */ - public function testTransactionCategory(): void - { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new expense account: - $expense = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 4, // expense account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 1, // withdrawal - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - - // create category: - $category = Category::create( - [ - 'user_id' => $this->user()->id, - 'name' => 'Random category #' . random_int(1, 10000), - 'active' => 1, - ] - ); - - // basic transactions - $one = Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] - ); - $two = Transaction::create( - ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] - ); - $one->categories()->save($category); - $two->categories()->save($category); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Withdrawal', $result['type']); - - // budget and category: - $this->assertNull($result['budget_id']); - $this->assertNull($result['budget_name']); - $this->assertNotNull($result['category_id']); - $this->assertNotNull($result['category_name']); - - // source: - $this->assertEquals($asset->name, $result['source_name']); - $this->assertEquals($asset->iban, $result['source_iban']); - $this->assertEquals($asset->id, $result['source_id']); - $this->assertEquals('Asset account', $result['source_type']); - - // destination: - $this->assertEquals($expense->name, $result['destination_name']); - $this->assertEquals($expense->iban, $result['destination_iban']); - $this->assertEquals($expense->id, $result['destination_id']); - $this->assertEquals('Expense account', $result['destination_type']); - - // budget ID and name: - $this->assertEquals($category->id, $result['category_id']); - $this->assertEquals($category->name, $result['category_name']); - } - - /** - * Basic journal (withdrawal) with a description for transactions. - * - * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException - */ - public function testTransactionDescription(): void - { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $asset = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new expense account: - $expense = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 4, // expense account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 1, // withdrawal - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - // basic transactions - Transaction::create( - ['account_id' => $asset->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0, 'description' => 'Hello'] - ); - Transaction::create( - ['account_id' => $expense->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0, 'description' => 'Hello'] - ); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Withdrawal', $result['type']); - $this->assertEquals('Hello (' . $journal->description . ')', $result['description']); - - // budget and category: - $this->assertNull($result['budget_id']); - $this->assertNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); - - // source: - $this->assertEquals($asset->name, $result['source_name']); - $this->assertEquals($asset->iban, $result['source_iban']); - $this->assertEquals($asset->id, $result['source_id']); - $this->assertEquals('Asset account', $result['source_type']); - - // destination: - $this->assertEquals($expense->name, $result['destination_name']); - $this->assertEquals($expense->iban, $result['destination_iban']); - $this->assertEquals($expense->id, $result['destination_id']); - $this->assertEquals('Expense account', $result['destination_type']); - } - - /** - * Basic journal (transfer) - * - * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException - */ - public function testTransferOne(): void - { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $left = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new asset account: - $right = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 3, // transfer - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - // basic transactions - Transaction::create( - ['account_id' => $left->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] - ); - Transaction::create( - ['account_id' => $right->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] - ); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Transfer', $result['type']); - $this->assertEquals($journal->description, $result['description']); - - // budget and category: - $this->assertNull($result['budget_id']); - $this->assertNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); - - // source: - $this->assertEquals($left->name, $result['source_name']); - $this->assertEquals($left->iban, $result['source_iban']); - $this->assertEquals($left->id, $result['source_id']); - $this->assertEquals('Asset account', $result['source_type']); - - // destination: - $this->assertEquals($right->name, $result['destination_name']); - $this->assertEquals($right->iban, $result['destination_iban']); - $this->assertEquals($right->id, $result['destination_id']); - $this->assertEquals('Asset account', $result['destination_type']); - } - - /** - * Basic journal (transfer) - * - * @covers \FireflyIII\Transformers\TransactionTransformer - * throws \FireflyIII\Exceptions\FireflyException - */ - public function testTransferTwo(): void - { - $journalRepository = $this->mock(JournalRepositoryInterface::class); - $journalRepository->shouldReceive('getNoteText')->andReturn('Some notes')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaField')->andReturn('FieldValue')->atLeast()->once(); - $journalRepository->shouldReceive('getMetaDateString')->andReturn('2018-01-01')->atLeast()->once(); - - // make new asset account: - $left = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // make new asset account: - $right = Account::create( - [ - 'user_id' => $this->user()->id, - 'account_type_id' => 3, // asset account - 'name' => 'Random name #' . random_int(1, 10000), - 'virtual_balance' => 12.34, - 'iban' => 'NL85ABNA0466812694', - 'active' => 1, - 'encrypted' => 0, - ] - ); - - // create withdrawal - $journal = TransactionJournal::create( - [ - 'user_id' => $this->user()->id, - 'transaction_type_id' => 3, // transfer - 'transaction_currency_id' => 1, // EUR - 'description' => 'Some journal', - 'date' => '2018-01-01', - 'completed' => 1, - 'tag_count' => 0, - ] - ); - // basic transactions - - Transaction::create( - ['account_id' => $left->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => 45.67, 'identifier' => 0,] - ); - - Transaction::create( - ['account_id' => $right->id, 'transaction_journal_id' => $journal->id, - 'transaction_currency_id' => 1, 'amount' => -45.67, 'identifier' => 0,] - ); - - // use collector to get it: - $transaction = $this->getTransaction($journal); - $transformer = new TransactionTransformer(new ParameterBag); - try { - $result = $transformer->transform($transaction); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - // basic fields: - $this->assertEquals($journal->id, $result['journal_id']); - $this->assertEquals('Transfer', $result['type']); - $this->assertEquals($journal->description, $result['description']); - - // budget and category: - $this->assertNull($result['budget_id']); - $this->assertNull($result['budget_name']); - $this->assertNull($result['category_id']); - $this->assertNull($result['category_name']); - - // source: - $this->assertEquals($right->name, $result['source_name']); - $this->assertEquals($right->iban, $result['source_iban']); - $this->assertEquals($right->id, $result['source_id']); - $this->assertEquals('Asset account', $result['source_type']); - - // destination: - $this->assertEquals($left->name, $result['destination_name']); - $this->assertEquals($left->iban, $result['destination_iban']); - $this->assertEquals($left->id, $result['destination_id']); - $this->assertEquals('Asset account', $result['destination_type']); - } - - /** - * @param TransactionJournal $journal - * - * @return Transaction - */ - protected function getTransaction(TransactionJournal $journal): Transaction - { - $collector = new TransactionCollector; - $collector->setUser($this->user()); - $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation(); - $collector->setJournals(new Collection([$journal])); - - // add filter to remove transactions: - $transactionType = $journal->transactionType->type; - if ($transactionType === TransactionType::WITHDRAWAL) { - $collector->addFilter(PositiveAmountFilter::class); - } - if (!($transactionType === TransactionType::WITHDRAWAL)) { - $collector->addFilter(NegativeAmountFilter::class); - } - $journals = $collector->getTransactions(); - - return $journals->first(); + $repository = $this->mock(JournalRepositoryInterface::class); + $transformer = app(TransactionTransformer::class); + $transformer->setParameters(new ParameterBag()); + + $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('getNoteText')->once()->andReturn('Notes'); + + // all meta fields: + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-cc'])->andReturn('a')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-ct-op'])->andReturn('b')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-ct-ud'])->andReturn('c')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-db'])->andReturn('d')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-country'])->andReturn('e')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-ep'])->andReturn('f')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-ci'])->andReturn('g')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'sepa-batch-id'])->andReturn('h')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'internal_reference'])->andReturn('h')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'bunq_payment_id'])->andReturn('12345')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'importHashV2'])->andReturn('abcdef')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'recurrence_id'])->andReturn('5')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'external_id'])->andReturn('1')->atLeast()->once(); + $repository->shouldReceive('getMetaField')->withArgs([Mockery::any(), 'original-source'])->andReturn('test')->atLeast()->once(); + + // all meta dates. + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'interest_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'book_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'process_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'due_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'payment_date'])->andReturn('2018-01-01')->atLeast()->once(); + $repository->shouldReceive('getMetaDateString')->withArgs([Mockery::any(), 'invoice_date'])->andReturn('2018-01-01')->atLeast()->once(); + + // get tags + $repository->shouldReceive('getTags')->once()->andReturn(['a', 'b']); + + // create fake transaction object: + $transaction = new Transaction; + $journal = TransactionJournal::find(1); + + // fill transaction with details + $transaction->transactionJournal = $journal; + $transaction->created_at = new Carbon; + $transaction->updated_at = new Carbon; + $transaction->description = 'Some description'; + $transaction->transaction_description = 'Some expanded description'; + $transaction->date = new Carbon; + $transaction->identifier = 0; + $transaction->journal_id = 1; + $transaction->reconciled = false; + $transaction->transaction_amount = '123.456'; + $transaction->transaction_currency_id = 1; + $transaction->transaction_currency_code = 'EUR'; + $transaction->transaction_currency_symbol = 'x'; + $transaction->transaction_currency_dp = 2; + $transaction->bill_id = 1; + $transaction->bill_name = 'Bill'; + $transaction->transaction_type_type = 'Withdrawal'; + $transaction->transaction_budget_id = 1; + $transaction->transaction_budget_name = 'X'; + $transaction->transaction_category_id = 2; + $transaction->transaction_category_name = 'xab'; + + // foreign amount info: + $transaction->transaction_foreign_amount = '456.789'; + $transaction->foreign_currency_dp = 2; + $transaction->foreign_currency_code = 'USD'; + $transaction->foreign_currency_symbol = 'x'; + + // account info (for a withdrawal): + $transaction->account_id = 1; + $transaction->account_name = 'Some source'; + $transaction->account_iban = 'IBAN'; + $transaction->account_type = 'Asset account'; + $transaction->opposing_account_id = 3; + $transaction->opposing_account_name = 'Some destination'; + $transaction->opposing_account_iban = 'IBAN2'; + $transaction->opposing_account_type = 'Expense'; + + + // next test: foreign currency info. + + + $transformer = app(TransactionTransformer::class); + $transformer->setParameters(new ParameterBag); + $result = $transformer->transform($transaction); + $this->assertEquals('Some source', $result['source_name']); + $this->assertEquals(456.79, $result['foreign_amount']); + $this->assertEquals('Some expanded description', $result['transaction_description']); + $this->assertEquals('Some description', $result['journal_description']); + $this->assertEquals('Some expanded description (Some description)', $result['description']); } diff --git a/tests/Unit/Transformers/UserTransformerTest.php b/tests/Unit/Transformers/UserTransformerTest.php index c7c7791387..9dfdcc3bb1 100644 --- a/tests/Unit/Transformers/UserTransformerTest.php +++ b/tests/Unit/Transformers/UserTransformerTest.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace Tests\Unit\Transformers; +use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\Transformers\UserTransformer; use Symfony\Component\HttpFoundation\ParameterBag; use Tests\TestCase; @@ -41,27 +42,15 @@ class UserTransformerTest extends TestCase */ public function testBasic(): void { - $user = $this->user(); - $transformer = new UserTransformer(new ParameterBag()); - $result = $transformer->transform($user); + $repository = $this->mock(UserRepositoryInterface::class); + $repository->shouldReceive('getRoleByUser')->atLeast()->once()->andReturn('owner'); + $user = $this->user(); + + $transformer = app(UserTransformer::class); + $transformer->setParameters(new ParameterBag); + $result = $transformer->transform($user); $this->assertEquals($user->email, $result['email']); $this->assertEquals('owner', $result['role']); } - - /** - * Test basic transformer. - * - * @covers \FireflyIII\Transformers\UserTransformer - */ - public function testEmptyUser(): void - { - $user = $this->emptyUser(); - $transformer = new UserTransformer(new ParameterBag()); - $result = $transformer->transform($user); - - $this->assertEquals($user->email, $result['email']); - $this->assertNull($result['role']); - } - }