KnownMtimeTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace lib\Files\Storage\Wrapper;
  7. use OC\Files\Storage\Temporary;
  8. use OC\Files\Storage\Wrapper\KnownMtime;
  9. use PHPUnit\Framework\MockObject\MockObject;
  10. use Psr\Clock\ClockInterface;
  11. use Test\Files\Storage\Storage;
  12. /**
  13. * @group DB
  14. */
  15. class KnownMtimeTest extends Storage {
  16. /** @var Temporary */
  17. private $sourceStorage;
  18. /** @var ClockInterface|MockObject */
  19. private $clock;
  20. private int $fakeTime = 0;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->fakeTime = 0;
  24. $this->sourceStorage = new Temporary([]);
  25. $this->clock = $this->createMock(ClockInterface::class);
  26. $this->clock->method('now')->willReturnCallback(function () {
  27. if ($this->fakeTime) {
  28. return new \DateTimeImmutable("@{$this->fakeTime}");
  29. } else {
  30. return new \DateTimeImmutable();
  31. }
  32. });
  33. $this->instance = $this->getWrappedStorage();
  34. }
  35. protected function tearDown(): void {
  36. $this->sourceStorage->cleanUp();
  37. parent::tearDown();
  38. }
  39. protected function getWrappedStorage() {
  40. return new KnownMtime([
  41. 'storage' => $this->sourceStorage,
  42. 'clock' => $this->clock,
  43. ]);
  44. }
  45. public function testNewerKnownMtime(): void {
  46. $future = time() + 1000;
  47. $this->fakeTime = $future;
  48. $this->instance->file_put_contents('foo.txt', 'bar');
  49. // fuzzy match since the clock might have ticked
  50. $this->assertLessThan(2, abs(time() - $this->sourceStorage->filemtime('foo.txt')));
  51. $this->assertEquals($this->sourceStorage->filemtime('foo.txt'), $this->sourceStorage->stat('foo.txt')['mtime']);
  52. $this->assertEquals($this->sourceStorage->filemtime('foo.txt'), $this->sourceStorage->getMetaData('foo.txt')['mtime']);
  53. $this->assertEquals($future, $this->instance->filemtime('foo.txt'));
  54. $this->assertEquals($future, $this->instance->stat('foo.txt')['mtime']);
  55. $this->assertEquals($future, $this->instance->getMetaData('foo.txt')['mtime']);
  56. }
  57. }