Release 2.41 (#5653)
This commit is contained in:
@@ -28,7 +28,7 @@ use PHPUnit\Framework\TestCase;
|
||||
#[CoversClass(RateCalculator::class)]
|
||||
class RateCalculatorTest extends TestCase
|
||||
{
|
||||
protected function getRateRepositoryMock(array $rates = [])
|
||||
protected function getRateRepositoryMock(array $rates = []): TimesheetRepository
|
||||
{
|
||||
$mock = $this->getMockBuilder(TimesheetRepository::class)->disableOriginalConstructor()->getMock();
|
||||
if (!empty($rates)) {
|
||||
@@ -38,18 +38,26 @@ class RateCalculatorTest extends TestCase
|
||||
return $mock;
|
||||
}
|
||||
|
||||
public function testCalculateWithTimesheetHourlyRate(): void
|
||||
private function assertRateByTimesheetHourlyRate(int $duration, float $hourlyRate, float $rate): void
|
||||
{
|
||||
$record = new Timesheet();
|
||||
$record->setEnd(new \DateTime());
|
||||
$record->setDuration(1800);
|
||||
$record->setHourlyRate(100);
|
||||
$record->setDuration($duration);
|
||||
$record->setHourlyRate($hourlyRate);
|
||||
$record->setActivity(new Activity());
|
||||
$record->setUser($this->getTestUser());
|
||||
|
||||
$sut = new RateCalculator(new RateService([], $this->getRateRepositoryMock()));
|
||||
$sut->calculate($record, []);
|
||||
self::assertEquals(50, $record->getRate());
|
||||
self::assertEquals($rate, $record->getRate());
|
||||
}
|
||||
|
||||
public function testCalculateWithTimesheetHourlyRate(): void
|
||||
{
|
||||
$this->assertRateByTimesheetHourlyRate(1800, 100, 50);
|
||||
$this->assertRateByTimesheetHourlyRate(400, 100, 11);
|
||||
$this->assertRateByTimesheetHourlyRate(1234, 100, 34);
|
||||
$this->assertRateByTimesheetHourlyRate(2739, 100, 76);
|
||||
}
|
||||
|
||||
public function testCalculateWithTimesheetFixedRate(): void
|
||||
@@ -176,7 +184,7 @@ class RateCalculatorTest extends TestCase
|
||||
self::assertEquals($expectedInternalRate, $timesheet->getInternalRate());
|
||||
}
|
||||
|
||||
protected function getTestUser($rate = 75, $internalRate = 75)
|
||||
protected function getTestUser(?float $rate = 75.0, ?float $internalRate = 75.0): User
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
@@ -230,19 +238,19 @@ class RateCalculatorTest extends TestCase
|
||||
self::assertEquals($expectedRate, $record->getRate());
|
||||
}
|
||||
|
||||
public static function getRuleDefinitions()
|
||||
public static function getRuleDefinitions(): array
|
||||
{
|
||||
$start = new \DateTime('12:00:00', new \DateTimeZone('UTC'));
|
||||
$day = $start->format('l');
|
||||
|
||||
return [
|
||||
[
|
||||
31837,
|
||||
31837, // 31824 = 8,84
|
||||
[],
|
||||
663.2708
|
||||
663
|
||||
],
|
||||
[
|
||||
31837,
|
||||
31837, // 31824 = 8,84
|
||||
[
|
||||
'default' => [
|
||||
'days' => [$day],
|
||||
@@ -253,10 +261,10 @@ class RateCalculatorTest extends TestCase
|
||||
'factor' => 1.5
|
||||
],
|
||||
],
|
||||
1326.5417
|
||||
1326 // 8,84 * 75 (see user) * 2
|
||||
],
|
||||
[
|
||||
31837,
|
||||
31837, // 31824 = 8,84
|
||||
[
|
||||
'default' => [
|
||||
'days' => [$day],
|
||||
@@ -267,7 +275,7 @@ class RateCalculatorTest extends TestCase
|
||||
'factor' => 1.5
|
||||
],
|
||||
],
|
||||
2321.4479
|
||||
2320.5 // 75 * 8,84 * 3,5
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ use PHPUnit\Framework\TestCase;
|
||||
#[CoversClass(RateService::class)]
|
||||
class RateServiceTest extends TestCase
|
||||
{
|
||||
protected function getRateRepositoryMock(array $rates = [])
|
||||
protected function getRateRepositoryMock(array $rates = []): TimesheetRepository
|
||||
{
|
||||
$mock = $this->getMockBuilder(TimesheetRepository::class)->disableOriginalConstructor()->getMock();
|
||||
if (!empty($rates)) {
|
||||
@@ -37,7 +37,7 @@ class RateServiceTest extends TestCase
|
||||
return $mock;
|
||||
}
|
||||
|
||||
private static function createDateTime(string $datetime = null): \DateTime
|
||||
private static function createDateTime(?string $datetime = null): \DateTime
|
||||
{
|
||||
return new \DateTime($datetime ?? 'now', new \DateTimeZone('UTC'));
|
||||
}
|
||||
@@ -124,7 +124,7 @@ class RateServiceTest extends TestCase
|
||||
$customerRate,
|
||||
$customerInternal,
|
||||
$customerIsFixed
|
||||
) {
|
||||
): void {
|
||||
$customer = new Customer('foo');
|
||||
|
||||
$project = new Project();
|
||||
@@ -180,7 +180,7 @@ class RateServiceTest extends TestCase
|
||||
self::assertEquals($expectedInternalRate, $rate->getInternalRate());
|
||||
}
|
||||
|
||||
protected function getTestUser($rate = 75, $internalRate = 75)
|
||||
protected function getTestUser(?float $rate = 75.0, ?float $internalRate = 75.0): User
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
@@ -234,7 +234,7 @@ class RateServiceTest extends TestCase
|
||||
self::assertEquals($expectedRate, $rate->getRate());
|
||||
}
|
||||
|
||||
public static function getRuleDefinitions()
|
||||
public static function getRuleDefinitions(): array
|
||||
{
|
||||
$start = self::createDateTime('12:00:00');
|
||||
$day = $start->format('l');
|
||||
@@ -243,7 +243,7 @@ class RateServiceTest extends TestCase
|
||||
[
|
||||
31837,
|
||||
[],
|
||||
663.2708
|
||||
663
|
||||
],
|
||||
[
|
||||
31837,
|
||||
@@ -257,7 +257,7 @@ class RateServiceTest extends TestCase
|
||||
'factor' => 1.5
|
||||
],
|
||||
],
|
||||
1326.5417
|
||||
1326 // 8,84 * 75 (see user) * 2
|
||||
],
|
||||
[
|
||||
31837,
|
||||
@@ -271,7 +271,7 @@ class RateServiceTest extends TestCase
|
||||
'factor' => 1.5
|
||||
],
|
||||
],
|
||||
2321.4479
|
||||
2320.5 // 75 * 8,84 * 3,5
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -18,23 +18,28 @@ use PHPUnit\Framework\TestCase;
|
||||
class UtilTest extends TestCase
|
||||
{
|
||||
#[DataProvider('getRateCalculationData')]
|
||||
public function testCalculateRate(int|float $hourlyRate, int $duration, int|float $expectedRate): void
|
||||
public function testCalculateRate(float $hourlyRate, int $duration, float $expectedRate): void
|
||||
{
|
||||
self::assertEquals($expectedRate, Util::calculateRate($hourlyRate, $duration));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<float, int, >>|\Generator
|
||||
*/
|
||||
public static function getRateCalculationData()
|
||||
{
|
||||
yield [0, 0, 0];
|
||||
yield [1, 100, 0.0278];
|
||||
yield [1, 900, 0.25];
|
||||
yield [1, 1800, 0.5];
|
||||
yield [10000, 1, 2.7778];
|
||||
yield [736, 123, 25.1467];
|
||||
yield [7360, 1234, 2522.8444];
|
||||
yield [7360.34, 1234, 2522.961];
|
||||
yield [7360.01, 1234, 2522.8479];
|
||||
yield [7360.99, 1234, 2523.1838];
|
||||
yield [0.00, 0, 0.00];
|
||||
yield [10.00, 7260, 20.2];
|
||||
yield [1.00, 3600, 1.00];
|
||||
yield [1.00, 100, 0.03];
|
||||
yield [1.00, 900, 0.25];
|
||||
yield [1.00, 1800, 0.5];
|
||||
yield [10000.00, 60, 200.00];
|
||||
yield [736.00, 123, 22.08];
|
||||
yield [7360.00, 1234, 2502.4];
|
||||
yield [7360.34, 1234, 2502.52];
|
||||
yield [7360.01, 1234, 2502.4];
|
||||
yield [7360.99, 1234, 2502.74];
|
||||
}
|
||||
|
||||
public function testCalculateRateWithRounding(): void
|
||||
@@ -43,29 +48,37 @@ class UtilTest extends TestCase
|
||||
$seconds = 0;
|
||||
$repeat = 130;
|
||||
|
||||
for ($a = 0; $a < $repeat; $a++) {
|
||||
$inputs = [
|
||||
900,
|
||||
1600,
|
||||
4200,
|
||||
8763,
|
||||
3300,
|
||||
600,
|
||||
1300,
|
||||
1837,
|
||||
4217,
|
||||
5400,
|
||||
3283,
|
||||
600,
|
||||
];
|
||||
$inputs = [
|
||||
[900, 28.69, 0],
|
||||
[1600, 50.49, 0],
|
||||
[4200, 134.26, 0],
|
||||
[8763, 278.84, 0],
|
||||
[3300, 105.57, 0],
|
||||
[600, 19.51, 0],
|
||||
[1300, 41.31, 0],
|
||||
[1837, 58.52, 0],
|
||||
[4217, 134.26, 0],
|
||||
[5400, 172.13, 0],
|
||||
[3283, 104.42, 0],
|
||||
[600, 19.51, 0],
|
||||
];
|
||||
|
||||
foreach ($inputs as $i) {
|
||||
$seconds += $i;
|
||||
$total += Util::calculateRate(114.75, $i);
|
||||
$totalExpected = 0.00;
|
||||
|
||||
for ($a = 0; $a < $repeat; $a++) {
|
||||
foreach ($inputs as $row) {
|
||||
[$duration, $rate] = $row;
|
||||
$seconds += $duration;
|
||||
$totalExpected += $rate;
|
||||
$tmp = Util::calculateRate(114.75, $duration);
|
||||
self::assertEquals($rate, $tmp);
|
||||
$total += $tmp;
|
||||
}
|
||||
}
|
||||
|
||||
self::assertEquals(36000 * $repeat, $seconds);
|
||||
self::assertEquals(1147.50 * $repeat, $total);
|
||||
self::assertEquals($totalExpected, $total);
|
||||
self::assertEqualsWithDelta(1147.51 * $repeat, $total, 0.00001);
|
||||
self::assertEqualsWithDelta(149176.3, $total, 0.00001);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user