5 * @package akeebaengine
6 * @copyright Copyright (c)2006-2023 Nicholas K. Dionysopoulos / Akeeba Ltd
7 * @license GNU General Public License version 3, or later
10 namespace Akeeba\MiniTest\Test;
13 use Akeeba\S3\Connector;
17 * Upload, download and delete small XML files (under 1MB) using a string source
19 * @package Akeeba\MiniTest\Test
21 class SmallInlineXMLFiles extends SmallFiles
23 public static function upload10KbRoot(Connector $s3, array $options): bool
25 return static::upload($s3, $options, AbstractTest::TEN_KB, 'root_10kb.xml');
28 public static function upload10KbRootGreek(Connector $s3, array $options): bool
30 return static::upload($s3, $options, AbstractTest::TEN_KB, 'δοκιμή_10kb.xml');
33 public static function upload10KbFolderGreek(Connector $s3, array $options): bool
35 return static::upload($s3, $options, AbstractTest::TEN_KB, 'ο_φάκελός_μου/δοκιμή_10kb.xml');
38 public static function upload600KbRoot(Connector $s3, array $options): bool
40 return static::upload($s3, $options, AbstractTest::SIX_HUNDRED_KB, 'root_600kb.xml');
43 public static function upload10KbFolder(Connector $s3, array $options): bool
45 return static::upload($s3, $options, AbstractTest::TEN_KB, 'my_folder/10kb.xml');
48 public static function upload600KbFolder(Connector $s3, array $options): bool
50 return static::upload($s3, $options, AbstractTest::SIX_HUNDRED_KB, 'my_folder/600kb.xml');
53 protected static function upload(Connector $s3, array $options, int $size, string $uri): bool
55 // Randomize the name. Required for archive buckets where you cannot overwrite data.
56 $dotPos = strrpos($uri, '.');
57 $uri = substr($uri, 0, $dotPos) . '.' . md5(microtime(false)) . substr($uri, $dotPos);
59 // Create some random data to upload
60 $sourceData = static::createXMLFile($size);
62 // Upload the data. Throws exception if it fails.
63 $bucket = $options['bucket'];
64 $input = Input::createFromData($sourceData);
66 $s3->putObject($input, $bucket, $uri);
68 // Tentatively accept that this method succeeded.
71 // Should I download the file and compare its contents with my random data?
72 if (static::$downloadAfter)
74 $downloadedData = $s3->getObject($bucket, $uri);
76 $result = static::areStringsEqual($sourceData, $downloadedData);
79 // Should I delete the remotely stored file?
80 if (static::$deleteRemote)
82 // Delete the remote file. Throws exception if it fails.
83 $s3->deleteObject($bucket, $uri);
89 private static function createXMLFile(int $size): string
92 <?xml version="1.0" encoding="utf-8" ?>
96 $chunks = floor(($size - 55) / 1024);
98 for ($i = 1; $i <= $chunks; $i++)
100 $randomBlock = static::genRandomData(1024 - 63);
104 <data><![CDATA[$randomBlock]]></data>
118 private static function genRandomData(int $length): string
120 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890';
121 $maxLength = strlen($chars) - 1;
124 for ($i = 0; $i < $length; $i++)
126 $salt .= substr($chars, random_int(0, $maxLength), 1);