2022-02-20 21:22:07 +01:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Akeeba Engine
|
|
|
|
|
*
|
|
|
|
|
* @package akeebaengine
|
2023-12-19 03:28:16 +01:00
|
|
|
|
* @copyright Copyright (c)2006-2023 Nicholas K. Dionysopoulos / Akeeba Ltd
|
2022-02-20 21:22:07 +01:00
|
|
|
|
* @license GNU General Public License version 3, or later
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Akeeba\MiniTest\Test;
|
|
|
|
|
|
|
|
|
|
|
2023-12-19 03:28:16 +01:00
|
|
|
|
use Akeeba\S3\Connector;
|
|
|
|
|
use Akeeba\S3\Input;
|
2022-02-20 21:22:07 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Upload, download and delete small files (under 1MB) using a file source
|
|
|
|
|
*
|
|
|
|
|
* @package Akeeba\MiniTest\Test
|
|
|
|
|
*/
|
|
|
|
|
class SmallFiles extends AbstractTest
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Should I download the file after uploading it to test for contents consistency?
|
|
|
|
|
*
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
protected static $downloadAfter = true;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Should I delete the uploaded file after the test case is done?
|
|
|
|
|
*
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
protected static $deleteRemote = true;
|
|
|
|
|
|
|
|
|
|
public static function upload10KbRoot(Connector $s3, array $options): bool
|
|
|
|
|
{
|
2023-12-19 03:28:16 +01:00
|
|
|
|
return static::upload($s3, $options, AbstractTest::TEN_KB, 'root_10kb.dat');
|
2022-02-20 21:22:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function upload10KbRootGreek(Connector $s3, array $options): bool
|
|
|
|
|
{
|
2023-12-19 03:28:16 +01:00
|
|
|
|
return static::upload($s3, $options, AbstractTest::TEN_KB, 'δοκιμή_10kb.dat');
|
2022-02-20 21:22:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function upload10KbFolderGreek(Connector $s3, array $options): bool
|
|
|
|
|
{
|
2023-12-19 03:28:16 +01:00
|
|
|
|
return static::upload($s3, $options, AbstractTest::TEN_KB, 'ο_φάκελός_μου/δοκιμή_10kb.dat');
|
2022-02-20 21:22:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function upload600KbRoot(Connector $s3, array $options): bool
|
|
|
|
|
{
|
2023-12-19 03:28:16 +01:00
|
|
|
|
return static::upload($s3, $options, AbstractTest::SIX_HUNDRED_KB, 'root_600kb.dat');
|
2022-02-20 21:22:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function upload10KbFolder(Connector $s3, array $options): bool
|
|
|
|
|
{
|
2023-12-19 03:28:16 +01:00
|
|
|
|
return static::upload($s3, $options, AbstractTest::TEN_KB, 'my_folder/10kb.dat');
|
2022-02-20 21:22:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function upload600KbFolder(Connector $s3, array $options): bool
|
|
|
|
|
{
|
2023-12-19 03:28:16 +01:00
|
|
|
|
return static::upload($s3, $options, AbstractTest::SIX_HUNDRED_KB, 'my_folder/600kb.dat');
|
2022-02-20 21:22:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static function upload(Connector $s3, array $options, int $size, string $uri): bool
|
|
|
|
|
{
|
|
|
|
|
// Randomize the name. Required for archive buckets where you cannot overwrite data.
|
|
|
|
|
$dotPos = strrpos($uri, '.');
|
|
|
|
|
$uri = substr($uri, 0, $dotPos) . '.' . md5(microtime(false)) . substr($uri, $dotPos);
|
|
|
|
|
|
|
|
|
|
// Create a file with random data
|
2023-12-19 03:28:16 +01:00
|
|
|
|
$sourceFile = static::createFile($size);
|
2022-02-20 21:22:07 +01:00
|
|
|
|
|
|
|
|
|
// Upload the file. Throws exception if it fails.
|
|
|
|
|
$bucket = $options['bucket'];
|
|
|
|
|
$input = Input::createFromFile($sourceFile);
|
|
|
|
|
|
|
|
|
|
$s3->putObject($input, $bucket, $uri);
|
|
|
|
|
|
|
|
|
|
// Tentatively accept that this method succeeded.
|
|
|
|
|
$result = true;
|
|
|
|
|
|
|
|
|
|
// Should I download the file and compare its contents?
|
2023-12-19 03:28:16 +01:00
|
|
|
|
if (static::$downloadAfter)
|
2022-02-20 21:22:07 +01:00
|
|
|
|
{
|
|
|
|
|
// Donwload the data. Throws exception if it fails.
|
2023-12-19 03:28:16 +01:00
|
|
|
|
$downloadedFile = tempnam(static::getTempFolder(), 'as3');
|
2022-02-20 21:22:07 +01:00
|
|
|
|
$s3->getObject($bucket, $uri, $downloadedFile);
|
|
|
|
|
|
|
|
|
|
// Compare the file contents.
|
2023-12-19 03:28:16 +01:00
|
|
|
|
$result = static::areFilesEqual($sourceFile, $downloadedFile);
|
2022-02-20 21:22:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove the local files
|
|
|
|
|
@unlink($sourceFile);
|
|
|
|
|
@unlink($downloadedFile);
|
|
|
|
|
|
|
|
|
|
// Should I delete the remotely stored file?
|
2023-12-19 03:28:16 +01:00
|
|
|
|
if (static::$deleteRemote)
|
2022-02-20 21:22:07 +01:00
|
|
|
|
{
|
|
|
|
|
// Delete the remote file. Throws exception if it fails.
|
|
|
|
|
$s3->deleteObject($bucket, $uri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
}
|