|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\AwsS3\Test\Mftf\Helper; |
| 9 | + |
| 10 | +use Aws\S3\S3Client; |
| 11 | +use Codeception\Lib\ModuleContainer; |
| 12 | +use League\Flysystem\AwsS3v3\AwsS3Adapter; |
| 13 | +use Magento\AwsS3\Driver\AwsS3; |
| 14 | +use Magento\FunctionalTestingFramework\Helper\Helper; |
| 15 | +use Magento\Framework\Filesystem\DriverInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class for MFTF helpers for doing file assertions using S3. |
| 19 | + */ |
| 20 | +class S3FileAssertions extends Helper |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var DriverInterface $driver |
| 24 | + */ |
| 25 | + private $driver; |
| 26 | + |
| 27 | + /** |
| 28 | + * Call the parent constructor then create the AwsS3 driver from environment variables |
| 29 | + * |
| 30 | + * @param ModuleContainer $moduleContainer |
| 31 | + * @param array|null $config |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + public function __construct(ModuleContainer $moduleContainer, ?array $config = null) |
| 35 | + { |
| 36 | + parent::__construct($moduleContainer, $config); |
| 37 | + |
| 38 | + $region = getenv('REMOTE_STORAGE_AWSS3_REGION'); |
| 39 | + $prefix = getenv('REMOTE_STORAGE_AWSS3_PREFIX'); |
| 40 | + $bucket = getenv('REMOTE_STORAGE_AWSS3_BUCKET'); |
| 41 | + $accessKey = getenv('REMOTE_STORAGE_AWSS3_ACCESS_KEY'); |
| 42 | + $secretKey = getenv('REMOTE_STORAGE_AWSS3_SECRET_KEY'); |
| 43 | + |
| 44 | + $config = [ |
| 45 | + 'version' => 'latest', |
| 46 | + 'credentials' => [ |
| 47 | + 'key' => $accessKey, |
| 48 | + 'secret' => $secretKey |
| 49 | + ], |
| 50 | + 'bucket' => $bucket, |
| 51 | + 'region' => $region |
| 52 | + ]; |
| 53 | + |
| 54 | + if (empty($config['credentials']['key']) || empty($config['credentials']['secret'])) { |
| 55 | + unset($config['credentials']); |
| 56 | + } |
| 57 | + |
| 58 | + $client = new S3Client($config); |
| 59 | + $adapter = new AwsS3Adapter($client, $config['bucket'], $prefix); |
| 60 | + $objectUrl = $client->getObjectUrl($adapter->getBucket(), $adapter->applyPathPrefix('.')); |
| 61 | + $s3Driver = new AwsS3($adapter, new MockTestLogger(), $objectUrl); |
| 62 | + |
| 63 | + $this->driver = $s3Driver; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Create a file in the S3 bucket |
| 68 | + * |
| 69 | + * @param string $filePath |
| 70 | + * @param string $text |
| 71 | + * @return void |
| 72 | + * |
| 73 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 74 | + */ |
| 75 | + public function createTextFile($filePath, $text): void |
| 76 | + { |
| 77 | + $this->driver->filePutContents($filePath, $text); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Delete a file from the S3 bucket if it exists |
| 82 | + * |
| 83 | + * @param string $filePath |
| 84 | + * @return void |
| 85 | + * |
| 86 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 87 | + */ |
| 88 | + public function deleteFileIfExists($filePath): void |
| 89 | + { |
| 90 | + if ($this->driver->isExists($filePath)) { |
| 91 | + $this->driver->deleteFile($filePath); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Assert a file exists on the remote storage system |
| 97 | + * |
| 98 | + * @param string $filePath |
| 99 | + * @param string $message |
| 100 | + * @return void |
| 101 | + * |
| 102 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 103 | + */ |
| 104 | + public function assertFileExists($filePath, $message = ''): void |
| 105 | + { |
| 106 | + $this->assertTrue($this->driver->isExists($filePath), $message); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Assert a file does not exist on the remote storage system |
| 111 | + * |
| 112 | + * @param string $filePath |
| 113 | + * @param string $message |
| 114 | + * @return void |
| 115 | + * |
| 116 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 117 | + */ |
| 118 | + public function assertFileDoesNotExist($filePath, $message = ''): void |
| 119 | + { |
| 120 | + $this->assertFalse($this->driver->isExists($filePath), $message); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Assert a file on the remote storage system has no contents |
| 125 | + * |
| 126 | + * @param string $filePath |
| 127 | + * @param string $message |
| 128 | + * @return void |
| 129 | + * |
| 130 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 131 | + */ |
| 132 | + public function assertFileEmpty($filePath, $message = ""): void |
| 133 | + { |
| 134 | + $this->assertEmpty($this->driver->fileGetContents($filePath), $message); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Assert a file on the remote storage system is not empty |
| 139 | + * |
| 140 | + * @param string $filePath |
| 141 | + * @param string $message |
| 142 | + * @return void |
| 143 | + * |
| 144 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 145 | + */ |
| 146 | + public function assertFileNotEmpty($filePath, $message = ""): void |
| 147 | + { |
| 148 | + $this->assertNotEmpty($this->driver->fileGetContents($filePath), $message); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Assert a file on the remote storage system contains a given string |
| 153 | + * |
| 154 | + * @param string $filePath |
| 155 | + * @param string $text |
| 156 | + * @param string $message |
| 157 | + * @return void |
| 158 | + * |
| 159 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 160 | + */ |
| 161 | + public function assertFileContainsString($filePath, $text, $message = ""): void |
| 162 | + { |
| 163 | + $this->assertStringContainsString($text, $this->driver->fileGetContents($filePath), $message); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Assert a file on the remote storage system does not contain a given string |
| 168 | + * |
| 169 | + * @param string $filePath |
| 170 | + * @param string $text |
| 171 | + * @param string $message |
| 172 | + * @return void |
| 173 | + * |
| 174 | + * @throws \Magento\Framework\Exception\FileSystemException |
| 175 | + */ |
| 176 | + public function assertFileDoesNotContain($filePath, $text, $message = ""): void |
| 177 | + { |
| 178 | + $this->assertStringNotContainsString($text, $this->driver->fileGetContents($filePath), $message); |
| 179 | + } |
| 180 | +} |
0 commit comments