Skip to content
Browse files

Use local temp dir for testing.

Should make it so that the average dev wanting to run tests only needs
to set up the db credentials. Also resolves some problems with different
users running the tests on the same system by no longer monopolizing the
system's temp dir.
  • Loading branch information...
1 parent dc16bfc commit b547156d8c3827b9747dd746c9fe4d3be8bbfdb1 @zerocrates zerocrates committed Aug 17, 2012
View
3 .gitignore
@@ -11,6 +11,9 @@
/application/logs/processes.log
/application/tests/log.xml
+# Test files
+/application/tests/_files/temp/
+
# Plugins and themes
/plugins/
/themes/
View
10 application/libraries/Omeka/Storage/Adapter/Filesystem.php
@@ -20,9 +20,9 @@ class Omeka_Storage_Adapter_Filesystem implements Omeka_Storage_Adapter_AdapterI
*
* @var string
*/
- private $_localDir;
+ protected $_localDir;
- private $_subDirs = array(
+ protected $_subDirs = array(
'thumbnails',
'square_thumbnails',
'fullsize',
@@ -35,7 +35,7 @@ class Omeka_Storage_Adapter_Filesystem implements Omeka_Storage_Adapter_AdapterI
*
* @var string
*/
- private $_webDir;
+ protected $_webDir;
/**
* Set options for the storage adapter.
@@ -187,7 +187,7 @@ public function setLocalDir($dir)
* @param string $path Storage path.
* @return string Absolute local filesystem path.
*/
- private function _getAbsPath($path)
+ protected function _getAbsPath($path)
{
return $this->_localDir . '/' . $path;
}
@@ -196,7 +196,7 @@ private function _getAbsPath($path)
* @throws Omeka_Storage_Exception
* @return boolean
*/
- private function _rename($source, $dest)
+ protected function _rename($source, $dest)
{
$destDir = dirname($dest);
if (!is_writable($destDir)) {
View
23 application/libraries/Omeka/Storage/Adapter/TempFilesystem.php
@@ -17,27 +17,6 @@
*/
class Omeka_Storage_Adapter_TempFilesystem extends Omeka_Storage_Adapter_Filesystem
{
- private $_storageDir;
-
- private $_files = array();
-
- public function __construct(array $options = array())
- {
- $this->_storageDir = sys_get_temp_dir() . '/files' . mt_rand();
- mkdir($this->_storageDir);
- $webDir = '/'; // CLI tests don't care whether the URL is valid.
- parent::__construct(array(
- 'localDir' => $this->_storageDir,
- 'webDir' => $webDir,
- ));
- }
-
- public function __destruct()
- {
- exec("rm -rf " . escapeshellarg($this->_storageDir));
- }
-
-
/**
* No need to perform this check.
*/
@@ -77,7 +56,7 @@ public function getUri($path)
private function _mkdir($filepath)
{
- $absPath = $this->_storageDir . '/' . $filepath;
+ $absPath = $this->_localDir . '/' . $filepath;
// Meant to stub out filesystem behavior, prevent failure due to
// missing subdirectories.
if (!is_dir(dirname($absPath))) {
View
24 application/libraries/Omeka/Test/Helper/Mail.php
@@ -25,13 +25,6 @@ class Omeka_Test_Helper_Mail
*/
public function __construct($path)
{
- if (!is_dir($path) && !mkdir($path)) {
- throw new RuntimeException(
- 'The mail path must be set in paths.maildir in '
- . 'the tests configuration.'
- );
- }
-
if (!is_writable($path)) {
throw new RuntimeException('The mail path '.$path.' must be writable by this user.');
}
@@ -46,27 +39,12 @@ public function __construct($path)
*/
public static function factory()
{
- $testConfig = Zend_Registry::get('test_config');
- $path = $testConfig->paths->maildir;
+ $path = Zend_Registry::get('test_mail_dir');
$helper = new self($path);
return $helper;
}
/**
- * Empty the fakemail storage directory between test runs.
- *
- * @return void
- */
- public function reset()
- {
- foreach ($this->_getIterator() as $file) {
- if ($this->_isMailFile($file)) {
- unlink($file->getRealPath());
- }
- }
- }
-
- /**
* Get an iterator over the fakemail directory.
*
* @return DirectoryIterator
View
22 application/libraries/Omeka/Test/Resource/Mail.php
@@ -20,25 +20,23 @@ class Omeka_Test_Resource_Mail extends Zend_Application_Resource_ResourceAbstrac
*/
public function init()
{
- $this->getBootstrap()->bootstrap('Config');
-
- $config = Zend_Registry::get('test_config');
-
- // If there's no path set, configure a blank path.
- // This avoids errors on non-mail tests, but you'll still get
- // an exception on mail tests to remind you to set the path.
- if (isset($config->paths)) {
- $path = $config->paths->maildir;
- } else {
- $path = '';
+ $bootstrap = $this->getBootstrap();
+ if (!$bootstrap->hasPluginResource('Tempdir')) {
+ $bootstrap->registerPluginResource('Tempdir');
}
+ $bootstrap->bootstrap('Tempdir');
+ $tempDir = $bootstrap->getResource('Tempdir');
+
+ $path = "{$tempDir}/mail";
+ mkdir($path);
$transport = new Zend_Mail_Transport_File(array(
'path' => $path,
'callback' => array(get_class($this), 'mailCallback')));
Zend_Mail::setDefaultTransport($transport);
+ Zend_Registry::set('test_mail_dir', $path);
- return new Zend_Mail;
+ return new Zend_Mail;
}
/**
View
14 application/libraries/Omeka/Test/Resource/Storage.php
@@ -12,8 +12,22 @@ class Omeka_Test_Resource_Storage extends Zend_Application_Resource_ResourceAbst
{
public function init()
{
+ $bootstrap = $this->getBootstrap();
+ if (!$bootstrap->hasPluginResource('Tempdir')) {
+ $bootstrap->registerPluginResource('Tempdir');
+ }
+ $bootstrap->bootstrap('Tempdir');
+ $tempDir = $bootstrap->getResource('Tempdir');
+
+ $storageDirName = $tempDir . '/storage_files_' . mt_rand();
+ mkdir($storageDirName);
$storage = new Omeka_Storage(array(
Omeka_Storage::OPTION_ADAPTER => 'Omeka_Storage_Adapter_TempFilesystem',
+ Omeka_Storage::OPTION_TEMP_DIR => $tempDir,
+ Omeka_Storage::OPTION_ADAPTER_OPTIONS => array(
+ 'localDir' => "$tempDir/$storageDirName",
+ 'webDir' => '/'
+ )
));
Zend_Registry::set('storage', $storage);
return $storage;
View
47 application/libraries/Omeka/Test/Resource/Tempdir.php
@@ -0,0 +1,47 @@
+<?php
+/**
+ * @copyright Roy Rosenzweig Center for History and New Media, 2011
+ * @license http://www.gnu.org/licenses/gpl-3.0.txt
+ * @package Omeka
+ */
+
+/**
+ * Bootstrap resource for storage in test environment.
+ */
+class Omeka_Test_Resource_Tempdir extends Zend_Application_Resource_ResourceAbstract
+{
+ public function init()
+ {
+ if ($bootstrap = $this->getBootstrap()) {
+ $bootstrap->bootstrap('Config');
+ $config = Zend_Registry::get('test_config');
+ }
+
+ if (!empty($config->tempDir)) {
+ $tempDir = $config->tempDir;
+ } else {
+ $tempDir = APP_DIR . '/tests/_files/temp';
+ }
+
+ $this->cleanDir($tempDir);
+
+ return $tempDir;
+ }
+
+ public function cleanDir($dir)
+ {
+ $filenames = scandir($dir);
+ foreach ($filenames as $filename) {
+ if ($filename == '.' || $filename == '..' || $filename == '.keep') {
+ continue;
+ }
+ $path = "$dir/$filename";
+ if (is_dir($path)) {
+ $this->cleanDir($path);
+ rmdir($path);
+ } else {
+ unlink($path);
+ }
+ }
+ }
+}
View
0 application/tests/_files/temp/.keep
No changes.
View
9 application/tests/config.ini.changeme
@@ -6,7 +6,14 @@ db.password = ""
db.dbname = ""
paths.imagemagick = ""
-paths.maildir = ""
+
+; A temp directory for the tests to write to. If not set, the tests will use
+; _files/temp under this directory.
+;
+; Warning: the tests wipe this directory on each run, so don't specify a directory
+; that contains anything.
+;
+;paths.tempDir = ""
; The [site] section below will be merged with the main config.ini such that it
; overrides settings in that file. These config settings do not need to change
View
1 application/tests/suite/Controllers/UsersControllerTest.php
@@ -20,7 +20,6 @@ public function setUp()
$this->mailHelper = Omeka_Test_Helper_Mail::factory();
$this->email = 'user@example.com';
- $this->mailHelper->reset();
}
public function testAddingNewUserSendsActivationEmail()
View
36 application/tests/suite/Omeka/Storage/Adapter/FilesystemTest.php
@@ -7,6 +7,14 @@ class Omeka_Storage_Adapter_FilesystemTest extends PHPUnit_Framework_TestCase
'webDir' => '/foobar',
);
+ public static $tempDir;
+
+ public static function setUpBeforeClass()
+ {
+ $tempDirResource = new Omeka_Test_Resource_Tempdir;
+ self::$tempDir = $tempDirResource->init();
+ }
+
public function testDefaultLocalDir()
{
$storage = new Omeka_Storage_Adapter_Filesystem();
@@ -47,7 +55,7 @@ public function testCanStore()
{
$cantStore = new Omeka_Storage_Adapter_Filesystem($this->_options);
$this->assertFalse($cantStore->canStore());
- $tempDir = sys_get_temp_dir();
+ $tempDir = self::$tempDir;
$canStore = new Omeka_Storage_Adapter_Filesystem(array(
'localDir' => $tempDir,
));
@@ -58,7 +66,7 @@ public function testCanStore()
public static function localDirs()
{
return array(
- array(sys_get_temp_dir(), false),
+ array(null, false),
array('/foo/bar' . mt_rand(), true),
);
}
@@ -68,10 +76,14 @@ public static function localDirs()
*/
public function testMove($localDir, $throwsException)
{
+ if (!$localDir) {
+ $localDir = self::$tempDir;
+ }
+
$storage = new Omeka_Storage_Adapter_Filesystem(array(
'localDir' => $localDir,
));
- $testFile = tempnam($localDir, 'omeka_storage_filesystem_test');
+ $testFile = tempnam(self::$tempDir, 'omeka_storage_filesystem_test');
try {
$storage->move(basename($testFile), 'foo.txt');
$this->assertTrue(file_exists("$localDir/foo.txt"));
@@ -90,10 +102,14 @@ public function testMove($localDir, $throwsException)
*/
public function testStore($localDir, $throwsException)
{
+ if (!$localDir) {
+ $localDir = self::$tempDir;
+ }
+
$storage = new Omeka_Storage_Adapter_Filesystem(array(
'localDir' => $localDir,
));
- $testFile = tempnam(sys_get_temp_dir(), 'omeka_storage_filesystem_test');
+ $testFile = tempnam(self::$tempDir, 'omeka_storage_filesystem_test');
try {
$storage->store($testFile, 'foo.txt');
$this->assertTrue(file_exists("$localDir/foo.txt"));
@@ -109,7 +125,7 @@ public function testStore($localDir, $throwsException)
public function testDelete()
{
- $tempDir = sys_get_temp_dir();
+ $tempDir = self::$tempDir;
$storage = new Omeka_Storage_Adapter_Filesystem(array(
'localDir' => $tempDir,
));
@@ -128,7 +144,7 @@ public function testDelete()
public static function notWritable()
{
return array(
- array('store', array(self::_getWritableFile(),
+ array('store', array(self::_getRandomFilename(),
self::_getRandomFilename())),
array('move', array(self::_getRandomFilename(),
self::_getRandomFilename())),
@@ -153,12 +169,4 @@ private static function _getRandomFilename()
{
return 'foo.txt' . mt_rand();
}
-
- private static function _getWritableFile($dir = null)
- {
- if ($dir === null) {
- $dir = sys_get_temp_dir();
- }
- return tempnam($dir, 'omeka_storage_filesystem_test');
- }
}

0 comments on commit b547156

Please sign in to comment.
Something went wrong with that request. Please try again.