From 65e9fcdecd6e5b873b23ccdcf317521db1b15f94 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Mon, 6 Feb 2023 13:36:26 +0100 Subject: [PATCH] fix(tests): Mock time function in controller to be able to set a specific time Signed-off-by: Ferdinand Thiessen --- tests/Unit/Controller/ApiControllerTest.php | 24 ++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Controller/ApiControllerTest.php b/tests/Unit/Controller/ApiControllerTest.php index f3a3d8a906..019060aa92 100644 --- a/tests/Unit/Controller/ApiControllerTest.php +++ b/tests/Unit/Controller/ApiControllerTest.php @@ -22,6 +22,27 @@ * along with this program. If not, see . * */ + +namespace OCA\Forms\Controller; + +/** + * mock time() function used in controllers + * @param int|false|null $expected the value that should be returned when called + */ +function time($expected = null) { + static $value; + if ($expected === false) { + $value = null; + } elseif (!is_null($expected)) { + $value = $expected; + } + // Return real time if no mocked value is set + if (is_null($value)) { + return \time(); + } + return $value; +} + namespace OCA\Forms\Tests\Unit\Controller; use OCA\Forms\Activity\ActivityManager; @@ -358,6 +379,8 @@ public function testCreateNewForm($expectedForm) { $this->userManager, $this->createUserSession() ])->getMock(); + // Set the time that should be set for `lastUpdated` + \OCA\Forms\Controller\time(123456789); $this->configService->expects($this->once()) ->method('canCreateForms') @@ -372,7 +395,6 @@ public function testCreateNewForm($expectedForm) { ->with(self::callback(self::createFormValidator($expected))) ->willReturnCallback(function ($form) { $form->setId(7); - $form->setLastUpdated(123456789); return $form; }); $apiController->expects($this->once())