2023-10-20 17:00:45 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Tests\unit\Support;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Support\Navigation;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-02-03 03:08:34 -06:00
|
|
|
use Tests\integration\TestCase;
|
2023-10-20 17:00:45 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group unit-test
|
|
|
|
* @group support
|
|
|
|
* @group navigation
|
2023-12-20 12:45:12 -06:00
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @coversNothing
|
2023-10-20 17:00:45 -05:00
|
|
|
*/
|
2023-12-20 12:45:12 -06:00
|
|
|
final class NavigationEndOfPeriodTest extends TestCase
|
2023-10-20 17:00:45 -05:00
|
|
|
{
|
|
|
|
private Navigation $navigation;
|
|
|
|
|
|
|
|
public function __construct(string $name)
|
|
|
|
{
|
|
|
|
parent::__construct($name);
|
|
|
|
$this->navigation = new Navigation();
|
|
|
|
}
|
|
|
|
|
2023-11-04 08:26:35 -05:00
|
|
|
public static function provideDates(): iterable
|
2023-10-20 17:00:45 -05:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'1D' => ['frequency' => '1D', 'from' => Carbon::now(), 'expected' => Carbon::now()->endOfDay()],
|
|
|
|
'daily' => ['frequency' => 'daily', 'from' => Carbon::now(), 'expected' => Carbon::now()->endOfDay()],
|
|
|
|
'1W' => ['frequency' => '1W', 'from' => Carbon::now(), 'expected' => Carbon::now()->addWeek()->subDay()->endOfDay()],
|
|
|
|
'week' => ['frequency' => 'week', 'from' => Carbon::now(), 'expected' => Carbon::now()->addWeek()->subDay()->endOfDay()],
|
|
|
|
'weekly' => ['frequency' => 'weekly', 'from' => Carbon::now(), 'expected' => Carbon::now()->addWeek()->subDay()->endOfDay()],
|
|
|
|
'month' => ['frequency' => 'month', 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonth()->subDay()->endOfDay()],
|
|
|
|
'1M' => ['frequency' => '1M', 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonth()->subDay()->endOfDay()],
|
|
|
|
'monthly' => ['frequency' => 'monthly', 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonth()->subDay()->endOfDay()],
|
2024-04-01 01:46:13 -05:00
|
|
|
'3M' => ['frequency' => '3M', 'from' => Carbon::now(), 'expected' => Carbon::now()->addQuarter()->subDay()->endOfDay()],
|
|
|
|
'quarter' => ['frequency' => 'quarter', 'from' => Carbon::now(), 'expected' => Carbon::now()->addQuarter()->subDay()->endOfDay()],
|
|
|
|
'quarterly' => ['frequency' => 'quarterly', 'from' => Carbon::now(), 'expected' => Carbon::now()->addQuarter()->subDay()->endOfDay()],
|
2023-10-20 17:00:45 -05:00
|
|
|
'year' => ['frequency' => 'year', 'from' => Carbon::now(), 'expected' => Carbon::now()->addYearNoOverflow()->subDay()->endOfDay()],
|
|
|
|
'yearly' => ['frequency' => 'yearly', 'from' => Carbon::now(), 'expected' => Carbon::now()->addYearNoOverflow()->subDay()->endOfDay()],
|
|
|
|
'1Y' => ['frequency' => '1Y', 'from' => Carbon::now(), 'expected' => Carbon::now()->addYearNoOverflow()->subDay()->endOfDay()],
|
|
|
|
'half-year' => ['frequency' => 'half-year', 'from' => Carbon::parse('2023-05-20'), 'expected' => Carbon::parse('2023-11-19')->endOfDay()],
|
|
|
|
'6M' => ['frequency' => '6M', 'from' => Carbon::parse('2023-08-20'), 'expected' => Carbon::parse('2024-02-19')],
|
|
|
|
'last7' => ['frequency' => 'last7', 'from' => Carbon::now(), 'expected' => Carbon::now()->addDays(7)->endOfDay()],
|
|
|
|
'last30' => ['frequency' => 'last30', 'from' => Carbon::now(), 'expected' => Carbon::now()->addDays(30)->endOfDay()],
|
|
|
|
'last90' => ['frequency' => 'last90', 'from' => Carbon::now(), 'expected' => Carbon::now()->addDays(90)->endOfDay()],
|
|
|
|
'last365' => ['frequency' => 'last365', 'from' => Carbon::now(), 'expected' => Carbon::now()->addDays(365)->endOfDay()],
|
2024-08-27 01:44:47 -05:00
|
|
|
'MTD' => ['frequency' => 'MTD', 'from' => Carbon::now(),
|
2024-09-01 22:06:53 -05:00
|
|
|
'expected' => Carbon::now()->isSameMonth(Carbon::now()) ? Carbon::now()->endOfDay() : Carbon::now()->endOfMonth()],
|
2023-10-20 17:00:45 -05:00
|
|
|
'QTD' => ['frequency' => 'QTD', 'from' => Carbon::now(), 'expected' => Carbon::now()->firstOfQuarter()->startOfDay()],
|
2024-08-27 01:44:47 -05:00
|
|
|
'YTD' => ['frequency' => 'YTD', 'from' => Carbon::now(), 'expected' => Carbon::now()->firstOfYear()->startOfDay()],
|
2023-10-20 17:00:45 -05:00
|
|
|
'week 2023-08-05 to 2023-08-11' => ['frequency' => '1W', 'from' => Carbon::parse('2023-08-05'), 'expected' => Carbon::parse('2023-08-11')->endOfDay()],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-11-04 08:26:35 -05:00
|
|
|
public static function provideUnknownFrequencies(): iterable
|
2023-10-20 17:00:45 -05:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'1day' => ['frequency' => '1day', 'from' => Carbon::now(), 'expected' => Carbon::now()],
|
|
|
|
'unknown' => ['frequency' => 'unknown', 'from' => Carbon::now(), 'expected' => Carbon::now()->startOfDay()],
|
|
|
|
'empty' => ['frequency' => '', 'from' => Carbon::now(), 'expected' => Carbon::now()->startOfDay()],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-04-02 08:40:33 -05:00
|
|
|
/**
|
|
|
|
* @dataProvider provideDates
|
|
|
|
*/
|
|
|
|
public function testGivenADateAndFrequencyWhenCalculateTheDateThenReturnsTheExpectedDateSuccessful(string $frequency, Carbon $from, Carbon $expected): void
|
|
|
|
{
|
|
|
|
$period = clone $this->navigation->endOfPeriod($from, $frequency);
|
|
|
|
self::assertSame($expected->toDateString(), $period->toDateString());
|
|
|
|
}
|
|
|
|
|
2023-10-20 17:00:45 -05:00
|
|
|
/**
|
|
|
|
* @dataProvider provideUnknownFrequencies
|
|
|
|
*/
|
2023-12-20 22:06:17 -06:00
|
|
|
public function testGivenADateAndUnknownFrequencyWhenCalculateTheDateThenReturnsTheSameDateSuccessful(string $frequency, Carbon $from, Carbon $expected): void
|
2023-10-20 17:00:45 -05:00
|
|
|
{
|
|
|
|
Log::spy();
|
|
|
|
|
2024-01-01 08:17:11 -06:00
|
|
|
$period = $this->navigation->endOfPeriod($from, $frequency);
|
2023-11-04 08:26:35 -05:00
|
|
|
self::assertSame($expected->toDateString(), $period->toDateString());
|
2023-10-20 17:00:45 -05:00
|
|
|
$expectedMessage = sprintf('Cannot do endOfPeriod for $repeat_freq "%s"', $frequency);
|
|
|
|
|
|
|
|
Log::shouldHaveReceived('error', [$expectedMessage]);
|
|
|
|
}
|
|
|
|
}
|