mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 02:26:58 +00:00
Some basic date tests
This commit is contained in:
@@ -47,12 +47,23 @@ class Navigation
|
|||||||
$add = ($skip + 1);
|
$add = ($skip + 1);
|
||||||
|
|
||||||
$functionMap = [
|
$functionMap = [
|
||||||
'1D' => 'addDays', 'daily' => 'addDays',
|
'1D' => 'addDays',
|
||||||
'1W' => 'addWeeks', 'weekly' => 'addWeeks', 'week' => 'addWeeks',
|
'daily' => 'addDays',
|
||||||
'1M' => 'addMonths', 'month' => 'addMonths', 'monthly' => 'addMonths', '3M' => 'addMonths',
|
'1W' => 'addWeeks',
|
||||||
'quarter' => 'addMonths', 'quarterly' => 'addMonths', '6M' => 'addMonths', 'half-year' => 'addMonths',
|
'weekly' => 'addWeeks',
|
||||||
'year' => 'addYears', 'yearly' => 'addYears', '1Y' => 'addYears',
|
'week' => 'addWeeks',
|
||||||
'custom' => 'addMonths', // custom? just add one month.
|
'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 = [
|
$modifierMap = [
|
||||||
'quarter' => 3,
|
'quarter' => 3,
|
||||||
@@ -71,11 +82,16 @@ class Navigation
|
|||||||
$function = $functionMap[$repeatFreq];
|
$function = $functionMap[$repeatFreq];
|
||||||
$date->$function($add);
|
$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'];
|
$months = ['1M', 'month', 'monthly'];
|
||||||
$difference = $date->month - $theDate->month;
|
$difference = $date->month - $theDate->month;
|
||||||
if (2 === $difference && 1 === $date->day && \in_array($repeatFreq, $months, true)) {
|
if (2 === $difference && $date->day > 0 && \in_array($repeatFreq, $months, true)) {
|
||||||
$date->subDay();
|
$date->subDays($date->day);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $date;
|
return $date;
|
||||||
@@ -162,11 +178,22 @@ class Navigation
|
|||||||
$currentEnd = clone $end;
|
$currentEnd = clone $end;
|
||||||
|
|
||||||
$functionMap = [
|
$functionMap = [
|
||||||
'1D' => 'endOfDay', 'daily' => 'endOfDay',
|
'1D' => 'endOfDay',
|
||||||
'1W' => 'addWeek', 'week' => 'addWeek', 'weekly' => 'addWeek',
|
'daily' => 'endOfDay',
|
||||||
'1M' => 'addMonth', 'month' => 'addMonth', 'monthly' => 'addMonth',
|
'1W' => 'addWeek',
|
||||||
'3M' => 'addMonths', 'quarter' => 'addMonths', 'quarterly' => 'addMonths', '6M' => 'addMonths', 'half-year' => 'addMonths',
|
'week' => 'addWeek',
|
||||||
'year' => 'addYear', 'yearly' => 'addYear', '1Y' => 'addYear',
|
'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 = [
|
$modifierMap = [
|
||||||
'quarter' => 3,
|
'quarter' => 3,
|
||||||
@@ -200,14 +227,15 @@ class Navigation
|
|||||||
|
|
||||||
if (isset($modifierMap[$repeatFreq])) {
|
if (isset($modifierMap[$repeatFreq])) {
|
||||||
$currentEnd->$function($modifierMap[$repeatFreq]);
|
$currentEnd->$function($modifierMap[$repeatFreq]);
|
||||||
|
|
||||||
if (\in_array($repeatFreq, $subDay, true)) {
|
if (\in_array($repeatFreq, $subDay, true)) {
|
||||||
$currentEnd->subDay();
|
$currentEnd->subDay();
|
||||||
}
|
}
|
||||||
|
$currentEnd->endOfDay();
|
||||||
|
|
||||||
return $currentEnd;
|
return $currentEnd;
|
||||||
}
|
}
|
||||||
$currentEnd->$function();
|
$currentEnd->$function();
|
||||||
|
$currentEnd->endOfDay();
|
||||||
if (\in_array($repeatFreq, $subDay, true)) {
|
if (\in_array($repeatFreq, $subDay, true)) {
|
||||||
$currentEnd->subDay();
|
$currentEnd->subDay();
|
||||||
}
|
}
|
||||||
|
166
tests/Unit/Support/NavigationTest.php
Normal file
166
tests/Unit/Support/NavigationTest.php
Normal file
@@ -0,0 +1,166 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* NavigationTest.php
|
||||||
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
||||||
|
*
|
||||||
|
* 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'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user