Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Fix code and tests.
James Coleb359d51d3a5c
diff --git a/app/Support/Navigation.php b/app/Support/Navigation.phpindex 09c02e4c03..99c53eb4e1 100644--- a/app/Support/Navigation.php+++ b/app/Support/Navigation.php@@ -161,7 +161,7 @@ class Navigationpublic function startOfPeriod(Carbon $theDate, string $repeatFreq): Carbon{$date = clone $theDate;-+ Log::debug(sprintf('Now in startOfPeriod("%s", "%s")', $date->toIso8601String(), $repeatFreq));$functionMap = ['1D' => 'startOfDay','daily' => 'startOfDay',@@ -178,15 +178,33 @@ class Navigation'yearly' => 'startOfYear','1Y' => 'startOfYear',];++ $parameterMap = [+ 'startOfWeek' => [Carbon::MONDAY],+ ];+if (array_key_exists($repeatFreq, $functionMap)) {$function = $functionMap[$repeatFreq];+ Log::debug(sprintf('Function is ->%s()', $function));+ if(array_key_exists($function, $parameterMap)) {+ Log::debug(sprintf('Parameter map, function becomes ->%s(%s)', $function, join(', ', $parameterMap[$function])));+ $date->{$function}($parameterMap[$function][0]);+ Log::debug(sprintf('Result is "%s"', $date->toIso8601String()));++ return $date;+ }++$date->{$function}(); // @phpstan-ignore-line+ Log::debug(sprintf('Result is "%s"', $date->toIso8601String()));return $date;}if ('half-year' === $repeatFreq || '6M' === $repeatFreq) {$skipTo = $date->month > 7 ? 6 : 0;$date->startOfYear()->addMonths($skipTo);+ Log::debug(sprintf('Custom call for "%s": addMonths(%d)', $repeatFreq, $skipTo));+ Log::debug(sprintf('Result is "%s"', $date->toIso8601String()));return $date;}@@ -202,10 +220,12 @@ class Navigationdefault => null,};if (null !== $result) {+ Log::debug(sprintf('Result is "%s"', $date->toIso8601String()));return $result;}if ('custom' === $repeatFreq) {+ Log::debug(sprintf('Custom, result is "%s"', $date->toIso8601String()));return $date; // the date is already at the start.}Log::error(sprintf('Cannot do startOfPeriod for $repeat_freq "%s"', $repeatFreq));@@ -216,6 +236,7 @@ class Navigationpublic function endOfPeriod(Carbon $end, string $repeatFreq): Carbon{$currentEnd = clone $end;+ Log::debug(sprintf('Now in endOfPeriod("%s", "%s").', $currentEnd->toIso8601String(), $repeatFreq));$functionMap = ['1D' => 'endOfDay',@@ -296,6 +317,7 @@ class Navigationif (in_array($repeatFreq, $subDay, true)) {$currentEnd->subDay();}+ Log::debug(sprintf('Final result: %s', $currentEnd->toIso8601String()));return $currentEnd;}@@ -471,11 +493,11 @@ class Navigationpublic function preferredCarbonFormat(Carbon $start, Carbon $end): string{$format = 'Y-m-d';- if ($start->diffInMonths($end, true) > 0) {+ if ((int)$start->diffInMonths($end, true) > 1) {$format = 'Y-m';}- if ($start->diffInMonths($end, true) > 12) {+ if ((int)$start->diffInMonths($end, true) > 12) {$format = 'Y';}@@ -558,11 +580,11 @@ class Navigationpublic function preferredEndOfPeriod(Carbon $start, Carbon $end): string{$format = 'endOfDay';- if ($start->diffInMonths($end, true) > 1) {+ if ((int)$start->diffInMonths($end, true) > 1) {$format = 'endOfMonth';}- if ($start->diffInMonths($end, true) > 12) {+ if ((int)$start->diffInMonths($end, true) > 12) {$format = 'endOfYear';}@@ -576,11 +598,11 @@ class Navigationpublic function preferredRangeFormat(Carbon $start, Carbon $end): string{$format = '1D';- if ($start->diffInMonths($end, true) > 1) {+ if ((int)$start->diffInMonths($end, true) > 1) {$format = '1M';}- if ($start->diffInMonths($end, true) > 12) {+ if ((int)$start->diffInMonths($end, true) > 12) {$format = '1Y';}@@ -594,11 +616,11 @@ class Navigationpublic function preferredSqlFormat(Carbon $start, Carbon $end): string{$format = '%Y-%m-%d';- if ($start->diffInMonths($end, true) > 1) {+ if ((int)$start->diffInMonths($end, true) > 1) {$format = '%Y-%m';}- if ($start->diffInMonths($end, true) > 12) {+ if ((int)$start->diffInMonths($end, true) > 12) {$format = '%Y';}diff --git a/app/Support/ParseDateString.php b/app/Support/ParseDateString.phpindex 3140b93c76..07aea2fca8 100644--- a/app/Support/ParseDateString.php+++ b/app/Support/ParseDateString.php@@ -126,8 +126,8 @@ class ParseDateStringdefault => $today,'yesterday' => $today->subDay(),'tomorrow' => $today->addDay(),- 'start of this week' => $today->startOfWeek(),- 'end of this week' => $today->endOfWeek(),+ 'start of this week' => $today->startOfWeek(Carbon::MONDAY),+ 'end of this week' => $today->endOfWeek(Carbon::SUNDAY),'start of this month' => $today->startOfMonth(),'end of this month' => $today->endOfMonth(),'start of this quarter' => $today->startOfQuarter(),diff --git a/tests/unit/Support/Calendar/CalculatorTest.php b/tests/unit/Support/Calendar/CalculatorTest.phpindex 27d4e28efd..ea68753634 100644--- a/tests/unit/Support/Calendar/CalculatorTest.php+++ b/tests/unit/Support/Calendar/CalculatorTest.php@@ -27,7 +27,7 @@ namespace Tests\unit\Support\Calendar;use FireflyIII\Exceptions\IntervalException;use FireflyIII\Support\Calendar\Calculator;use FireflyIII\Support\Calendar\Periodicity;-use PHPUnit\Framework\TestCase;+use Tests\integration\TestCase;use Tests\unit\Support\Calendar\Periodicity\BimonthlyTest;use Tests\unit\Support\Calendar\Periodicity\DailyTest;use Tests\unit\Support\Calendar\Periodicity\FortnightlyTest;diff --git a/tests/unit/Support/Calendar/Periodicity/IntervalTestCase.php b/tests/unit/Support/Calendar/Periodicity/IntervalTestCase.phpindex 477a113189..2153f1b9a0 100644--- a/tests/unit/Support/Calendar/Periodicity/IntervalTestCase.php+++ b/tests/unit/Support/Calendar/Periodicity/IntervalTestCase.php@@ -25,7 +25,7 @@ declare(strict_types=1);namespace Tests\unit\Support\Calendar\Periodicity;use FireflyIII\Support\Calendar\Periodicity\Interval;-use PHPUnit\Framework\TestCase;+use Tests\integration\TestCase;abstract class IntervalTestCase extends TestCase{diff --git a/tests/unit/Support/NavigationAddPeriodTest.php b/tests/unit/Support/NavigationAddPeriodTest.phpindex 70bce9defc..cb0cedc1f0 100644--- a/tests/unit/Support/NavigationAddPeriodTest.php+++ b/tests/unit/Support/NavigationAddPeriodTest.php@@ -27,7 +27,7 @@ namespace Tests\unit\Support;use Carbon\Carbon;use FireflyIII\Support\Calendar\Periodicity;use FireflyIII\Support\Navigation;-use PHPUnit\Framework\TestCase;+use Tests\integration\TestCase;/*** @group unit-testdiff --git a/tests/unit/Support/NavigationEndOfPeriodTest.php b/tests/unit/Support/NavigationEndOfPeriodTest.phpindex 8196d318c9..910deedf3b 100644--- a/tests/unit/Support/NavigationEndOfPeriodTest.php+++ b/tests/unit/Support/NavigationEndOfPeriodTest.php@@ -82,8 +82,9 @@ final class NavigationEndOfPeriodTest extends TestCase*/public function testGivenADateAndFrequencyWhenCalculateTheDateThenReturnsTheExpectedDateSuccessful(string $frequency, Carbon $from, Carbon $expected): void{- $period = $this->navigation->endOfPeriod($from, $frequency);+ $period = clone $this->navigation->endOfPeriod($from, $frequency);self::assertSame($expected->toDateString(), $period->toDateString());+}public static function provideUnknownFrequencies(): iterablediff --git a/tests/unit/Support/NavigationPreferredCarbonFormatByPeriodTest.php b/tests/unit/Support/NavigationPreferredCarbonFormatByPeriodTest.phpindex 986644aab1..fcb0165476 100644--- a/tests/unit/Support/NavigationPreferredCarbonFormatByPeriodTest.php+++ b/tests/unit/Support/NavigationPreferredCarbonFormatByPeriodTest.php@@ -25,7 +25,7 @@ declare(strict_types=1);namespace Tests\unit\Support;use FireflyIII\Support\Navigation;-use PHPUnit\Framework\TestCase;+use Tests\integration\TestCase;/*** @group unit-testdiff --git a/tests/unit/Support/NavigationPreferredCarbonFormatTest.php b/tests/unit/Support/NavigationPreferredCarbonFormatTest.phpindex 304d6b9bbf..667cd77333 100644--- a/tests/unit/Support/NavigationPreferredCarbonFormatTest.php+++ b/tests/unit/Support/NavigationPreferredCarbonFormatTest.php@@ -26,7 +26,7 @@ namespace Tests\unit\Support;use Carbon\Carbon;use FireflyIII\Support\Navigation;-use PHPUnit\Framework\TestCase;+use Tests\integration\TestCase;/*** @group unit-testdiff --git a/tests/unit/Support/NavigationPreferredEndOfPeriodTest.php b/tests/unit/Support/NavigationPreferredEndOfPeriodTest.phpindex 54e8eb132c..d5f56ff678 100644--- a/tests/unit/Support/NavigationPreferredEndOfPeriodTest.php+++ b/tests/unit/Support/NavigationPreferredEndOfPeriodTest.php@@ -26,7 +26,7 @@ namespace Tests\unit\Support;use Carbon\Carbon;use FireflyIII\Support\Navigation;-use PHPUnit\Framework\TestCase;+use Tests\integration\TestCase;/*** @group unit-testdiff --git a/tests/unit/Support/NavigationPreferredRangeFormatTest.php b/tests/unit/Support/NavigationPreferredRangeFormatTest.phpindex 3d328e8e5c..fede42ff47 100644--- a/tests/unit/Support/NavigationPreferredRangeFormatTest.php+++ b/tests/unit/Support/NavigationPreferredRangeFormatTest.php@@ -26,7 +26,7 @@ namespace Tests\unit\Support;use Carbon\Carbon;use FireflyIII\Support\Navigation;-use PHPUnit\Framework\TestCase;+use Tests\integration\TestCase;/*** @group unit-testdiff --git a/tests/unit/Support/NavigationPreferredSqlFormatTest.php b/tests/unit/Support/NavigationPreferredSqlFormatTest.phpindex 6e1d1eac32..24a9c174fa 100644--- a/tests/unit/Support/NavigationPreferredSqlFormatTest.php+++ b/tests/unit/Support/NavigationPreferredSqlFormatTest.php@@ -26,7 +26,7 @@ namespace Tests\unit\Support;use Carbon\Carbon;use FireflyIII\Support\Navigation;-use PHPUnit\Framework\TestCase;+use Tests\integration\TestCase;/*** @group unit-testdiff --git a/tests/unit/Support/NavigationStartOfPeriodTest.php b/tests/unit/Support/NavigationStartOfPeriodTest.phpindex f2e2528742..315cb1695c 100644--- a/tests/unit/Support/NavigationStartOfPeriodTest.php+++ b/tests/unit/Support/NavigationStartOfPeriodTest.php@@ -27,7 +27,7 @@ namespace Tests\unit\Support;use Carbon\Carbon;use FireflyIII\Support\Navigation;use Illuminate\Support\Facades\Log;-use PHPUnit\Framework\TestCase;+use Tests\integration\TestCase;/*** @group unit-test@@ -101,6 +101,8 @@ final class NavigationStartOfPeriodTest extends TestCase*/public function testGivenADateAndUnknownFrequencyWhenCalculateTheDateThenReturnsTheSameDateSuccessful(string $frequency, Carbon $from, Carbon $expected): void{+ Log::spy();+Log::shouldReceive('error')->with(sprintf('Cannot do startOfPeriod for $repeat_freq "%s"', $frequency))->andReturnNull()