mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-18 18:44:16 +00:00
Expand factory tests.
This commit is contained in:
@@ -25,8 +25,8 @@ namespace FireflyIII\Factory;
|
||||
|
||||
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
@@ -34,17 +34,26 @@ use Log;
|
||||
*/
|
||||
class CategoryFactory
|
||||
{
|
||||
/** @var CategoryRepositoryInterface */
|
||||
private $repository;
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* CategoryFactory constructor.
|
||||
* @param string $name
|
||||
*
|
||||
* @return Category|null
|
||||
*/
|
||||
public function __construct()
|
||||
public function findByName(string $name): ?Category
|
||||
{
|
||||
$this->repository = app(CategoryRepositoryInterface::class);
|
||||
/** @var Collection $collection */
|
||||
$collection = $this->user->categories()->get();
|
||||
/** @var Category $category */
|
||||
foreach ($collection as $category) {
|
||||
if ($category->name === $name) {
|
||||
return $category;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,14 +74,15 @@ class CategoryFactory
|
||||
}
|
||||
// first by ID:
|
||||
if ($categoryId > 0) {
|
||||
$category = $this->repository->findNull($categoryId);
|
||||
/** @var Category $category */
|
||||
$category = $this->user->categories()->find($categoryId);
|
||||
if (!is_null($category)) {
|
||||
return $category;
|
||||
}
|
||||
}
|
||||
|
||||
if (strlen($categoryName) > 0) {
|
||||
$category = $this->repository->findByName($categoryName);
|
||||
$category = $this->findByName($categoryName);
|
||||
if (!is_null($category)) {
|
||||
return $category;
|
||||
}
|
||||
@@ -94,7 +104,6 @@ class CategoryFactory
|
||||
public function setUser(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->repository->setUser($user);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user