fix Entity/UserTest and phpstan error

This commit is contained in:
Mathieu Jaumotte 2023-09-26 11:27:36 +02:00
parent 6665a443b9
commit baeccf0970
2 changed files with 30 additions and 56 deletions

View File

@ -17,6 +17,7 @@ use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Iterator; use Iterator;
use RuntimeException; use RuntimeException;
@ -117,11 +118,11 @@ class User implements UserInterface, \Stringable
private ?Location $mainLocation = null; private ?Location $mainLocation = null;
/** /**
* @var Collection<UserScopeHistory> * @var Collection&Selectable<int, UserScopeHistory>
* @ORM\OneToMany(targetEntity=UserScopeHistory::class, * @ORM\OneToMany(targetEntity=UserScopeHistory::class,
* mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true) * mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true)
*/ */
private Collection $scopeHistories; private Collection&Selectable $scopeHistories;
/** /**
* @ORM\Column(type="string", length=255) * @ORM\Column(type="string", length=255)
@ -136,11 +137,11 @@ class User implements UserInterface, \Stringable
private ?string $salt = null; private ?string $salt = null;
/** /**
* @var Collection<UserJobHistory> * @var Collection&Selectable<int, UserJobHistory>
* @ORM\OneToMany(targetEntity=UserJobHistory::class, * @ORM\OneToMany(targetEntity=UserJobHistory::class,
* mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true) * mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true)
*/ */
private Collection $jobHistories; private Collection&Selectable $jobHistories;
/** /**
* @ORM\Column(type="string", length=80) * @ORM\Column(type="string", length=80)
@ -266,23 +267,12 @@ class User implements UserInterface, \Stringable
{ {
$at ??= new DateTimeImmutable('now'); $at ??= new DateTimeImmutable('now');
$criteria = new Criteria(); foreach ($this->scopeHistories as $scopeHistory) {
$expr = Criteria::expr(); if ($at >= $scopeHistory->getStartDate() && (
null === $scopeHistory->getEndDate() || $at < $scopeHistory->getEndDate()
$criteria->where( )) {
$expr->andX( return $scopeHistory->getScope();
$expr->orX( }
$expr->isNull('endDate'),
$expr->gt('endDate', $at)
),
$expr->lte('startDate', $at)
)
);
$scopes = $this->scopeHistories->matching($criteria)->getIterator();
if ($scopes->count() > 0) {
return $scopes->first()->getScope();
} }
return null; return null;
@ -315,23 +305,12 @@ class User implements UserInterface, \Stringable
{ {
$at ??= new DateTimeImmutable('now'); $at ??= new DateTimeImmutable('now');
$criteria = new Criteria(); foreach ($this->jobHistories as $jobHistory) {
$expr = Criteria::expr(); if ($at >= $jobHistory->getStartDate() && (
null === $jobHistory->getEndDate() || $at < $jobHistory->getEndDate()
$criteria->where( )) {
$expr->andX( return $jobHistory->getJob();
$expr->orX( }
$expr->isNull('endDate'),
$expr->gt('endDate', $at)
),
$expr->lte('startDate', $at)
)
);
$jobs = $this->jobHistories->matching($criteria)->getIterator();
if ($jobs->count() > 0) {
return $jobs->first()->getJob();
} }
return null; return null;

View File

@ -38,15 +38,13 @@ class UserTest extends TestCase
self::assertSame($scopeB, $user->getMainScope()); self::assertSame($scopeB, $user->getMainScope());
// 2. get scopeA history, check endDate is not null // 2. get scopeA history, check endDate is not null
$histories = $user->getMainScopeHistories(); self::assertNotNull(
$scopeHistoryA = null; $user
foreach ($histories as $row) { ->getMainScopeHistories()
/** @var User\UserScopeHistory $row */ ->filter(fn (User\UserScopeHistory $userScopeHistory) => $userScopeHistory->getScope() === $scopeA )
if ($scopeA === $row->getScope()) { ->first()->getEndDate()
$scopeHistoryA = $row; );
}
}
self::assertNotNull($scopeHistoryA->getEndDate());
} }
public function testUserJobHistory() public function testUserJobHistory()
@ -62,15 +60,12 @@ class UserTest extends TestCase
self::assertSame($jobB, $user->getUserJob()); self::assertSame($jobB, $user->getUserJob());
// 2. get jobA history, check endDate is not null // 2. get jobA history, check endDate is not null
$histories = $user->getUserJobHistories(); self::assertNotNull(
$jobHistoryA = null; $user
foreach ($histories as $row) { ->getUserJobHistories()
/** @var User\UserJobHistory $row */ ->filter(fn(User\UserJobHistory $userJobHistory) => $userJobHistory->getJob() === $jobA )
if ($jobA === $row->getJob()) { ->first()->getEndDate()
$jobHistoryA = $row; );
}
}
self::assertNotNull($jobHistoryA->getEndDate());
} }
} }