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

@@ -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));
}
}