Code for new release

This commit is contained in:
James Cole
2023-07-15 16:02:42 +02:00
parent f43b539470
commit b557805eeb
213 changed files with 1942 additions and 1426 deletions

View File

@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);
/**
/*
* CalculatorProvider.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -21,44 +21,30 @@ declare(strict_types=1);
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\unit\Support\Calendar;
use Carbon\Carbon;
use FireflyIII\Support\Calendar\Periodicity;
use Generator;
use Tests\unit\Support\Calendar\Periodicity\IntervalProvider;
readonly class CalculatorProvider
{
public IntervalProvider $intervalProvider;
public Periodicity $periodicity;
public string $label;
public int $skip;
public string $label;
public Periodicity $periodicity;
public int $skip;
private function __construct(IntervalProvider $intervalProvider, Periodicity $periodicity, int $skip = 0)
{
private function __construct(IntervalProvider $intervalProvider, Periodicity $periodicity, int $skip = 0) {
$this->skip = $skip;
$this->intervalProvider = $intervalProvider;
$this->periodicity = $periodicity;
$this->label = "{$periodicity->name} {$intervalProvider->label}";
}
public static function from(Periodicity $periodicity, IntervalProvider $interval, int $skip = 0): CalculatorProvider
{
return new self($interval, $periodicity, $skip);
}
public function epoch(): Carbon
{
return $this->intervalProvider->epoch;
}
public function expected(): Carbon
{
return $this->intervalProvider->expected;
}
public static function providePeriodicityWithSkippedIntervals(): \Generator
{
public static function providePeriodicityWithSkippedIntervals(): Generator {
$intervals = [
CalculatorProvider::from(Periodicity::Daily, new IntervalProvider(Carbon::now(), Carbon::now()->addDays(2)), 1),
CalculatorProvider::from(Periodicity::Daily, new IntervalProvider(Carbon::now(), Carbon::now()->addDays(3)), 2),
@@ -130,4 +116,16 @@ readonly class CalculatorProvider
yield "#{$index} {$interval->label}" => [$interval];
}
}
public static function from(Periodicity $periodicity, IntervalProvider $interval, int $skip = 0): CalculatorProvider {
return new self($interval, $periodicity, $skip);
}
public function epoch(): Carbon {
return $this->intervalProvider->epoch;
}
public function expected(): Carbon {
return $this->intervalProvider->expected;
}
}

View File

@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);
/**
/*
* CalculatorTest.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -21,11 +21,14 @@ declare(strict_types=1);
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\unit\Support\Calendar;
use FireflyIII\Support\Calendar\Calculator;
use FireflyIII\Support\Calendar\Exceptions\IntervalException;
use FireflyIII\Support\Calendar\Periodicity;
use Generator;
use PHPUnit\Framework\TestCase;
use Tests\unit\Support\Calendar\Periodicity\BimonthlyTest;
use Tests\unit\Support\Calendar\Periodicity\DailyTest;
@@ -45,20 +48,7 @@ use Tests\unit\Support\Calendar\Periodicity\YearlyTest;
*/
class CalculatorTest extends TestCase
{
private static function convert(Periodicity $periodicity, array $intervals): array
{
$periodicityIntervals = [];
/** @var IntervalProvider $interval */
foreach ($intervals as $index => $interval) {
$calculator = CalculatorProvider::from($periodicity, $interval);
$periodicityIntervals["#{$index} {$calculator->label}"] = [$calculator];
}
return $periodicityIntervals;
}
public static function provideAllPeriodicity(): \Generator
{
public static function provideAllPeriodicity(): Generator {
$intervals = [];
$intervals = array_merge($intervals, self::convert(Periodicity::Daily, DailyTest::provideIntervals()));
$intervals = array_merge($intervals, self::convert(Periodicity::Weekly, WeeklyTest::provideIntervals()));
@@ -75,28 +65,36 @@ class CalculatorTest extends TestCase
}
}
private static function convert(Periodicity $periodicity, array $intervals): array {
$periodicityIntervals = [];
/** @var IntervalProvider $interval */
foreach ($intervals as $index => $interval) {
$calculator = CalculatorProvider::from($periodicity, $interval);
$periodicityIntervals["#{$index} {$calculator->label}"] = [$calculator];
}
return $periodicityIntervals;
}
public static function provideSkippedIntervals(): Generator {
return CalculatorProvider::providePeriodicityWithSkippedIntervals();
}
/**
* @dataProvider provideAllPeriodicity
* @throws IntervalException
*/
public function testGivenADailyPeriodicityWhenCallTheNextDateByIntervalMethodThenReturnsTheExpectedDateSuccessful(CalculatorProvider $provider)
{
public function testGivenADailyPeriodicityWhenCallTheNextDateByIntervalMethodThenReturnsTheExpectedDateSuccessful(CalculatorProvider $provider) {
$calculator = new Calculator();
$period = $calculator->nextDateByInterval($provider->epoch(), $provider->periodicity);
$this->assertEquals($provider->expected()->toDateString(), $period->toDateString());
}
public static function provideSkippedIntervals(): \Generator
{
return CalculatorProvider::providePeriodicityWithSkippedIntervals();
}
/**
* @dataProvider provideSkippedIntervals
* @throws IntervalException
*/
public function testGivenAnEpochWithSkipIntervalNumberWhenCallTheNextDateBySkippedIntervalMethodThenReturnsTheExpectedDateSuccessful(CalculatorProvider $provider)
{
public function testGivenAnEpochWithSkipIntervalNumberWhenCallTheNextDateBySkippedIntervalMethodThenReturnsTheExpectedDateSuccessful(CalculatorProvider $provider) {
$calculator = new Calculator();
$period = $calculator->nextDateByInterval($provider->epoch(), $provider->periodicity, $provider->skip);
$this->assertEquals($provider->expected()->toDateString(), $period->toDateString());

View File

@@ -1,8 +1,7 @@
<?php
declare(strict_types=1);
/**
/*
* BimonthlyTest.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -21,6 +20,8 @@ declare(strict_types=1);
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\unit\Support\Calendar\Periodicity;
use Carbon\Carbon;

View File

@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);
/**
/*
* DailyTest.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -21,6 +21,8 @@ declare(strict_types=1);
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\unit\Support\Calendar\Periodicity;
use Carbon\Carbon;

View File

@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);
/**
/*
* FortnightlyTest.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -21,6 +21,8 @@ declare(strict_types=1);
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\unit\Support\Calendar\Periodicity;
use Carbon\Carbon;

View File

@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);
/**
/*
* HalfYearlyTest.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -21,6 +21,8 @@ declare(strict_types=1);
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\unit\Support\Calendar\Periodicity;
use Carbon\Carbon;

View File

@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);
/**
/*
* IntervalProvider.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -21,6 +21,8 @@ declare(strict_types=1);
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\unit\Support\Calendar\Periodicity;
use Carbon\Carbon;

View File

@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);
/**
/*
* IntervalTestCase.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -21,18 +21,17 @@ declare(strict_types=1);
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\unit\Support\Calendar\Periodicity;
use FireflyIII\Support\Calendar\Periodicity\Interval;
use Generator;
use PHPUnit\Framework\TestCase;
abstract class IntervalTestCase extends TestCase
{
abstract public static function factory(): Interval;
abstract public static function provideIntervals(): array;
public static function provider(): \Generator
public static function provider(): Generator
{
$intervals = static::provideIntervals();
/** @var IntervalProvider $interval */
@@ -41,9 +40,13 @@ abstract class IntervalTestCase extends TestCase
}
}
abstract public static function provideIntervals(): array;
/**
* @dataProvider provider
*
* @param IntervalProvider $provider
*
* @return void
*/
public function testGivenAnEpochWhenCallTheNextDateThenReturnsTheExpectedDateSuccessful(IntervalProvider $provider): void
@@ -51,4 +54,6 @@ abstract class IntervalTestCase extends TestCase
$period = static::factory()->nextDate($provider->epoch);
$this->assertEquals($provider->expected->toDateString(), $period->toDateString());
}
abstract public static function factory(): Interval;
}

