Files
firefly-iii/tests/Unit/Factory/CategoryFactoryTest.php

118 lines
3.5 KiB
PHP
Raw Normal View History

2018-03-01 20:54:50 +01:00
<?php
/**
* CategoryFactoryTest.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
2018-03-01 20:54:50 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-03-01 20:54:50 +01: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
*
* 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
* GNU Affero General Public License for more details.
2018-03-01 20:54:50 +01: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;
use FireflyIII\Factory\CategoryFactory;
2018-08-24 07:18:33 +02:00
use Log;
2018-03-01 20:54:50 +01:00
use Tests\TestCase;
/**
* Class CategoryFactoryTest
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 CategoryFactoryTest 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\CategoryFactory
*/
2018-05-11 19:58:10 +02:00
public function testFindOrCreateExistingID(): void
2018-03-01 20:54:50 +01:00
{
$existing = $this->user()->categories()->first();
/** @var CategoryFactory $factory */
$factory = app(CategoryFactory::class);
$factory->setUser($this->user());
$category = $factory->findOrCreate($existing->id, null);
$this->assertEquals($existing->id, $category->id);
}
/**
* @covers \FireflyIII\Factory\CategoryFactory
*/
2018-05-11 19:58:10 +02:00
public function testFindOrCreateExistingName(): void
2018-03-01 20:54:50 +01:00
{
$existing = $this->user()->categories()->first();
/** @var CategoryFactory $factory */
$factory = app(CategoryFactory::class);
$factory->setUser($this->user());
$category = $factory->findOrCreate(null, $existing->name);
$this->assertEquals($existing->id, $category->id);
}
/**
* You can force a NULL result by presenting an invalid ID and no valid name.
*
* @covers \FireflyIII\Factory\CategoryFactory
*/
2018-05-11 19:58:10 +02:00
public function testFindOrCreateInvalidID(): void
2018-03-01 20:54:50 +01:00
{
$existing = $this->user()->categories()->max('id');
2018-03-28 19:38:20 +02:00
$existing += 4;
2018-03-01 20:54:50 +01:00
/** @var CategoryFactory $factory */
$factory = app(CategoryFactory::class);
$factory->setUser($this->user());
$this->assertNull($factory->findOrCreate($existing, ''));
}
/**
* @covers \FireflyIII\Factory\CategoryFactory
*/
2018-05-11 19:58:10 +02:00
public function testFindOrCreateNewName(): void
2018-03-01 20:54:50 +01:00
{
2019-06-16 13:16:46 +02:00
$name = sprintf('Some new category #%d', $this->randomInt());
2018-03-01 20:54:50 +01:00
/** @var CategoryFactory $factory */
$factory = app(CategoryFactory::class);
$factory->setUser($this->user());
$category = $factory->findOrCreate(null, $name);
$this->assertEquals($name, $category->name);
}
/**
* @covers \FireflyIII\Factory\CategoryFactory
*/
2018-05-11 19:58:10 +02:00
public function testFindOrCreateNull(): void
2018-03-01 20:54:50 +01:00
{
/** @var CategoryFactory $factory */
$factory = app(CategoryFactory::class);
$factory->setUser($this->user());
$this->assertNull($factory->findOrCreate(null, null));
}
2018-03-04 15:14:29 +01:00
}