KnownMtimeTest.php 2.0 KB

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