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
|
|
|
/**
|
2024-02-18 02:18:31 +01:00
|
|
|
* @return array{0: string, 1: string, 2: boolean}[]
|
2022-07-31 19:17:44 +02:00
|
|
|
*/
|
|
|
|
public function nightShiftData(): array
|
|
|
|
{
|
|
|
|
// $start, $end, $isNightShift
|
|
|
|
return [
|
|
|
|
// Is night shift
|
2024-02-18 02:18:31 +01:00
|
|
|
['2042-01-01 04:00', '2042-01-01 05:00', true],
|
2022-07-31 19:17:44 +02:00
|
|
|
// Starts as night shift
|
2024-02-18 02:18:31 +01:00
|
|
|
['2042-01-01 05:45', '2042-01-01 07:00', true],
|
2022-07-31 19:17:44 +02:00
|
|
|
// Ends as night shift
|
2024-02-18 02:18:31 +01:00
|
|
|
['2042-01-01 00:00', '2042-01-01 03:00', true],
|
|
|
|
// Contains night shift
|
|
|
|
['2042-01-01 01:00', '2042-01-01 09:00', true],
|
2022-07-31 19:17:44 +02:00
|
|
|
// Too early
|
2024-02-18 02:18:31 +01:00
|
|
|
['2042-01-01 00:00', '2042-01-01 02:00', false],
|
2022-07-31 19:17:44 +02:00
|
|
|
// Too late
|
2024-02-18 02:18:31 +01:00
|
|
|
['2042-01-01 06:00', '2042-01-01 10:00', false],
|
2022-07-31 19:17:44 +02:00
|
|
|
];
|
|
|
|
}
|
2022-07-31 16:50:35 +02:00
|
|
|
|
2022-07-31 19:17:44 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\Shifts::isNightShift
|
|
|
|
* @dataProvider nightShiftData
|
|
|
|
*/
|
2024-02-18 02:18:31 +01:00
|
|
|
public function testIsNightShiftEnabled(string $start, string $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
|
|
|
|
2024-02-18 02:18:31 +01:00
|
|
|
$this->assertEquals($isNightShift, Shifts::isNightShift(new Carbon($start), new Carbon($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')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|