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

@@ -23,8 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Factory;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Services\Internal\Support\AccountServiceTrait;
@@ -44,8 +42,6 @@ class AccountFactory
/**
* @param array $data
*
* @throws FireflyException
* @throws Exception
* @return Account
*/
public function create(array $data): Account
@@ -80,10 +76,10 @@ class AccountFactory
$newAccount = Account::create($databaseData);
$this->updateMetadata($newAccount, $data);
if ($this->validIBData($data)) {
if ($this->validIBData($data) && $type->type === AccountType::ASSET) {
$this->updateIB($newAccount, $data);
}
if (!$this->validIBData($data)) {
if (!$this->validIBData($data) && $type->type === AccountType::ASSET) {
$this->deleteIB($newAccount);
}
// update note:
@@ -99,8 +95,6 @@ class AccountFactory
* @param string $accountType
*
* @return Account|null
* @throws Exception
* @throws FireflyException
*/
public function find(string $accountName, string $accountType): ?Account
{
@@ -122,8 +116,6 @@ class AccountFactory
* @param string $accountType
*
* @return Account
* @throws Exception
* @throws FireflyException
*/
public function findOrCreate(string $accountName, string $accountType): Account
{
@@ -143,7 +135,7 @@ class AccountFactory
'name' => $accountName,
'account_type_id' => $type->id,
'accountType' => null,
'virtualBalance' => '0',
'virtualBalance' => '0',
'iban' => null,
'active' => true,
]

View File

@@ -24,9 +24,10 @@ declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Models\Bill;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Services\Internal\Support\BillServiceTrait;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
/**
* Class BillFactory
@@ -34,66 +35,15 @@ use FireflyIII\User;
class BillFactory
{
use BillServiceTrait;
/** @var BillRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* BillFactory constructor.
*/
public function __construct()
{
$this->repository = app(BillRepositoryInterface::class);
}
/**
* @param int|null $billId
* @param null|string $billName
*
* @return Bill|null
*/
public function find(?int $billId, ?string $billName): ?Bill
{
$billId = intval($billId);
$billName = strval($billName);
// first find by ID:
if ($billId > 0) {
/** @var Bill $bill */
$bill = $this->repository->find($billId);
if (!is_null($bill)) {
return $bill;
}
}
// then find by name:
if (strlen($billName) > 0) {
$bill = $this->repository->findByName($billName);
if (!is_null($bill)) {
return $bill;
}
}
return null;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}
/**
* @param array $data
*
* @return Bill|null
*/
public function store(array $data): ?Bill
public function create(array $data): ?Bill
{
$matchArray = explode(',', $data['match']);
$matchArray = array_unique($matchArray);
@@ -123,4 +73,65 @@ class BillFactory
return $bill;
}
/**
* @param int|null $billId
* @param null|string $billName
*
* @return Bill|null
*/
public function find(?int $billId, ?string $billName): ?Bill
{
$billId = intval($billId);
$billName = strval($billName);
// first find by ID:
if ($billId > 0) {
/** @var Bill $bill */
$bill = $this->user->bills()->find($billId);
if (!is_null($bill)) {
return $bill;
}
}
// then find by name:
if (strlen($billName) > 0) {
$bill = $this->findByName($billName);
if (!is_null($bill)) {
return $bill;
}
}
return null;
}
/**
* @param string $name
*
* @return Bill|null
*/
public function findByName(string $name): ?Bill
{
/** @var Collection $collection */
$collection = $this->user->bills()->get();
/** @var Bill $bill */
foreach ($collection as $bill) {
Log::debug(sprintf('"%s" vs. "%s"', $bill->name, $name));
if ($bill->name === $name) {
return $bill;
}
}
Log::debug(sprintf('Bill::Find by name returns NULL based on "%s"', $name));
return null;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
}

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

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

View File

@@ -25,7 +25,6 @@ namespace FireflyIII\Factory;
use FireflyIII\Models\PiggyBank;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\User;
/**
@@ -33,19 +32,9 @@ use FireflyIII\User;
*/
class PiggyBankFactory
{
/** @var PiggyBankRepositoryInterface */
private $repository;
/** @var User */
private $user;
/**
* PiggyBankFactory constructor.
*/
public function __construct()
{
$this->repository = app(PiggyBankRepositoryInterface::class);
}
/**
* @param int|null $piggyBankId
* @param null|string $piggyBankName
@@ -62,7 +51,7 @@ class PiggyBankFactory
// first find by ID:
if ($piggyBankId > 0) {
/** @var PiggyBank $piggyBank */
$piggyBank = $this->repository->find($piggyBankId);
$piggyBank = $this->user->piggyBanks()->find($piggyBankId);
if (!is_null($piggyBank)) {
return $piggyBank;
}
@@ -71,7 +60,7 @@ class PiggyBankFactory
// then find by name:
if (strlen($piggyBankName) > 0) {
/** @var PiggyBank $piggyBank */
$piggyBank = $this->repository->findByName($piggyBankName);
$piggyBank = $this->findByName($piggyBankName);
if (!is_null($piggyBank)) {
return $piggyBank;
}
@@ -81,13 +70,30 @@ class PiggyBankFactory
}
/**
* @param string $name
*
* @return PiggyBank|null
*/
public function findByName(string $name): ?PiggyBank
{
$set = $this->user->piggyBanks()->get();
/** @var PiggyBank $piggy */
foreach ($set as $piggy) {
if ($piggy->name === $name) {
return $piggy;
}
}
return null;
}
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
$this->repository->setUser($user);
}

View File

@@ -24,7 +24,6 @@ declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Services\Internal\Support\TransactionServiceTrait;
@@ -72,8 +71,6 @@ class TransactionFactory
* @param array $data
*
* @return Collection
* @throws FireflyException
* @throws \Exception
*/
public function createPair(TransactionJournal $journal, array $data): Collection
{

View File

@@ -24,7 +24,6 @@ declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Services\Internal\Support\JournalServiceTrait;
@@ -100,6 +99,7 @@ class TransactionJournalFactory
$this->storeMeta($journal, $data, 'invoice_date');
$this->storeMeta($journal, $data, 'internal_reference');
Log::debug('End of TransactionJournalFactory::create()');
return $journal;
}
@@ -145,7 +145,7 @@ class TransactionJournalFactory
$factory = app(TransactionTypeFactory::class);
$transactionType = $factory->find($type);
if (is_null($transactionType)) {
throw new FireflyException(sprintf('Could not find transaction type for "%s"', $type));
throw new FireflyException(sprintf('Could not find transaction type for "%s"', $type)); // @codeCoverageIgnore
}
return $transactionType;

View File

@@ -24,7 +24,9 @@ declare(strict_types=1);
namespace FireflyIII\Factory;
use Carbon\Carbon;
use Exception;
use FireflyIII\Models\TransactionJournalMeta;
use Log;
/**
* Class TransactionJournalMetaFactory
@@ -35,7 +37,6 @@ class TransactionJournalMetaFactory
* @param array $data
*
* @return TransactionJournalMeta|null
* @throws \Exception
*/
public function updateOrCreate(array $data): ?TransactionJournalMeta
{
@@ -43,7 +44,11 @@ class TransactionJournalMetaFactory
/** @var TransactionJournalMeta $entry */
$entry = $data['journal']->transactionJournalMeta()->where('name', $data['name'])->first();
if (is_null($value) && !is_null($entry)) {
$entry->delete();
try {
$entry->delete();
} catch (Exception $e) { // @codeCoverageIgnore
Log::error(sprintf('Could not delete transaction journal meta: %s', $e->getMessage())); // @codeCoverageIgnore
}
return null;
}

View File

@@ -26,6 +26,7 @@ namespace FireflyIII\Factory;
use FireflyIII\Models\TransactionType;
/**
* @codeCoverageIgnore
* Class TransactionTypeFactory
*/
class TransactionTypeFactory