Basic testing, not full coverage.

This commit is contained in:
James Cole
2014-08-19 13:24:11 +02:00
parent f5330728d4
commit f00dfd2859
17 changed files with 325 additions and 162 deletions

View File

@@ -0,0 +1,62 @@
<?php
namespace Firefly\Trigger\Budgets;
use Illuminate\Events\Dispatcher;
/**
* Class EloquentLimitTrigger
*
* @package Firefly\Trigger\Budgets
*/
class EloquentBudgetTrigger
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
//$events->listen('budgets.change', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
$events->listen('budgets.destroy', 'Firefly\Trigger\Budgets\EloquentBudgetTrigger@destroy');
$events->listen('budgets.store', 'Firefly\Trigger\Budgets\EloquentBudgetTrigger@store');
$events->listen('budgets.update', 'Firefly\Trigger\Budgets\EloquentBudgetTrigger@update');
}
/**
* Destroying a budget doesn't do much either.
*
* @param \Budget $budget
* @return bool
*/
public function destroy(\Budget $budget)
{
return true;
}
/**
* A new budget is just there, there is nothing to trigger.
*
* @param \Budget $budget
* @return bool
*/
public function store(\Budget $budget)
{
return true;
}
/**
* Same. Doesn't do much.
*
* @param \Budget $budget
* @return bool
*/
public function update(\Budget $budget)
{
return true;
}
}

View File

@@ -2,7 +2,6 @@
namespace Firefly\Trigger\Limits;
use Carbon\Carbon;
use Illuminate\Events\Dispatcher;
/**
@@ -18,71 +17,102 @@ class EloquentLimitTrigger
*/
public function subscribe(Dispatcher $events)
{
$events->listen('budgets.change', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
//$events->listen('budgets.change', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
$events->listen('limits.destroy', 'Firefly\Trigger\Limits\EloquentLimitTrigger@destroy');
$events->listen('limits.store', 'Firefly\Trigger\Limits\EloquentLimitTrigger@store');
$events->listen('limits.update', 'Firefly\Trigger\Limits\EloquentLimitTrigger@update');
}
/**
*
*/
public function updateLimitRepetitions()
public function destroy(\Limit $limit)
{
if (!\Auth::check() || is_null(\Auth::user())) {
\Log::debug('No user for updateLimitRepetitions.');
return;
}
return true;
// get budgets with limits:
$budgets = \Auth::user()->budgets()
->with(
['limits', 'limits.limitrepetitions']
)
->where('components.class', 'Budget')
->get(['components.*']);
// double check the non-repeating budgetlimits first.
foreach ($budgets as $budget) {
\Log::debug('Budgetstart: ' . $budget->name);
foreach ($budget->limits as $limit) {
if ($limit->repeats == 0) {
$limit->createRepetition($limit->startdate);
}
if ($limit->repeats == 1) {
$start = $limit->startdate;
$end = new Carbon;
// repeat for period:
$current = clone $start;
\Log::debug('Create repeating limit for #' . $limit->id . ' starting on ' . $current);
while ($current <= $end) {
\Log::debug('Current is now: ' . $current);
$limit->createRepetition(clone $current);
// switch period, add time:
switch ($limit->repeat_freq) {
case 'daily':
$current->addDay();
break;
case 'weekly':
$current->addWeek();
break;
case 'monthly':
$current->addMonth();
break;
case 'quarterly':
$current->addMonths(3);
break;
case 'half-year':
$current->addMonths(6);
break;
case 'yearly':
$current->addYear();
break;
}
}
}
}
}
}
public function store(\Limit $limit)
{
// create a repetition (repetitions) for this limit (we ignore "repeats"):
$limit->createRepetition($limit->startdate);
// we may want to build a routine that does this for repeating limits.
// TODO.
return true;
}
public function update(\Limit $limit)
{
// remove and recreate limit repetitions.
// if limit is not repeating, simply update the repetition to match the limit,
// even though deleting everything is easier.
foreach($limit->limitrepetitions()->get() as $l) {
$l->delete();
}
$limit->createRepetition($limit->startdate);
return true;
}
// /**
// *
// */
// public function updateLimitRepetitions()
// {
// if (!\Auth::check() || is_null(\Auth::user())) {
// \Log::debug('No user for updateLimitRepetitions.');
// return;
// }
//
// // get budgets with limits:
// $budgets = \Auth::user()->budgets()
// ->with(
// ['limits', 'limits.limitrepetitions']
// )
// ->where('components.class', 'Budget')
// ->get(['components.*']);
//
// // double check the non-repeating budgetlimits first.
// foreach ($budgets as $budget) {
// \Log::debug('Budgetstart: ' . $budget->name);
// foreach ($budget->limits as $limit) {
// if ($limit->repeats == 0) {
// $limit->createRepetition($limit->startdate);
// }
// if ($limit->repeats == 1) {
// $start = $limit->startdate;
// $end = new Carbon;
//
// // repeat for period:
// $current = clone $start;
// \Log::debug('Create repeating limit for #' . $limit->id . ' starting on ' . $current);
// while ($current <= $end) {
// \Log::debug('Current is now: ' . $current);
// $limit->createRepetition(clone $current);
// // switch period, add time:
// switch ($limit->repeat_freq) {
// case 'daily':
// $current->addDay();
// break;
// case 'weekly':
// $current->addWeek();
// break;
// case 'monthly':
// $current->addMonth();
// break;
// case 'quarterly':
// $current->addMonths(3);
// break;
// case 'half-year':
// $current->addMonths(6);
// break;
// case 'yearly':
// $current->addYear();
// break;
// }
//
// }
// }
// }
//
// }
// }
}

View File

@@ -13,12 +13,13 @@ use Illuminate\Events\Dispatcher;
class EloquentPiggybankTrigger
{
/**
* @param \Piggybank $piggyBank
* @param \Piggybank $piggyBank
* @param \TransactionJournal $journal
*/
public function createRelatedTransfer(
\Piggybank $piggyBank, \TransactionJournal $journal, \Transaction $transaction
) {
)
{
$repetition = $piggyBank->repetitionForDate($journal->date);
if (!is_null($repetition)) {
// get the amount transferred TO this
@@ -226,15 +227,17 @@ class EloquentPiggybankTrigger
}
}
public function updateRelatedTransfer(\Piggybank $piggyBank, \TransactionJournal $journal)
public function updateRelatedTransfer(\Piggybank $piggyBank)
{
die('no impl updateRelatedTransfer');
// fire the "update" trigger which should handle things just fine:
\Event::fire('piggybanks.update', [$piggyBank]);
}
//
// /**
// *