65 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
declare(strict_types=1);
 | 
						|
 | 
						|
namespace Tests\Unit\Services\WorkingDays;
 | 
						|
 | 
						|
use App\Services\Utils\DurationConvertor;
 | 
						|
use App\Services\WorkingDays\DurationSolver;
 | 
						|
use App\Services\WorkingDays\WorkingDayDeterminerInterface;
 | 
						|
use PHPUnit\Framework\Attributes\DataProvider;
 | 
						|
use Tests\TestCase;
 | 
						|
 | 
						|
class DurationSolverTest extends TestCase
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * @return array<array<int|array<int>>>
 | 
						|
     */
 | 
						|
    public static function solverDurationProvider(): array
 | 
						|
    {
 | 
						|
        // 4 days is required state
 | 
						|
        return [
 | 
						|
            [3, [4]], /* without holidays */
 | 
						|
            [4, [3, 4]], /* 1 day */
 | 
						|
            [6, [2, 3, 4]], /* 1 weekend, monday holiday */
 | 
						|
            [5, [2, 4]], /* 1 weekend */
 | 
						|
        ];
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param int $expectedResult
 | 
						|
     * @param array<int> $solverResults
 | 
						|
     * @return void
 | 
						|
     * @throws \PHPUnit\Framework\MockObject\Exception
 | 
						|
     */
 | 
						|
    #[DataProvider('solverDurationProvider')]
 | 
						|
    public function testSolverWillSolveDuration(int $expectedResult, array $solverResults): void
 | 
						|
    {
 | 
						|
        $workingDayDeterminer = $this->createMock(WorkingDayDeterminerInterface::class);
 | 
						|
        $workingDayDeterminer->method('getWorkingDaysCount')->willReturnCallback(function () use ($solverResults) {
 | 
						|
            static $calledCount = 0;
 | 
						|
            return $solverResults[$calledCount++] ?? throw new \Exception("Not enough solver results for try $calledCount" . print_r($solverResults, true));
 | 
						|
        });
 | 
						|
        $durationSolver = new DurationSolver(
 | 
						|
            $workingDayDeterminer,
 | 
						|
            new DurationConvertor()
 | 
						|
        );
 | 
						|
        $this->assertEquals(
 | 
						|
            (new \DateTimeImmutable('2022-01-01 23:59:59'))->add(new \DateInterval(sprintf('P%dD', $expectedResult))),
 | 
						|
            $durationSolver->calculateDuration(new \DateTimeImmutable('2022-01-01 00:00:00'), new \DateTimeImmutable('2022-01-04 23:59:59'))
 | 
						|
        );
 | 
						|
    }
 | 
						|
 | 
						|
    public function testDurationSolverWillThrowExceptionWhenCantSolveDuration(): void
 | 
						|
    {
 | 
						|
        $workingDayDeterminer = $this->createMock(WorkingDayDeterminerInterface::class);
 | 
						|
        $workingDayDeterminer->method('getWorkingDaysCount')->willReturn(0);
 | 
						|
        $durationSolver = new DurationSolver(
 | 
						|
            $workingDayDeterminer,
 | 
						|
            new DurationConvertor()
 | 
						|
        );
 | 
						|
        $this->expectException(\InvalidArgumentException::class);
 | 
						|
        $durationSolver->calculateDuration(new \DateTimeImmutable('2022-01-01 00:00:00'), new \DateTimeImmutable('2022-01-04 23:59:59'));
 | 
						|
    }
 | 
						|
}
 |