Update Person::getLastAddress() based on feedback.

This commit is contained in:
Pol Dellaiera
2021-03-22 12:32:29 +01:00
parent c205bbddd3
commit 03601b9707
3 changed files with 165 additions and 179 deletions

View File

@@ -2,8 +2,8 @@
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
*
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
@@ -25,6 +25,10 @@ namespace Chill\PersonBundle\Tests\Entity;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\MainBundle\Entity\Address;
use DateInterval;
use DateTime;
use DateTimeImmutable;
use Generator;
/**
* Unit tests for the person Entity
@@ -32,167 +36,166 @@ use Chill\MainBundle\Entity\Address;
class PersonTest extends \PHPUnit\Framework\TestCase
{
/**
* Test the creation of an accompanying, its closure and the access to
* Test the creation of an accompanying, its closure and the access to
* the current accompaniying period via the getCurrentAccompanyingPeriod
* function.
*/
public function testGetCurrentAccompanyingPeriod()
{
$d = new \DateTime('yesterday');
$d = new \DateTimeImmutable('yesterday');
$p = new Person($d);
$period = $p->getCurrentAccompanyingPeriod();
$this->assertInstanceOf('Chill\PersonBundle\Entity\AccompanyingPeriod', $period);
$this->assertTrue($period->isOpen());
$this->assertEquals($d, $period->getOpeningDate());
//close and test
$period->setClosingDate(new \DateTime('tomorrow'));
$period->setClosingDate(new \DateTimeImmutable('tomorrow'));
$shouldBeNull = $p->getCurrentAccompanyingPeriod();
$this->assertNull($shouldBeNull);
}
/**
* Test if the getAccompanyingPeriodsOrdered function return a list of
* periods ordered ascendency.
*/
public function testAccompanyingPeriodOrderWithUnorderedAccompanyingPeriod()
{
$d = new \DateTime("2013/2/1");
{
$d = new \DateTimeImmutable("2013/2/1");
$p = new Person($d);
$e = new \DateTime("2013/3/1");
$e = new \DateTimeImmutable("2013/3/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($e);
$p->close($period);
$f = new \DateTime("2013/1/1");
$f = new \DateTimeImmutable("2013/1/1");
$p->open(new AccompanyingPeriod($f));
$g = new \DateTime("2013/4/1");
$g = new \DateTimeImmutable("2013/4/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($g);
$p->close($period);
$r = $p->getAccompanyingPeriodsOrdered();
$date = $r[0]->getOpeningDate()->format('Y-m-d');
$this->assertEquals($date, '2013-01-01');
}
/**
* Test if the getAccompanyingPeriodsOrdered function, for periods
* starting at the same time order regarding to the closing date.
*/
public function testAccompanyingPeriodOrderSameDateOpening() {
$d = new \DateTime("2013/2/1");
$d = new \DateTimeImmutable("2013/2/1");
$p = new Person($d);
$g = new \DateTime("2013/4/1");
$g = new \DateTimeImmutable("2013/4/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($g);
$p->close($period);
$f = new \DateTime("2013/2/1");
$f = new \DateTimeImmutable("2013/2/1");
$p->open(new AccompanyingPeriod($f));
$e = new \DateTime("2013/3/1");
$e = new \DateTimeImmutable("2013/3/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($e);
$p->close($period);
$r = $p->getAccompanyingPeriodsOrdered();
$date = $r[0]->getClosingDate()->format('Y-m-d');
$this->assertEquals($date, '2013-03-01');
}
/**
* Test if the function checkAccompanyingPeriodIsNotCovering returns
* the good constant when two periods are collapsing : a period
* is covering another one : start_1 < start_2 & end_2 < end_1
*/
public function testDateCoveringWithCoveringAccompanyingPeriod() {
$d = new \DateTime("2013/2/1");
$d = new \DateTimeImmutable("2013/2/1");
$p = new Person($d);
$e = new \DateTime("2013/3/1");
$e = new \DateTimeImmutable("2013/3/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($e);
$p->close($period);
$f = new \DateTime("2013/1/1");
$f = new \DateTimeImmutable("2013/1/1");
$p->open(new AccompanyingPeriod($f));
$g = new \DateTime("2013/4/1");
$g = new \DateTimeImmutable("2013/4/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($g);
$p->close($period);
$r = $p->checkAccompanyingPeriodsAreNotCollapsing();
$this->assertEquals($r['result'], Person::ERROR_PERIODS_ARE_COLLAPSING);
}
/**
* Test if the function checkAccompanyingPeriodIsNotCovering returns
* the good constant when two periods are collapsing : a period is open
* before an existing period
*/
public function testNotOpenAFileReOpenedLater() {
$d = new \DateTime("2013/2/1");
$d = new \DateTimeImmutable("2013/2/1");
$p = new Person($d);
$e = new \DateTime("2013/3/1");
$e = new \DateTimeImmutable("2013/3/1");
$period = $p->getCurrentAccompanyingPeriod()->setClosingDate($e);
$p->close($period);
$f = new \DateTime("2013/1/1");
$f = new \DateTimeImmutable("2013/1/1");
$p->open(new AccompanyingPeriod($f));
$r = $p->checkAccompanyingPeriodsAreNotCollapsing();
$this->assertEquals($r['result'], Person::ERROR_ADDIND_PERIOD_AFTER_AN_OPEN_PERIOD);
}
public function testGetLastAddress()
public function dateProvider(): Generator
{
$date0 = (\DateTime::createFromFormat('Y-m-d', '2021-02-05'))->settime(0,0);
$date1 = (\DateTime::createFromFormat('Y-m-d', '2021-03-05'))->settime(0,0);
$date2 = (\DateTime::createFromFormat('Y-m-d', '2021-04-05'))->settime(0,0);
$date3 = (\DateTime::createFromFormat('Y-m-d', '2021-05-05'))->settime(0,0);
$date4 = (\DateTime::createFromFormat('Y-m-d', '2021-06-05'))->settime(0,0);
$date5 = (\DateTime::createFromFormat('Y-m-d', '2021-07-05'))->settime(0,0);
$date6 = (\DateTime::createFromFormat('Y-m-d', '2021-08-05'))->settime(0,0);
$p = new Person($date1);
$this->assertNull($p->getLastAddress($date1));
// add some address
$add1 = (new Address())->setValidFrom($date1);
// no address with date 2
$add3 = (new Address())->setValidFrom($date3);
// no address with date 4
$add5 = (new Address())->setValidFrom($date5);
$p->addAddress($add1);
// test that, if only one address, that does work:
$this->assertSame($add1, $p->getLastAddress($date1));
$this->assertSame($add1, $p->getLastAddress($date2));
$this->assertSame($add1, $p->getLastAddress());
// adress before the date should not work
$this->assertNull($p->getLastAddress($date0));
// add addresses
$p->addAddress($add3);
$p->addAddress($add5);
// test retrieval of address
$this->assertSame($add1, $p->getLastAddress($date2));
//$this->assertSame($add3, $p->getLastAddress($date3));
dump($p->getLastAddress($date4), $add3);
$this->assertSame($add3, $p->getLastAddress($date4));
//$this->assertSame($add5, $p->getLastAddress($date5));
$this->assertSame($add5, $p->getLastAddress($date6));
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)];
}
/**
* @dataProvider dateProvider
*/
public function testGetLastAddress(DateTimeImmutable $date)
{
$p = new Person($date);
// Make sure that there is no last address.
$this->assertNull($p->getLastAddress());
// Take an arbitrary date before the $date in parameter.
$addressDate = $date->sub(new DateInterval('PT180M'));
// 1. Smoke test: Test that the first address added is the last one.
$address1 = (new Address())->setValidFrom($addressDate);
$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.
$address2 = (new Address())->setValidFrom($addressDate->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.
$address3 = (new Address())->setValidFrom($addressDate->add(new DateInterval('PT30M')));
$p->addAddress($address3);
$this->assertCount(3, $p->getAddresses());
$this->assertSame($address3, $p->getLastAddress());
}
}