Small changes to make code more testable.

This commit is contained in:
James Cole 2017-04-28 18:04:57 +02:00
parent 9d1508049e
commit 0307b58d17
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
5 changed files with 73 additions and 7 deletions

View File

@ -16,10 +16,11 @@ namespace FireflyIII\Helpers\Attachments;
use Crypt; use Crypt;
use FireflyIII\Models\Attachment; use FireflyIII\Models\Attachment;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag; use Illuminate\Support\MessageBag;
use Storage; use Storage;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
use Log;
/** /**
* Class AttachmentHelper * Class AttachmentHelper
* *
@ -28,6 +29,8 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
class AttachmentHelper implements AttachmentHelperInterface class AttachmentHelper implements AttachmentHelperInterface
{ {
/** @var Collection */
public $attachments;
/** @var MessageBag */ /** @var MessageBag */
public $errors; public $errors;
/** @var MessageBag */ /** @var MessageBag */
@ -49,6 +52,7 @@ class AttachmentHelper implements AttachmentHelperInterface
$this->allowedMimes = (array)config('firefly.allowedMimes'); $this->allowedMimes = (array)config('firefly.allowedMimes');
$this->errors = new MessageBag; $this->errors = new MessageBag;
$this->messages = new MessageBag; $this->messages = new MessageBag;
$this->attachments = new Collection;
$this->uploadDisk = Storage::disk('upload'); $this->uploadDisk = Storage::disk('upload');
} }
@ -64,6 +68,14 @@ class AttachmentHelper implements AttachmentHelperInterface
return $path; return $path;
} }
/**
* @return Collection
*/
public function getAttachments(): Collection
{
return $this->attachments;
}
/** /**
* @return MessageBag * @return MessageBag
*/ */
@ -110,7 +122,7 @@ class AttachmentHelper implements AttachmentHelperInterface
$md5 = md5_file($file->getRealPath()); $md5 = md5_file($file->getRealPath());
$name = $file->getClientOriginalName(); $name = $file->getClientOriginalName();
$class = get_class($model); $class = get_class($model);
$count = auth()->user()->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count(); $count = $model->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count();
if ($count > 0) { if ($count > 0) {
$msg = (string)trans('validation.file_already_attached', ['name' => $name]); $msg = (string)trans('validation.file_already_attached', ['name' => $name]);
@ -137,7 +149,7 @@ class AttachmentHelper implements AttachmentHelperInterface
} }
$attachment = new Attachment; // create Attachment object. $attachment = new Attachment; // create Attachment object.
$attachment->user()->associate(auth()->user()); $attachment->user()->associate($model->user);
$attachment->attachable()->associate($model); $attachment->attachable()->associate($model);
$attachment->md5 = md5_file($file->getRealPath()); $attachment->md5 = md5_file($file->getRealPath());
$attachment->filename = $file->getClientOriginalName(); $attachment->filename = $file->getClientOriginalName();
@ -156,6 +168,7 @@ class AttachmentHelper implements AttachmentHelperInterface
$attachment->uploaded = 1; // update attachment $attachment->uploaded = 1; // update attachment
$attachment->save(); $attachment->save();
$this->attachments->push($attachment);
$name = e($file->getClientOriginalName()); // add message: $name = e($file->getClientOriginalName()); // add message:
$msg = (string)trans('validation.file_attached', ['name' => $name]); $msg = (string)trans('validation.file_attached', ['name' => $name]);
@ -188,6 +201,7 @@ class AttachmentHelper implements AttachmentHelperInterface
} }
/** /**
* @codeCoverageIgnore
* @param UploadedFile $file * @param UploadedFile $file
* *
* @return bool * @return bool
@ -218,7 +232,7 @@ class AttachmentHelper implements AttachmentHelperInterface
return false; return false;
} }
if (!$this->validSize($file)) { if (!$this->validSize($file)) {
return false; return false; // @codeCoverageIgnore
} }
if ($this->hasFile($file, $model)) { if ($this->hasFile($file, $model)) {
return false; return false;

View File

@ -15,6 +15,7 @@ namespace FireflyIII\Helpers\Attachments;
use FireflyIII\Models\Attachment; use FireflyIII\Models\Attachment;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag; use Illuminate\Support\MessageBag;
/** /**
@ -32,6 +33,11 @@ interface AttachmentHelperInterface
*/ */
public function getAttachmentLocation(Attachment $attachment): string; public function getAttachmentLocation(Attachment $attachment): string;
/**
* @return Collection
*/
public function getAttachments(): Collection;
/** /**
* @return MessageBag * @return MessageBag
*/ */

