Expand factory tests.

This commit is contained in:
James Cole
2018-03-01 20:54:50 +01:00
parent 5b8479f3a4
commit 06dc8a499b
25 changed files with 2384 additions and 135 deletions

View File

@@ -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);
}
}