diff --git a/apps/files_trashbin/tests/ExpirationTest.php b/apps/files_trashbin/tests/ExpirationTest.php index 396fbdfb88755cb6364174442184ba57d431a7ed..7bfbea19256a6010c7109b3bb73bae81242d0392 100644 --- a/apps/files_trashbin/tests/ExpirationTest.php +++ b/apps/files_trashbin/tests/ExpirationTest.php @@ -23,8 +23,9 @@ */ use OCA\Files_Trashbin\Expiration; -use \OCA\Files_Trashbin\Tests; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\IConfig; +use PHPUnit\Framework\MockObject\MockObject; class ExpirationTest extends \Test\TestCase { const SECONDS_PER_DAY = 86400; //60*60*24 @@ -182,58 +183,27 @@ class ExpirationTest extends \Test\TestCase { } /** - * * @param int $time - * @return \OCP\AppFramework\Utility\ITimeFactory + * @return ITimeFactory|MockObject */ private function getMockedTimeFactory($time){ - $mockedTimeFactory = $this->getMockBuilder('\OCP\AppFramework\Utility\ITimeFactory') - ->disableOriginalConstructor() - ->setMethods(['getTime']) - ->getMock() - ; - $mockedTimeFactory->expects($this->any())->method('getTime')->will( - $this->returnValue($time) - ); + $mockedTimeFactory = $this->createMock(ITimeFactory::class); + $mockedTimeFactory->expects($this->any()) + ->method('getTime') + ->willReturn($time); return $mockedTimeFactory; } /** - * * @param string $returnValue - * @return IConfig + * @return IConfig|MockObject */ private function getMockedConfig($returnValue){ - $mockedConfig = $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->setMethods( - [ - 'setSystemValues', - 'setSystemValue', - 'getSystemValue', - 'getFilteredSystemValue', - 'deleteSystemValue', - 'getAppKeys', - 'setAppValue', - 'getAppValue', - 'deleteAppValue', - 'deleteAppValues', - 'setUserValue', - 'getUserValue', - 'getUserValueForUsers', - 'getUserKeys', - 'deleteUserValue', - 'deleteAllUserValues', - 'deleteAppFromAllUsers', - 'getUsersForUserValue' - ] - ) - ->getMock() - ; - $mockedConfig->expects($this->any())->method('getSystemValue')->will( - $this->returnValue($returnValue) - ); + $mockedConfig = $this->createMock(IConfig::class); + $mockedConfig->expects($this->any()) + ->method('getSystemValue') + ->willReturn($returnValue); return $mockedConfig; } diff --git a/apps/files_versions/tests/ExpirationTest.php b/apps/files_versions/tests/ExpirationTest.php index 8cd288d75cc7d7d02756e6a5dfe092fb7670ab6e..a7c34b7ac7023541af4eee1e51a15a455f740466 100644 --- a/apps/files_versions/tests/ExpirationTest.php +++ b/apps/files_versions/tests/ExpirationTest.php @@ -25,7 +25,9 @@ namespace OCA\Files_Versions\Tests; use \OCA\Files_Versions\Expiration; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\IConfig; +use PHPUnit\Framework\MockObject\MockObject; class ExpirationTest extends \Test\TestCase { const SECONDS_PER_DAY = 86400; //60*60*24 @@ -151,58 +153,27 @@ class ExpirationTest extends \Test\TestCase { } /** - * * @param int $time - * @return \OCP\AppFramework\Utility\ITimeFactory + * @return ITimeFactory|MockObject */ private function getMockedTimeFactory($time){ - $mockedTimeFactory = $this->getMockBuilder('\OCP\AppFramework\Utility\ITimeFactory') - ->disableOriginalConstructor() - ->setMethods(['getTime']) - ->getMock() - ; - $mockedTimeFactory->expects($this->any())->method('getTime')->will( - $this->returnValue($time) - ); + $mockedTimeFactory = $this->createMock(ITimeFactory::class); + $mockedTimeFactory->expects($this->any()) + ->method('getTime') + ->willReturn($time); return $mockedTimeFactory; } /** - * * @param string $returnValue - * @return \OCP\IConfig + * @return IConfig|MockObject */ private function getMockedConfig($returnValue){ - $mockedConfig = $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->setMethods( - [ - 'setSystemValues', - 'setSystemValue', - 'getSystemValue', - 'getFilteredSystemValue', - 'deleteSystemValue', - 'getAppKeys', - 'setAppValue', - 'getAppValue', - 'deleteAppValue', - 'deleteAppValues', - 'setUserValue', - 'getUserValue', - 'getUserValueForUsers', - 'getUserKeys', - 'deleteUserValue', - 'deleteAllUserValues', - 'deleteAppFromAllUsers', - 'getUsersForUserValue' - ] - ) - ->getMock() - ; - $mockedConfig->expects($this->any())->method('getSystemValue')->will( - $this->returnValue($returnValue) - ); + $mockedConfig = $this->createMock(IConfig::class); + $mockedConfig->expects($this->any()) + ->method('getSystemValue') + ->willReturn($returnValue); return $mockedConfig; } diff --git a/lib/private/AppFramework/Utility/TimeFactory.php b/lib/private/AppFramework/Utility/TimeFactory.php index fc3bf72609fbefae0c01c4f61ca1736668ba028e..4526f9b1abb1791db4c1f4e4cd2c0629833b5641 100644 --- a/lib/private/AppFramework/Utility/TimeFactory.php +++ b/lib/private/AppFramework/Utility/TimeFactory.php @@ -37,9 +37,18 @@ class TimeFactory implements ITimeFactory { /** * @return int the result of a call to time() */ - public function getTime() : int { + public function getTime(): int { return time(); } + /** + * @param string $time + * @param \DateTimeZone $timezone + * @return \DateTime + * @since 15.0.0 + */ + public function getDateTime(string $time = 'now', \DateTimeZone $timezone = null): \DateTime { + return new \DateTime($time, $timezone); + } } diff --git a/lib/public/AppFramework/Utility/ITimeFactory.php b/lib/public/AppFramework/Utility/ITimeFactory.php index 7951d6ad8e611931db58dd18658819ba43e874c0..0367f037a6e899b305c4fc0e1b9fb736b1046e78 100644 --- a/lib/public/AppFramework/Utility/ITimeFactory.php +++ b/lib/public/AppFramework/Utility/ITimeFactory.php @@ -35,6 +35,14 @@ interface ITimeFactory { * @return int the result of a call to time() * @since 8.0.0 */ - public function getTime() : int; + public function getTime(): int; + + /** + * @param string $time + * @param \DateTimeZone $timezone + * @return \DateTime + * @since 15.0.0 + */ + public function getDateTime(string $time = 'now', \DateTimeZone $timezone = null): \DateTime; }