| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-15 16:02:42 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * NavigationStartOfPeriodTest.php | 
					
						
							| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  |  * 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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-27 17:09:35 +02:00
										 |  |  | use Override; | 
					
						
							| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use FireflyIII\Support\Navigation; | 
					
						
							|  |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2025-05-24 06:07:59 +02:00
										 |  |  | use PHPUnit\Framework\Attributes\DataProvider; | 
					
						
							| 
									
										
										
										
											2024-03-17 12:26:56 +01:00
										 |  |  | use Tests\integration\TestCase; | 
					
						
							| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @group unit-test | 
					
						
							|  |  |  |  * @group support | 
					
						
							|  |  |  |  * @group navigation | 
					
						
							| 
									
										
										
										
											2023-12-20 19:45:12 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @internal | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @coversNothing | 
					
						
							| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-12-20 19:45:12 +01:00
										 |  |  | final class NavigationStartOfPeriodTest extends TestCase | 
					
						
							| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-05-24 06:07:59 +02:00
										 |  |  |     private Navigation $navigation; | 
					
						
							| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-27 17:09:35 +02:00
										 |  |  |     #[Override]
 | 
					
						
							| 
									
										
										
										
											2025-05-24 06:24:17 +02:00
										 |  |  |     protected function setUp(): void | 
					
						
							| 
									
										
										
										
											2023-07-17 20:33:26 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-05-24 06:07:59 +02:00
										 |  |  |         parent::setUp(); | 
					
						
							| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  |         $this->navigation = new Navigation(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-05-24 06:24:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-24 06:07:59 +02:00
										 |  |  |     #[DataProvider('provideDates')]
 | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:43 +01:00
										 |  |  |     public function testGivenADateAndFrequencyWhenCalculateTheDateThenReturnsTheExpectedDateSuccessful(string $frequency, Carbon $from, Carbon $expected): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $period = $this->navigation->startOfPeriod($from, $frequency); | 
					
						
							| 
									
										
										
										
											2025-06-26 11:57:15 +02:00
										 |  |  |         $this->assertSame($expected->toDateString(), $period->toDateString()); | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:43 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-07 05:34:03 +02:00
										 |  |  |     public static function provideDates(): iterable | 
					
						
							| 
									
										
										
										
											2023-07-17 20:33:26 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-05-27 17:09:35 +02:00
										 |  |  |         yield 'custom' => ['custom', Carbon::now(), Carbon::now()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield '1D' => ['1D', Carbon::now(), Carbon::now()->startOfDay()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'daily' => ['daily', Carbon::now(), Carbon::now()->startOfDay()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield '1W' => ['1W', Carbon::now(), Carbon::now()->startOfWeek()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'week' => ['week', Carbon::now(), Carbon::now()->startOfWeek()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'weekly' => ['weekly', Carbon::now(), Carbon::now()->startOfWeek()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'month' => ['month', Carbon::now(), Carbon::now()->startOfMonth()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield '1M' => ['1M', Carbon::now(), Carbon::now()->startOfMonth()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'monthly' => ['monthly', Carbon::now(), Carbon::now()->startOfMonth()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield '3M' => ['3M', Carbon::now(), Carbon::now()->firstOfQuarter()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'quarter' => ['quarter', Carbon::now(), Carbon::now()->firstOfQuarter()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'quarterly' => ['quarterly', Carbon::now(), Carbon::now()->firstOfQuarter()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'year' => ['year', Carbon::now(), Carbon::now()->startOfYear()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'yearly' => ['yearly', Carbon::now(), Carbon::now()->startOfYear()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield '1Y' => ['1Y', Carbon::now(), Carbon::now()->startOfYear()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'half-year' => ['half-year', Carbon::parse('2023-05-20'), Carbon::parse('2023-01-01')->startOfYear()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield '6M' => ['6M', Carbon::parse('2023-08-20'), Carbon::parse('2023-07-01')]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'last7' => ['last7', Carbon::now(), Carbon::now()->subDays(7)->startOfDay()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'last30' => ['last30', Carbon::now(), Carbon::now()->subDays(30)->startOfDay()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'last90' => ['last90', Carbon::now(), Carbon::now()->subDays(90)->startOfDay()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'last365' => ['last365', Carbon::now(), Carbon::now()->subDays(365)->startOfDay()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'MTD' => ['MTD', Carbon::now(), Carbon::now()->startOfMonth()->startOfDay()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'QTD' => ['QTD', Carbon::now(), Carbon::now()->firstOfQuarter()->startOfDay()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'YTD' => ['YTD', Carbon::now(), Carbon::now()->startOfYear()->startOfDay()]; | 
					
						
							| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-24 06:07:59 +02:00
										 |  |  |     #[DataProvider('provideUnknownFrequencies')]
 | 
					
						
							| 
									
										
										
										
											2023-12-21 05:06:17 +01:00
										 |  |  |     public function testGivenADateAndUnknownFrequencyWhenCalculateTheDateThenReturnsTheSameDateSuccessful(string $frequency, Carbon $from, Carbon $expected): void | 
					
						
							| 
									
										
										
										
											2023-07-17 20:33:26 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-03-17 12:26:56 +01:00
										 |  |  |         Log::spy(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  |         Log::shouldReceive('error') | 
					
						
							| 
									
										
										
										
											2023-12-20 19:45:12 +01:00
										 |  |  |             ->with(sprintf('Cannot do startOfPeriod for $repeat_freq "%s"', $frequency)) | 
					
						
							|  |  |  |             ->andReturnNull() | 
					
						
							|  |  |  |         ; | 
					
						
							| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $period = $this->navigation->startOfPeriod($from, $frequency); | 
					
						
							| 
									
										
										
										
											2025-06-26 11:57:15 +02:00
										 |  |  |         $this->assertSame($expected->toDateString(), $period->toDateString()); | 
					
						
							| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-07 05:34:03 +02:00
										 |  |  |     public static function provideUnknownFrequencies(): iterable | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:43 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-05-27 17:09:35 +02:00
										 |  |  |         yield '1day' => ['1day', Carbon::now(), Carbon::now()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'unknown' => ['unknown', Carbon::now(), Carbon::now()->startOfDay()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         yield 'empty' => ['', Carbon::now(), Carbon::now()->startOfDay()]; | 
					
						
							| 
									
										
										
										
											2025-02-23 12:28:43 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-07-04 14:11:56 -03:00
										 |  |  | } |