From 2c28449b58b1fd13bcb65714a05ddf23fa547d6e Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Fri, 4 Nov 2022 12:23:02 +0100 Subject: [PATCH] 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] --- mailpoet/tests/_support/IntegrationTester.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mailpoet/tests/_support/IntegrationTester.php b/mailpoet/tests/_support/IntegrationTester.php index a71a125378..de0186bc24 100644 --- a/mailpoet/tests/_support/IntegrationTester.php +++ b/mailpoet/tests/_support/IntegrationTester.php @@ -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); + } }