Do not use DateTimeImmutable.

This commit is contained in:
Pol Dellaiera 2021-04-02 10:44:21 +02:00
parent 777fb25860
commit 48e2d2ceab
3 changed files with 64 additions and 50 deletions

View File

@ -25,7 +25,6 @@ namespace Chill\PersonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
/**
* AccompanyingPeriod
@ -35,9 +34,11 @@ class AccompanyingPeriod
/** @var integer */
private $id;
private DateTimeImmutable $openingDate;
/** @var \DateTime */
private $openingDate;
private ?DateTimeImmutable $closingDate = null;
/** @var \DateTime */
private $closingDate;
/** @var string */
private $remark = '';
@ -56,11 +57,11 @@ class AccompanyingPeriod
private $user;
/**
*
* @param \DateTimeImmutable $dateOpening
*
* @param \DateTime $dateOpening
* @uses AccompanyingPeriod::setClosingDate()
*/
public function __construct(\DateTimeImmutable $dateOpening) {
public function __construct(\DateTime $dateOpening) {
$this->setOpeningDate($dateOpening);
}
@ -74,14 +75,25 @@ class AccompanyingPeriod
return $this->id;
}
public function setOpeningDate(DateTimeImmutable $openingDate): self
/**
* Set openingDate
*
* @param \DateTime $dateOpening
* @return AccompanyingPeriod
*/
public function setOpeningDate($openingDate)
{
$this->openingDate = $openingDate;
return $this;
}
public function getOpeningDate(): DateTimeImmutable
/**
* Get openingDate
*
* @return \DateTime
*/
public function getOpeningDate()
{
return $this->openingDate;
}
@ -91,14 +103,19 @@ class AccompanyingPeriod
*
* For closing a Person file, you should use Person::setClosed instead.
*/
public function setClosingDate(DateTimeImmutable $closingDate): self
public function setClosingDate($closingDate)
{
$this->closingDate = $closingDate;
return $this;
}
public function getClosingDate(): ?DateTimeImmutable
/**
* Get closingDate
*
* @return \DateTime
*/
public function getClosingDate()
{
return $this->closingDate;
}
@ -109,7 +126,7 @@ class AccompanyingPeriod
*/
public function isOpen(): bool
{
if ($this->getOpeningDate() > new \DateTimeImmutable('now')) {
if ($this->getOpeningDate() > new \DateTime('now')) {
return false;
}

View File

@ -22,6 +22,7 @@ namespace Chill\PersonBundle\Entity;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use ArrayIterator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\MainBundle\Entity\Country;
use Chill\PersonBundle\Entity\MaritalStatus;
@ -29,9 +30,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Chill\MainBundle\Entity\HasCenterInterface;
use Chill\MainBundle\Entity\Address;
use DateTime;
use DateTimeImmutable;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\VarDumper\VarDumper;
/**
* Person
@ -128,14 +127,14 @@ class Person implements HasCenterInterface {
*/
private $fullnameCanonical;
public function __construct(\DateTimeImmutable $opening = null) {
public function __construct(\DateTime $opening = null) {
$this->accompanyingPeriods = new ArrayCollection();
$this->spokenLanguages = new ArrayCollection();
$this->addresses = new ArrayCollection();
$this->altNames = new ArrayCollection();
if ($opening === null) {
$opening = new \DateTimeImmutable();
$opening = new \DateTime();
}
$this->open(new AccompanyingPeriod($opening));
@ -747,18 +746,17 @@ class Person implements HasCenterInterface {
/**
* By default, the addresses are ordered by date, descending (the most
* recent first)
*
* @return ArrayCollection
*/
public function getAddresses(): ArrayCollection
{
return $this->addresses;
}
public function getLastAddress(?DateTimeImmutable $from = null): ?Address
public function getLastAddress(DateTime $from = null)
{
$from ??= new DateTimeImmutable('now');
$from ??= new DateTime('now');
/** @var ArrayIterator $addressesIterator */
$addressesIterator = $this->getAddresses()
->filter(static fn (Address $address): bool => $address->getValidFrom() <= $from)
->getIterator();

View File

@ -27,7 +27,6 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\MainBundle\Entity\Address;
use DateInterval;
use DateTime;
use DateTimeImmutable;
use Generator;
/**
@ -42,7 +41,7 @@ class PersonTest extends \PHPUnit\Framework\TestCase
*/
public function testGetCurrentAccompanyingPeriod()
{
$d = new \DateTimeImmutable('yesterday');
$d = new \DateTime('yesterday');
$p = new Person($d);
$period = $p->getCurrentAccompanyingPeriod();
@ -52,7 +51,7 @@ class PersonTest extends \PHPUnit\Framework\TestCase
$this->assertEquals($d, $period->getOpeningDate());
//close and test
$period->setClosingDate(new \DateTimeImmutable('tomorrow'));
$period->setClosingDate(new \DateTime('tomorrow'));
$shouldBeNull = $p->getCurrentAccompanyingPeriod();
$this->assertNull($shouldBeNull);
@ -64,17 +63,17 @@ class PersonTest extends \PHPUnit\Framework\TestCase
*/
public function testAccompanyingPeriodOrderWithUnorderedAccompanyingPeriod()
{
$d = new \DateTimeImmutable("2013/2/1");
$d = new \DateTime("2013/2/1");
$p = new Person($d);
$e = new \DateTimeImmutable("2013/3/1");
$e = new \DateTime("2013/3/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($e);
$p->close($period);
$f = new \DateTimeImmutable("2013/1/1");
$f = new \DateTime("2013/1/1");
$p->open(new AccompanyingPeriod($f));
$g = new \DateTimeImmutable("2013/4/1");
$g = new \DateTime("2013/4/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($g);
$p->close($period);
@ -90,17 +89,17 @@ class PersonTest extends \PHPUnit\Framework\TestCase
* starting at the same time order regarding to the closing date.
*/
public function testAccompanyingPeriodOrderSameDateOpening() {
$d = new \DateTimeImmutable("2013/2/1");
$d = new \DateTime("2013/2/1");
$p = new Person($d);
$g = new \DateTimeImmutable("2013/4/1");
$g = new \DateTime("2013/4/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($g);
$p->close($period);
$f = new \DateTimeImmutable("2013/2/1");
$f = new \DateTime("2013/2/1");
$p->open(new AccompanyingPeriod($f));
$e = new \DateTimeImmutable("2013/3/1");
$e = new \DateTime("2013/3/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($e);
$p->close($period);
@ -117,17 +116,17 @@ class PersonTest extends \PHPUnit\Framework\TestCase
* is covering another one : start_1 < start_2 & end_2 < end_1
*/
public function testDateCoveringWithCoveringAccompanyingPeriod() {
$d = new \DateTimeImmutable("2013/2/1");
$d = new \DateTime("2013/2/1");
$p = new Person($d);
$e = new \DateTimeImmutable("2013/3/1");
$e = new \DateTime("2013/3/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($e);
$p->close($period);
$f = new \DateTimeImmutable("2013/1/1");
$f = new \DateTime("2013/1/1");
$p->open(new AccompanyingPeriod($f));
$g = new \DateTimeImmutable("2013/4/1");
$g = new \DateTime("2013/4/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($g);
$p->close($period);
@ -142,14 +141,14 @@ class PersonTest extends \PHPUnit\Framework\TestCase
* before an existing period
*/
public function testNotOpenAFileReOpenedLater() {
$d = new \DateTimeImmutable("2013/2/1");
$d = new \DateTime("2013/2/1");
$p = new Person($d);
$e = new \DateTimeImmutable("2013/3/1");
$e = new \DateTime("2013/3/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($e);
$p->close($period);
$f = new \DateTimeImmutable("2013/1/1");
$f = new \DateTime("2013/1/1");
$p->open(new AccompanyingPeriod($f));
$r = $p->checkAccompanyingPeriodsAreNotCollapsing();
@ -159,15 +158,15 @@ class PersonTest extends \PHPUnit\Framework\TestCase
public function dateProvider(): Generator
{
yield [(DateTimeImmutable::createFromFormat('Y-m-d', '2021-01-05'))->settime(0, 0)];
yield [(DateTimeImmutable::createFromFormat('Y-m-d', '2021-02-05'))->settime(0, 0)];
yield [(DateTimeImmutable::createFromFormat('Y-m-d', '2021-03-05'))->settime(0, 0)];
yield [(DateTime::createFromFormat('Y-m-d', '2021-01-05'))->settime(0, 0)];
yield [(DateTime::createFromFormat('Y-m-d', '2021-02-05'))->settime(0, 0)];
yield [(DateTime::createFromFormat('Y-m-d', '2021-03-05'))->settime(0, 0)];
}
/**
* @dataProvider dateProvider
*/
public function testGetLastAddress(DateTimeImmutable $date)
public function testGetLastAddress(DateTime $date)
{
$p = new Person($date);
@ -175,35 +174,35 @@ class PersonTest extends \PHPUnit\Framework\TestCase
$this::assertNull($p->getLastAddress());
// Take an arbitrary date before the $date in parameter.
$addressDate = $date->sub(new DateInterval('PT180M'));
$addressDate = clone $date;
// 1. Smoke test: Test that the first address added is the last one.
$address1 = (new Address())->setValidFrom($addressDate);
$address1 = (new Address())->setValidFrom($addressDate->sub(new DateInterval('PT180M')));
$p->addAddress($address1);
$this::assertCount(1, $p->getAddresses());
$this::assertSame($address1, $p->getLastAddress());
// 2. Add an older address, which should not be the last address.
$addressDate2 = $addressDate->sub(new DateInterval('PT30M'));
$address2 = (new Address())->setValidFrom($addressDate2);
$addressDate2 = clone $addressDate;
$address2 = (new Address())->setValidFrom($addressDate2->sub(new DateInterval('PT30M')));
$p->addAddress($address2);
$this::assertCount(2, $p->getAddresses());
$this::assertSame($address1, $p->getLastAddress());
// 3. Add a newer address, which should be the last address.
$addressDate3 = $addressDate->add(new DateInterval('PT30M'));
$address3 = (new Address())->setValidFrom($addressDate3);
$addressDate3 = clone $addressDate;
$address3 = (new Address())->setValidFrom($addressDate3->add(new DateInterval('PT30M')));
$p->addAddress($address3);
$this::assertCount(3, $p->getAddresses());
$this::assertSame($address3, $p->getLastAddress());
// 4. Get the last address from a specific date.
$this::assertSame($address1, $p->getLastAddress($addressDate));
$this::assertSame($address2, $p->getLastAddress($addressDate2));
$this::assertSame($address3, $p->getLastAddress($addressDate3));
$this::assertEquals($address1, $p->getLastAddress($addressDate));
$this::assertEquals($address2, $p->getLastAddress($addressDate2));
$this::assertEquals($address3, $p->getLastAddress($addressDate3));
}
}