Skip to content

Commit

Permalink
fix(tests): Mock time function in controller to be able to set a spec…
Browse files Browse the repository at this point in the history
…ific time

Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux authored and Chartman123 committed Feb 18, 2023
1 parent c187a4f commit 65e9fcd
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion tests/Unit/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

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;
Expand Down Expand Up @@ -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')
Expand All @@ -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())
Expand Down

0 comments on commit 65e9fcd

Please sign in to comment.