Skip to content

Firefly III: a personal finances manager

Some basic date tests

James Colec51c1b80982b
diff --git a/app/Support/Navigation.php b/app/Support/Navigation.php
index 0b7b4262ad..7f7d0ef55f 100644
--- a/app/Support/Navigation.php
+++ b/app/Support/Navigation.php
@@ -47,12 +47,23 @@ class Navigation
$add = ($skip + 1);
$functionMap = [
- '1D' => 'addDays', 'daily' => 'addDays',
- '1W' => 'addWeeks', 'weekly' => 'addWeeks', 'week' => 'addWeeks',
- '1M' => 'addMonths', 'month' => 'addMonths', 'monthly' => 'addMonths', '3M' => 'addMonths',
- 'quarter' => 'addMonths', 'quarterly' => 'addMonths', '6M' => 'addMonths', 'half-year' => 'addMonths',
- 'year' => 'addYears', 'yearly' => 'addYears', '1Y' => 'addYears',
- 'custom' => 'addMonths', // custom? just add one month.
+ '1D' => 'addDays',
+ 'daily' => 'addDays',
+ '1W' => 'addWeeks',
+ 'weekly' => 'addWeeks',
+ 'week' => 'addWeeks',
+ '1M' => 'addMonths',
+ 'month' => 'addMonths',
+ 'monthly' => 'addMonths',
+ '3M' => 'addMonths',
+ 'quarter' => 'addMonths',
+ 'quarterly' => 'addMonths',
+ '6M' => 'addMonths',
+ 'half-year' => 'addMonths',
+ 'year' => 'addYears',
+ 'yearly' => 'addYears',
+ '1Y' => 'addYears',
+ 'custom' => 'addMonths', // custom? just add one month.
];
$modifierMap = [
'quarter' => 3,
@@ -71,11 +82,16 @@ class Navigation
$function = $functionMap[$repeatFreq];
$date->$function($add);
- // if period is 1M and diff in month is 2 and new DOM = 1, sub a day:
+ // if period is 1M and diff in month is 2 and new DOM > 1, sub a number of days:
+ // result is:
+ // '2019-01-29', '2019-02-28'
+ // '2019-01-30', '2019-02-28'
+ // '2019-01-31', '2019-02-28'
+
$months = ['1M', 'month', 'monthly'];
$difference = $date->month - $theDate->month;
- if (2 === $difference && 1 === $date->day && \in_array($repeatFreq, $months, true)) {
- $date->subDay();
+ if (2 === $difference && $date->day > 0 && \in_array($repeatFreq, $months, true)) {
+ $date->subDays($date->day);
}
return $date;
@@ -162,11 +178,22 @@ class Navigation
$currentEnd = clone $end;
$functionMap = [
- '1D' => 'endOfDay', 'daily' => 'endOfDay',
- '1W' => 'addWeek', 'week' => 'addWeek', 'weekly' => 'addWeek',
- '1M' => 'addMonth', 'month' => 'addMonth', 'monthly' => 'addMonth',
- '3M' => 'addMonths', 'quarter' => 'addMonths', 'quarterly' => 'addMonths', '6M' => 'addMonths', 'half-year' => 'addMonths',
- 'year' => 'addYear', 'yearly' => 'addYear', '1Y' => 'addYear',
+ '1D' => 'endOfDay',
+ 'daily' => 'endOfDay',
+ '1W' => 'addWeek',
+ 'week' => 'addWeek',
+ 'weekly' => 'addWeek',
+ '1M' => 'addMonth',
+ 'month' => 'addMonth',
+ 'monthly' => 'addMonth',
+ '3M' => 'addMonths',
+ 'quarter' => 'addMonths',
+ 'quarterly' => 'addMonths',
+ '6M' => 'addMonths',
+ 'half-year' => 'addMonths',
+ 'year' => 'addYear',
+ 'yearly' => 'addYear',
+ '1Y' => 'addYear',
];
$modifierMap = [
'quarter' => 3,
@@ -200,14 +227,15 @@ class Navigation
if (isset($modifierMap[$repeatFreq])) {
$currentEnd->$function($modifierMap[$repeatFreq]);
-
if (\in_array($repeatFreq, $subDay, true)) {
$currentEnd->subDay();
}
+ $currentEnd->endOfDay();
return $currentEnd;
}
$currentEnd->$function();
+ $currentEnd->endOfDay();
if (\in_array($repeatFreq, $subDay, true)) {
$currentEnd->subDay();
}
diff --git a/tests/Unit/Support/NavigationTest.php b/tests/Unit/Support/NavigationTest.php
new file mode 100644
index 0000000000..cb5b4e09e4
--- /dev/null
+++ b/tests/Unit/Support/NavigationTest.php
@@ -0,0 +1,166 @@
+<?php
+/**
+ * NavigationTest.php
+ * Copyright (c) 2019 [email protected]
+ *
+ * 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\Support;
+
+
+use Carbon\Carbon;
+use FireflyIII\Exceptions\FireflyException;
+use FireflyIII\Support\Navigation;
+use Log;
+use Tests\TestCase;
+
+/**
+ *
+ * Class NavigationTest
+ */
+class NavigationTest extends TestCase
+{
+
+ /**
+ *
+ */
+ public function setUp(): void
+ {
+ parent::setUp();
+ Log::info(sprintf('Now in %s.', \get_class($this)));
+ }
+
+ /**
+ * @covers \FireflyIII\Support\Navigation
+ */
+ public function testAddPeriod(): void
+ {
+ $tests = [
+ // period, skip, start, expected end
+ ['1D', 0, '2018-01-01', '2018-01-02'],
+ ['1D', 1, '2018-01-01', '2018-01-03'],
+ ['1D', 1, '2018-02-28', '2018-03-02'],
+ ['1W', 0, '2015-02-28', '2015-03-07'],
+ ['1W', 0, '2016-02-28', '2016-03-06'], // leap year
+ ['1W', 1, '2015-02-28', '2015-03-14'],
+ ['1W', 1, '2016-02-28', '2016-03-13'], // leap year
+ ['1W', 0, '2018-01-01', '2018-01-08'],
+
+ ['1M', 0, '2018-01-01', '2018-02-01'],
+ ['1M', 0, '2018-02-01', '2018-03-01'],
+ ['1M', 0, '2018-01-28', '2018-02-28'],
+ ['1M', 0, '2016-01-29', '2016-02-29'], // leap year makes it work
+ ['1M', 0, '2019-01-29', '2019-02-28'], // jump to end of next month.
+ ['1M', 0, '2019-01-30', '2019-02-28'], // jump to end of next month.
+ ['1M', 0, '2019-01-31', '2019-02-28'], // jump to end of next month.
+ ['1M', 0, '2019-02-01', '2019-03-01'],
+ ['1M', 1, '2019-02-01', '2019-03-31'], // weird but OK.
+ ['1M', 2, '2019-01-01', '2019-04-01'],
+
+ ['quarter', 0, '2019-01-01', '2019-04-01'],
+ ['quarter', 1, '2019-01-01', '2019-07-01'],
+
+ ['6M', 0, '2019-01-01', '2019-07-01'],
+ ['6M', 1, '2019-01-01', '2020-01-01'],
+ ['6M', 0, '2019-08-01', '2020-02-01'],
+
+ ['year', 0, '2019-01-01', '2020-01-01'],
+ ['year', 1, '2019-01-01', '2021-01-01'],
+ ['custom', 1, '2019-01-01', '2019-03-01'],
+
+ ];
+
+ /** @var array $test */
+ foreach ($tests as $test) {
+
+ $freq = $test[0];
+ /** @noinspection MultiAssignmentUsageInspection */
+ $skip = $test[1];
+ $start = new Carbon($test[2]);
+ $expected = new Carbon($test[3]);
+ $nav = new Navigation;
+ try {
+ $result = $nav->addPeriod($start, $freq, $skip);
+ } catch (FireflyException $e) {
+ $this->assertFalse(true, $e->getMessage());
+ }
+
+ $this->assertEquals($expected->format('Y-m-d'), $result->format('Y-m-d'));
+ }
+ }
+
+ /**
+ * @covers \FireflyIII\Support\Navigation
+ */
+ public function testAddPeriodError(): void
+ {
+ $tests = [
+ // period, skip, start, expected end
+ ['bla', 0, '2018-01-01', '2018-01-02'],
+ ];
+
+ /** @var array $test */
+ foreach ($tests as $test) {
+
+ $freq = $test[0];
+ /** @noinspection MultiAssignmentUsageInspection */
+ $skip = $test[1];
+ $start = new Carbon($test[2]);
+ $nav = new Navigation;
+ try {
+ $nav->addPeriod($start, $freq, $skip);
+ } catch (FireflyException $e) {
+ $this->assertEquals('Cannot do addPeriod for $repeat_freq "bla"', $e->getMessage());
+ }
+ }
+ }
+
+ /**
+ * @covers \FireflyIII\Support\Navigation
+ */
+ public function testEndOfPeriod(): void
+ {
+
+ $tests = [
+ ['1D', '2018-01-01 00:00:00', '2018-01-01 23:59:59'],
+ ['1W', '2018-01-01 00:00:00', '2018-01-07 23:59:59'],
+ ['1M', '2018-01-01 00:00:00', '2018-01-31 23:59:59'],
+ ['3M', '2018-01-01 00:00:00', '2018-03-31 23:59:59'],
+ ['6M', '2018-01-01 00:00:00', '2018-06-30 23:59:59'],
+ ['1Y', '2018-01-01 00:00:00', '2018-12-31 23:59:59'],
+ ];
+
+ /** @var array $test */
+ foreach ($tests as $test) {
+
+ $freq = $test[0];
+ /** @noinspection MultiAssignmentUsageInspection */
+ $start = new Carbon($test[1]);
+ $expected = new Carbon($test[2]);
+ $nav = new Navigation;
+ try {
+ $result = $nav->endOfPeriod($start, $freq);
+ } catch (FireflyException $e) {
+ $this->assertFalse(true, $e->getMessage());
+ }
+
+ $this->assertEquals($expected->format('Y-m-d H:i:s'), $result->format('Y-m-d H:i:s'));
+ }
+ }
+}
\ No newline at end of file