mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-27 16:00:59 +01:00
Make file storage more testable
Summary: Fixes T2474. Adds a storage dummy storage engine for unit tests, and adds a couple of simple tests for basic file storage. Test Plan: Ran `arc unit` to execute unit tests. Reviewers: kwadwon Reviewed By: kwadwon CC: aran Maniphest Tasks: T2474 Differential Revision: https://secure.phabricator.com/D4777
This commit is contained in:
parent
e518135dfb
commit
361ce491f2
4 changed files with 110 additions and 2 deletions
|
@ -896,6 +896,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorFileStorageConfigurationException' => 'applications/files/exception/PhabricatorFileStorageConfigurationException.php',
|
||||
'PhabricatorFileStorageEngine' => 'applications/files/engine/PhabricatorFileStorageEngine.php',
|
||||
'PhabricatorFileStorageEngineSelector' => 'applications/files/engineselector/PhabricatorFileStorageEngineSelector.php',
|
||||
'PhabricatorFileTestCase' => 'applications/files/storage/__tests__/PhabricatorFileTestCase.php',
|
||||
'PhabricatorFileTransformController' => 'applications/files/controller/PhabricatorFileTransformController.php',
|
||||
'PhabricatorFileUploadController' => 'applications/files/controller/PhabricatorFileUploadController.php',
|
||||
'PhabricatorFileUploadException' => 'applications/files/exception/PhabricatorFileUploadException.php',
|
||||
|
@ -1301,6 +1302,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorTagView' => 'view/layout/PhabricatorTagView.php',
|
||||
'PhabricatorTaskmasterDaemon' => 'infrastructure/daemon/workers/PhabricatorTaskmasterDaemon.php',
|
||||
'PhabricatorTestCase' => 'infrastructure/testing/PhabricatorTestCase.php',
|
||||
'PhabricatorTestStorageEngine' => 'applications/files/engine/PhabricatorTestStorageEngine.php',
|
||||
'PhabricatorTestWorker' => 'infrastructure/daemon/workers/__tests__/PhabricatorTestWorker.php',
|
||||
'PhabricatorTimelineCursor' => 'infrastructure/daemon/timeline/storage/PhabricatorTimelineCursor.php',
|
||||
'PhabricatorTimelineDAO' => 'infrastructure/daemon/timeline/storage/PhabricatorTimelineDAO.php',
|
||||
|
@ -2338,6 +2340,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorFileShortcutController' => 'PhabricatorFileController',
|
||||
'PhabricatorFileStorageBlob' => 'PhabricatorFileDAO',
|
||||
'PhabricatorFileStorageConfigurationException' => 'Exception',
|
||||
'PhabricatorFileTestCase' => 'PhabricatorTestCase',
|
||||
'PhabricatorFileTransformController' => 'PhabricatorFileController',
|
||||
'PhabricatorFileUploadController' => 'PhabricatorFileController',
|
||||
'PhabricatorFileUploadException' => 'Exception',
|
||||
|
@ -2703,6 +2706,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorTagView' => 'AphrontView',
|
||||
'PhabricatorTaskmasterDaemon' => 'PhabricatorDaemon',
|
||||
'PhabricatorTestCase' => 'ArcanistPhutilTestCase',
|
||||
'PhabricatorTestStorageEngine' => 'PhabricatorFileStorageEngine',
|
||||
'PhabricatorTestWorker' => 'PhabricatorWorker',
|
||||
'PhabricatorTimelineCursor' => 'PhabricatorTimelineDAO',
|
||||
'PhabricatorTimelineDAO' => 'PhabricatorLiskDAO',
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Test storage engine. Does not actually store files. Used for unit tests.
|
||||
*
|
||||
* @group filestorage
|
||||
*/
|
||||
final class PhabricatorTestStorageEngine
|
||||
extends PhabricatorFileStorageEngine {
|
||||
|
||||
private static $storage = array();
|
||||
private static $nextHandle = 1;
|
||||
|
||||
public function getEngineIdentifier() {
|
||||
return 'unit-test';
|
||||
}
|
||||
|
||||
public function writeFile($data, array $params) {
|
||||
AphrontWriteGuard::willWrite();
|
||||
self::$storage[self::$nextHandle] = $data;
|
||||
return self::$nextHandle++;
|
||||
}
|
||||
|
||||
public function readFile($handle) {
|
||||
if (isset(self::$storage[$handle])) {
|
||||
return self::$storage[$handle];
|
||||
}
|
||||
throw new Exception("No such file with handle '{$handle}'!");
|
||||
}
|
||||
|
||||
public function deleteFile($handle) {
|
||||
AphrontWriteGuard::willWrite();
|
||||
unset(self::$storage[$handle]);
|
||||
}
|
||||
|
||||
}
|
|
@ -134,9 +134,16 @@ final class PhabricatorFile extends PhabricatorFileDAO
|
|||
|
||||
|
||||
public static function newFromFileData($data, array $params = array()) {
|
||||
$selector = PhabricatorEnv::newObjectFromConfig('storage.engine-selector');
|
||||
|
||||
$engines = $selector->selectStorageEngines($data, $params);
|
||||
if (isset($params['storageEngines'])) {
|
||||
$engines = $params['storageEngines'];
|
||||
} else {
|
||||
$selector = PhabricatorEnv::newObjectFromConfig(
|
||||
'storage.engine-selector');
|
||||
$engines = $selector->selectStorageEngines($data, $params);
|
||||
}
|
||||
|
||||
assert_instances_of($engines, 'PhabricatorFileStorageEngine');
|
||||
if (!$engines) {
|
||||
throw new Exception("No valid storage engines are available!");
|
||||
}
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
final class PhabricatorFileTestCase extends PhabricatorTestCase {
|
||||
|
||||
public function getPhabricatorTestCaseConfiguration() {
|
||||
return array(
|
||||
self::PHABRICATOR_TESTCONFIG_BUILD_STORAGE_FIXTURES => true,
|
||||
);
|
||||
}
|
||||
|
||||
public function testFileStorageReadWrite() {
|
||||
$engine = new PhabricatorTestStorageEngine();
|
||||
|
||||
$data = Filesystem::readRandomCharacters(64);
|
||||
|
||||
$params = array(
|
||||
'name' => 'test.dat',
|
||||
'storageEngines' => array(
|
||||
$engine,
|
||||
),
|
||||
);
|
||||
|
||||
$file = PhabricatorFile::newFromFileData($data, $params);
|
||||
|
||||
// Test that the storage engine worked, and was the target of the write. We
|
||||
// don't actually care what the data is (future changes may compress or
|
||||
// encrypt it), just that it exists in the test storage engine.
|
||||
$engine->readFile($file->getStorageHandle());
|
||||
|
||||
// Now test that we get the same data back out.
|
||||
$this->assertEqual($data, $file->loadFileData());
|
||||
}
|
||||
|
||||
|
||||
public function testFileStorageDelete() {
|
||||
$engine = new PhabricatorTestStorageEngine();
|
||||
|
||||
$data = Filesystem::readRandomCharacters(64);
|
||||
|
||||
$params = array(
|
||||
'name' => 'test.dat',
|
||||
'storageEngines' => array(
|
||||
$engine,
|
||||
),
|
||||
);
|
||||
|
||||
$file = PhabricatorFile::newFromFileData($data, $params);
|
||||
$handle = $file->getStorageHandle();
|
||||
$file->delete();
|
||||
|
||||
$caught = null;
|
||||
try {
|
||||
$engine->readFile($handle);
|
||||
} catch (Exception $ex) {
|
||||
$caught = $ex;
|
||||
}
|
||||
|
||||
$this->assertEqual(true, $caught instanceof Exception);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue