. */ 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')); } } }