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

@@ -24,26 +24,17 @@ declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Models\Budget;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
/**
* Class BudgetFactory
*/
class BudgetFactory
{
/** @var BudgetRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* BudgetFactory constructor.
*/
public function __construct()
{
$this->repository = app(BudgetRepositoryInterface::class);
}
/**
* @param int|null $budgetId
@@ -62,14 +53,15 @@ class BudgetFactory
// first by ID:
if ($budgetId > 0) {
$budget = $this->repository->findNull($budgetId);
/** @var Budget $budget */
$budget = $this->user->budgets()->find($budgetId);
if (!is_null($budget)) {
return $budget;
}
}
if (strlen($budgetName) > 0) {
$budget = $this->repository->findByName($budgetName);
$budget = $this->findByName($budgetName);
if (!is_null($budget)) {
return $budget;
}
@@ -78,13 +70,31 @@ class BudgetFactory
return null;
}
/**
* @param string $name
*
* @return Budget|null
*/
public function findByName(string $name): ?Budget
{
/** @var Collection $collection */
$collection = $this->user->budgets()->get();
/** @var Budget $budget */
foreach ($collection as $budget) {
if ($budget->name === $name) {
return $budget;
}
}
return null;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
}