View File

@ -23,6 +23,7 @@ use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface; use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log;
use Steam; use Steam;
/** /**
@ -91,6 +92,7 @@ class MetaPieChart implements MetaPieChartInterface
if ($this->collectOtherObjects && $direction === 'expense') { if ($this->collectOtherObjects && $direction === 'expense') {
/** @var JournalCollectorInterface $collector */ /** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class); $collector = app(JournalCollectorInterface::class);
$collector->setUser($this->user);
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)->setTypes([TransactionType::WITHDRAWAL]); $collector->setAccounts($this->accounts)->setRange($this->start, $this->end)->setTypes([TransactionType::WITHDRAWAL]);
$journals = $collector->getJournals(); $journals = $collector->getJournals();
$sum = strval($journals->sum('transaction_amount')); $sum = strval($journals->sum('transaction_amount'));
@ -102,6 +104,7 @@ class MetaPieChart implements MetaPieChartInterface
if ($this->collectOtherObjects && $direction === 'income') { if ($this->collectOtherObjects && $direction === 'income') {
/** @var JournalCollectorInterface $collector */ /** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class); $collector = app(JournalCollectorInterface::class);
$collector->setUser($this->user);
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)->setTypes([TransactionType::DEPOSIT]); $collector->setAccounts($this->accounts)->setRange($this->start, $this->end)->setTypes([TransactionType::DEPOSIT]);
$journals = $collector->getJournals(); $journals = $collector->getJournals();
$sum = strval($journals->sum('transaction_amount')); $sum = strval($journals->sum('transaction_amount'));
@ -114,6 +117,8 @@ class MetaPieChart implements MetaPieChartInterface
} }
/** /**
* @codeCoverageIgnore
*
* @param Collection $accounts * @param Collection $accounts
* *
* @return MetaPieChartInterface * @return MetaPieChartInterface
@ -126,6 +131,8 @@ class MetaPieChart implements MetaPieChartInterface
} }
/** /**
* @codeCoverageIgnore
*
* @param Collection $budgets * @param Collection $budgets
* *
* @return MetaPieChartInterface * @return MetaPieChartInterface
@ -138,6 +145,8 @@ class MetaPieChart implements MetaPieChartInterface
} }
/** /**
* @codeCoverageIgnore
*
* @param Collection $categories * @param Collection $categories
* *
* @return MetaPieChartInterface * @return MetaPieChartInterface
@ -150,6 +159,8 @@ class MetaPieChart implements MetaPieChartInterface
} }
/** /**
* @codeCoverageIgnore
*
* @param bool $collectOtherObjects * @param bool $collectOtherObjects
* *
* @return MetaPieChartInterface * @return MetaPieChartInterface
@ -162,6 +173,8 @@ class MetaPieChart implements MetaPieChartInterface
} }
/** /**
* @codeCoverageIgnore
*
* @param Carbon $end * @param Carbon $end
* *
* @return MetaPieChartInterface * @return MetaPieChartInterface
@ -174,6 +187,8 @@ class MetaPieChart implements MetaPieChartInterface
} }
/** /**
* @codeCoverageIgnore
*
* @param Carbon $start * @param Carbon $start
* *
* @return MetaPieChartInterface * @return MetaPieChartInterface
@ -186,6 +201,8 @@ class MetaPieChart implements MetaPieChartInterface
} }
/** /**
* @codeCoverageIgnore
*
* @param Collection $tags * @param Collection $tags
* *
* @return MetaPieChartInterface * @return MetaPieChartInterface
@ -198,6 +215,8 @@ class MetaPieChart implements MetaPieChartInterface
} }
/** /**
* @codeCoverageIgnore
*
* @param User $user * @param User $user
* *
* @return MetaPieChartInterface * @return MetaPieChartInterface
@ -209,7 +228,12 @@ class MetaPieChart implements MetaPieChartInterface
return $this; return $this;
} }
protected function getTransactions(string $direction) /**
* @param string $direction
*
* @return Collection
*/
protected function getTransactions(string $direction): Collection
{ {
$types = [TransactionType::DEPOSIT, TransactionType::TRANSFER]; $types = [TransactionType::DEPOSIT, TransactionType::TRANSFER];
$modifier = -1; $modifier = -1;
@ -219,6 +243,7 @@ class MetaPieChart implements MetaPieChartInterface
} }
/** @var JournalCollectorInterface $collector */ /** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class); $collector = app(JournalCollectorInterface::class);
$collector->setUser($this->user);
$collector->setAccounts($this->accounts); $collector->setAccounts($this->accounts);
$collector->setRange($this->start, $this->end); $collector->setRange($this->start, $this->end);
$collector->setTypes($types); $collector->setTypes($types);
@ -242,7 +267,19 @@ class MetaPieChart implements MetaPieChartInterface
$accountIds = $this->accounts->pluck('id')->toArray(); $accountIds = $this->accounts->pluck('id')->toArray();
$transactions = $collector->getJournals(); $transactions = $collector->getJournals();
$set = Support::filterTransactions($transactions, $accountIds, $modifier); Log::debug(sprintf('Modifier is %d', $modifier));
foreach ($transactions as $transaction) {
Log::debug(
sprintf(
'Included %s #%d (part of #%d) ("%s") amount %f',
$transaction->transaction_type_type, $transaction->id,
$transaction->journal_id
, $transaction->description, $transaction->transaction_amount
)
);
}
$set = Support::filterTransactions($transactions, $accountIds, $modifier);
return $set; return $set;
} }
@ -286,6 +323,7 @@ class MetaPieChart implements MetaPieChartInterface
$chartData = []; $chartData = [];
$names = []; $names = [];
$repository = app($this->repositories[$type]); $repository = app($this->repositories[$type]);
$repository->setUser($this->user);
foreach ($array as $objectId => $amount) { foreach ($array as $objectId => $amount) {
if (!isset($names[$objectId])) { if (!isset($names[$objectId])) {
$object = $repository->find(intval($objectId)); $object = $repository->find(intval($objectId));
@ -300,6 +338,11 @@ class MetaPieChart implements MetaPieChartInterface
} }
/**
* @param Collection $set
*
* @return array
*/
private function groupByTag(Collection $set): array private function groupByTag(Collection $set): array
{ {
$grouped = []; $grouped = [];

View File

@ -464,7 +464,9 @@ class JournalCollector implements JournalCollectorInterface
*/ */
public function setUser(User $user) public function setUser(User $user)
{ {
Log::debug(sprintf('Journal collector now collecting for user #%d', $user->id));
$this->user = $user; $this->user = $user;
$this->startQuery();
} }
/** /**
@ -472,6 +474,7 @@ class JournalCollector implements JournalCollectorInterface
*/ */
public function startQuery() public function startQuery()
{ {
Log::debug('journalCollector::startQuery');
/** @var EloquentBuilder $query */ /** @var EloquentBuilder $query */
$query = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') $query = Transaction::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('transaction_currencies', 'transaction_currencies.id', 'transaction_journals.transaction_currency_id') ->leftJoin('transaction_currencies', 'transaction_currencies.id', 'transaction_journals.transaction_currency_id')

View File

@ -62,7 +62,7 @@ class JournalServiceProvider extends ServiceProvider
if ($app->auth->check()) { if ($app->auth->check()) {
$collector->setUser(auth()->user()); $collector->setUser(auth()->user());
} }
$collector->startQuery();
return $collector; return $collector;
} }