added new rounding mode: closest (#611)

This commit is contained in:
Kevin Papst
2019-03-03 20:33:35 +01:00
committed by GitHub
parent bea4be881b
commit 642db480ce
11 changed files with 554 additions and 73 deletions

View File

@@ -64,6 +64,7 @@ class DurationCalculatorTest extends TestCase
'begin' => 15,
'end' => 15,
'duration' => 0,
'mode' => 'default',
],
],
(clone $start)->setTime(12, 17, 35),
@@ -77,6 +78,7 @@ class DurationCalculatorTest extends TestCase
'begin' => 0,
'end' => 0,
'duration' => 0,
'mode' => 'default',
],
],
(clone $start)->setTime(12, 17, 35),
@@ -90,6 +92,7 @@ class DurationCalculatorTest extends TestCase
'begin' => 1,
'end' => 1,
'duration' => 0,
'mode' => 'default',
],
],
(clone $start)->setTime(12, 17, 35),
@@ -103,6 +106,7 @@ class DurationCalculatorTest extends TestCase
'begin' => 0,
'end' => 0,
'duration' => 30,
'mode' => 'default',
],
],
(clone $start)->setTime(12, 10, 51),
@@ -116,12 +120,14 @@ class DurationCalculatorTest extends TestCase
'begin' => 15,
'end' => 0,
'duration' => 0,
'mode' => 'default',
],
'weekdays' => [
'days' => ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'],
'begin' => 0,
'end' => 1,
'duration' => 30,
'mode' => 'default',
],
],
(clone $start)->setTime(12, 27, 35), // 12:15
@@ -135,12 +141,14 @@ class DurationCalculatorTest extends TestCase
'begin' => 15,
'end' => 0,
'duration' => 30,
'mode' => 'default',
],
'weekdays' => [
'days' => ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'],
'begin' => 0,
'end' => 1,
'duration' => 0,
'mode' => 'default',
],
],
(clone $start)->setTime(12, 27, 35), // 12:15
@@ -154,12 +162,14 @@ class DurationCalculatorTest extends TestCase
'begin' => 0,
'end' => 0,
'duration' => 1,
'mode' => 'default',
],
'weekdays' => [
'days' => ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'],
'begin' => 0,
'end' => 0,
'duration' => 1,
'mode' => 'default',
],
],
(clone $start)->setTime(12, 27, 35), // no diff, to test ...
@@ -173,12 +183,14 @@ class DurationCalculatorTest extends TestCase
'begin' => 1,
'end' => 1,
'duration' => 1,
'mode' => 'default',
],
'weekdays' => [
'days' => ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'],
'begin' => 1,
'end' => 1,
'duration' => 1,
'mode' => 'default',
],
],
(clone $start)->setTime(12, 27, 00), // no diff, to test ...
@@ -192,12 +204,14 @@ class DurationCalculatorTest extends TestCase
'begin' => 0,
'end' => 0,
'duration' => 0,
'mode' => 'default',
],
'weekdays' => [
'days' => ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'],
'begin' => 0,
'end' => 0,
'duration' => 0,
'mode' => 'default',
],
],
(clone $start)->setTime(12, 27, 35), // no diff, to test ...

View File

