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

104 lines
3.1 KiB
PHP
Raw Normal View History

2018-03-01 13:54:50 -06:00
<?php
/**
* CategoryFactoryTest.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 FireflyIII\Factory\CategoryFactory;
use Tests\TestCase;
/**
* Class CategoryFactoryTest
*/
class CategoryFactoryTest extends TestCase
{
/**
* @covers \FireflyIII\Factory\CategoryFactory
*/
2018-05-11 12:58:10 -05:00
public function testFindOrCreateExistingID(): void
2018-03-01 13:54:50 -06: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 12:58:10 -05:00
public function testFindOrCreateExistingName(): void
2018-03-01 13:54:50 -06: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 12:58:10 -05:00
public function testFindOrCreateInvalidID(): void
2018-03-01 13:54:50 -06:00
{
$existing = $this->user()->categories()->max('id');
2018-03-28 12:38:20 -05:00
$existing += 4;
2018-03-01 13:54:50 -06:00
/** @var CategoryFactory $factory */
$factory = app(CategoryFactory::class);
$factory->setUser($this->user());
$this->assertNull($factory->findOrCreate($existing, ''));
}
/**
* @covers \FireflyIII\Factory\CategoryFactory
*/
2018-05-11 12:58:10 -05:00
public function testFindOrCreateNewName(): void
2018-03-01 13:54:50 -06:00
{
2018-03-28 12:38:20 -05:00
$name = 'Some new category #' . random_int(1, 1000);
2018-03-01 13:54:50 -06: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 12:58:10 -05:00
public function testFindOrCreateNull(): void
2018-03-01 13:54:50 -06:00
{
/** @var CategoryFactory $factory */
$factory = app(CategoryFactory::class);
$factory->setUser($this->user());
$this->assertNull($factory->findOrCreate(null, null));
}
2018-03-04 08:14:29 -06:00
}