2018-03-01 20:54:50 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* BillFactoryTest.php
|
2019-10-02 06:45:03 +02:00
|
|
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
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;
|
|
|
|
|
|
|
|
|
2019-04-09 15:32:48 +02:00
|
|
|
use Amount;
|
2018-03-01 20:54:50 +01:00
|
|
|
use FireflyIII\Factory\BillFactory;
|
2018-12-21 16:38:10 +01:00
|
|
|
use FireflyIII\Factory\TransactionCurrencyFactory;
|
2018-08-24 07:18:33 +02:00
|
|
|
use Log;
|
2018-03-01 20:54:50 +01:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BillFactoryTest
|
2019-08-17 10:48:28 +02:00
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
|
2018-03-01 20:54:50 +01:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Create basic bill with minimum data.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
{
|
2018-12-21 16:38:10 +01:00
|
|
|
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
2019-06-16 13:16:46 +02:00
|
|
|
$euro = $this->getEuro();
|
2018-12-21 16:38:10 +01: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
|
|
|
$currencyFactory->shouldReceive('find')->atLeast()->once()
|
2019-06-16 13:16:46 +02:00
|
|
|
->withArgs([1, ''])->andReturn($euro);
|
2018-12-21 16:38:10 +01:00
|
|
|
|
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user());
|
|
|
|
$bill = $factory->create($data);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create basic bill with minimum data.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Factory\BillFactory
|
|
|
|
* @covers \FireflyIII\Services\Internal\Support\BillServiceTrait
|
|
|
|
*/
|
|
|
|
public function testCreateDifferentCurrency(): void
|
|
|
|
{
|
|
|
|
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
2019-06-16 13:16:46 +02:00
|
|
|
$dollar = $this->getDollar();
|
2018-12-21 16:38:10 +01: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',
|
2019-06-16 13:16:46 +02:00
|
|
|
'currency_code' => $dollar->code,
|
2018-12-21 16:38:10 +01:00
|
|
|
'amount_max' => '10',
|
|
|
|
'date' => '2018-01-01',
|
|
|
|
'repeat_freq' => 'monthly',
|
|
|
|
'skip' => 0,
|
|
|
|
'automatch' => true,
|
|
|
|
'active' => true,
|
|
|
|
'notes' => 'Hello!',
|
|
|
|
];
|
|
|
|
|
|
|
|
$currencyFactory->shouldReceive('find')->atLeast()->once()
|
2019-06-16 13:16:46 +02:00
|
|
|
->withArgs([0, $dollar->code])->andReturn($dollar);
|
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());
|
|
|
|
$bill = $factory->create($data);
|
|
|
|
|
|
|
|
$this->assertEquals($data['name'], $bill->name);
|
|
|
|
$this->assertEquals($data['amount_min'], $bill->amount_min);
|
2019-06-16 13:16:46 +02:00
|
|
|
$this->assertEquals($dollar->id, $bill->transaction_currency_id);
|
2018-12-21 16:38:10 +01:00
|
|
|
$this->assertEquals($data['repeat_freq'], $bill->repeat_freq);
|
|
|
|
$note = $bill->notes()->first();
|
|
|
|
$this->assertEquals($data['notes'], $note->text);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create basic bill with minimum data.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
{
|
|
|
|
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
2019-06-16 13:16:46 +02:00
|
|
|
$euro = $this->getEuro();
|
2018-12-21 16:38:10 +01: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',
|
|
|
|
'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
|
|
|
];
|
|
|
|
|
|
|
|
$currencyFactory->shouldReceive('find')->atLeast()->once()
|
2019-06-16 13:16:46 +02:00
|
|
|
->withArgs([1, ''])->andReturn($euro);
|
2018-12-21 16:38:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user());
|
|
|
|
$bill = $factory->create($data);
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-04 07:56:30 +01:00
|
|
|
/**
|
|
|
|
* Create basic bill with minimum data.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Factory\BillFactory
|
|
|
|
* @covers \FireflyIII\Services\Internal\Support\BillServiceTrait
|
|
|
|
*/
|
2019-04-09 15:32:48 +02:00
|
|
|
public function testCreateNoCurrency(): void
|
2018-03-04 07:56:30 +01:00
|
|
|
{
|
2018-12-21 16:38:10 +01:00
|
|
|
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
2019-06-16 13:16:46 +02:00
|
|
|
$dollar = $this->getDollar();
|
2018-12-21 16:38:10 +01:00
|
|
|
$data = [
|
2019-06-16 13:16:46 +02:00
|
|
|
'name' => sprintf('Some new bill #%d', $this->randomInt()),
|
2019-04-09 15:32:48 +02:00
|
|
|
'amount_min' => '5',
|
|
|
|
'amount_max' => '10',
|
|
|
|
'date' => '2018-01-01',
|
|
|
|
'repeat_freq' => 'monthly',
|
|
|
|
'skip' => 0,
|
|
|
|
'automatch' => true,
|
|
|
|
'active' => true,
|
|
|
|
'notes' => 'Hello!',
|
2018-03-04 07:56:30 +01:00
|
|
|
];
|
|
|
|
|
2018-12-21 16:38:10 +01:00
|
|
|
$currencyFactory->shouldReceive('find')->atLeast()->once()
|
2019-04-09 15:32:48 +02:00
|
|
|
->withArgs([0, ''])->andReturnNull();
|
|
|
|
|
2019-06-16 13:16:46 +02:00
|
|
|
Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn($dollar);
|
2018-12-21 16:38:10 +01:00
|
|
|
|
|
|
|
|
2018-03-04 07:56:30 +01:00
|
|
|
/** @var BillFactory $factory */
|
|
|
|
$factory = app(BillFactory::class);
|
|
|
|
$factory->setUser($this->user());
|
|
|
|
$bill = $factory->create($data);
|
|
|
|
|
|
|
|
$this->assertEquals($data['name'], $bill->name);
|
|
|
|
$this->assertEquals($data['amount_min'], $bill->amount_min);
|
2019-06-16 13:16:46 +02:00
|
|
|
$this->assertEquals($dollar->id, $bill->transaction_currency_id);
|
2018-03-04 07:56:30 +01:00
|
|
|
$this->assertEquals($data['repeat_freq'], $bill->repeat_freq);
|
2019-04-09 15:32:48 +02:00
|
|
|
$note = $bill->notes()->first();
|
|
|
|
$this->assertEquals($data['notes'], $note->text);
|
2018-03-04 07:56:30 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2018-12-21 16:38:10 +01:00
|
|
|
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
|
|
|
$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
|
|
|
{
|
2018-12-21 16:38:10 +01:00
|
|
|
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
|
|
|
$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
|
|
|
{
|
2018-12-21 16:38:10 +01:00
|
|
|
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
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
|
|
|
{
|
2018-12-21 16:38:10 +01:00
|
|
|
$currencyFactory = $this->mock(TransactionCurrencyFactory::class);
|
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
|
|
|
}
|