2018-03-01 20:54:50 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* BillFactoryTest.php
|
2020-02-16 13:59:55 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-03-01 20:54:50 +01:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-03-01 20:54:50 +01:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2018-03-01 20:54:50 +01:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-03-01 20:54:50 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:45:03 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-03-01 20:54:50 +01:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-03-01 20:54:50 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Tests\Unit\Factory;
|
|
|
|
|
|
|
|
|
2020-08-01 15:06:02 +02:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2018-03-01 20:54:50 +01:00
|
|
|
use FireflyIII\Factory\BillFactory;
|
2020-08-01 15:06:02 +02:00
|
|
|
use FireflyIII\Models\ObjectGroup;
|
2018-08-24 07:18:33 +02:00
|
|
|
use Log;
|
2018-03-01 20:54:50 +01:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BillFactoryTest
|
|
|
|
*/
|
|
|
|
class BillFactoryTest extends TestCase
|
|
|
|
{
|
2018-08-24 07:18:33 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2019-04-09 20:05:20 +02:00
|
|
|
Log::info(sprintf('Now in %s.', get_class($this)));
|
2018-08-24 07:18:33 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 20:54:50 +01:00
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Factory\BillFactory
|
|
|
|
* @covers \FireflyIII\Services\Internal\Support\BillServiceTrait
|
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testCreateBasic(): void
|
2018-03-01 20:54:50 +01:00
|
|
|
{
|
2020-08-01 15:06:02 +02:00
|
|
|
$data = [
|
2019-06-16 13:16:46 +02:00
|
|
|
'name' => sprintf('Some new bill #%d', $this->randomInt()),
|
2018-08-23 18:33:39 +02:00
|
|
|
'amount_min' => '5',
|
|
|
|
'currency_id' => 1,
|
|
|
|
'currency_code' => '',
|
|
|
|
'amount_max' => '10',
|
|
|
|
'date' => '2018-01-01',
|
|
|
|
'repeat_freq' => 'monthly',
|
|
|
|
'skip' => 0,
|
|
|
|
'automatch' => true,
|
|
|
|
'active' => true,
|
|
|
|
'notes' => 'Hello!',
|
2018-03-01 20:54:50 +01:00
|
|
|
];
|
|
|
|
|
2018-12-21 16:38:10 +01:00
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user());
|
2020-08-01 15:06:02 +02:00
|
|
|
try {
|
|
|
|
$bill = $factory->create($data);
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
$this->assertTrue(false, $e->getMessage());
|
|
|
|
}
|
2018-12-21 16:38:10 +01:00
|
|
|
|
|
|
|
$this->assertEquals($data['name'], $bill->name);
|
|
|
|
$this->assertEquals($data['amount_min'], $bill->amount_min);
|
|
|
|
$this->assertEquals(1, $bill->transaction_currency_id);
|
|
|
|
$this->assertEquals($data['repeat_freq'], $bill->repeat_freq);
|
|
|
|
$note = $bill->notes()->first();
|
|
|
|
$this->assertEquals($data['notes'], $note->text);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Factory\BillFactory
|
|
|
|
* @covers \FireflyIII\Services\Internal\Support\BillServiceTrait
|
|
|
|
*/
|
2020-08-01 15:06:02 +02:00
|
|
|
public function testCreateQueryException(): void
|
2018-12-21 16:38:10 +01:00
|
|
|
{
|
2020-08-01 15:06:02 +02:00
|
|
|
$data = [
|
2019-06-16 13:16:46 +02:00
|
|
|
'name' => sprintf('Some new bill #%d', $this->randomInt()),
|
2018-12-21 16:38:10 +01:00
|
|
|
'amount_min' => '5',
|
2020-08-01 15:06:02 +02:00
|
|
|
'currency_id' => 1,
|
|
|
|
'currency_code' => '',
|
2018-12-21 16:38:10 +01:00
|
|
|
'amount_max' => '10',
|
|
|
|
'date' => '2018-01-01',
|
|
|
|
'repeat_freq' => 'monthly',
|
|
|
|
'skip' => 0,
|
|
|
|
'automatch' => true,
|
2020-08-01 15:06:02 +02:00
|
|
|
'active' => 'I AM A STRING',
|
2018-12-21 16:38:10 +01:00
|
|
|
'notes' => 'Hello!',
|
|
|
|
];
|
|
|
|
|
2020-08-01 15:06:02 +02:00
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user());
|
|
|
|
try {
|
|
|
|
$factory->create($data);
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
$this->assertEquals('400000: Could not store bill.', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Factory\BillFactory
|
|
|
|
* @covers \FireflyIII\Services\Internal\Support\BillServiceTrait
|
|
|
|
* @covers \FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups
|
|
|
|
*/
|
|
|
|
public function testCreateObjectGroup(): void
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'name' => sprintf('Some new bill #%d', $this->randomInt()),
|
|
|
|
'amount_min' => '5',
|
|
|
|
'amount_max' => '10',
|
|
|
|
'date' => '2018-01-01',
|
|
|
|
'repeat_freq' => 'monthly',
|
|
|
|
'skip' => 0,
|
|
|
|
'object_group' => 'Test',
|
|
|
|
'automatch' => true,
|
|
|
|
'active' => 1,
|
|
|
|
'notes' => 'Hello!',
|
|
|
|
];
|
2018-12-21 16:38:10 +01:00
|
|
|
|
2018-03-01 20:54:50 +01:00
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user());
|
2020-08-01 15:06:02 +02:00
|
|
|
try {
|
|
|
|
$bill = $factory->create($data);
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
$this->assertFalse(true, $e->getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->assertCount(1, $bill->objectGroups()->get());
|
|
|
|
}
|
2018-03-01 20:54:50 +01:00
|
|
|
|
2018-12-21 16:38:10 +01:00
|
|
|
|
2020-08-01 15:06:02 +02:00
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Factory\BillFactory
|
|
|
|
* @covers \FireflyIII\Services\Internal\Support\BillServiceTrait
|
|
|
|
* @covers \FireflyIII\Repositories\ObjectGroup\CreatesObjectGroups
|
|
|
|
*/
|
|
|
|
public function testCreateObjectGroupById(): void
|
|
|
|
{
|
|
|
|
$group = ObjectGroup::create(
|
|
|
|
[
|
|
|
|
'user_id' => $this->user()->id,
|
|
|
|
'title' => sprintf('Random object group #%d', $this->randomInt()),
|
|
|
|
'order' => 1,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$data = [
|
|
|
|
'name' => sprintf('Some new bill #%d', $this->randomInt()),
|
|
|
|
'amount_min' => '5',
|
|
|
|
'currency_id' => 1,
|
|
|
|
'amount_max' => '10',
|
|
|
|
'date' => '2018-01-01',
|
|
|
|
'repeat_freq' => 'monthly',
|
|
|
|
'skip' => 0,
|
|
|
|
'object_group_id' => $group->id,
|
|
|
|
'automatch' => true,
|
|
|
|
'active' => 1,
|
|
|
|
'notes' => 'Hello!',
|
|
|
|
];
|
|
|
|
|
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user());
|
|
|
|
try {
|
|
|
|
$bill = $factory->create($data);
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
$this->assertFalse(true, $e->getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->assertCount(1, $bill->objectGroups()->get());
|
2018-12-21 16:38:10 +01:00
|
|
|
}
|
|
|
|
|
2020-08-01 15:06:02 +02:00
|
|
|
|
2018-12-21 16:38:10 +01:00
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Factory\BillFactory
|
|
|
|
* @covers \FireflyIII\Services\Internal\Support\BillServiceTrait
|
|
|
|
*/
|
2019-04-09 15:32:48 +02:00
|
|
|
public function testCreateEmptyNotes(): void
|
2018-12-21 16:38:10 +01:00
|
|
|
{
|
2020-08-01 15:06:02 +02:00
|
|
|
$euro = $this->getEuro();
|
|
|
|
$data = [
|
2019-06-16 13:16:46 +02:00
|
|
|
'name' => sprintf('Some new bill #%d', $this->randomInt()),
|
2018-12-21 16:38:10 +01:00
|
|
|
'amount_min' => '5',
|
|
|
|
'amount_max' => '10',
|
|
|
|
'date' => '2018-01-01',
|
|
|
|
'repeat_freq' => 'monthly',
|
2019-06-16 13:16:46 +02:00
|
|
|
'currency_id' => $euro->id,
|
2019-04-09 15:32:48 +02:00
|
|
|
'currency_code' => '',
|
2018-12-21 16:38:10 +01:00
|
|
|
'skip' => 0,
|
|
|
|
'automatch' => true,
|
|
|
|
'active' => true,
|
2019-04-09 15:32:48 +02:00
|
|
|
'notes' => '',
|
2018-12-21 16:38:10 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user());
|
2020-08-01 15:06:02 +02:00
|
|
|
try {
|
|
|
|
$bill = $factory->create($data);
|
|
|
|
} catch (FireflyException $e) {
|
|
|
|
$this->assertTrue(false, $e->getMessage());
|
|
|
|
}
|
2018-12-21 16:38:10 +01:00
|
|
|
|
|
|
|
$this->assertEquals($data['name'], $bill->name);
|
2019-06-16 13:16:46 +02:00
|
|
|
$this->assertEquals($euro->id, $bill->transaction_currency_id);
|
2018-12-21 16:38:10 +01:00
|
|
|
$this->assertEquals($data['amount_min'], $bill->amount_min);
|
2018-03-01 20:54:50 +01:00
|
|
|
$this->assertEquals($data['repeat_freq'], $bill->repeat_freq);
|
2019-04-09 15:32:48 +02:00
|
|
|
$this->assertEquals(0, $bill->notes()->count());
|
2018-03-01 20:54:50 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find by ID
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Factory\BillFactory
|
|
|
|
*
|
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testFindById(): void
|
2018-03-01 20:54:50 +01:00
|
|
|
{
|
2020-08-01 15:06:02 +02:00
|
|
|
$existing = $this->user()->piggyBanks()->first();
|
2018-03-01 20:54:50 +01:00
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user());
|
|
|
|
$piggy = $factory->find($existing->id, null);
|
|
|
|
$this->assertEquals($existing->id, $piggy->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find by name
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Factory\BillFactory
|
|
|
|
*
|
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testFindByName(): void
|
2018-03-01 20:54:50 +01:00
|
|
|
{
|
2020-08-01 15:06:02 +02:00
|
|
|
$existing = $this->user()->bills()->first();
|
2018-03-01 20:54:50 +01:00
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user());
|
|
|
|
$piggy = $factory->find(null, $existing->name);
|
|
|
|
|
|
|
|
$this->assertEquals($existing->id, $piggy->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find by unknown name
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Factory\BillFactory
|
|
|
|
*
|
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testFindByUnknownName(): void
|
2018-03-01 20:54:50 +01:00
|
|
|
{
|
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user());
|
2019-06-16 13:16:46 +02:00
|
|
|
$piggy = $factory->find(null, sprintf('I dont exist #%d', $this->randomInt()));
|
2018-03-01 20:54:50 +01:00
|
|
|
|
|
|
|
$this->assertNull($piggy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find NULL
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Factory\BillFactory
|
|
|
|
*
|
|
|
|
*/
|
2018-05-11 19:58:10 +02:00
|
|
|
public function testFindNull(): void
|
2018-03-01 20:54:50 +01:00
|
|
|
{
|
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user());
|
|
|
|
$this->assertNull($factory->find(null, null));
|
|
|
|
}
|
|
|
|
|
2018-03-04 15:14:29 +01:00
|
|
|
}
|