View File

@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);
/**
/*
* MonthlyTest.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -21,6 +21,8 @@ declare(strict_types=1);
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\unit\Support\Calendar\Periodicity;
use Carbon\Carbon;
@@ -35,13 +37,11 @@ use FireflyIII\Support\Calendar\Periodicity\Interval;
*/
class MonthlyTest extends IntervalTestCase
{
public static function factory(): Interval
{
public static function factory(): Interval {
return new Periodicity\Monthly();
}
public static function provideIntervals(): array
{
public static function provideIntervals(): array {
return [
new IntervalProvider(Carbon::now(), Carbon::now()->addMonth(1)),
new IntervalProvider(Carbon::parse('2019-01-01'), Carbon::parse('2019-02-01')),

View File

@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);
/**
/*
* QuarterlyTest.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -21,6 +21,8 @@ declare(strict_types=1);
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\unit\Support\Calendar\Periodicity;
use Carbon\Carbon;
@@ -35,13 +37,11 @@ use FireflyIII\Support\Calendar\Periodicity\Interval;
*/
class QuarterlyTest extends IntervalTestCase
{
public static function factory(): Interval
{
public static function factory(): Interval {
return new Periodicity\Quarterly();
}
public static function provideIntervals(): array
{
public static function provideIntervals(): array {
return [
new IntervalProvider(Carbon::now(), Carbon::now()->addMonths(3)),
new IntervalProvider(Carbon::parse('2019-01-29'), Carbon::parse('2019-04-29')),

View File

@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);
/**
/*
* WeeklyTest.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -21,6 +21,8 @@ declare(strict_types=1);
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\unit\Support\Calendar\Periodicity;
use Carbon\Carbon;
@@ -35,13 +37,11 @@ use FireflyIII\Support\Calendar\Periodicity\Interval;
*/
class WeeklyTest extends IntervalTestCase
{
public static function factory(): Interval
{
public static function factory(): Interval {
return new Periodicity\Weekly();
}
public static function provideIntervals(): array
{
public static function provideIntervals(): array {
return [
new IntervalProvider(Carbon::now(), Carbon::now()->addWeek()),
new IntervalProvider(Carbon::parse('2023-01-31'), Carbon::parse('2023-02-07')),

View File

@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);
/**
/*
* YearlyTest.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -21,6 +21,8 @@ declare(strict_types=1);
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\unit\Support\Calendar\Periodicity;
use Carbon\Carbon;
@@ -35,13 +37,11 @@ use FireflyIII\Support\Calendar\Periodicity\Interval;
*/
class YearlyTest extends IntervalTestCase
{
public static function factory(): Interval
{
public static function factory(): Interval {
return new Periodicity\Yearly();
}
public static function provideIntervals(): array
{
public static function provideIntervals(): array {
return [
new IntervalProvider(Carbon::now(), Carbon::now()->addYears(1)),
new IntervalProvider(Carbon::parse('2019-01-29'), Carbon::parse('2020-01-29')),

View File

@@ -1,6 +1,7 @@
<?php
/**
/*
* NavigationAddPeriodTest.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -26,6 +27,7 @@ namespace Tests\unit\Support;
use Carbon\Carbon;
use FireflyIII\Support\Calendar\Periodicity;
use FireflyIII\Support\Navigation;
use Generator;
use PHPUnit\Framework\TestCase;
/**
@@ -37,14 +39,54 @@ class NavigationAddPeriodTest extends TestCase
{
private Navigation $navigation;
public function __construct(string $name)
{
public function __construct(string $name) {
parent::__construct($name);
$this->navigation = new Navigation();
}
public static function providePeriods(): array
{
public static function provideFrequencies(): array {
return [
Periodicity::Daily->name => ['periodicity' => Periodicity::Daily, 'from' => Carbon::now(), 'expected' => Carbon::tomorrow()],
Periodicity::Weekly->name => ['periodicity' => Periodicity::Weekly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addWeeks(1)],
Periodicity::Fortnightly->name => ['periodicity' => Periodicity::Fortnightly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addWeeks(2)],
Periodicity::Monthly->name => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonths(1)],
'2019-01-01 to 2019-02-01' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2019-01-01'), 'expected' => Carbon::parse('2019-02-01')],
'2019-01-29 to 2019-02-28' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2019-01-29'), 'expected' => Carbon::parse('2019-02-28')],
'2019-01-30 to 2019-02-28' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2019-01-30'), 'expected' => Carbon::parse('2019-02-28')],
'2019-01-31 to 2019-02-28' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2019-01-31'), 'expected' => Carbon::parse('2019-02-28')],
'2023-03-31 to 2023-04-30' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2023-03-31'), 'expected' => Carbon::parse('2023-04-30')],
'2023-05-31 to 2023-06-30' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2023-05-31'), 'expected' => Carbon::parse('2023-06-30')],
'2023-08-31 to 2023-09-30' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2023-08-31'), 'expected' => Carbon::parse('2023-09-30')],
'2023-10-31 to 2023-11-30' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2023-10-31'), 'expected' => Carbon::parse('2023-11-30')],
Periodicity::Quarterly->name => ['periodicity' => Periodicity::Quarterly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonths(3)],
'2019-01-29 to 2020-04-29' => ['periodicity' => Periodicity::Quarterly, 'from' => Carbon::parse('2019-01-29'), 'expected' => Carbon::parse('2019-04-29')],
'2019-01-30 to 2020-04-30' => ['periodicity' => Periodicity::Quarterly, 'from' => Carbon::parse('2019-01-30'), 'expected' => Carbon::parse('2019-04-30')],
'2019-01-31 to 2020-04-30' => ['periodicity' => Periodicity::Quarterly, 'from' => Carbon::parse('2019-01-31'), 'expected' => Carbon::parse('2019-04-30')],
Periodicity::HalfYearly->name => ['periodicity' => Periodicity::HalfYearly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonths(6)],
'2019-01-31 to 2020-07-29' => ['periodicity' => Periodicity::HalfYearly, 'from' => Carbon::parse('2019-01-29'), 'expected' => Carbon::parse('2019-07-29')],
'2019-01-31 to 2020-07-30' => ['periodicity' => Periodicity::HalfYearly, 'from' => Carbon::parse('2019-01-30'), 'expected' => Carbon::parse('2019-07-30')],
'2019-01-31 to 2020-07-31' => ['periodicity' => Periodicity::HalfYearly, 'from' => Carbon::parse('2019-01-31'), 'expected' => Carbon::parse('2019-07-31')],
Periodicity::Yearly->name => ['periodicity' => Periodicity::Yearly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addYears(1)],
'2020-02-29 to 2021-02-28' => ['periodicity' => Periodicity::Yearly, 'from' => Carbon::parse('2020-02-29'), 'expected' => Carbon::parse('2021-02-28')],
];
}
public static function provideMonthPeriods(): array {
return [
'1M' => ['frequency' => '1M', 'from' => Carbon::parse('2023-06-25'), 'expected' => Carbon::parse('2023-06-25')->addMonths(1)],
'month' => ['frequency' => 'month', 'from' => Carbon::parse('2023-06-25'), 'expected' => Carbon::parse('2023-06-25')->addMonths(1)],
'monthly' => ['frequency' => 'monthly', 'from' => Carbon::parse('2023-06-25'), 'expected' => Carbon::parse('2023-06-25')->addMonths(1)],
'2019-01-29 to 2019-02-28' => ['frequency' => 'monthly', 'from' => Carbon::parse('2019-01-29'), 'expected' => Carbon::parse('2019-02-28')],
'2019-01-30 to 2019-02-28' => ['frequency' => 'monthly', 'from' => Carbon::parse('2019-01-30'), 'expected' => Carbon::parse('2019-02-28')],
'2019-01-31 to 2019-02-28' => ['frequency' => 'monthly', 'from' => Carbon::parse('2019-01-31'), 'expected' => Carbon::parse('2019-02-28')],
'2023-03-31 to 2023-04-30' => ['frequency' => 'monthly', 'from' => Carbon::parse('2023-03-31'), 'expected' => Carbon::parse('2023-04-30')],
'2023-05-31 to 2023-06-30' => ['frequency' => 'monthly', 'from' => Carbon::parse('2023-05-31'), 'expected' => Carbon::parse('2023-06-30')],
'2023-08-31 to 2023-09-30' => ['frequency' => 'monthly', 'from' => Carbon::parse('2023-08-31'), 'expected' => Carbon::parse('2023-09-30')],
'2023-10-31 to 2023-11-30' => ['frequency' => 'monthly', 'from' => Carbon::parse('2023-10-31'), 'expected' => Carbon::parse('2023-11-30')],
];
}
public static function providePeriods(): array {
return [
'1D' => ['frequency' => '1D', 'from' => Carbon::now(), 'expected' => Carbon::tomorrow()],
'daily' => ['frequency' => 'daily', 'from' => Carbon::now(), 'expected' => Carbon::tomorrow()],
@@ -69,42 +111,7 @@ class NavigationAddPeriodTest extends TestCase
];
}
/**
* @dataProvider providePeriods
*/
public function testGivenAFrequencyWhenCalculateTheDateThenReturnsTheExpectedDateSuccessful(string $frequency, Carbon $from, Carbon $expected)
{
$period = $this->navigation->addPeriod($from, $frequency, 0);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}
public static function provideMonthPeriods(): array
{
return [
'1M' => ['frequency' => '1M', 'from' => Carbon::parse('2023-06-25'), 'expected' => Carbon::parse('2023-06-25')->addMonths(1)],
'month' => ['frequency' => 'month', 'from' => Carbon::parse('2023-06-25'), 'expected' => Carbon::parse('2023-06-25')->addMonths(1)],
'monthly' => ['frequency' => 'monthly', 'from' => Carbon::parse('2023-06-25'), 'expected' => Carbon::parse('2023-06-25')->addMonths(1)],
'2019-01-29 to 2019-02-28' => ['frequency' => 'monthly', 'from' => Carbon::parse('2019-01-29'), 'expected' => Carbon::parse('2019-02-28')],
'2019-01-30 to 2019-02-28' => ['frequency' => 'monthly', 'from' => Carbon::parse('2019-01-30'), 'expected' => Carbon::parse('2019-02-28')],
'2019-01-31 to 2019-02-28' => ['frequency' => 'monthly', 'from' => Carbon::parse('2019-01-31'), 'expected' => Carbon::parse('2019-02-28')],
'2023-03-31 to 2023-04-30' => ['frequency' => 'monthly', 'from' => Carbon::parse('2023-03-31'), 'expected' => Carbon::parse('2023-04-30')],
'2023-05-31 to 2023-06-30' => ['frequency' => 'monthly', 'from' => Carbon::parse('2023-05-31'), 'expected' => Carbon::parse('2023-06-30')],
'2023-08-31 to 2023-09-30' => ['frequency' => 'monthly', 'from' => Carbon::parse('2023-08-31'), 'expected' => Carbon::parse('2023-09-30')],
'2023-10-31 to 2023-11-30' => ['frequency' => 'monthly', 'from' => Carbon::parse('2023-10-31'), 'expected' => Carbon::parse('2023-11-30')],
];
}
/**
* @dataProvider provideMonthPeriods
*/
public function testGivenAMonthFrequencyWhenCalculateTheDateThenReturnsTheLastDayOfMonthSuccessful(string $frequency, Carbon $from, Carbon $expected)
{
$period = $this->navigation->addPeriod($from, $frequency, 0);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}
public static function providePeriodsWithSkippingParam(): \Generator
{
public static function providePeriodsWithSkippingParam(): Generator {
$intervals = [
'2019-01-31 to 2019-02-11' => ['skip' => 10, 'frequency' => 'daily', 'from' => Carbon::parse('2019-01-31'), 'expected' => Carbon::parse('2019-02-11')],
'1D' => ['skip' => 1, 'frequency' => '1D', 'from' => Carbon::now(), 'expected' => Carbon::now()->addDays(2)],
@@ -141,53 +148,39 @@ class NavigationAddPeriodTest extends TestCase
'YTD' => ['skip' => 1, 'frequency' => 'YTD', 'from' => Carbon::now(), 'expected' => Carbon::now()->addYears(2)],
];
foreach ($intervals as $interval) {
yield "{$interval["frequency"]} {$interval["from"]->toDateString()} to {$interval["expected"]->toDateString()}" => $interval;
yield "{$interval['frequency']} {$interval['from']->toDateString()} to {$interval['expected']->toDateString()}" => $interval;
}
}
/**
* @dataProvider providePeriodsWithSkippingParam
*/
public function testGivenAFrequencyAndSkipIntervalWhenCalculateTheDateThenReturnsTheSkippedDateSuccessful(int $skip, string $frequency, Carbon $from, Carbon $expected)
{
public function testGivenAFrequencyAndSkipIntervalWhenCalculateTheDateThenReturnsTheSkippedDateSuccessful(int $skip, string $frequency, Carbon $from, Carbon $expected) {
$period = $this->navigation->addPeriod($from, $frequency, $skip);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}
public static function provideFrequencies(): array
{
return [
Periodicity::Daily->name => ['periodicity' => Periodicity::Daily, 'from' => Carbon::now(), 'expected' => Carbon::tomorrow()],
Periodicity::Weekly->name => ['periodicity' => Periodicity::Weekly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addWeeks(1)],
Periodicity::Fortnightly->name => ['periodicity' => Periodicity::Fortnightly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addWeeks(2)],
Periodicity::Monthly->name => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonths(1)],
'2019-01-01 to 2019-02-01' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2019-01-01'), 'expected' => Carbon::parse('2019-02-01')],
'2019-01-29 to 2019-02-28' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2019-01-29'), 'expected' => Carbon::parse('2019-02-28')],
'2019-01-30 to 2019-02-28' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2019-01-30'), 'expected' => Carbon::parse('2019-02-28')],
'2019-01-31 to 2019-02-28' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2019-01-31'), 'expected' => Carbon::parse('2019-02-28')],
'2023-03-31 to 2023-04-30' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2023-03-31'), 'expected' => Carbon::parse('2023-04-30')],
'2023-05-31 to 2023-06-30' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2023-05-31'), 'expected' => Carbon::parse('2023-06-30')],
'2023-08-31 to 2023-09-30' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2023-08-31'), 'expected' => Carbon::parse('2023-09-30')],
'2023-10-31 to 2023-11-30' => ['periodicity' => Periodicity::Monthly, 'from' => Carbon::parse('2023-10-31'), 'expected' => Carbon::parse('2023-11-30')],
Periodicity::Quarterly->name => ['periodicity' => Periodicity::Quarterly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonths(3)],
'2019-01-29 to 2020-04-29' => ['periodicity' => Periodicity::Quarterly, 'from' => Carbon::parse('2019-01-29'), 'expected' => Carbon::parse('2019-04-29')],
'2019-01-30 to 2020-04-30' => ['periodicity' => Periodicity::Quarterly, 'from' => Carbon::parse('2019-01-30'), 'expected' => Carbon::parse('2019-04-30')],
'2019-01-31 to 2020-04-30' => ['periodicity' => Periodicity::Quarterly, 'from' => Carbon::parse('2019-01-31'), 'expected' => Carbon::parse('2019-04-30')],
Periodicity::HalfYearly->name => ['periodicity' => Periodicity::HalfYearly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addMonths(6)],
'2019-01-31 to 2020-07-29' => ['periodicity' => Periodicity::HalfYearly, 'from' => Carbon::parse('2019-01-29'), 'expected' => Carbon::parse('2019-07-29')],
'2019-01-31 to 2020-07-30' => ['periodicity' => Periodicity::HalfYearly, 'from' => Carbon::parse('2019-01-30'), 'expected' => Carbon::parse('2019-07-30')],
'2019-01-31 to 2020-07-31' => ['periodicity' => Periodicity::HalfYearly, 'from' => Carbon::parse('2019-01-31'), 'expected' => Carbon::parse('2019-07-31')],
Periodicity::Yearly->name => ['periodicity' => Periodicity::Yearly, 'from' => Carbon::now(), 'expected' => Carbon::now()->addYears(1)],
'2020-02-29 to 2021-02-28' => ['periodicity' => Periodicity::Yearly, 'from' => Carbon::parse('2020-02-29'), 'expected' => Carbon::parse('2021-02-28')],
];
/**
* @dataProvider providePeriods
*/
public function testGivenAFrequencyWhenCalculateTheDateThenReturnsTheExpectedDateSuccessful(string $frequency, Carbon $from, Carbon $expected) {
$period = $this->navigation->addPeriod($from, $frequency, 0);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}
/**
* @dataProvider provideFrequencies
*/
public function testGivenAIntervalWhenCallTheNextDateByIntervalMethodThenReturnsTheExpectedDateSuccessful(Periodicity $periodicity, Carbon $from, Carbon $expected)
{
public function testGivenAIntervalWhenCallTheNextDateByIntervalMethodThenReturnsTheExpectedDateSuccessful(Periodicity $periodicity, Carbon $from, Carbon $expected) {
$period = $this->navigation->nextDateByInterval($from, $periodicity);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}
/**
* @dataProvider provideMonthPeriods
*/
public function testGivenAMonthFrequencyWhenCalculateTheDateThenReturnsTheLastDayOfMonthSuccessful(string $frequency, Carbon $from, Carbon $expected) {
$period = $this->navigation->addPeriod($from, $frequency, 0);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}
}

View File

@@ -1,6 +1,7 @@
<?php
/**
/*
* NavigationStartOfPeriodTest.php
* Copyright (c) 2023 Antonio Spinelli <https://github.com/tonicospinelli>
*
* This file is part of Firefly III (https://github.com/firefly-iii).
@@ -37,14 +38,12 @@ class NavigationStartOfPeriodTest extends TestCase
{
private Navigation $navigation;
public function __construct(string $name)
{
public function __construct(string $name) {
parent::__construct($name);
$this->navigation = new Navigation();
}
public static function provideDates(): array
{
public static function provideDates(): array {
return [
'custom' => ['frequency' => 'custom', 'from' => Carbon::now(), 'expected' => Carbon::now()],
'1D' => ['frequency' => '1D', 'from' => Carbon::now(), 'expected' => Carbon::now()->startOfDay()],
@@ -73,17 +72,7 @@ class NavigationStartOfPeriodTest extends TestCase
];
}
/**
* @dataProvider provideDates
*/
public function testGivenADateAndFrequencyWhenCalculateTheDateThenReturnsTheExpectedDateSuccessful(string $frequency, Carbon $from, Carbon $expected)
{
$period = $this->navigation->startOfPeriod($from, $frequency);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}
public static function provideUnknownFrequencies(): array
{
public static function provideUnknownFrequencies(): array {
return [
'1day' => ['frequency' => '1day', 'from' => Carbon::now(), 'expected' => Carbon::now()],
'unknown' => ['frequency' => 'unknown', 'from' => Carbon::now(), 'expected' => Carbon::now()->startOfDay()],
@@ -91,14 +80,21 @@ class NavigationStartOfPeriodTest extends TestCase
];
}
/**
* @dataProvider provideDates
*/
public function testGivenADateAndFrequencyWhenCalculateTheDateThenReturnsTheExpectedDateSuccessful(string $frequency, Carbon $from, Carbon $expected) {
$period = $this->navigation->startOfPeriod($from, $frequency);
$this->assertEquals($expected->toDateString(), $period->toDateString());
}
/**
* @dataProvider provideUnknownFrequencies
*/
public function testGivenADateAndUnknownFrequencyWhenCalculateTheDateThenReturnsTheSameDateSuccessful(string $frequency, Carbon $from, Carbon $expected)
{
public function testGivenADateAndUnknownFrequencyWhenCalculateTheDateThenReturnsTheSameDateSuccessful(string $frequency, Carbon $from, Carbon $expected) {
Log::shouldReceive('error')
->with(sprintf('Cannot do startOfPeriod for $repeat_freq "%s"', $frequency))
->andReturnNull();
->with(sprintf('Cannot do startOfPeriod for $repeat_freq "%s"', $frequency))
->andReturnNull();
$period = $this->navigation->startOfPeriod($from, $frequency);
$this->assertEquals($expected->toDateString(), $period->toDateString());