Skip to content

Commit

Permalink
dev: Make sure ContractTimeAccounting add time spent to contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
marien-probesys committed Dec 11, 2024
1 parent b1508f3 commit b27013c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Service/ContractTimeAccounting.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function accountTime(Contract $contract, int $time): TimeSpent
$timeSpent = new TimeSpent();
$timeSpent->setTime($timeAccounted);
$timeSpent->setRealTime($time);
$timeSpent->setContract($contract);

$contract->addTimeSpent($timeSpent);

return $timeSpent;
}
Expand Down Expand Up @@ -72,7 +73,8 @@ public function accountTimeSpents(Contract $contract, array $timeSpents): void
);

$timeSpent->setTime($timeAccounted);
$timeSpent->setContract($contract);

$contract->addTimeSpent($timeSpent);

$availableTime = $availableTime - $timeAccounted;
}
Expand All @@ -84,7 +86,11 @@ public function accountTimeSpents(Contract $contract, array $timeSpents): void
public function unaccountTimeSpents(array $timeSpents): void
{
foreach ($timeSpents as $timeSpent) {
$timeSpent->setContract(null);
$contract = $timeSpent->getContract();
if ($contract) {
$contract->removeTimeSpent($timeSpent);
}

$realTime = $timeSpent->getRealTime();
$timeSpent->setTime($realTime);
}
Expand Down
50 changes: 50 additions & 0 deletions tests/Service/ContractTimeAccountingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function testAccountTime(): void
$this->assertSame($contract->getId(), $timeSpent->getContract()->getId());
$this->assertSame($minutes, $timeSpent->getTime());
$this->assertSame($minutes, $timeSpent->getRealTime());
$contractTimeSpents = $contract->getTimeSpents();
$this->assertSame(1, count($contractTimeSpents));
}

public function testAccountTimeAccountsMaximumOfAvailableTime(): void
Expand All @@ -63,6 +65,8 @@ public function testAccountTimeAccountsMaximumOfAvailableTime(): void
$this->assertSame($contract->getId(), $timeSpent->getContract()->getId());
$this->assertSame(60, $timeSpent->getTime());
$this->assertSame(60, $timeSpent->getRealTime());
$contractTimeSpents = $contract->getTimeSpents();
$this->assertSame(1, count($contractTimeSpents));
}

public function testAccountTimeAccountsConsideringTimeAccountingUnit(): void
Expand All @@ -78,6 +82,8 @@ public function testAccountTimeAccountsConsideringTimeAccountingUnit(): void
$this->assertSame($contract->getId(), $timeSpent->getContract()->getId());
$this->assertSame(30, $timeSpent->getTime());
$this->assertSame($minutes, $timeSpent->getRealTime());
$contractTimeSpents = $contract->getTimeSpents();
$this->assertSame(1, count($contractTimeSpents));
}

public function testAccountTimeWithTimeAccountingUnitDoesNotAccountMoreThanAvailableTime(): void
Expand All @@ -97,6 +103,8 @@ public function testAccountTimeWithTimeAccountingUnitDoesNotAccountMoreThanAvail
$this->assertSame($contract->getId(), $timeSpent->getContract()->getId());
$this->assertSame(15, $timeSpent->getTime());
$this->assertSame($minutes, $timeSpent->getRealTime());
$contractTimeSpents = $contract->getTimeSpents();
$this->assertSame(2, count($contractTimeSpents));
}

public function testAccountTimeSpents(): void
Expand All @@ -116,6 +124,8 @@ public function testAccountTimeSpents(): void
$this->assertSame($contract->getId(), $timeSpent->getContract()->getId());
$this->assertSame($minutes, $timeSpent->getTime());
$this->assertSame($minutes, $timeSpent->getRealTime());
$contractTimeSpents = $contract->getTimeSpents();
$this->assertSame(1, count($contractTimeSpents));
}

public function testAccountTimeSpentsDoNotAccountMoreThanAvailableTime(): void
Expand Down Expand Up @@ -147,6 +157,9 @@ public function testAccountTimeSpentsDoNotAccountMoreThanAvailableTime(): void
// the ContractTimeAccounting service stopped at the first TimeSpent
// overflowing the available time.
$this->assertNull($timeSpent3->getContract());
$contractTimeSpents = $contract->getTimeSpents();
$this->assertSame(1, count($contractTimeSpents));
$this->assertSame($timeSpent1->getId(), $contractTimeSpents[0]->getId());
}

public function testAccountTimeSpentsAccountsConsideringTimeAccountingUnit(): void
Expand All @@ -166,5 +179,42 @@ public function testAccountTimeSpentsAccountsConsideringTimeAccountingUnit(): vo
$this->assertSame($contract->getId(), $timeSpent->getContract()->getId());
$this->assertSame(30, $timeSpent->getTime());
$this->assertSame($minutes, $timeSpent->getRealTime());
$contractTimeSpents = $contract->getTimeSpents();
$this->assertSame(1, count($contractTimeSpents));
}

public function testUnaccountTimeSpents(): void
{
$contract = ContractFactory::createOne([
'maxHours' => 1,
'timeAccountingUnit' => 30,
])->_real();
$timeSpent = TimeSpentFactory::createOne([
'contract' => $contract,
'realTime' => 20,
'time' => 30,
])->_real();

$this->contractTimeAccounting->unaccountTimeSpents([$timeSpent]);

$this->assertNull($timeSpent->getContract());
$this->assertSame(20, $timeSpent->getTime());
$this->assertSame(20, $timeSpent->getRealTime());
$this->assertEmpty($contract->getTimeSpents());
}

public function testUnaccountTimeSpentsWithTimeSpentWithoutContract(): void
{
$timeSpent = TimeSpentFactory::createOne([
'contract' => null,
'realTime' => 20,
'time' => 20,
])->_real();

$this->contractTimeAccounting->unaccountTimeSpents([$timeSpent]);

$this->assertNull($timeSpent->getContract());
$this->assertSame(20, $timeSpent->getTime());
$this->assertSame(20, $timeSpent->getRealTime());
}
}

0 comments on commit b27013c

Please sign in to comment.