@@ -0,0 +1,152 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Timesheet\Calculator;
use App\Entity\Timesheet;
use App\Timesheet\Rounding\ClosestRounding;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Timesheet\Rounding\ClosestRounding
*/
class ClosestRoundingTest extends TestCase
{
/**
* @dataProvider getTestData
*/
public function testCalculate($roundBegin, $roundEnd, $roundDuration, \DateTime $start, \DateTime $end, \DateTime $expectedStart, \DateTime $expectedEnd, $expectedDuration)
{
$record = new Timesheet();
$record->setBegin($start);
$record->setEnd($end);
$this->assertEquals(0, $record->getDuration());
$record->setDuration($record->getEnd()->getTimestamp() - $record->getBegin()->getTimestamp());
$sut = new ClosestRounding();
$sut->roundBegin($record, $roundBegin);
$sut->roundEnd($record, $roundEnd);
$record->setDuration($record->getEnd()->getTimestamp() - $record->getBegin()->getTimestamp());
$sut->roundDuration($record, $roundDuration);
$this->assertEquals($expectedStart->getTimestamp(), $record->getBegin()->getTimestamp());
$this->assertEquals($expectedEnd->getTimestamp(), $record->getEnd()->getTimestamp());
$this->assertEquals($expectedDuration, $record->getDuration());
}
public function getTestData()
{
$start = new \DateTime();
$start->setTime(12, 0, 0);
return [
[
0,
0,
0,
$start,
(clone $start)->setTimestamp($start->getTimestamp() + 1837),
$start,
(clone $start)->setTimestamp($start->getTimestamp() + 1837),
1837
],
[
15,
15,
0,
(clone $start)->setTime(12, 17, 35),
(clone $start)->setTime(13, 32, 52),
(clone $start)->setTime(12, 15, 00),
(clone $start)->setTime(13, 30, 00),
4500
],
[
0,
0,
0,
(clone $start)->setTime(12, 17, 35),
(clone $start)->setTime(13, 32, 52),
(clone $start)->setTime(12, 17, 35),
(clone $start)->setTime(13, 32, 52),
4517
],
[
1,
1,
0,
(clone $start)->setTime(12, 17, 35),
(clone $start)->setTime(13, 32, 52),
(clone $start)->setTime(12, 18, 00),
(clone $start)->setTime(13, 33, 00),
4500
],
[
0,
0,
30,
(clone $start)->setTime(12, 10, 51),
(clone $start)->setTime(14, 40, 52),
(clone $start)->setTime(12, 10, 51),
(clone $start)->setTime(14, 40, 52),
9000
],
[
0,
1,
30,
(clone $start)->setTime(12, 27, 35), // 12:15
(clone $start)->setTime(14, 32, 52), // 14:33 => 2:18 => 2:30
(clone $start)->setTime(12, 27, 35),
(clone $start)->setTime(14, 33, 00),
7200
],
[
15,
0,
30,
(clone $start)->setTime(12, 27, 35), // 12:15
(clone $start)->setTime(14, 32, 52), // 14:33 => 2:18 (second duration will not be rounded)
(clone $start)->setTime(12, 30, 00), // 12:15
(clone $start)->setTime(14, 32, 52), // 14:33 => 2:18 (second duration will not be rounded)
7200
],
[
0,
0,
1,
(clone $start)->setTime(12, 27, 35), // no diff, to test ...
(clone $start)->setTime(12, 27, 35), // ... that no rounding is applied
(clone $start)->setTime(12, 27, 35), // no diff, to test ...
(clone $start)->setTime(12, 27, 35), // ... that no rounding is applied
0
],
[
1,
1,
1,
(clone $start)->setTime(12, 27, 00), // no diff, to test ...
(clone $start)->setTime(12, 27, 00), // ... that no rounding is applied
(clone $start)->setTime(12, 27, 00), // no diff, to test ...
(clone $start)->setTime(12, 27, 00), // ... that no rounding is applied
0
],
[
0,
0,
0,
(clone $start)->setTime(12, 27, 35), // no diff, to test ...
(clone $start)->setTime(12, 27, 35), // ... that no rounding is applied
(clone $start)->setTime(12, 27, 35), // no diff, to test ...
(clone $start)->setTime(12, 27, 35), // ... that no rounding is applied
0
],
];
}
}

View File

