mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-01 20:43:49 +00:00
Update StoredObjectManager to handle versioned StoredObjects
The StoredObjectManager and related test cases have been updated to handle versioned StoredObjects, allowing the same methods to work with either a StoredObject or its versions. The changes also involve return information for the write method and enhancements to the write test procedure. This provides more functionality and flexibility for handling StoredObjects in different versions.
This commit is contained in:
@@ -31,23 +31,25 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
*/
|
||||
final class StoredObjectManagerTest extends TestCase
|
||||
{
|
||||
public static function getDataProvider(): \Generator
|
||||
public static function getDataProviderForRead(): \Generator
|
||||
{
|
||||
/* HAPPY SCENARIO */
|
||||
|
||||
// Encrypted object
|
||||
yield [
|
||||
(new StoredObject())
|
||||
->setFilename('encrypted.txt')
|
||||
->setKeyInfos(['k' => base64_encode('S9NIHMaFHOWzLPez3jZOIHBaNfBrMQUR5zvqBz6kme8')])
|
||||
->setIv(unpack('C*', 'abcdefghijklmnop')),
|
||||
->registerVersion(
|
||||
unpack('C*', 'abcdefghijklmnop'),
|
||||
['k' => base64_encode('S9NIHMaFHOWzLPez3jZOIHBaNfBrMQUR5zvqBz6kme8')],
|
||||
filename: 'encrypted.txt'
|
||||
)->getStoredObject(),
|
||||
hex2bin('741237d255fd4f7eddaaa9058912a84caae28a41b10b34d4e3e3abe41d3b9b47cb0dd8f22c3c883d4f0e9defa75ff662'), // Binary encoded string
|
||||
'The quick brown fox jumps over the lazy dog', // clear
|
||||
];
|
||||
|
||||
// Non-encrypted object
|
||||
yield [
|
||||
(new StoredObject())->setFilename('non-encrypted.txt'), // The StoredObject
|
||||
(new StoredObject())->registerVersion(filename: 'non-encrypted.txt')->getStoredObject(), // The StoredObject
|
||||
'The quick brown fox jumps over the lazy dog', // Encrypted
|
||||
'The quick brown fox jumps over the lazy dog', // Clear
|
||||
];
|
||||
@@ -57,9 +59,11 @@ final class StoredObjectManagerTest extends TestCase
|
||||
// Encrypted object with issue during HTTP communication
|
||||
yield [
|
||||
(new StoredObject())
|
||||
->setFilename('error_during_http_request.txt')
|
||||
->setKeyInfos(['k' => base64_encode('S9NIHMaFHOWzLPez3jZOIHBaNfBrMQUR5zvqBz6kme8')])
|
||||
->setIv(unpack('C*', 'abcdefghijklmnop')),
|
||||
->registerVersion(
|
||||
unpack('C*', 'abcdefghijklmnop'),
|
||||
['k' => base64_encode('S9NIHMaFHOWzLPez3jZOIHBaNfBrMQUR5zvqBz6kme8')],
|
||||
filename: 'error_during_http_request.txt'
|
||||
)->getStoredObject(),
|
||||
hex2bin('741237d255fd4f7eddaaa9058912a84caae28a41b10b34d4e3e3abe41d3b9b47cb0dd8f22c3c883d4f0e9defa75ff662'), // Binary encoded string
|
||||
'The quick brown fox jumps over the lazy dog', // clear
|
||||
StoredObjectManagerException::class,
|
||||
@@ -68,9 +72,11 @@ final class StoredObjectManagerTest extends TestCase
|
||||
// Encrypted object with issue during HTTP communication: Invalid status code
|
||||
yield [
|
||||
(new StoredObject())
|
||||
->setFilename('invalid_statuscode.txt')
|
||||
->setKeyInfos(['k' => base64_encode('S9NIHMaFHOWzLPez3jZOIHBaNfBrMQUR5zvqBz6kme8')])
|
||||
->setIv(unpack('C*', 'abcdefghijklmnop')),
|
||||
->registerVersion(
|
||||
unpack('C*', 'abcdefghijklmnop'),
|
||||
['k' => base64_encode('S9NIHMaFHOWzLPez3jZOIHBaNfBrMQUR5zvqBz6kme8')],
|
||||
filename: 'invalid_statuscode.txt'
|
||||
)->getStoredObject(),
|
||||
hex2bin('741237d255fd4f7eddaaa9058912a84caae28a41b10b34d4e3e3abe41d3b9b47cb0dd8f22c3c883d4f0e9defa75ff662'), // Binary encoded string
|
||||
'The quick brown fox jumps over the lazy dog', // clear
|
||||
StoredObjectManagerException::class,
|
||||
@@ -79,17 +85,73 @@ final class StoredObjectManagerTest extends TestCase
|
||||
// Erroneous encrypted: Unable to decrypt exception.
|
||||
yield [
|
||||
(new StoredObject())
|
||||
->setFilename('unable_to_decrypt.txt')
|
||||
->setKeyInfos(['k' => base64_encode('WRONG_PASS_PHRASE')])
|
||||
->setIv(unpack('C*', 'abcdefghijklmnop')),
|
||||
->registerVersion(
|
||||
unpack('C*', 'abcdefghijklmnop'),
|
||||
['k' => base64_encode('WRONG_PASS_PHRASE')],
|
||||
filename: 'unable_to_decrypt.txt'
|
||||
)->getStoredObject(),
|
||||
'WRONG_ENCODED_VALUE', // Binary encoded string
|
||||
'The quick brown fox jumps over the lazy dog', // clear
|
||||
StoredObjectManagerException::class,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getDataProviderForWrite(): \Generator
|
||||
{
|
||||
/* HAPPY SCENARIO */
|
||||
|
||||
// Encrypted object
|
||||
yield [
|
||||
(new StoredObject())
|
||||
->registerVersion(
|
||||
unpack('C*', 'abcdefghijklmnop'),
|
||||
['k' => base64_encode('S9NIHMaFHOWzLPez3jZOIHBaNfBrMQUR5zvqBz6kme8')],
|
||||
filename: 'encrypted.txt'
|
||||
)->getStoredObject(),
|
||||
hex2bin('741237d255fd4f7eddaaa9058912a84caae28a41b10b34d4e3e3abe41d3b9b47cb0dd8f22c3c883d4f0e9defa75ff662'), // Binary encoded string
|
||||
'The quick brown fox jumps over the lazy dog', // clear
|
||||
];
|
||||
|
||||
// Non-encrypted object
|
||||
yield [
|
||||
(new StoredObject())->registerVersion(filename: 'non-encrypted.txt')->getStoredObject(), // The StoredObject
|
||||
'The quick brown fox jumps over the lazy dog', // Encrypted
|
||||
'The quick brown fox jumps over the lazy dog', // Clear
|
||||
];
|
||||
|
||||
/* UNHAPPY SCENARIO */
|
||||
|
||||
// Encrypted object with issue during HTTP communication
|
||||
yield [
|
||||
(new StoredObject())
|
||||
->registerVersion(
|
||||
unpack('C*', 'abcdefghijklmnop'),
|
||||
['k' => base64_encode('S9NIHMaFHOWzLPez3jZOIHBaNfBrMQUR5zvqBz6kme8')],
|
||||
filename: 'error_during_http_request.txt'
|
||||
)->getStoredObject(),
|
||||
hex2bin('741237d255fd4f7eddaaa9058912a84caae28a41b10b34d4e3e3abe41d3b9b47cb0dd8f22c3c883d4f0e9defa75ff662'), // Binary encoded string
|
||||
'The quick brown fox jumps over the lazy dog', // clear
|
||||
StoredObjectManagerException::class,
|
||||
-1,
|
||||
];
|
||||
|
||||
// Encrypted object with issue during HTTP communication: Invalid status code
|
||||
yield [
|
||||
(new StoredObject())
|
||||
->registerVersion(
|
||||
unpack('C*', 'abcdefghijklmnop'),
|
||||
['k' => base64_encode('S9NIHMaFHOWzLPez3jZOIHBaNfBrMQUR5zvqBz6kme8')],
|
||||
filename: 'invalid_statuscode.txt'
|
||||
)->getStoredObject(),
|
||||
hex2bin('741237d255fd4f7eddaaa9058912a84caae28a41b10b34d4e3e3abe41d3b9b47cb0dd8f22c3c883d4f0e9defa75ff662'), // Binary encoded string
|
||||
'The quick brown fox jumps over the lazy dog', // clear
|
||||
StoredObjectManagerException::class,
|
||||
408,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getDataProvider
|
||||
* @dataProvider getDataProviderForRead
|
||||
*/
|
||||
public function testRead(StoredObject $storedObject, string $encodedContent, string $clearContent, ?string $exceptionClass = null)
|
||||
{
|
||||
@@ -103,19 +165,37 @@ final class StoredObjectManagerTest extends TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getDataProvider
|
||||
* @dataProvider getDataProviderForWrite
|
||||
*/
|
||||
public function testWrite(StoredObject $storedObject, string $encodedContent, string $clearContent, ?string $exceptionClass = null)
|
||||
public function testWrite(StoredObject $storedObject, string $encodedContent, string $clearContent, ?string $exceptionClass = null, ?int $errorCode = null)
|
||||
{
|
||||
if (null !== $exceptionClass) {
|
||||
$this->expectException($exceptionClass);
|
||||
}
|
||||
|
||||
$storedObjectManager = $this->getSubject($storedObject, $encodedContent);
|
||||
$previousVersion = $storedObject->getCurrentVersion();
|
||||
$previousFilename = $previousVersion->getFilename();
|
||||
|
||||
$storedObjectManager->write($storedObject, $clearContent);
|
||||
$client = new MockHttpClient(function ($method, $url, $options) use ($encodedContent, $previousFilename, $errorCode) {
|
||||
self::assertEquals('PUT', $method);
|
||||
self::assertStringStartsWith('https://example.com/', $url);
|
||||
self::assertStringNotContainsString($previousFilename, $url, 'test that the PUT operation is not performed on the same file');
|
||||
self::assertArrayHasKey('body', $options);
|
||||
self::assertEquals($encodedContent, $options['body']);
|
||||
|
||||
self::assertEquals($clearContent, $storedObjectManager->read($storedObject));
|
||||
if (-1 === $errorCode) {
|
||||
throw new TransportException();
|
||||
}
|
||||
|
||||
return new MockResponse('', ['http_code' => $errorCode ?? 201]);
|
||||
});
|
||||
|
||||
$storedObjectManager = new StoredObjectManager($client, $this->getTempUrlGenerator($storedObject));
|
||||
|
||||
$newVersion = $storedObjectManager->write($storedObject, $clearContent);
|
||||
|
||||
self::assertNotSame($previousVersion, $newVersion);
|
||||
self::assertSame($storedObject->getCurrentVersion(), $newVersion);
|
||||
}
|
||||
|
||||
public function testWriteWithDeleteAt()
|
||||
@@ -170,19 +250,6 @@ final class StoredObjectManagerTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
if (Request::METHOD_PUT === $method) {
|
||||
switch ($url) {
|
||||
case 'https://example.com/non-encrypted.txt':
|
||||
case 'https://example.com/encrypted.txt':
|
||||
return new MockResponse($encodedContent, ['http_code' => 201]);
|
||||
|
||||
case 'https://example.com/error_during_http_request.txt':
|
||||
throw new TransportException('error_during_http_request.txt');
|
||||
case 'https://example.com/invalid_statuscode.txt':
|
||||
return new MockResponse($encodedContent, ['http_code' => 404]);
|
||||
}
|
||||
}
|
||||
|
||||
return new MockResponse('Not found');
|
||||
};
|
||||
|
||||
@@ -209,9 +276,16 @@ final class StoredObjectManagerTest extends TestCase
|
||||
$tempUrlGenerator = $this->createMock(TempUrlGeneratorInterface::class);
|
||||
|
||||
$tempUrlGenerator
|
||||
->expects($this->atLeastOnce())
|
||||
->method('generate')
|
||||
->withAnyParameters()
|
||||
->willReturn($response);
|
||||
->with($this->logicalOr($this->identicalTo('GET'), $this->identicalTo('PUT')), $this->isType('string'))
|
||||
->willReturnCallback(function (string $method, string $objectName) {
|
||||
return new SignedUrl(
|
||||
$method,
|
||||
'https://example.com/'.$objectName,
|
||||
new \DateTimeImmutable('1 hours')
|
||||
);
|
||||
});
|
||||
|
||||
return $tempUrlGenerator;
|
||||
}
|
||||
|
Reference in New Issue
Block a user