2018-03-01 20:54:50 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* TransactionJournalMetaFactoryTest.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\Factory;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Factory\TransactionJournalMetaFactory;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Models\TransactionJournalMeta;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class TransactionJournalMetaFactoryTest
|
|
|
|
*/
|
|
|
|
class TransactionJournalMetaFactoryTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Factory\TransactionJournalMetaFactory
|
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testUpdateOrCreateBasic(): void
|
2018-03-01 20:54:50 +01:00
|
|
|
{
|
|
|
|
/** @var TransactionJournal $journal */
|
2018-03-30 22:40:20 +02:00
|
|
|
$journal = $this->user()->transactionJournals()->inRandomOrder()->first();
|
|
|
|
$journal->transactionJournalMeta()->delete();
|
|
|
|
$set = [
|
2018-03-01 20:54:50 +01:00
|
|
|
'journal' => $journal,
|
|
|
|
'name' => 'hello',
|
|
|
|
'data' => 'bye!',
|
|
|
|
];
|
|
|
|
/** @var TransactionJournalMetaFactory $factory */
|
|
|
|
$factory = app(TransactionJournalMetaFactory::class);
|
|
|
|
$result = $factory->updateOrCreate($set);
|
|
|
|
|
|
|
|
$this->assertEquals(1, $journal->transactionJournalMeta()->count());
|
|
|
|
$this->assertEquals($set['data'], $result->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Factory\TransactionJournalMetaFactory
|
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testUpdateOrCreateDate(): void
|
2018-03-01 20:54:50 +01:00
|
|
|
{
|
|
|
|
/** @var TransactionJournal $journal */
|
2018-03-30 22:40:20 +02:00
|
|
|
$journal = $this->user()->transactionJournals()->inRandomOrder()->first();
|
|
|
|
$journal->transactionJournalMeta()->delete();
|
|
|
|
$set = [
|
2018-03-01 20:54:50 +01:00
|
|
|
'journal' => $journal,
|
|
|
|
'name' => 'hello',
|
|
|
|
'data' => new Carbon('2012-01-01'),
|
|
|
|
];
|
|
|
|
/** @var TransactionJournalMetaFactory $factory */
|
|
|
|
$factory = app(TransactionJournalMetaFactory::class);
|
|
|
|
$result = $factory->updateOrCreate($set);
|
|
|
|
|
|
|
|
$this->assertEquals(1, $journal->transactionJournalMeta()->count());
|
|
|
|
$this->assertEquals($set['data']->toW3cString(), $result->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Factory\TransactionJournalMetaFactory
|
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testUpdateOrCreateDeleteExisting(): void
|
2018-03-01 20:54:50 +01:00
|
|
|
{
|
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
$journal = $this->user()->transactionJournals()->where('transaction_type_id', 3)->first();
|
|
|
|
$meta = TransactionJournalMeta::create(
|
|
|
|
[
|
|
|
|
'transaction_journal_id' => $journal->id,
|
|
|
|
'name' => 'hello',
|
|
|
|
'data' => 'bye!',
|
|
|
|
]
|
|
|
|
);
|
2018-08-06 19:14:30 +02:00
|
|
|
$count = $journal->transactionJournalMeta()->count();
|
2018-03-01 20:54:50 +01:00
|
|
|
|
|
|
|
$set = [
|
|
|
|
'journal' => $journal,
|
|
|
|
'name' => 'hello',
|
|
|
|
'data' => null,
|
|
|
|
];
|
|
|
|
/** @var TransactionJournalMetaFactory $factory */
|
|
|
|
$factory = app(TransactionJournalMetaFactory::class);
|
|
|
|
$factory->updateOrCreate($set);
|
|
|
|
|
2018-08-06 19:14:30 +02:00
|
|
|
$this->assertEquals($count - 1, $journal->transactionJournalMeta()->count());
|
2018-03-01 20:54:50 +01:00
|
|
|
}
|
|
|
|
|
2018-03-30 22:40:20 +02:00
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Factory\TransactionJournalMetaFactory
|
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testUpdateOrCreateEmpty(): void
|
2018-03-30 22:40:20 +02:00
|
|
|
{
|
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
$journal = $this->user()->transactionJournals()->inRandomOrder()->first();
|
|
|
|
$journal->transactionJournalMeta()->delete();
|
|
|
|
$set = [
|
|
|
|
'journal' => $journal,
|
|
|
|
'name' => 'hello',
|
|
|
|
'data' => '',
|
|
|
|
];
|
|
|
|
/** @var TransactionJournalMetaFactory $factory */
|
|
|
|
$factory = app(TransactionJournalMetaFactory::class);
|
|
|
|
$result = $factory->updateOrCreate($set);
|
|
|
|
|
|
|
|
$this->assertEquals(0, $journal->transactionJournalMeta()->count());
|
|
|
|
$this->assertNull($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Factory\TransactionJournalMetaFactory
|
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testUpdateOrCreateExistingEmpty(): void
|
2018-03-30 22:40:20 +02:00
|
|
|
{
|
|
|
|
/** @var TransactionJournal $journal */
|
|
|
|
$journal = $this->user()->transactionJournals()->inRandomOrder()->first();
|
|
|
|
$journal->transactionJournalMeta()->delete();
|
|
|
|
$set = [
|
|
|
|
'journal' => $journal,
|
|
|
|
'name' => 'hello',
|
|
|
|
'data' => 'SomeData',
|
|
|
|
];
|
|
|
|
/** @var TransactionJournalMetaFactory $factory */
|
|
|
|
$factory = app(TransactionJournalMetaFactory::class);
|
|
|
|
$result = $factory->updateOrCreate($set);
|
|
|
|
|
|
|
|
$this->assertEquals(1, $journal->transactionJournalMeta()->count());
|
|
|
|
$this->assertNotNull($result);
|
|
|
|
|
|
|
|
// overrule with empty entry:
|
|
|
|
$set = [
|
|
|
|
'journal' => $journal,
|
|
|
|
'name' => 'hello',
|
|
|
|
'data' => '',
|
|
|
|
];
|
|
|
|
/** @var TransactionJournalMetaFactory $factory */
|
|
|
|
$factory = app(TransactionJournalMetaFactory::class);
|
|
|
|
$result = $factory->updateOrCreate($set);
|
|
|
|
|
|
|
|
$this->assertEquals(0, $journal->transactionJournalMeta()->count());
|
|
|
|
$this->assertNull($result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-01 20:54:50 +01:00
|
|
|
|
2018-03-04 15:14:29 +01:00
|
|
|
}
|