@@ -0,0 +1,152 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Timesheet\Calculator;
use App\Entity\Timesheet;
use App\Timesheet\Rounding\DefaultRounding;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Timesheet\Rounding\DefaultRounding
*/
class DefaultRoundingTest extends TestCase
{
/**
* @dataProvider getTestData
*/
public function testCalculate($roundBegin, $roundEnd, $roundDuration, \DateTime $start, \DateTime $end, \DateTime $expectedStart, \DateTime $expectedEnd, $expectedDuration)
{
$record = new Timesheet();
$record->setBegin($start);
$record->setEnd($end);
$this->assertEquals(0, $record->getDuration());
$record->setDuration($record->getEnd()->getTimestamp() - $record->getBegin()->getTimestamp());
$sut = new DefaultRounding();
$sut->roundBegin($record, $roundBegin);
$sut->roundEnd($record, $roundEnd);
$record->setDuration($record->getEnd()->getTimestamp() - $record->getBegin()->getTimestamp());
$sut->roundDuration($record, $roundDuration);
$this->assertEquals($expectedStart->getTimestamp(), $record->getBegin()->getTimestamp());
$this->assertEquals($expectedEnd->getTimestamp(), $record->getEnd()->getTimestamp());
$this->assertEquals($expectedDuration, $record->getDuration());
}
public function getTestData()
{
$start = new \DateTime();
$start->setTime(12, 0, 0);
return [
[
0,
0,
0,
$start,
(clone $start)->setTimestamp($start->getTimestamp() + 1837),
$start,
(clone $start)->setTimestamp($start->getTimestamp() + 1837),
1837
],
[
15,
15,
0,
(clone $start)->setTime(12, 17, 35),
(clone $start)->setTime(13, 32, 52),
(clone $start)->setTime(12, 15, 00),
(clone $start)->setTime(13, 45, 00),
5400
],
[
0,
0,
0,
(clone $start)->setTime(12, 17, 35),
(clone $start)->setTime(13, 32, 52),
(clone $start)->setTime(12, 17, 35),
(clone $start)->setTime(13, 32, 52),
4517
],
[
1,
1,
0,
(clone $start)->setTime(12, 17, 35),
(clone $start)->setTime(13, 32, 52),
(clone $start)->setTime(12, 17, 00),
(clone $start)->setTime(13, 33, 00),
4560
],
[
0,
0,
30,
(clone $start)->setTime(12, 10, 51),
(clone $start)->setTime(14, 40, 52),
(clone $start)->setTime(12, 10, 51),
(clone $start)->setTime(14, 40, 52),
10800
],
[
0,
1,
30,
(clone $start)->setTime(12, 27, 35), // 12:15
(clone $start)->setTime(14, 32, 52), // 14:33 => 2:18 => 2:30
(clone $start)->setTime(12, 27, 35),
(clone $start)->setTime(14, 33, 00),
9000
],
[
15,
0,
30,
(clone $start)->setTime(12, 27, 35), // 12:15
(clone $start)->setTime(14, 32, 52), // 14:33 => 2:18 (second duration will not be rounded)
(clone $start)->setTime(12, 15, 00), // 12:15
(clone $start)->setTime(14, 32, 52), // 14:33 => 2:18 (second duration will not be rounded)
9000
],
[
0,
0,
1,
(clone $start)->setTime(12, 27, 35), // no diff, to test ...
(clone $start)->setTime(12, 27, 35), // ... that no rounding is applied
(clone $start)->setTime(12, 27, 35), // no diff, to test ...
(clone $start)->setTime(12, 27, 35), // ... that no rounding is applied
0
],
[
1,
1,
1,
(clone $start)->setTime(12, 27, 00), // no diff, to test ...
(clone $start)->setTime(12, 27, 00), // ... that no rounding is applied
(clone $start)->setTime(12, 27, 00), // no diff, to test ...
(clone $start)->setTime(12, 27, 00), // ... that no rounding is applied
0
],
[
0,
0,
0,
(clone $start)->setTime(12, 27, 35), // no diff, to test ...
(clone $start)->setTime(12, 27, 35), // ... that no rounding is applied
(clone $start)->setTime(12, 27, 35), // no diff, to test ...
(clone $start)->setTime(12, 27, 35), // ... that no rounding is applied
0
],
];
}
}