From baeccf0970babf52f57375607eb890ae89fb959e Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Tue, 26 Sep 2023 11:27:36 +0200 Subject: [PATCH] fix Entity/UserTest and phpstan error --- src/Bundle/ChillMainBundle/Entity/User.php | 55 ++++++------------- .../ChillMainBundle/Tests/Entity/UserTest.php | 31 +++++------ 2 files changed, 30 insertions(+), 56 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Entity/User.php b/src/Bundle/ChillMainBundle/Entity/User.php index db9f8ade6..18d208c99 100644 --- a/src/Bundle/ChillMainBundle/Entity/User.php +++ b/src/Bundle/ChillMainBundle/Entity/User.php @@ -17,6 +17,7 @@ use DateTimeImmutable; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Criteria; +use Doctrine\Common\Collections\Selectable; use Doctrine\ORM\Mapping as ORM; use Iterator; use RuntimeException; @@ -117,11 +118,11 @@ class User implements UserInterface, \Stringable private ?Location $mainLocation = null; /** - * @var Collection + * @var Collection&Selectable * @ORM\OneToMany(targetEntity=UserScopeHistory::class, * mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true) */ - private Collection $scopeHistories; + private Collection&Selectable $scopeHistories; /** * @ORM\Column(type="string", length=255) @@ -136,11 +137,11 @@ class User implements UserInterface, \Stringable private ?string $salt = null; /** - * @var Collection + * @var Collection&Selectable * @ORM\OneToMany(targetEntity=UserJobHistory::class, * mappedBy="user", cascade={"persist", "remove"}, orphanRemoval=true) */ - private Collection $jobHistories; + private Collection&Selectable $jobHistories; /** * @ORM\Column(type="string", length=80) @@ -266,23 +267,12 @@ class User implements UserInterface, \Stringable { $at ??= new DateTimeImmutable('now'); - $criteria = new Criteria(); - $expr = Criteria::expr(); - - $criteria->where( - $expr->andX( - $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(); + foreach ($this->scopeHistories as $scopeHistory) { + if ($at >= $scopeHistory->getStartDate() && ( + null === $scopeHistory->getEndDate() || $at < $scopeHistory->getEndDate() + )) { + return $scopeHistory->getScope(); + } } return null; @@ -315,23 +305,12 @@ class User implements UserInterface, \Stringable { $at ??= new DateTimeImmutable('now'); - $criteria = new Criteria(); - $expr = Criteria::expr(); - - $criteria->where( - $expr->andX( - $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(); + foreach ($this->jobHistories as $jobHistory) { + if ($at >= $jobHistory->getStartDate() && ( + null === $jobHistory->getEndDate() || $at < $jobHistory->getEndDate() + )) { + return $jobHistory->getJob(); + } } return null; diff --git a/src/Bundle/ChillMainBundle/Tests/Entity/UserTest.php b/src/Bundle/ChillMainBundle/Tests/Entity/UserTest.php index a303354ed..a0502593e 100644 --- a/src/Bundle/ChillMainBundle/Tests/Entity/UserTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Entity/UserTest.php @@ -38,15 +38,13 @@ class UserTest extends TestCase self::assertSame($scopeB, $user->getMainScope()); // 2. get scopeA history, check endDate is not null - $histories = $user->getMainScopeHistories(); - $scopeHistoryA = null; - foreach ($histories as $row) { - /** @var User\UserScopeHistory $row */ - if ($scopeA === $row->getScope()) { - $scopeHistoryA = $row; - } - } - self::assertNotNull($scopeHistoryA->getEndDate()); + self::assertNotNull( + $user + ->getMainScopeHistories() + ->filter(fn (User\UserScopeHistory $userScopeHistory) => $userScopeHistory->getScope() === $scopeA ) + ->first()->getEndDate() + ); + } public function testUserJobHistory() @@ -62,15 +60,12 @@ class UserTest extends TestCase self::assertSame($jobB, $user->getUserJob()); // 2. get jobA history, check endDate is not null - $histories = $user->getUserJobHistories(); - $jobHistoryA = null; - foreach ($histories as $row) { - /** @var User\UserJobHistory $row */ - if ($jobA === $row->getJob()) { - $jobHistoryA = $row; - } - } - self::assertNotNull($jobHistoryA->getEndDate()); + self::assertNotNull( + $user + ->getUserJobHistories() + ->filter(fn(User\UserJobHistory $userJobHistory) => $userJobHistory->getJob() === $jobA ) + ->first()->getEndDate() + ); } }