2022-07-31 16:50:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Helpers;
|
|
|
|
|
|
|
|
use Engelsystem\Config\Config;
|
|
|
|
use Engelsystem\Helpers\Carbon;
|
|
|
|
use Engelsystem\Helpers\Shifts;
|
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
|
|
|
|
|
|
|
class ShiftsTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\Shifts::isNightShift
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testIsNightShiftDisabled(): void
|
2022-07-31 16:50:35 +02:00
|
|
|
{
|
|
|
|
$config = new Config(['night_shifts' => [
|
|
|
|
'enabled' => false,
|
|
|
|
'start' => 2,
|
|
|
|
'end' => 6,
|
|
|
|
'multiplier' => 2,
|
|
|
|
]]);
|
|
|
|
$this->app->instance('config', $config);
|
|
|
|
|
|
|
|
// At night but disabled
|
|
|
|
$this->assertFalse(Shifts::isNightShift(
|
|
|
|
new Carbon('2042-01-01 04:00'),
|
|
|
|
new Carbon('2042-01-01 05:00')
|
|
|
|
));
|
2022-07-31 19:17:44 +02:00
|
|
|
}
|
2022-07-31 16:50:35 +02:00
|
|
|
|
2022-07-31 19:17:44 +02:00
|
|
|
/**
|
2023-04-01 14:39:25 +02:00
|
|
|
* @return array{0: Carbon, 1: Carbon, 2: boolean}[]
|
2022-07-31 19:17:44 +02:00
|
|
|
*/
|
|
|
|
public function nightShiftData(): array
|
|
|
|
{
|
|
|
|
// $start, $end, $isNightShift
|
|
|
|
return [
|
|
|
|
// Is night shift
|
|
|
|
[new Carbon('2042-01-01 04:00'), new Carbon('2042-01-01 05:00'), true],
|
|
|
|
// Starts as night shift
|
|
|
|
[new Carbon('2042-01-01 05:45'), new Carbon('2042-01-01 07:00'), true],
|
|
|
|
// Ends as night shift
|
|
|
|
[new Carbon('2042-01-01 00:00'), new Carbon('2042-01-01 02:15'), true],
|
|
|
|
// Too early
|
|
|
|
[new Carbon('2042-01-01 00:00'), new Carbon('2042-01-01 01:59'), false],
|
|
|
|
// Too late
|
|
|
|
[new Carbon('2042-01-01 06:00'), new Carbon('2042-01-01 09:59'), false],
|
|
|
|
];
|
|
|
|
}
|
2022-07-31 16:50:35 +02:00
|
|
|
|
2022-07-31 19:17:44 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\Shifts::isNightShift
|
|
|
|
* @dataProvider nightShiftData
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testIsNightShiftEnabled(Carbon $start, Carbon $end, bool $isNightShift): void
|
2022-07-31 19:17:44 +02:00
|
|
|
{
|
|
|
|
$config = new Config(['night_shifts' => [
|
|
|
|
'enabled' => true,
|
|
|
|
'start' => 2,
|
|
|
|
'end' => 6,
|
|
|
|
'multiplier' => 2,
|
|
|
|
]]);
|
|
|
|
$this->app->instance('config', $config);
|
2022-07-31 16:50:35 +02:00
|
|
|
|
2022-07-31 19:17:44 +02:00
|
|
|
$this->assertEquals($isNightShift, Shifts::isNightShift($start, $end));
|
2022-07-31 16:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\Shifts::getNightShiftMultiplier
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testGetNightShiftMultiplier(): void
|
2022-07-31 16:50:35 +02:00
|
|
|
{
|
|
|
|
$config = new Config(['night_shifts' => [
|
|
|
|
'enabled' => true,
|
|
|
|
'start' => 2,
|
|
|
|
'end' => 6,
|
|
|
|
'multiplier' => 2,
|
|
|
|
]]);
|
|
|
|
$this->app->instance('config', $config);
|
|
|
|
|
|
|
|
$this->assertEquals(2, Shifts::getNightShiftMultiplier(
|
|
|
|
new Carbon('2042-01-01 02:00'),
|
|
|
|
new Carbon('2042-01-01 04:00')
|
|
|
|
));
|
|
|
|
|
|
|
|
$config->set('night_shifts', array_merge($config->get('night_shifts'), ['enabled' => false]));
|
|
|
|
$this->assertEquals(1, Shifts::getNightShiftMultiplier(
|
|
|
|
new Carbon('2042-01-01 02:00'),
|
|
|
|
new Carbon('2042-01-01 04:00')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|