mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Use "createdAt" from database to compute the last modified datetime in StoredObjectManager
The code has been updated to use 'createdAt' from StoredObjectVersion entity in StoredObjectManager. Specifically, if a 'createdAt' datetime is set, we return that datetime. This change also includes corresponding test cases to validate the functionality. The situation helps deal with files created before July 2024.
This commit is contained in:
parent
cb90261309
commit
67d24cb951
@ -37,6 +37,14 @@ final class StoredObjectManager implements StoredObjectManagerInterface
|
||||
{
|
||||
$version = $document instanceof StoredObject ? $document->getCurrentVersion() : $document;
|
||||
|
||||
if (null !== $createdAt = $version->getCreatedAt()) {
|
||||
// as a createdAt datetime is set, return the date and time from database
|
||||
return $createdAt;
|
||||
}
|
||||
|
||||
// if no createdAt version exists in the database, we fetch the date and time from the
|
||||
// file. This situation happens for files created before July 2024.
|
||||
|
||||
if ($this->hasCache($version)) {
|
||||
$response = $this->getResponseFromCache($version);
|
||||
} else {
|
||||
|
@ -233,6 +233,64 @@ final class StoredObjectManagerTest extends TestCase
|
||||
$manager->write($storedObject, 'ok');
|
||||
}
|
||||
|
||||
public function testGetLastModifiedWithDateTimeInEntity(): void
|
||||
{
|
||||
$version = ($storedObject = new StoredObject())->registerVersion();
|
||||
$version->setCreatedAt(new \DateTimeImmutable('2024-07-09 15:09:47', new \DateTimeZone('+00:00')));
|
||||
$client = $this->createMock(HttpClientInterface::class);
|
||||
$client->expects(self::never())->method('request')->withAnyParameters();
|
||||
|
||||
$tempUrlGenerator = $this->createMock(TempUrlGeneratorInterface::class);
|
||||
$tempUrlGenerator
|
||||
->expects($this->never())
|
||||
->method('generate')
|
||||
->with($this->identicalTo('HEAD'), $this->isType('string'))
|
||||
->willReturnCallback(function (string $method, string $objectName) {
|
||||
return new SignedUrl(
|
||||
$method,
|
||||
'https://example.com/'.$objectName,
|
||||
new \DateTimeImmutable('1 hours'),
|
||||
$objectName
|
||||
);
|
||||
});
|
||||
|
||||
$manager = new StoredObjectManager($client, $tempUrlGenerator);
|
||||
|
||||
$actual = $manager->getLastModified($storedObject);
|
||||
|
||||
self::assertEquals(new \DateTimeImmutable('2024-07-09 15:09:47 GMT'), $actual);
|
||||
}
|
||||
|
||||
public function testGetLastModifiedWithDateTimeFromResponse(): void
|
||||
{
|
||||
$storedObject = (new StoredObject())->registerVersion()->getStoredObject();
|
||||
|
||||
$client = new MockHttpClient(
|
||||
new MockResponse('', ['http_code' => 200, 'response_headers' => [
|
||||
'last-modified' => 'Tue, 09 Jul 2024 15:09:47 GMT',
|
||||
]])
|
||||
);
|
||||
|
||||
$tempUrlGenerator = $this->createMock(TempUrlGeneratorInterface::class);
|
||||
$tempUrlGenerator
|
||||
->expects($this->atLeastOnce())
|
||||
->method('generate')
|
||||
->with($this->identicalTo('HEAD'), $this->isType('string'))
|
||||
->willReturnCallback(function (string $method, string $objectName) {
|
||||
return new SignedUrl(
|
||||
$method,
|
||||
'https://example.com/'.$objectName,
|
||||
new \DateTimeImmutable('1 hours')
|
||||
);
|
||||
});
|
||||
|
||||
$manager = new StoredObjectManager($client, $tempUrlGenerator);
|
||||
|
||||
$actual = $manager->getLastModified($storedObject);
|
||||
|
||||
self::assertEquals(new \DateTimeImmutable('2024-07-09 15:09:47 GMT'), $actual);
|
||||
}
|
||||
|
||||
private function getHttpClient(string $encodedContent): HttpClientInterface
|
||||
{
|
||||
$callback = static function ($method, $url, $options) use ($encodedContent) {
|
||||
@ -283,7 +341,8 @@ final class StoredObjectManagerTest extends TestCase
|
||||
return new SignedUrl(
|
||||
$method,
|
||||
'https://example.com/'.$objectName,
|
||||
new \DateTimeImmutable('1 hours')
|
||||
new \DateTimeImmutable('1 hours'),
|
||||
$objectName
|
||||
);
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user