TimeFactoryTest.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\AppFramework\Utility;
  8. use OC\AppFramework\Utility\TimeFactory;
  9. class TimeFactoryTest extends \Test\TestCase {
  10. protected TimeFactory $timeFactory;
  11. protected function setUp(): void {
  12. $this->timeFactory = new TimeFactory();
  13. }
  14. public function testNow(): void {
  15. $now = $this->timeFactory->now();
  16. self::assertSame('UTC', $now->getTimezone()->getName());
  17. }
  18. public function testNowWithTimeZone(): void {
  19. $timezone = new \DateTimeZone('Europe/Berlin');
  20. $withTimeZone = $this->timeFactory->withTimeZone($timezone);
  21. $now = $withTimeZone->now();
  22. self::assertSame('Europe/Berlin', $now->getTimezone()->getName());
  23. }
  24. public function testGetTimeZone(): void {
  25. $expected = new \DateTimeZone('Europe/Berlin');
  26. $actual = $this->timeFactory->getTimeZone('Europe/Berlin');
  27. self::assertEquals($expected, $actual);
  28. }
  29. public function testGetTimeZoneUTC(): void {
  30. $expected = new \DateTimeZone('UTC');
  31. $actual = $this->timeFactory->getTimeZone();
  32. self::assertEquals($expected, $actual);
  33. }
  34. public function testGetTimeZoneInvalid(): void {
  35. $this->expectException(\Exception::class);
  36. $this->timeFactory->getTimeZone('blubblub');
  37. }
  38. }