0
0
mirror of https://github.com/nextcloud/server.git synced 2024-09-28 07:18:42 +00:00
nextcloud_server/tests/lib/Files/Storage/Wrapper/KnownMtimeTest.php
Christoph Wurst 49dd79eabb
refactor: Add void return type to PHPUnit test methods
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
2024-09-15 22:32:31 +02:00

69 lines
2.0 KiB
PHP

<?php
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace lib\Files\Storage\Wrapper;
use OC\Files\Storage\Temporary;
use OC\Files\Storage\Wrapper\KnownMtime;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Clock\ClockInterface;
use Test\Files\Storage\Storage;
/**
* @group DB
*/
class KnownMtimeTest extends Storage {
/** @var Temporary */
private $sourceStorage;
/** @var ClockInterface|MockObject */
private $clock;
private int $fakeTime = 0;
protected function setUp(): void {
parent::setUp();
$this->fakeTime = 0;
$this->sourceStorage = new Temporary([]);
$this->clock = $this->createMock(ClockInterface::class);
$this->clock->method('now')->willReturnCallback(function () {
if ($this->fakeTime) {
return new \DateTimeImmutable("@{$this->fakeTime}");
} else {
return new \DateTimeImmutable();
}
});
$this->instance = $this->getWrappedStorage();
}
protected function tearDown(): void {
$this->sourceStorage->cleanUp();
parent::tearDown();
}
protected function getWrappedStorage() {
return new KnownMtime([
'storage' => $this->sourceStorage,
'clock' => $this->clock,
]);
}
public function testNewerKnownMtime(): void {
$future = time() + 1000;
$this->fakeTime = $future;
$this->instance->file_put_contents('foo.txt', 'bar');
// fuzzy match since the clock might have ticked
$this->assertLessThan(2, abs(time() - $this->sourceStorage->filemtime('foo.txt')));
$this->assertEquals($this->sourceStorage->filemtime('foo.txt'), $this->sourceStorage->stat('foo.txt')['mtime']);
$this->assertEquals($this->sourceStorage->filemtime('foo.txt'), $this->sourceStorage->getMetaData('foo.txt')['mtime']);
$this->assertEquals($future, $this->instance->filemtime('foo.txt'));
$this->assertEquals($future, $this->instance->stat('foo.txt')['mtime']);
$this->assertEquals($future, $this->instance->getMetaData('foo.txt')['mtime']);
}
}