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

View File

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

View File

@ -23,6 +23,7 @@ use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
use Steam;
/**
@ -91,6 +92,7 @@ class MetaPieChart implements MetaPieChartInterface
if ($this->collectOtherObjects && $direction === 'expense') {
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setUser($this->user);
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)->setTypes([TransactionType::WITHDRAWAL]);
$journals = $collector->getJournals();
$sum = strval($journals->sum('transaction_amount'));
@ -102,6 +104,7 @@ class MetaPieChart implements MetaPieChartInterface
if ($this->collectOtherObjects && $direction === 'income') {
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setUser($this->user);
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)->setTypes([TransactionType::DEPOSIT]);
$journals = $collector->getJournals();
$sum = strval($journals->sum('transaction_amount'));
@ -114,6 +117,8 @@ class MetaPieChart implements MetaPieChartInterface
}
/**
* @codeCoverageIgnore
*
* @param Collection $accounts
*
* @return MetaPieChartInterface
@ -126,6 +131,8 @@ class MetaPieChart implements MetaPieChartInterface
}
/**
* @codeCoverageIgnore
*
* @param Collection $budgets
*
* @return MetaPieChartInterface
@ -138,6 +145,8 @@ class MetaPieChart implements MetaPieChartInterface
}
/**
* @codeCoverageIgnore
*
* @param Collection $categories
*
* @return MetaPieChartInterface
@ -150,6 +159,8 @@ class MetaPieChart implements MetaPieChartInterface
}
/**
* @codeCoverageIgnore
*
* @param bool $collectOtherObjects
*
* @return MetaPieChartInterface
@ -162,6 +173,8 @@ class MetaPieChart implements MetaPieChartInterface
}
/**
* @codeCoverageIgnore
*
* @param Carbon $end
*
* @return MetaPieChartInterface
@ -174,6 +187,8 @@ class MetaPieChart implements MetaPieChartInterface
}
/**
* @codeCoverageIgnore
*
* @param Carbon $start
*
* @return MetaPieChartInterface
@ -186,6 +201,8 @@ class MetaPieChart implements MetaPieChartInterface
}
/**
* @codeCoverageIgnore
*
* @param Collection $tags
*
* @return MetaPieChartInterface
@ -198,6 +215,8 @@ class MetaPieChart implements MetaPieChartInterface
}
/**
* @codeCoverageIgnore
*
* @param User $user
*
* @return MetaPieChartInterface
@ -209,7 +228,12 @@ class MetaPieChart implements MetaPieChartInterface
return $this;
}
protected function getTransactions(string $direction)
/**
* @param string $direction
*
* @return Collection
*/
protected function getTransactions(string $direction): Collection
{
$types = [TransactionType::DEPOSIT, TransactionType::TRANSFER];
$modifier = -1;
@ -219,6 +243,7 @@ class MetaPieChart implements MetaPieChartInterface
}
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setUser($this->user);
$collector->setAccounts($this->accounts);
$collector->setRange($this->start, $this->end);
$collector->setTypes($types);
@ -242,7 +267,19 @@ class MetaPieChart implements MetaPieChartInterface
$accountIds = $this->accounts->pluck('id')->toArray();
$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;
}
@ -286,6 +323,7 @@ class MetaPieChart implements MetaPieChartInterface
$chartData = [];
$names = [];
$repository = app($this->repositories[$type]);
$repository->setUser($this->user);
foreach ($array as $objectId => $amount) {
if (!isset($names[$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
{
$grouped = [];

View File

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