Add helper method for comparing DateTimeInterface objects

This commit adds a method to the integration tester that allows
comparing two DateTimeInterface objects and specify tolerated delta.
It also allows passing null and assert the DateTimeInterface internally.
This is because often our entities have return type DateTimeInterface|null
So with the internal checks we don't have to make those instance of checks
each time we pass date from entity into the method.
Note: I was not able to use $this->assertInstanceOf because PHPStan was not accepting that
and still complained.
[MAILPOET-4723]
This commit is contained in:
Rostislav Wolny
2022-11-04 12:23:02 +01:00
committed by Jan Lysý
parent 43197bf859
commit 2c28449b58

View File

@ -98,4 +98,18 @@ class IntegrationTester extends \Codeception\Actor {
public function uniqueId($length = 10): string {
return Security::generateRandomString($length);
}
/**
* Compares two DateTimeInterface objects by comparing timestamp values.
* $delta parameter specifies tolerated difference
*/
public function assertEqualDateTimes(DateTimeInterface $date1 = null, DateTimeInterface $date2 = null, int $delta = 0) {
if (!$date1 instanceof DateTimeInterface) {
throw new \Exception('$date1 is not DateTimeInterface');
}
if (!$date2 instanceof DateTimeInterface) {
throw new \Exception('$date2 is not DateTimeInterface');
}
expect($date1->getTimestamp())->equals($date2->getTimestamp(), $delta);
}
}