mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
New tests for object transformers.
This commit is contained in:
parent
78ba0f749c
commit
049e57d578
@ -132,6 +132,8 @@ class AccountTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the account.
|
||||
*
|
||||
* @param Account $account
|
||||
*
|
||||
* @return array
|
||||
|
@ -32,6 +32,7 @@ use Illuminate\Support\Collection;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use Log;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
/**
|
||||
@ -212,13 +213,18 @@ class BillTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data the bill was paid and predict the next expected match.
|
||||
*
|
||||
* @param Bill $bill
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function paidData(Bill $bill): array
|
||||
{
|
||||
Log::debug(sprintf('Now in paidData for bill #%d', $bill->id));
|
||||
if (is_null($this->parameters->get('start')) || is_null($this->parameters->get('end'))) {
|
||||
Log::debug('parameters are NULL, return empty array');
|
||||
|
||||
return [
|
||||
'paid_dates' => [],
|
||||
'next_expected_match' => null,
|
||||
@ -228,7 +234,8 @@ class BillTransformer extends TransformerAbstract
|
||||
/** @var BillRepositoryInterface $repository */
|
||||
$repository = app(BillRepositoryInterface::class);
|
||||
$repository->setUser($bill->user);
|
||||
$set = $repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end'));
|
||||
$set = $repository->getPaidDatesInRange($bill, $this->parameters->get('start'), $this->parameters->get('end'));
|
||||
Log::debug(sprintf('Count %d entries in getPaidDatesInRange()', $set->count()));
|
||||
$simple = $set->map(
|
||||
function (Carbon $date) {
|
||||
return $date->format('Y-m-d');
|
||||
|
@ -24,7 +24,11 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Helpers\Collector\JournalCollector;
|
||||
use FireflyIII\Models\Budget;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
@ -50,7 +54,9 @@ class BudgetTransformer extends TransformerAbstract
|
||||
protected $parameters;
|
||||
|
||||
/**
|
||||
* BillTransformer constructor.
|
||||
* BudgetTransformer constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param ParameterBag $parameters
|
||||
*/
|
||||
@ -60,6 +66,48 @@ class BudgetTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Include any transactions.
|
||||
*
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return FractalCollection
|
||||
*/
|
||||
public function includeTransactions(Budget $budget): FractalCollection
|
||||
{
|
||||
$pageSize = intval(app('preferences')->getForUser($budget->user, 'listPageSize', 50)->data);
|
||||
|
||||
// journals always use collector and limited using URL parameters.
|
||||
$collector = new JournalCollector;
|
||||
$collector->setUser($budget->user);
|
||||
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
|
||||
$collector->setAllAssetAccounts();
|
||||
$collector->setBudgets(new Collection([$budget]));
|
||||
if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) {
|
||||
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
|
||||
}
|
||||
$collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
|
||||
$journals = $collector->getJournals();
|
||||
|
||||
return $this->collection($journals, new TransactionTransformer($this->parameters), 'transactions');
|
||||
}
|
||||
|
||||
/**
|
||||
* Include the user.
|
||||
*
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return Item
|
||||
*/
|
||||
public function includeUser(Budget $budget): Item
|
||||
{
|
||||
return $this->item($budget->user, new UserTransformer($this->parameters), 'users');
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform a budget.
|
||||
*
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @return array
|
||||
@ -70,6 +118,7 @@ class BudgetTransformer extends TransformerAbstract
|
||||
'id' => (int)$budget->id,
|
||||
'updated_at' => $budget->updated_at->toAtomString(),
|
||||
'created_at' => $budget->created_at->toAtomString(),
|
||||
'active' => intval($budget->active) === 1,
|
||||
'name' => $budget->name,
|
||||
'links' => [
|
||||
[
|
||||
|
@ -24,7 +24,11 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Helpers\Collector\JournalCollector;
|
||||
use FireflyIII\Models\Category;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
@ -38,7 +42,7 @@ class CategoryTransformer extends TransformerAbstract
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = ['user','transactions'];
|
||||
protected $availableIncludes = ['user', 'transactions'];
|
||||
/**
|
||||
* List of resources to automatically include
|
||||
*
|
||||
@ -50,7 +54,9 @@ class CategoryTransformer extends TransformerAbstract
|
||||
protected $parameters;
|
||||
|
||||
/**
|
||||
* BillTransformer constructor.
|
||||
* CategoryTransformer constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param ParameterBag $parameters
|
||||
*/
|
||||
@ -60,6 +66,48 @@ class CategoryTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Include any transactions.
|
||||
*
|
||||
* @param Category $category
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return FractalCollection
|
||||
*/
|
||||
public function includeTransactions(Category $category): FractalCollection
|
||||
{
|
||||
$pageSize = intval(app('preferences')->getForUser($category->user, 'listPageSize', 50)->data);
|
||||
|
||||
// journals always use collector and limited using URL parameters.
|
||||
$collector = new JournalCollector;
|
||||
$collector->setUser($category->user);
|
||||
$collector->withOpposingAccount()->withCategoryInformation()->withCategoryInformation();
|
||||
$collector->setAllAssetAccounts();
|
||||
$collector->setCategories(new Collection([$category]));
|
||||
if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) {
|
||||
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
|
||||
}
|
||||
$collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
|
||||
$journals = $collector->getJournals();
|
||||
|
||||
return $this->collection($journals, new TransactionTransformer($this->parameters), 'transactions');
|
||||
}
|
||||
|
||||
/**
|
||||
* Include the user.
|
||||
*
|
||||
* @param Category $category
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return Item
|
||||
*/
|
||||
public function includeUser(Category $category): Item
|
||||
{
|
||||
return $this->item($category->user, new UserTransformer($this->parameters), 'users');
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert category.
|
||||
*
|
||||
* @param Category $category
|
||||
*
|
||||
* @return array
|
||||
|
@ -24,7 +24,10 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Helpers\Collector\JournalCollector;
|
||||
use FireflyIII\Models\TransactionJournalMeta;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
@ -38,7 +41,7 @@ class JournalMetaTransformer extends TransformerAbstract
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = ['journal'];
|
||||
protected $availableIncludes = ['transactions'];
|
||||
/**
|
||||
* List of resources to automatically include
|
||||
*
|
||||
@ -50,7 +53,9 @@ class JournalMetaTransformer extends TransformerAbstract
|
||||
protected $parameters;
|
||||
|
||||
/**
|
||||
* BillTransformer constructor.
|
||||
* JournalMetaTransformer constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param ParameterBag $parameters
|
||||
*/
|
||||
@ -60,6 +65,36 @@ class JournalMetaTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Include any transactions.
|
||||
*
|
||||
* @param TransactionJournalMeta $meta
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return FractalCollection
|
||||
*/
|
||||
public function includeTransactions(TransactionJournalMeta $meta): FractalCollection
|
||||
{
|
||||
$journal = $meta->transactionJournal;
|
||||
$pageSize = intval(app('preferences')->getForUser($journal->user, 'listPageSize', 50)->data);
|
||||
|
||||
// journals always use collector and limited using URL parameters.
|
||||
$collector = new JournalCollector;
|
||||
$collector->setUser($journal->user);
|
||||
$collector->withOpposingAccount()->withCategoryInformation()->withCategoryInformation();
|
||||
$collector->setAllAssetAccounts();
|
||||
$collector->setJournals(new Collection([$journal]));
|
||||
if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) {
|
||||
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
|
||||
}
|
||||
$collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
|
||||
$journals = $collector->getJournals();
|
||||
|
||||
return $this->collection($journals, new TransactionTransformer($this->parameters), 'transactions');
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert meta object.
|
||||
*
|
||||
* @param TransactionJournalMeta $meta
|
||||
*
|
||||
* @return array
|
||||
|
@ -24,8 +24,11 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Helpers\Collector\JournalCollector;
|
||||
use FireflyIII\Models\PiggyBankEvent;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
@ -39,7 +42,7 @@ class PiggyBankEventTransformer extends TransformerAbstract
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = ['piggy_bank', 'transactions'];
|
||||
protected $availableIncludes = ['piggy_bank', 'transaction'];
|
||||
/**
|
||||
* List of resources to automatically include
|
||||
*
|
||||
@ -51,7 +54,9 @@ class PiggyBankEventTransformer extends TransformerAbstract
|
||||
protected $parameters;
|
||||
|
||||
/**
|
||||
* BillTransformer constructor.
|
||||
* PiggyBankEventTransformer constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param ParameterBag $parameters
|
||||
*/
|
||||
@ -61,6 +66,51 @@ class PiggyBankEventTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Include piggy bank into end result.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param PiggyBankEvent $event
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function includePiggyBank(PiggyBankEvent $event): Item
|
||||
{
|
||||
return $this->item($event->piggyBank, new PiggyBankTransformer($this->parameters), 'piggy_banks');
|
||||
}
|
||||
|
||||
/**
|
||||
* Include transaction into end result.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param PiggyBankEvent $event
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function includeTransaction(PiggyBankEvent $event): Item
|
||||
{
|
||||
$journal = $event->transactionJournal;
|
||||
$pageSize = intval(app('preferences')->getForUser($journal->user, 'listPageSize', 50)->data);
|
||||
|
||||
// journals always use collector and limited using URL parameters.
|
||||
$collector = new JournalCollector;
|
||||
$collector->setUser($journal->user);
|
||||
$collector->withOpposingAccount()->withCategoryInformation()->withCategoryInformation();
|
||||
$collector->setAllAssetAccounts();
|
||||
$collector->setJournals(new Collection([$journal]));
|
||||
if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) {
|
||||
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
|
||||
}
|
||||
$collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
|
||||
$journals = $collector->getJournals();
|
||||
|
||||
return $this->item($journals->first(), new TransactionTransformer($this->parameters), 'transactions');
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert piggy bank event.
|
||||
*
|
||||
* @param PiggyBankEvent $event
|
||||
*
|
||||
* @return array
|
||||
@ -71,7 +121,10 @@ class PiggyBankEventTransformer extends TransformerAbstract
|
||||
$currencyId = intval($account->getMeta('currency_id'));
|
||||
$decimalPlaces = 2;
|
||||
if ($currencyId > 0) {
|
||||
$currency = TransactionCurrency::find($currencyId);
|
||||
/** @var CurrencyRepositoryInterface $repository */
|
||||
$repository = app(CurrencyRepositoryInterface::class);
|
||||
$repository->setUser($account->user);
|
||||
$currency = $repository->findNull($currencyId);
|
||||
$decimalPlaces = $currency->decimal_places;
|
||||
}
|
||||
|
||||
|
@ -26,6 +26,10 @@ namespace FireflyIII\Transformers;
|
||||
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
@ -51,7 +55,9 @@ class PiggyBankTransformer extends TransformerAbstract
|
||||
protected $parameters;
|
||||
|
||||
/**
|
||||
* BillTransformer constructor.
|
||||
* PiggyBankTransformer constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param ParameterBag $parameters
|
||||
*/
|
||||
@ -61,20 +67,88 @@ class PiggyBankTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Include account.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param PiggyBank $piggyBank
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function includeAccount(PiggyBank $piggyBank): Item
|
||||
{
|
||||
return $this->item($piggyBank->account, new AccountTransformer($this->parameters), 'accounts');
|
||||
}
|
||||
|
||||
/**
|
||||
* Include events.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param PiggyBank $piggyBank
|
||||
*
|
||||
* @return FractalCollection
|
||||
*/
|
||||
public function includePiggyBankEvents(PiggyBank $piggyBank): FractalCollection
|
||||
{
|
||||
return $this->collection($piggyBank->piggyBankEvents, new PiggyBankEventTransformer($this->parameters), 'piggy_bank_events');
|
||||
}
|
||||
|
||||
/**
|
||||
* Include the user.
|
||||
*
|
||||
* @param PiggyBank $piggyBank
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return Item
|
||||
*/
|
||||
public function includeUser(PiggyBank $piggyBank): Item
|
||||
{
|
||||
return $this->item($piggyBank->account->user, new UserTransformer($this->parameters), 'users');
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the piggy bank.
|
||||
*
|
||||
* @param PiggyBank $piggyBank
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(PiggyBank $piggyBank): array
|
||||
{
|
||||
$account = $piggyBank->account;
|
||||
$currencyId = intval($account->getMeta('currency_id'));
|
||||
$decimalPlaces = 2;
|
||||
if ($currencyId > 0) {
|
||||
/** @var CurrencyRepositoryInterface $repository */
|
||||
$repository = app(CurrencyRepositoryInterface::class);
|
||||
$repository->setUser($account->user);
|
||||
$currency = $repository->findNull($currencyId);
|
||||
$decimalPlaces = $currency->decimal_places;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'id' => (int)$piggyBank->id,
|
||||
'updated_at' => $piggyBank->updated_at->toAtomString(),
|
||||
'created_at' => $piggyBank->created_at->toAtomString(),
|
||||
'name' => $piggyBank->name,
|
||||
'notes' => null,
|
||||
'links' => [
|
||||
// get currently saved amount:
|
||||
/** @var PiggyBankRepositoryInterface $piggyRepos */
|
||||
$piggyRepos = app(PiggyBankRepositoryInterface::class);
|
||||
$piggyRepos->setUser($account->user);
|
||||
$currentAmount = round($piggyRepos->getCurrentAmount($piggyBank), $decimalPlaces);
|
||||
|
||||
$startDate = is_null($piggyBank->startdate) ? null : $piggyBank->startdate->format('Y-m-d');
|
||||
$targetDate = is_null($piggyBank->targetdate) ? null : $piggyBank->targetdate->format('Y-m-d');
|
||||
$targetAmount = round($piggyBank->targetamount, $decimalPlaces);
|
||||
$data = [
|
||||
'id' => (int)$piggyBank->id,
|
||||
'updated_at' => $piggyBank->updated_at->toAtomString(),
|
||||
'created_at' => $piggyBank->created_at->toAtomString(),
|
||||
'name' => $piggyBank->name,
|
||||
'target_amount' => $targetAmount,
|
||||
'current_amount' => $currentAmount,
|
||||
'startdate' => $startDate,
|
||||
'targetdate' => $targetDate,
|
||||
'order' => (int)$piggyBank->order,
|
||||
'active' => intval($piggyBank->active) === 1,
|
||||
'notes' => null,
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/piggy_banks/' . $piggyBank->id,
|
||||
|
@ -24,7 +24,10 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Helpers\Collector\JournalCollector;
|
||||
use FireflyIII\Models\Tag;
|
||||
use League\Fractal\Resource\Collection as FractalCollection;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\TransformerAbstract;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
|
||||
@ -38,7 +41,7 @@ class TagTransformer extends TransformerAbstract
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = ['user'];
|
||||
protected $availableIncludes = ['user', 'transactions'];
|
||||
/**
|
||||
* List of resources to automatically include
|
||||
*
|
||||
@ -50,7 +53,9 @@ class TagTransformer extends TransformerAbstract
|
||||
protected $parameters;
|
||||
|
||||
/**
|
||||
* BillTransformer constructor.
|
||||
* TagTransformer constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param ParameterBag $parameters
|
||||
*/
|
||||
@ -60,18 +65,67 @@ class TagTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Include any transactions.
|
||||
*
|
||||
* @param Tag $tag
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return FractalCollection
|
||||
*/
|
||||
public function includeTransactions(Tag $tag): FractalCollection
|
||||
{
|
||||
$pageSize = intval(app('preferences')->getForUser($tag->user, 'listPageSize', 50)->data);
|
||||
|
||||
// journals always use collector and limited using URL parameters.
|
||||
$collector = new JournalCollector;
|
||||
$collector->setUser($tag->user);
|
||||
$collector->withOpposingAccount()->withCategoryInformation()->withCategoryInformation();
|
||||
$collector->setAllAssetAccounts();
|
||||
$collector->setTag($tag);
|
||||
if (!is_null($this->parameters->get('start')) && !is_null($this->parameters->get('end'))) {
|
||||
$collector->setRange($this->parameters->get('start'), $this->parameters->get('end'));
|
||||
}
|
||||
$collector->setLimit($pageSize)->setPage($this->parameters->get('page'));
|
||||
$journals = $collector->getJournals();
|
||||
|
||||
return $this->collection($journals, new TransactionTransformer($this->parameters), 'transactions');
|
||||
}
|
||||
|
||||
/**
|
||||
* Include the user.
|
||||
*
|
||||
* @param Tag $tag
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
* @return Item
|
||||
*/
|
||||
public function includeUser(Tag $tag): Item
|
||||
{
|
||||
return $this->item($tag->user, new UserTransformer($this->parameters), 'users');
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform a tag.
|
||||
*
|
||||
* @param Tag $tag
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function transform(Tag $tag): array
|
||||
{
|
||||
$date = is_null($tag->date) ? null : $tag->date->format('Y-m-d');
|
||||
$data = [
|
||||
'id' => (int)$tag->id,
|
||||
'updated_at' => $tag->updated_at->toAtomString(),
|
||||
'created_at' => $tag->created_at->toAtomString(),
|
||||
'tag' => $tag->tag,
|
||||
'links' => [
|
||||
'id' => (int)$tag->id,
|
||||
'updated_at' => $tag->updated_at->toAtomString(),
|
||||
'created_at' => $tag->created_at->toAtomString(),
|
||||
'tag' => $tag->tag,
|
||||
'tag_mode' => $tag->tagMode,
|
||||
'date' => $date,
|
||||
'description' => $tag->description === '' ? null : $tag->description,
|
||||
'latitude' => (float)$tag->latitude,
|
||||
'longitude' => (float)$tag->longitude,
|
||||
'zoom_level' => (int)$tag->zoomLevel,
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
'uri' => '/tags/' . $tag->id,
|
||||
|
@ -54,7 +54,9 @@ class TransactionTransformer extends TransformerAbstract
|
||||
protected $parameters;
|
||||
|
||||
/**
|
||||
* BillTransformer constructor.
|
||||
* TransactionTransformer constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param ParameterBag $parameters
|
||||
*/
|
||||
@ -64,6 +66,10 @@ class TransactionTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Include attachments.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return FractalCollection
|
||||
@ -74,6 +80,10 @@ class TransactionTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Include meta data
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return FractalCollection
|
||||
@ -86,6 +96,10 @@ class TransactionTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Include piggy bank events
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return FractalCollection
|
||||
@ -98,6 +112,10 @@ class TransactionTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Include tags
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return FractalCollection
|
||||
@ -110,6 +128,10 @@ class TransactionTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Include the user.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return \League\Fractal\Resource\Item
|
||||
@ -121,6 +143,8 @@ class TransactionTransformer extends TransformerAbstract
|
||||
|
||||
|
||||
/**
|
||||
* Transform the journal.
|
||||
*
|
||||
* @param Transaction $transaction
|
||||
*
|
||||
* @return array
|
||||
@ -128,6 +152,21 @@ class TransactionTransformer extends TransformerAbstract
|
||||
*/
|
||||
public function transform(Transaction $transaction): array
|
||||
{
|
||||
$categoryId = null;
|
||||
$categoryName = null;
|
||||
$budgetId = null;
|
||||
$budgetName = null;
|
||||
$categoryId = is_null($transaction->transaction_category_id) ? $transaction->transaction_journal_category_id
|
||||
: $transaction->transaction_category_id;
|
||||
$categoryName = is_null($transaction->transaction_category_name) ? $transaction->transaction_journal_category_name
|
||||
: $transaction->transaction_category_name;
|
||||
|
||||
if ($transaction->transaction_type_type === TransactionType::WITHDRAWAL) {
|
||||
$budgetId = is_null($transaction->transaction_budget_id) ? $transaction->transaction_journal_budget_id
|
||||
: $transaction->transaction_budget_id;
|
||||
$budgetName = is_null($transaction->transaction_budget_name) ? $transaction->transaction_journal_budget_name
|
||||
: $transaction->transaction_budget_name;
|
||||
}
|
||||
$data = [
|
||||
'id' => (int)$transaction->id,
|
||||
'updated_at' => $transaction->updated_at->toAtomString(),
|
||||
@ -138,7 +177,7 @@ class TransactionTransformer extends TransformerAbstract
|
||||
'identifier' => $transaction->identifier,
|
||||
'journal_id' => (int)$transaction->journal_id,
|
||||
'reconciled' => (bool)$transaction->reconciled,
|
||||
'amount' => round($transaction->transaction_amount, $transaction->transaction_currency_dp),
|
||||
'amount' => round($transaction->transaction_amount, intval($transaction->transaction_currency_dp)),
|
||||
'currency_id' => $transaction->transaction_currency_id,
|
||||
'currency_code' => $transaction->transaction_currency_code,
|
||||
'currency_dp' => $transaction->transaction_currency_dp,
|
||||
@ -148,14 +187,10 @@ class TransactionTransformer extends TransformerAbstract
|
||||
'foreign_currency_dp' => $transaction->foreign_currency_dp,
|
||||
'bill_id' => $transaction->bill_id,
|
||||
'bill_name' => $transaction->bill_name,
|
||||
'category_id' => is_null($transaction->transaction_category_id) ? $transaction->transaction_journal_category_id
|
||||
: $transaction->transaction_category_id,
|
||||
'category_name' => is_null($transaction->transaction_category_name) ? $transaction->transaction_journal_category_name
|
||||
: $transaction->transaction_category_name,
|
||||
'budget_id' => is_null($transaction->transaction_budget_id) ? $transaction->transaction_journal_budget_id
|
||||
: $transaction->transaction_budget_id,
|
||||
'budget_name' => is_null($transaction->transaction_budget_name) ? $transaction->transaction_journal_budget_name
|
||||
: $transaction->transaction_budget_name,
|
||||
'category_id' => $categoryId,
|
||||
'category_name' => $categoryName,
|
||||
'budget_id' => $budgetId,
|
||||
'budget_name' => $budgetName,
|
||||
'links' => [
|
||||
[
|
||||
'rel' => 'self',
|
||||
@ -166,12 +201,12 @@ class TransactionTransformer extends TransformerAbstract
|
||||
|
||||
// expand foreign amount:
|
||||
if (!is_null($transaction->transaction_foreign_amount)) {
|
||||
$data['foreign_amount'] = round($transaction->transaction_foreign_amount, $transaction->foreign_currency_dp);
|
||||
$data['foreign_amount'] = round($transaction->transaction_foreign_amount, intval($transaction->foreign_currency_dp));
|
||||
}
|
||||
|
||||
// switch on type for consistency
|
||||
switch (true) {
|
||||
case TransactionType::WITHDRAWAL === $transaction->transaction_type_type:
|
||||
switch ($transaction->transaction_type_type) {
|
||||
case TransactionType::WITHDRAWAL:
|
||||
$data['source_id'] = $transaction->account_id;
|
||||
$data['source_name'] = $transaction->account_name;
|
||||
$data['source_iban'] = $transaction->account_iban;
|
||||
@ -181,7 +216,10 @@ class TransactionTransformer extends TransformerAbstract
|
||||
$data['destination_iban'] = $transaction->opposing_account_iban;
|
||||
$data['destination_type'] = $transaction->opposing_account_type;
|
||||
break;
|
||||
case TransactionType::DEPOSIT === $transaction->transaction_type_type:
|
||||
case TransactionType::DEPOSIT:
|
||||
case TransactionType::TRANSFER:
|
||||
case TransactionType::OPENING_BALANCE:
|
||||
case TransactionType::RECONCILIATION:
|
||||
$data['source_id'] = $transaction->opposing_account_id;
|
||||
$data['source_name'] = $transaction->opposing_account_name;
|
||||
$data['source_iban'] = $transaction->opposing_account_iban;
|
||||
@ -191,30 +229,12 @@ class TransactionTransformer extends TransformerAbstract
|
||||
$data['destination_iban'] = $transaction->account_iban;
|
||||
$data['destination_type'] = $transaction->account_type;
|
||||
break;
|
||||
case TransactionType::TRANSFER === $transaction->transaction_type_type && bccomp($transaction->transaction_amount, '0') > 0:
|
||||
$data['source_id'] = $transaction->opposing_account_id;
|
||||
$data['source_name'] = $transaction->opposing_account_name;
|
||||
$data['source_iban'] = $transaction->opposing_account_iban;
|
||||
$data['source_type'] = $transaction->opposing_account_type;
|
||||
$data['destination_id'] = $transaction->account_id;
|
||||
$data['destination_name'] = $transaction->account_name;
|
||||
$data['destination_iban'] = $transaction->account_iban;
|
||||
$data['destination_type'] = $transaction->account_type;
|
||||
break;
|
||||
case TransactionType::TRANSFER === $transaction->transaction_type_type && bccomp($transaction->transaction_amount, '0') < 0:
|
||||
$data['source_id'] = $transaction->account_id;
|
||||
$data['source_name'] = $transaction->account_name;
|
||||
$data['source_iban'] = $transaction->account_iban;
|
||||
$data['source_type'] = $transaction->account_type;
|
||||
$data['destination_id'] = $transaction->opposing_account_id;
|
||||
$data['destination_name'] = $transaction->opposing_account_name;
|
||||
$data['destination_iban'] = $transaction->opposing_account_iban;
|
||||
$data['destination_type'] = $transaction->opposing_account_type;
|
||||
$data['amount'] = $data['amount'] * -1;
|
||||
$data['foreign_amount'] = is_null($data['foreign_amount']) ? null : $data['foreign_amount'] * -1;
|
||||
break;
|
||||
default:
|
||||
throw new FireflyException(sprintf('Cannot handle % s!', $transaction->transaction_type_type));
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new FireflyException(
|
||||
sprintf('Transaction transformer cannot handle transactions of type "%s"!', $transaction->transaction_type_type)
|
||||
);
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
}
|
||||
|
||||
|
@ -38,7 +38,9 @@ class UserTransformer extends TransformerAbstract
|
||||
protected $parameters;
|
||||
|
||||
/**
|
||||
* BillTransformer constructor.
|
||||
* UserTransformer constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param ParameterBag $parameters
|
||||
*/
|
||||
@ -48,6 +50,8 @@ class UserTransformer extends TransformerAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform user.
|
||||
*
|
||||
* @param User $user
|
||||
*
|
||||
* @return array
|
||||
|
@ -75,7 +75,7 @@ class BillTransformerTest extends TestCase
|
||||
public function testNote()
|
||||
{
|
||||
|
||||
$bill = Bill::create(
|
||||
$bill = Bill::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'name' => 'Some bill ' . rand(1, 10000),
|
||||
@ -88,8 +88,8 @@ class BillTransformerTest extends TestCase
|
||||
'active' => 1,
|
||||
]
|
||||
);
|
||||
$noteText = 'I are a note ' . rand(1, 10000);
|
||||
$note = Note::create(
|
||||
$noteText = 'I are a note ' . rand(1, 10000);
|
||||
Note::create(
|
||||
[
|
||||
'noteable_id' => $bill->id,
|
||||
'noteable_type' => Bill::class,
|
||||
@ -118,7 +118,7 @@ class BillTransformerTest extends TestCase
|
||||
// mock stuff
|
||||
$repository = $this->mock(BillRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->andReturnSelf();
|
||||
$repository->shouldReceive('getPaidDatesInRange')->andReturn(new Collection([new Carbon]));
|
||||
$repository->shouldReceive('getPaidDatesInRange')->andReturn(new Collection([new Carbon('2018-01-02')]));
|
||||
$bill = Bill::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
@ -127,7 +127,7 @@ class BillTransformerTest extends TestCase
|
||||
'amount_min' => 12.34,
|
||||
'amount_max' => 45.67,
|
||||
'date' => '2018-01-02',
|
||||
'repeat_freq' => 'weekly',
|
||||
'repeat_freq' => 'monthly',
|
||||
'skip' => 0,
|
||||
'active' => 1,
|
||||
]
|
||||
@ -140,6 +140,10 @@ class BillTransformerTest extends TestCase
|
||||
|
||||
$this->assertEquals($bill->name, $result['name']);
|
||||
$this->assertTrue($result['active']);
|
||||
$this->assertCount(1, $result['pay_dates']);
|
||||
$this->assertEquals('2018-01-02', $result['pay_dates'][0]);
|
||||
$this->assertCount(1, $result['paid_dates']);
|
||||
$this->assertEquals('2018-01-02', $result['paid_dates'][0]);
|
||||
}
|
||||
|
||||
}
|
58
tests/Unit/Transformers/BudgetTransformerTest.php
Normal file
58
tests/Unit/Transformers/BudgetTransformerTest.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* BudgetTransformerTest.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\Transformers;
|
||||
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Transformers\BudgetTransformer;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
|
||||
/**
|
||||
* Class BudgetTransformerTest
|
||||
*/
|
||||
class BudgetTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Basic coverage
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\BudgetTransformer::transform
|
||||
*/
|
||||
public function testBasic()
|
||||
{
|
||||
|
||||
$budget = Budget::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'name' => 'Some budget ' . rand(1, 10000),
|
||||
'active' => 1,
|
||||
]
|
||||
);
|
||||
$transformer = new BudgetTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($budget);
|
||||
|
||||
$this->assertEquals($budget->name, $result['name']);
|
||||
$this->assertTrue($result['active']);
|
||||
}
|
||||
}
|
57
tests/Unit/Transformers/CategoryTransformerTest.php
Normal file
57
tests/Unit/Transformers/CategoryTransformerTest.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* CategoryTransformerTest.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\Transformers;
|
||||
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Transformers\CategoryTransformer;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
|
||||
/**
|
||||
* Class CategoryTransformerTest
|
||||
*/
|
||||
class CategoryTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Basic coverage
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\CategoryTransformer::transform
|
||||
*/
|
||||
public function testBasic()
|
||||
{
|
||||
|
||||
$category = Category::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'name' => 'Some budget ' . rand(1, 10000),
|
||||
'active' => 1,
|
||||
]
|
||||
);
|
||||
$transformer = new CategoryTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($category);
|
||||
|
||||
$this->assertEquals($category->name, $result['name']);
|
||||
}
|
||||
}
|
60
tests/Unit/Transformers/JournalMetaTransformerTest.php
Normal file
60
tests/Unit/Transformers/JournalMetaTransformerTest.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* JournalMetaTransformerTest.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\Transformers;
|
||||
|
||||
use FireflyIII\Models\TransactionJournalMeta;
|
||||
use FireflyIII\Transformers\JournalMetaTransformer;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class JournalMetaTransformerTest
|
||||
*/
|
||||
class JournalMetaTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Basic coverage
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\JournalMetaTransformer::transform
|
||||
*/
|
||||
public function testBasic()
|
||||
{
|
||||
$data = 'Lots of data';
|
||||
$hash = hash('sha256', json_encode($data));
|
||||
$meta = TransactionJournalMeta::create(
|
||||
[
|
||||
'transaction_journal_id' => 1,
|
||||
'name' => 'someField',
|
||||
'data' => $data,
|
||||
]
|
||||
);
|
||||
|
||||
$transformer = new JournalMetaTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($meta);
|
||||
|
||||
$this->assertEquals($meta->name, $result['name']);
|
||||
$this->assertEquals($hash, $result['hash']);
|
||||
}
|
||||
|
||||
}
|
144
tests/Unit/Transformers/PiggyBankEventTransformerTest.php
Normal file
144
tests/Unit/Transformers/PiggyBankEventTransformerTest.php
Normal file
@ -0,0 +1,144 @@
|
||||
<?php
|
||||
/**
|
||||
* PiggyBankEventTransformerTest.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\Transformers;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountMeta;
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Models\PiggyBankEvent;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Transformers\PiggyBankEventTransformer;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class PiggyBankEventTransformerTest
|
||||
*/
|
||||
class PiggyBankEventTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Basic test with no meta data.
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\PiggyBankEventTransformer::transform
|
||||
*/
|
||||
public function testBasic()
|
||||
{
|
||||
// make new account:
|
||||
$account = Account::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'account_type_id' => 3, // asset account
|
||||
'name' => 'Random name #' . rand(1, 10000),
|
||||
'virtual_balance' => 12.34,
|
||||
'iban' => 'NL85ABNA0466812694',
|
||||
'active' => 1,
|
||||
'encrypted' => 0,
|
||||
]
|
||||
);
|
||||
$piggy = PiggyBank::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'Some random piggy #' . rand(1, 10000),
|
||||
'targetamount' => '1000',
|
||||
'startdate' => '2018-01-01',
|
||||
'targetdate' => '2018-01-31',
|
||||
'order' => 1,
|
||||
'active' => 1,
|
||||
]
|
||||
);
|
||||
$event = PiggyBankEvent::create(
|
||||
[
|
||||
'piggy_bank_id' => $piggy->id,
|
||||
'date' => '2018-01-01',
|
||||
'amount' => '123.45',
|
||||
]
|
||||
);
|
||||
|
||||
$transformer = new PiggyBankEventTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($event);
|
||||
$this->assertEquals($event->id, $result['id']);
|
||||
$this->assertEquals(123.45, $result['amount']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic test with currency meta data.
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\PiggyBankEventTransformer::transform
|
||||
*/
|
||||
public function testBasicCurrency()
|
||||
{
|
||||
// mock repository.
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1))->once();
|
||||
|
||||
// make new account:
|
||||
$account = Account::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'account_type_id' => 3, // asset account
|
||||
'name' => 'Random name #' . rand(1, 10000),
|
||||
'virtual_balance' => 12.34,
|
||||
'iban' => 'NL85ABNA0466812694',
|
||||
'active' => 1,
|
||||
'encrypted' => 0,
|
||||
]
|
||||
);
|
||||
|
||||
// meta
|
||||
$accountMeta = AccountMeta::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'currency_id',
|
||||
'data' => 1,
|
||||
]
|
||||
);
|
||||
|
||||
$piggy = PiggyBank::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'Some random piggy #' . rand(1, 10000),
|
||||
'targetamount' => '1000',
|
||||
'startdate' => '2018-01-01',
|
||||
'targetdate' => '2018-01-31',
|
||||
'order' => 1,
|
||||
'active' => 1,
|
||||
]
|
||||
);
|
||||
$event = PiggyBankEvent::create(
|
||||
[
|
||||
'piggy_bank_id' => $piggy->id,
|
||||
'date' => '2018-01-01',
|
||||
'amount' => '123.45',
|
||||
]
|
||||
);
|
||||
|
||||
$transformer = new PiggyBankEventTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($event);
|
||||
$this->assertEquals($event->id, $result['id']);
|
||||
$this->assertEquals(123.45, $result['amount']);
|
||||
}
|
||||
|
||||
}
|
208
tests/Unit/Transformers/PiggyBankTransformerTest.php
Normal file
208
tests/Unit/Transformers/PiggyBankTransformerTest.php
Normal file
@ -0,0 +1,208 @@
|
||||
<?php
|
||||
/**
|
||||
* PiggyBankTransformerTest.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\Transformers;
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountMeta;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
|
||||
use FireflyIII\Transformers\PiggyBankTransformer;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class PiggyBankTransformerTest
|
||||
*/
|
||||
class PiggyBankTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test basic transformer.
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\PiggyBankTransformer::transform()
|
||||
*/
|
||||
public function testBasic()
|
||||
{
|
||||
// mock repository:
|
||||
$repository = $this->mock(PiggyBankRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('getCurrentAmount')->andReturn('12.34')->once();
|
||||
|
||||
// make new account and piggy
|
||||
$account = Account::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'account_type_id' => 3, // asset account
|
||||
'name' => 'Random name #' . rand(1, 10000),
|
||||
'virtual_balance' => 12.34,
|
||||
'iban' => 'NL85ABNA0466812694',
|
||||
'active' => 1,
|
||||
'encrypted' => 0,
|
||||
]
|
||||
);
|
||||
$piggy = PiggyBank::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'Some random piggy #' . rand(1, 10000),
|
||||
'targetamount' => '1000',
|
||||
'startdate' => '2018-01-01',
|
||||
'targetdate' => '2018-01-31',
|
||||
'order' => 1,
|
||||
'active' => 1,
|
||||
]
|
||||
);
|
||||
$transformer = new PiggyBankTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($piggy);
|
||||
$this->assertTrue($result['active']);
|
||||
$this->assertEquals(12.34, $result['current_amount']);
|
||||
$this->assertEquals($piggy->name, $result['name']);
|
||||
$this->assertEquals('', $result['notes']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test basic transformer with currency preference
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\PiggyBankTransformer::transform()
|
||||
*/
|
||||
public function testBasicWithCurrency()
|
||||
{
|
||||
// mock repository.
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$currencyRepos->shouldReceive('setUser')->once();
|
||||
$currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1))->once();
|
||||
|
||||
// mock repository:
|
||||
$repository = $this->mock(PiggyBankRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('getCurrentAmount')->andReturn('12.34')->once();
|
||||
|
||||
// make new account and piggy
|
||||
$account = Account::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'account_type_id' => 3, // asset account
|
||||
'name' => 'Random name #' . rand(1, 10000),
|
||||
'virtual_balance' => 12.34,
|
||||
'iban' => 'NL85ABNA0466812694',
|
||||
'active' => 1,
|
||||
'encrypted' => 0,
|
||||
]
|
||||
);
|
||||
// meta
|
||||
$accountMeta = AccountMeta::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'currency_id',
|
||||
'data' => 1,
|
||||
]
|
||||
);
|
||||
|
||||
$piggy = PiggyBank::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'Some random piggy #' . rand(1, 10000),
|
||||
'targetamount' => '1000',
|
||||
'startdate' => '2018-01-01',
|
||||
'targetdate' => '2018-01-31',
|
||||
'order' => 1,
|
||||
'active' => 1,
|
||||
]
|
||||
);
|
||||
$transformer = new PiggyBankTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($piggy);
|
||||
$this->assertTrue($result['active']);
|
||||
$this->assertEquals(12.34, $result['current_amount']);
|
||||
$this->assertEquals($piggy->name, $result['name']);
|
||||
$this->assertEquals('', $result['notes']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test basic transformer with currency preference and a note
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\PiggyBankTransformer::transform()
|
||||
*/
|
||||
public function testBasicWithCurrencyAndNote()
|
||||
{
|
||||
// mock repository.
|
||||
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$currencyRepos->shouldReceive('setUser')->once();
|
||||
$currencyRepos->shouldReceive('findNull')->withArgs([1])->andReturn(TransactionCurrency::find(1))->once();
|
||||
|
||||
// mock repository:
|
||||
$repository = $this->mock(PiggyBankRepositoryInterface::class);
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('getCurrentAmount')->andReturn('12.34')->once();
|
||||
|
||||
// make new account and piggy
|
||||
$account = Account::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'account_type_id' => 3, // asset account
|
||||
'name' => 'Random name #' . rand(1, 10000),
|
||||
'virtual_balance' => 12.34,
|
||||
'iban' => 'NL85ABNA0466812694',
|
||||
'active' => 1,
|
||||
'encrypted' => 0,
|
||||
]
|
||||
);
|
||||
// meta
|
||||
$accountMeta = AccountMeta::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'currency_id',
|
||||
'data' => 1,
|
||||
]
|
||||
);
|
||||
|
||||
$piggy = PiggyBank::create(
|
||||
[
|
||||
'account_id' => $account->id,
|
||||
'name' => 'Some random piggy #' . rand(1, 10000),
|
||||
'targetamount' => '1000',
|
||||
'startdate' => '2018-01-01',
|
||||
'targetdate' => '2018-01-31',
|
||||
'order' => 1,
|
||||
'active' => 1,
|
||||
]
|
||||
);
|
||||
|
||||
// note:
|
||||
Note::create(
|
||||
[
|
||||
'noteable_id' => $piggy->id,
|
||||
'noteable_type' => PiggyBank::class,
|
||||
'title' => null,
|
||||
'text' => 'I am a note.',
|
||||
]
|
||||
);
|
||||
$transformer = new PiggyBankTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($piggy);
|
||||
$this->assertTrue($result['active']);
|
||||
$this->assertEquals(12.34, $result['current_amount']);
|
||||
$this->assertEquals($piggy->name, $result['name']);
|
||||
$this->assertEquals('I am a note.', $result['notes']);
|
||||
}
|
||||
}
|
64
tests/Unit/Transformers/TagTransformerTest.php
Normal file
64
tests/Unit/Transformers/TagTransformerTest.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* TagTransformerTest.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\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Transformers\TagTransformer;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class TagTransformerTest
|
||||
*/
|
||||
class TagTransformerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test basic tag transformer
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\TagTransformer::transform
|
||||
*/
|
||||
public function testBasic()
|
||||
{
|
||||
$tag = Tag::create(
|
||||
[
|
||||
'user_id' => $this->user()->id,
|
||||
'tag' => 'Some tag ' . rand(1, 1000),
|
||||
'tagMode' => 'nothing',
|
||||
'date' => '2018-01-01',
|
||||
'description' => 'Some tag',
|
||||
'latitude' => 5.5,
|
||||
'longitude' => '6.6',
|
||||
'zoomLevel' => 3,
|
||||
]
|
||||
);
|
||||
$transformer = new TagTransformer(new ParameterBag);
|
||||
$result = $transformer->transform($tag);
|
||||
$this->assertEquals('nothing', $result['tag_mode']);
|
||||
$this->assertEquals($tag->tag, $result['tag']);
|
||||
$this->assertEquals(5.5, $result['latitude']);
|
||||
$this->assertEquals(6.6, $result['longitude']);
|
||||
}
|
||||
|
||||
}
|
1403
tests/Unit/Transformers/TransactionTransformerTest.php
Normal file
1403
tests/Unit/Transformers/TransactionTransformerTest.php
Normal file
File diff suppressed because it is too large
Load Diff
67
tests/Unit/Transformers/UserTransformerTest.php
Normal file
67
tests/Unit/Transformers/UserTransformerTest.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* UserTransformerTest.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\Transformers;
|
||||
|
||||
|
||||
use FireflyIII\Transformers\UserTransformer;
|
||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Class UserTransformerTest
|
||||
*/
|
||||
class UserTransformerTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* Test basic transformer.
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\UserTransformer::transform
|
||||
*/
|
||||
public function testBasic()
|
||||
{
|
||||
$user = $this->user();
|
||||
$transformer = new UserTransformer(new ParameterBag());
|
||||
$result = $transformer->transform($user);
|
||||
|
||||
$this->assertEquals($user->email, $result['email']);
|
||||
$this->assertEquals('owner', $result['role']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test basic transformer.
|
||||
*
|
||||
* @covers \FireflyIII\Transformers\UserTransformer::transform
|
||||
*/
|
||||
public function testEmptyUser()
|
||||
{
|
||||
$user = $this->emptyUser();
|
||||
$transformer = new UserTransformer(new ParameterBag());
|
||||
$result = $transformer->transform($user);
|
||||
|
||||
$this->assertEquals($user->email, $result['email']);
|
||||
$this->assertNull($result['role']);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user