You've already forked ugly-queue
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d20106a021 | |||
| 97dba637d9 | |||
| 793edc2d60 | |||
| 1e98b0cf7e | |||
| 89c51468eb | |||
| d33fd6bcba | |||
| 89cb1ae4b1 |
@@ -17,6 +17,11 @@
|
|||||||
<exclude>./tests/misc</exclude>
|
<exclude>./tests/misc</exclude>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
|
|
||||||
|
<testsuite name="UglyQueueManager">
|
||||||
|
<directory>./tests/UglyQueueManager</directory>
|
||||||
|
<exclude>./tests/misc</exclude>
|
||||||
|
</testsuite>
|
||||||
|
|
||||||
<filter>
|
<filter>
|
||||||
<whitelist addUncoveredFilesFromWhitelist="true">
|
<whitelist addUncoveredFilesFromWhitelist="true">
|
||||||
<directory suffix=".php">./src</directory>
|
<directory suffix=".php">./src</directory>
|
||||||
|
|||||||
@@ -5,159 +5,89 @@ use DCarbone\Helpers\FileHelper;
|
|||||||
/**
|
/**
|
||||||
* Class UglyQueue
|
* Class UglyQueue
|
||||||
* @package DCarbone
|
* @package DCarbone
|
||||||
|
*
|
||||||
|
* @property string name
|
||||||
|
* @property string path
|
||||||
|
* @property bool locked
|
||||||
*/
|
*/
|
||||||
class UglyQueue
|
class UglyQueue implements \Serializable, \SplSubject
|
||||||
{
|
{
|
||||||
|
const NOTIFY_QUEUE_INITIALIZED = 0;
|
||||||
|
const NOTIFY_QUEUE_LOCKED = 1;
|
||||||
|
const NOTIFY_QUEUE_FAILED_TO_LOCK = 2;
|
||||||
|
const NOTIFY_QUEUE_LOCKED_BY_OTHER_PROCESS = 3;
|
||||||
|
const NOTIFY_QUEUE_UNLOCKED = 4;
|
||||||
|
const NOTIFY_QUEUE_PROCESSING = 5;
|
||||||
|
const NOTIFY_QUEUE_REACHED_END = 6;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
public $notifyStatus;
|
||||||
|
|
||||||
|
const QUEUE_READONLY = 0;
|
||||||
|
const QUEUE_READWRITE = 1;
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
protected $config;
|
private $observers = array();
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
protected $mode = null;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $queueBaseDir;
|
protected $_name;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $queueGroup = null;
|
protected $_path;
|
||||||
|
|
||||||
/** @var string */
|
|
||||||
protected $queueGroupDirPath = null;
|
|
||||||
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
protected $haveLock = false;
|
protected $_locked = false;
|
||||||
|
|
||||||
/** @var bool */
|
|
||||||
protected $init = false;
|
|
||||||
|
|
||||||
/** @var resource */
|
/** @var resource */
|
||||||
protected $_tmpHandle;
|
protected $_tmpHandle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $config
|
* @param string $directoryPath
|
||||||
|
* @param array $observers
|
||||||
* @throws \RuntimeException
|
* @throws \RuntimeException
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
|
* @return UglyQueue
|
||||||
*/
|
*/
|
||||||
public function __construct(array $config)
|
public static function queueWithDirectoryPathAndObservers($directoryPath, array $observers = array())
|
||||||
{
|
{
|
||||||
if (!isset($config['queue-base-dir']))
|
if (!is_string($directoryPath))
|
||||||
throw new \InvalidArgumentException('UglyQueue::__construct - "$config" parameter "queue-base-dir" not seen.');
|
throw new \InvalidArgumentException('Argument 1 expected to be string, '.gettype($directoryPath).' seen');
|
||||||
|
|
||||||
if (!is_dir($config['queue-base-dir']) || !is_writable($config['queue-base-dir']))
|
if (($directoryPath = trim($directoryPath)) === '')
|
||||||
throw new \RuntimeException('UglyQueue::__construct - "$config[\'queue-base-dir\']" points to a directory that either doesn\'t exist or is not writable');
|
throw new \InvalidArgumentException('Empty string passed for argument 1');
|
||||||
|
|
||||||
$this->config = $config;
|
if (file_exists($directoryPath))
|
||||||
|
{
|
||||||
$this->queueBaseDir = $this->config['queue-base-dir'];
|
if (!is_dir($directoryPath))
|
||||||
|
throw new \RuntimeException('Argument 1 expected to be path to directory, path to non-directory seen');
|
||||||
|
}
|
||||||
|
else if (!@mkdir($directoryPath))
|
||||||
|
{
|
||||||
|
throw new \RuntimeException('Unable to create queue directory at path: "'.$directoryPath.'".');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
$uglyQueue = new UglyQueue();
|
||||||
* Destructor
|
$uglyQueue->observers = $observers;
|
||||||
*/
|
|
||||||
public function __destruct()
|
|
||||||
{
|
|
||||||
$this->unlock();
|
|
||||||
$this->_populateQueue();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
$split = preg_split('#[/\\\]+#', $directoryPath);
|
||||||
* @param int $ttl Time to live in seconds
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function lock($ttl = 250)
|
|
||||||
{
|
|
||||||
if (!is_int($ttl))
|
|
||||||
throw new \InvalidArgumentException('UglyQueue::lock - Argument 1 expected to be positive integer, "'.gettype($ttl).'" seen');
|
|
||||||
|
|
||||||
if ($ttl < 0)
|
$uglyQueue->_name = end($split);
|
||||||
throw new \InvalidArgumentException('UglyQueue::lock - Argument 1 expected to be positive integer, "'.$ttl.'" seen');
|
$uglyQueue->_path = rtrim(realpath(implode(DIRECTORY_SEPARATOR, $split)), "/\\").DIRECTORY_SEPARATOR;
|
||||||
|
|
||||||
$already_locked = $this->isLocked();
|
if (is_writable($uglyQueue->_path))
|
||||||
|
$uglyQueue->mode = self::QUEUE_READWRITE;
|
||||||
// If there is no lock, currently
|
else if (is_readable($uglyQueue->_path))
|
||||||
if ($already_locked === false)
|
$uglyQueue->mode = self::QUEUE_READONLY;
|
||||||
return $this->haveLock = $this->createQueueLock($ttl);
|
|
||||||
|
|
||||||
// If we make it this far, there is already a lock in place.
|
|
||||||
return $this->haveLock = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $ttl seconds to live
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
protected function createQueueLock($ttl)
|
|
||||||
{
|
|
||||||
$ok = (bool)@file_put_contents(
|
|
||||||
$this->queueGroupDirPath.'queue.lock',
|
|
||||||
json_encode(array('ttl' => $ttl, 'born' => time())));
|
|
||||||
|
|
||||||
if ($ok !== true)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
$this->haveLock = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close this ugly queue, writing out contents to file.
|
|
||||||
*/
|
|
||||||
public function unlock()
|
|
||||||
{
|
|
||||||
if ($this->haveLock === true)
|
|
||||||
{
|
|
||||||
unlink($this->queueGroupDirPath.'queue.lock');
|
|
||||||
$this->haveLock = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws \RuntimeException
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function isLocked()
|
|
||||||
{
|
|
||||||
if ($this->init === false)
|
|
||||||
throw new \RuntimeException('UglyQueue::isLocked - Must first initialize queue');
|
|
||||||
|
|
||||||
// First check for lock file
|
|
||||||
if (is_file($this->queueGroupDirPath.'queue.lock'))
|
|
||||||
{
|
|
||||||
$lock = json_decode(file_get_contents($this->queueGroupDirPath.'queue.lock'), true);
|
|
||||||
|
|
||||||
// If we have an invalid lock structure, THIS IS BAD.
|
|
||||||
if (!isset($lock['ttl']) || !isset($lock['born']))
|
|
||||||
throw new \RuntimeException('UglyQueue::isLocked - Invalid "queue.lock" file structure seen at "'.$this->queueGroupDirPath.'queue.lock'.'"');
|
|
||||||
|
|
||||||
// Otherwise...
|
|
||||||
$lock_ttl = ((int)$lock['born'] + (int)$lock['ttl']);
|
|
||||||
|
|
||||||
// If we're within the TTL of the lock, assume another thread is already processing.
|
|
||||||
// We'll pick it up on the next go around.
|
|
||||||
if ($lock_ttl > time())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// Else, remove lock file and assume we're good to go!
|
|
||||||
unlink($this->queueGroupDirPath.'queue.lock');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no file, assume not locked.
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $queueGroup
|
|
||||||
*/
|
|
||||||
public function initialize($queueGroup)
|
|
||||||
{
|
|
||||||
$this->queueGroup = $queueGroup;
|
|
||||||
$this->queueGroupDirPath = $this->queueBaseDir.$queueGroup.DIRECTORY_SEPARATOR;
|
|
||||||
|
|
||||||
// Create directory for this queue group
|
|
||||||
if (!is_dir($this->queueGroupDirPath))
|
|
||||||
mkdir($this->queueGroupDirPath);
|
|
||||||
|
|
||||||
// Insert "don't look here" index.html file
|
// Insert "don't look here" index.html file
|
||||||
if (!file_exists($this->queueGroupDirPath.'index.html'))
|
if (!file_exists($uglyQueue->_path.'index.html'))
|
||||||
{
|
{
|
||||||
|
if ($uglyQueue->mode === self::QUEUE_READONLY)
|
||||||
|
throw new \RuntimeException('Cannot initialize queue with name "'.$uglyQueue->_name.'", the user lacks permission to create files.');
|
||||||
|
|
||||||
$html = <<<HTML
|
$html = <<<HTML
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
@@ -168,13 +98,153 @@ class UglyQueue
|
|||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
HTML;
|
HTML;
|
||||||
file_put_contents($this->queueGroupDirPath.'index.html', $html);
|
file_put_contents($uglyQueue->_path.'index.html', $html);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!file_exists($this->queueGroupDirPath.'queue.txt'))
|
if (!file_exists($uglyQueue->_path.'queue.txt'))
|
||||||
file_put_contents($this->queueGroupDirPath.'queue.txt', '');
|
{
|
||||||
|
if ($uglyQueue->mode === self::QUEUE_READONLY)
|
||||||
|
throw new \RuntimeException('Cannot initialize queue with name "'.$uglyQueue->_name.'", the user lacks permission to create files.');
|
||||||
|
|
||||||
$this->init = true;
|
file_put_contents($uglyQueue->_path.'queue.txt', '');
|
||||||
|
}
|
||||||
|
|
||||||
|
$uglyQueue->notifyStatus = self::NOTIFY_QUEUE_INITIALIZED;
|
||||||
|
$uglyQueue->notify();
|
||||||
|
|
||||||
|
return $uglyQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $param
|
||||||
|
* @return string
|
||||||
|
* @throws \OutOfBoundsException
|
||||||
|
*/
|
||||||
|
public function __get($param)
|
||||||
|
{
|
||||||
|
switch($param)
|
||||||
|
{
|
||||||
|
case 'name' :
|
||||||
|
return $this->_name;
|
||||||
|
|
||||||
|
case 'path':
|
||||||
|
return $this->_path;
|
||||||
|
|
||||||
|
case 'locked':
|
||||||
|
return $this->_locked;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new \OutOfBoundsException(get_class($this).' does not have a property named "'.$param.'".');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destructor
|
||||||
|
*/
|
||||||
|
public function __destruct()
|
||||||
|
{
|
||||||
|
$this->_populateQueue();
|
||||||
|
$this->unlock();
|
||||||
|
file_put_contents($this->_path.UglyQueueManager::UGLY_QUEUE_SERIALIZED_NAME, serialize($this));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $ttl Time to live in seconds
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function lock($ttl = 250)
|
||||||
|
{
|
||||||
|
if (!is_int($ttl))
|
||||||
|
throw new \InvalidArgumentException('Argument 1 expected to be integer, "'.gettype($ttl).'" seen');
|
||||||
|
|
||||||
|
if ($ttl < 0)
|
||||||
|
throw new \InvalidArgumentException('Argument 1 expected to be positive integer, "'.$ttl.'" seen');
|
||||||
|
|
||||||
|
$alreadyLocked = $this->isLocked();
|
||||||
|
|
||||||
|
// If there is currently no lock
|
||||||
|
if ($alreadyLocked === false)
|
||||||
|
return $this->createLockFile($ttl);
|
||||||
|
|
||||||
|
// If we make it this far, there is already a lock in place.
|
||||||
|
$this->_locked = false;
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_LOCKED_BY_OTHER_PROCESS;
|
||||||
|
$this->notify();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $ttl seconds to live
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function createLockFile($ttl)
|
||||||
|
{
|
||||||
|
$ok = (bool)@file_put_contents(
|
||||||
|
$this->_path.'queue.lock',
|
||||||
|
json_encode(array('ttl' => $ttl, 'born' => time())));
|
||||||
|
|
||||||
|
if ($ok !== true)
|
||||||
|
{
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_FAILED_TO_LOCK;
|
||||||
|
$this->notify();
|
||||||
|
return $this->_locked = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->_locked = true;
|
||||||
|
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_LOCKED;
|
||||||
|
$this->notify();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close this ugly queue, writing out contents to file.
|
||||||
|
*/
|
||||||
|
public function unlock()
|
||||||
|
{
|
||||||
|
if ($this->_locked === true)
|
||||||
|
{
|
||||||
|
unlink($this->_path.'queue.lock');
|
||||||
|
$this->_locked = false;
|
||||||
|
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_UNLOCKED;
|
||||||
|
$this->notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \RuntimeException
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isLocked()
|
||||||
|
{
|
||||||
|
// First check for lock file
|
||||||
|
if (is_file($this->_path.'queue.lock'))
|
||||||
|
{
|
||||||
|
$lock = json_decode(file_get_contents($this->_path.'queue.lock'), true);
|
||||||
|
|
||||||
|
// If we have an invalid lock structure.
|
||||||
|
if (!isset($lock['ttl']) || !isset($lock['born']))
|
||||||
|
throw new \RuntimeException('Invalid "queue.lock" file structure seen at "'.$this->_path.'queue.lock".');
|
||||||
|
|
||||||
|
// Otherwise...
|
||||||
|
$lock_ttl = ((int)$lock['born'] + (int)$lock['ttl']);
|
||||||
|
|
||||||
|
// If we're within the TTL of the lock, assume another thread is already processing.
|
||||||
|
// We'll pick it up on the next go around.
|
||||||
|
if ($lock_ttl > time())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// Else, remove lock file and assume we're good to go!
|
||||||
|
unlink($this->_path.'queue.lock');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no file, assume not locked.
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -185,30 +255,36 @@ HTML;
|
|||||||
*/
|
*/
|
||||||
public function processQueue($count = 1)
|
public function processQueue($count = 1)
|
||||||
{
|
{
|
||||||
if ($this->init === false)
|
if ($this->mode === self::QUEUE_READONLY)
|
||||||
throw new \RuntimeException('UglyQueue::processQueue - Must first initialize queue!');
|
throw new \RuntimeException('Queue "'.$this->_name.'" cannot be processed. It was started in Read-Only mode (the user running this process does not have permission to write to the queue directory).');
|
||||||
|
|
||||||
// If we don't have a lock, assume issue and move on.
|
// If we don't have a lock, assume issue and move on.
|
||||||
if ($this->haveLock === false)
|
if ($this->_locked === false)
|
||||||
throw new \RuntimeException('UglyQueue::processQueue - Cannot process queue locked by another process');
|
throw new \RuntimeException('Cannot process queue named "'.$this->_name.'". It is locked by another process.');
|
||||||
|
|
||||||
// If non-int valid is passed
|
// If non-int valid is passed
|
||||||
if (!is_int($count))
|
if (!is_int($count))
|
||||||
throw new \InvalidArgumentException('UglyQueue::processQueue - Argument 1 expected to be integer greater than 0, "'.gettype($count).'" seen');
|
throw new \InvalidArgumentException('Argument 1 expected to be integer greater than 0, "'.gettype($count).'" seen');
|
||||||
|
|
||||||
// If negative integer passed
|
// If negative integer passed
|
||||||
if ($count <= 0)
|
if ($count <= 0)
|
||||||
throw new \InvalidArgumentException('UglyQueue::processQueue - Argument 1 expected to be integer greater than 0, "'.$count.'" seen');
|
throw new \InvalidArgumentException('Argument 1 expected to be integer greater than 0, "'.$count.'" seen');
|
||||||
|
|
||||||
|
if ($this->notifyStatus !== self::NOTIFY_QUEUE_PROCESSING)
|
||||||
|
{
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_PROCESSING;
|
||||||
|
$this->notify();
|
||||||
|
}
|
||||||
|
|
||||||
// Find number of lines in the queue file
|
// Find number of lines in the queue file
|
||||||
$lineCount = FileHelper::getLineCount($this->queueGroupDirPath.'queue.txt');
|
$lineCount = FileHelper::getLineCount($this->_path.'queue.txt');
|
||||||
|
|
||||||
// If queue line count is 0, assume empty
|
// If queue line count is 0, assume empty
|
||||||
if ($lineCount === 0)
|
if ($lineCount === 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Try to open the file for reading / writing.
|
// Try to open the file for reading / writing.
|
||||||
$queueFileHandle = fopen($this->queueGroupDirPath.'queue.txt', 'r+');
|
$queueFileHandle = fopen($this->_path.'queue.txt', 'r+');
|
||||||
if ($queueFileHandle === false)
|
if ($queueFileHandle === false)
|
||||||
$this->unlock();
|
$this->unlock();
|
||||||
|
|
||||||
@@ -231,11 +307,14 @@ HTML;
|
|||||||
rewind($queueFileHandle);
|
rewind($queueFileHandle);
|
||||||
ftruncate($queueFileHandle, 0);
|
ftruncate($queueFileHandle, 0);
|
||||||
fclose($queueFileHandle);
|
fclose($queueFileHandle);
|
||||||
|
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_REACHED_END;
|
||||||
|
$this->notify();
|
||||||
}
|
}
|
||||||
// Otherwise, create new queue file minus the processed lines.
|
// Otherwise, create new queue file minus the processed lines.
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$tmp = fopen($this->queueGroupDirPath.'queue.tmp', 'w+');
|
$tmp = fopen($this->_path.'queue.tmp', 'w+');
|
||||||
rewind($queueFileHandle);
|
rewind($queueFileHandle);
|
||||||
$i = 0;
|
$i = 0;
|
||||||
while (($line = fgets($queueFileHandle)) !== false && $i < $start_line)
|
while (($line = fgets($queueFileHandle)) !== false && $i < $start_line)
|
||||||
@@ -248,8 +327,8 @@ HTML;
|
|||||||
|
|
||||||
fclose($queueFileHandle);
|
fclose($queueFileHandle);
|
||||||
fclose($tmp);
|
fclose($tmp);
|
||||||
unlink($this->queueGroupDirPath.'queue.txt');
|
unlink($this->_path.'queue.txt');
|
||||||
rename($this->queueGroupDirPath.'queue.tmp', $this->queueGroupDirPath.'queue.txt');
|
rename($this->_path.'queue.tmp', $this->_path.'queue.txt');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
@@ -263,18 +342,18 @@ HTML;
|
|||||||
*/
|
*/
|
||||||
public function addToQueue($key, $value)
|
public function addToQueue($key, $value)
|
||||||
{
|
{
|
||||||
if ($this->init === false)
|
if ($this->mode === self::QUEUE_READONLY)
|
||||||
throw new \RuntimeException('UglyQueue::addToQueue - Must first initialize queue!');
|
throw new \RuntimeException('Cannot add items to queue "'.$this->_name.'" as it is in read-only mode');
|
||||||
|
|
||||||
// If we don't have a lock, assume issue and move on.
|
// If we don't have a lock, assume issue and move on.
|
||||||
if ($this->haveLock === false)
|
if ($this->_locked === false)
|
||||||
throw new \RuntimeException('UglyQueue::addToQueue - You do not have a lock on this queue');
|
throw new \RuntimeException('Cannot add items to queue "'.$this->_name.'". Queue is already locked by another process');
|
||||||
|
|
||||||
if (!is_resource($this->_tmpHandle))
|
if (!is_resource($this->_tmpHandle))
|
||||||
{
|
{
|
||||||
$this->_tmpHandle = fopen($this->queueGroupDirPath.'queue.tmp', 'w+');
|
$this->_tmpHandle = fopen($this->_path.'queue.tmp', 'w+');
|
||||||
if ($this->_tmpHandle === false)
|
if ($this->_tmpHandle === false)
|
||||||
throw new \RuntimeException('UglyQueue::addToQueue - Unable to create "queue.tmp" file');
|
throw new \RuntimeException('Unable to create "queue.tmp" file.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_array($value) || $value instanceof \stdClass)
|
if (is_array($value) || $value instanceof \stdClass)
|
||||||
@@ -295,9 +374,9 @@ HTML;
|
|||||||
{
|
{
|
||||||
if (is_resource($this->_tmpHandle))
|
if (is_resource($this->_tmpHandle))
|
||||||
{
|
{
|
||||||
if (file_exists($this->queueGroupDirPath.'queue.txt'))
|
if (file_exists($this->_path.'queue.txt'))
|
||||||
{
|
{
|
||||||
$queueFileHandle = fopen($this->queueGroupDirPath.'queue.txt', 'r+');
|
$queueFileHandle = fopen($this->_path.'queue.txt', 'r+');
|
||||||
while (($line = fgets($queueFileHandle)) !== false)
|
while (($line = fgets($queueFileHandle)) !== false)
|
||||||
{
|
{
|
||||||
if ($line !== "\n" && $line !== "")
|
if ($line !== "\n" && $line !== "")
|
||||||
@@ -305,11 +384,11 @@ HTML;
|
|||||||
}
|
}
|
||||||
|
|
||||||
fclose($queueFileHandle);
|
fclose($queueFileHandle);
|
||||||
unlink($this->queueGroupDirPath.'queue.txt');
|
unlink($this->_path.'queue.txt');
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose($this->_tmpHandle);
|
fclose($this->_tmpHandle);
|
||||||
rename($this->queueGroupDirPath.'queue.tmp', $this->queueGroupDirPath.'queue.txt');
|
rename($this->_path.'queue.tmp', $this->_path.'queue.txt');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -319,10 +398,7 @@ HTML;
|
|||||||
*/
|
*/
|
||||||
public function getQueueItemCount()
|
public function getQueueItemCount()
|
||||||
{
|
{
|
||||||
if ($this->init === false)
|
return FileHelper::getLineCount($this->_path.'queue.txt');
|
||||||
throw new \RuntimeException('UglyQueue::getQueueItemCount - Must first initialize queue');
|
|
||||||
|
|
||||||
return FileHelper::getLineCount($this->queueGroupDirPath.'queue.txt');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -332,19 +408,16 @@ HTML;
|
|||||||
*/
|
*/
|
||||||
public function keyExistsInQueue($key)
|
public function keyExistsInQueue($key)
|
||||||
{
|
{
|
||||||
if ($this->init === false)
|
|
||||||
throw new \RuntimeException('UglyQueue::keyExistsInQueue - Must first initialize queue');
|
|
||||||
|
|
||||||
$key = (string)$key;
|
$key = (string)$key;
|
||||||
|
|
||||||
// Try to open the file for reading / writing.
|
// Try to open the file for reading / writing.
|
||||||
$queueFileHandle = fopen($this->queueGroupDirPath.'queue.txt', 'r');
|
$queueFileHandle = fopen($this->_path.'queue.txt', 'r');
|
||||||
|
|
||||||
while(($line = fscanf($queueFileHandle, "%s\t")) !== false)
|
while(($line = fscanf($queueFileHandle, "%s\t%s\n")) !== false)
|
||||||
{
|
{
|
||||||
list($queueKey) = $line;
|
list ($lineKey, $lineValue) = $line;
|
||||||
|
|
||||||
if ($key === $queueKey)
|
if ($key === $lineKey)
|
||||||
{
|
{
|
||||||
fclose($queueFileHandle);
|
fclose($queueFileHandle);
|
||||||
return true;
|
return true;
|
||||||
@@ -356,34 +429,74 @@ HTML;
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return boolean
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* String representation of object
|
||||||
|
* @link http://php.net/manual/en/serializable.serialize.php
|
||||||
|
*
|
||||||
|
* @return string the string representation of the object or null
|
||||||
*/
|
*/
|
||||||
public function getInit()
|
public function serialize()
|
||||||
{
|
{
|
||||||
return $this->init;
|
return serialize(array($this->_name, $this->_path));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Constructs the object
|
||||||
|
* @link http://php.net/manual/en/serializable.unserialize.php
|
||||||
|
*
|
||||||
|
* @param string $serialized The string representation of the object.
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function getQueueBaseDir()
|
public function unserialize($serialized)
|
||||||
{
|
{
|
||||||
return $this->queueBaseDir;
|
/** @var \DCarbone\UglyQueue $uglyQueue */
|
||||||
|
$data = unserialize($serialized);
|
||||||
|
$this->_name = $data[0];
|
||||||
|
$this->_path = $data[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Attach an SplObserver
|
||||||
|
* @link http://php.net/manual/en/splsubject.attach.php
|
||||||
|
*
|
||||||
|
* @param \SplObserver $observer The SplObserver to attach.
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function getQueueGroupDirPath()
|
public function attach(\SplObserver $observer)
|
||||||
{
|
{
|
||||||
return $this->queueGroupDirPath;
|
if (!in_array($observer, $this->observers))
|
||||||
|
$this->observers[] = $observer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Detach an observer
|
||||||
|
* @link http://php.net/manual/en/splsubject.detach.php
|
||||||
|
*
|
||||||
|
* @param \SplObserver $observer The SplObserver to detach.
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function getQueueGroup()
|
public function detach(\SplObserver $observer)
|
||||||
{
|
{
|
||||||
return $this->queueGroup;
|
$idx = array_search($observer, $this->observers, true);
|
||||||
|
if ($idx !== false)
|
||||||
|
unset($this->observers[$idx]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Notify an observer
|
||||||
|
* @link http://php.net/manual/en/splsubject.notify.php
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function notify()
|
||||||
|
{
|
||||||
|
for ($i = 0, $count = count($this->observers); $i < $count; $i++)
|
||||||
|
{
|
||||||
|
$this->observers[$i]->notify($this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
230
src/UglyQueueManager.php
Normal file
230
src/UglyQueueManager.php
Normal file
@@ -0,0 +1,230 @@
|
|||||||
|
<?php namespace DCarbone;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UglyQueueManager
|
||||||
|
* @package DCarbone
|
||||||
|
*/
|
||||||
|
class UglyQueueManager implements \SplObserver, \SplSubject
|
||||||
|
{
|
||||||
|
const NOTIFY_MANAGER_INITIALIZED = 0;
|
||||||
|
const NOTIFY_QUEUE_ADDED = 1;
|
||||||
|
const NOTIFY_QUEUE_REMOVED = 2;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
public $notifyStatus;
|
||||||
|
|
||||||
|
const UGLY_QUEUE_SERIALIZED_NAME = 'ugly-queue.obj';
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
private $observers = array();
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
protected $queues = array();
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
protected $config = array();
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $queueBaseDir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param array $config
|
||||||
|
* @param array $observers
|
||||||
|
* @throws \RuntimeException
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
protected function __construct(array $config, array $observers = array())
|
||||||
|
{
|
||||||
|
if (!isset($config['queue-base-dir']))
|
||||||
|
throw new \InvalidArgumentException('"$config" parameter "queue-base-dir" not seen.');
|
||||||
|
|
||||||
|
if (!is_dir($config['queue-base-dir']))
|
||||||
|
throw new \RuntimeException('"queue-base-dir" points to a directory that does not exist.');
|
||||||
|
|
||||||
|
$this->config = $config;
|
||||||
|
$this->queueBaseDir = rtrim(realpath($this->config['queue-base-dir']), "/\\").DIRECTORY_SEPARATOR;
|
||||||
|
$this->observers = $observers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $config
|
||||||
|
* @param array $observers
|
||||||
|
* @return UglyQueueManager
|
||||||
|
*/
|
||||||
|
public static function init(array $config, array $observers = array())
|
||||||
|
{
|
||||||
|
/** @var \DCarbone\UglyQueueManager $manager */
|
||||||
|
$manager = new static($config, $observers);
|
||||||
|
|
||||||
|
/** @var \DCarbone\UglyQueue $uglyQueue */
|
||||||
|
|
||||||
|
foreach(glob($manager->queueBaseDir.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $queueDir)
|
||||||
|
{
|
||||||
|
// Try to avoid looking at hidden directories or magic dirs such as '.' and '..'
|
||||||
|
$split = preg_split('#[/\\\]+#', $queueDir);
|
||||||
|
if (strpos(end($split), '.') === 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (file_exists($queueDir.DIRECTORY_SEPARATOR.self::UGLY_QUEUE_SERIALIZED_NAME))
|
||||||
|
$uglyQueue = unserialize(file_get_contents($queueDir.DIRECTORY_SEPARATOR.self::UGLY_QUEUE_SERIALIZED_NAME));
|
||||||
|
else
|
||||||
|
$uglyQueue = UglyQueue::queueWithDirectoryPathAndObservers($queueDir, $manager->observers);
|
||||||
|
|
||||||
|
$manager->addQueue($uglyQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
$manager->notifyStatus = self::NOTIFY_MANAGER_INITIALIZED;
|
||||||
|
$manager->notify();
|
||||||
|
|
||||||
|
return $manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
* @throws \RuntimeException
|
||||||
|
*/
|
||||||
|
public function addQueue(UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
if ($this->containsQueueWithName($uglyQueue->name))
|
||||||
|
throw new \RuntimeException('Queue named "'.$uglyQueue->name.'" already exists in this manager.');
|
||||||
|
|
||||||
|
$this->queues[$uglyQueue->name] = $uglyQueue;
|
||||||
|
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_ADDED;
|
||||||
|
$this->notify();
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function addQueueAtPath($path)
|
||||||
|
{
|
||||||
|
$uglyQueue = UglyQueue::queueWithDirectoryPathAndObservers($path, $this->observers);
|
||||||
|
|
||||||
|
return $this->addQueue($uglyQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function removeQueue(UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
if ($this->containsQueueWithName($uglyQueue->name))
|
||||||
|
unset($this->queues[$uglyQueue->name]);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function removeQueueByName($name)
|
||||||
|
{
|
||||||
|
if ($this->containsQueueWithName($name))
|
||||||
|
{
|
||||||
|
unset($this->queues[$name]);
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_REMOVED;
|
||||||
|
$this->notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return \DCarbone\UglyQueue
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function &getQueueWithName($name)
|
||||||
|
{
|
||||||
|
if (isset($this->queues[$name]))
|
||||||
|
return $this->queues[$name];
|
||||||
|
|
||||||
|
throw new \InvalidArgumentException('Argument 1 expected to be valid queue name.');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function containsQueueWithName($name)
|
||||||
|
{
|
||||||
|
return isset($this->queues[$name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getQueueList()
|
||||||
|
{
|
||||||
|
return array_keys($this->queues);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Receive update from subject
|
||||||
|
* @link http://php.net/manual/en/splobserver.update.php
|
||||||
|
*
|
||||||
|
* @param \SplSubject $subject The SplSubject notifying the observer of an update.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function update(\SplSubject $subject)
|
||||||
|
{
|
||||||
|
for ($i = 0, $count = count($this->observers); $i < $count; $i++)
|
||||||
|
{
|
||||||
|
$this->observers[$i]->notify($subject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Attach an SplObserver
|
||||||
|
* @link http://php.net/manual/en/splsubject.attach.php
|
||||||
|
*
|
||||||
|
* @param \SplObserver $observer The SplObserver to attach.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function attach(\SplObserver $observer)
|
||||||
|
{
|
||||||
|
if (!in_array($observer, $this->observers, true))
|
||||||
|
$this->observers[] = $observer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Detach an observer
|
||||||
|
* @link http://php.net/manual/en/splsubject.detach.php
|
||||||
|
*
|
||||||
|
* @param \SplObserver $observer The SplObserver to detach.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function detach(\SplObserver $observer)
|
||||||
|
{
|
||||||
|
$idx = array_search($observer, $this->observers, true);
|
||||||
|
if ($idx !== false)
|
||||||
|
unset($this->observers[$idx]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Notify an observer
|
||||||
|
* @link http://php.net/manual/en/splsubject.notify.php
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function notify()
|
||||||
|
{
|
||||||
|
for ($i = 0, $count = count($this->observers); $i < $count; $i++)
|
||||||
|
{
|
||||||
|
$this->observers[$i]->notify($this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,17 +27,13 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::__construct
|
* @covers \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @return \DCarbone\UglyQueue
|
* @return \DCarbone\UglyQueue
|
||||||
*/
|
*/
|
||||||
public function testCanConstructUglyQueueWithValidParameter()
|
public function testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers()
|
||||||
{
|
{
|
||||||
$conf = array(
|
$uglyQueue = \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers(dirname(__DIR__).'/misc/queues/tasty-sandwich');
|
||||||
'queue-base-dir' => dirname(__DIR__).'/misc/',
|
|
||||||
);
|
|
||||||
|
|
||||||
$uglyQueue = new \DCarbone\UglyQueue($conf);
|
|
||||||
|
|
||||||
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
||||||
|
|
||||||
@@ -45,164 +41,19 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::__construct
|
* @covers \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function testExceptionThrownWhenConstructingUglyQueueWithEmptyOrInvalidConf()
|
public function testExceptionThrownWhenInitializingUglyQueueWithEmptyOrInvalidConf()
|
||||||
{
|
{
|
||||||
$conf = array();
|
$uglyQueue = \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers(array());
|
||||||
$uglyQueue = new \DCarbone\UglyQueue($conf);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::__construct
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @expectedException \RuntimeException
|
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenConstructingUglyQueueWithInvalidQueueBaseDirPath()
|
|
||||||
{
|
|
||||||
$conf = array(
|
|
||||||
'queue-base-dir' => 'sandwiches',
|
|
||||||
);
|
|
||||||
|
|
||||||
$uglyQueue = new \DCarbone\UglyQueue($conf);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::getQueueBaseDir
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testCanGetQueueBaseDir(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$queueBaseDir = $uglyQueue->getQueueBaseDir();
|
|
||||||
|
|
||||||
$this->assertFileExists(
|
|
||||||
$queueBaseDir,
|
|
||||||
'Could not verify that Queue Base Dir exists');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::getQueueGroup
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testGetQueueGroupReturnsNullBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$queueGroup = $uglyQueue->getQueueGroup();
|
|
||||||
|
|
||||||
$this->assertNull($queueGroup);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::getInit
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testGetInitReturnsFalseBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$init = $uglyQueue->getInit();
|
|
||||||
$this->assertFalse($init);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::getQueueGroupDirPath
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testGetQueueGroupDirPathReturnsNullBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$queueGroupDir = $uglyQueue->getQueueGroupDirPath();
|
|
||||||
|
|
||||||
$this->assertNull($queueGroupDir);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::isLocked
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @expectedException \RuntimeException
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenCallingIsLockedOnUninitializedQueue(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$isLocked = $uglyQueue->isLocked();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::addToQueue
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @expectedException \RuntimeException
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenTryingToAddItemsToUninitializedQueue(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$addToQueue = $uglyQueue->addToQueue('test', 'value');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::getQueueItemCount
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @expectedException \RuntimeException
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenTryingToGetCountOfItemsInQueueBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$itemCount = $uglyQueue->getQueueItemCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::keyExistsInQueue
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @expectedException \RuntimeException
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenTryingToFindKeyBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$keyExists = $uglyQueue->keyExistsInQueue(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::processQueue
|
* @covers \DCarbone\UglyQueue::processQueue
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
* @expectedException \RuntimeException
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenTryingToProcessQueueBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$process = $uglyQueue->processQueue();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::initialize
|
|
||||||
* @covers \DCarbone\UglyQueue::getInit
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
* @return \DCarbone\UglyQueue
|
|
||||||
*/
|
|
||||||
public function testCanInitializeNewUglyQueue(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$uglyQueue->initialize('tasty-sandwich');
|
|
||||||
|
|
||||||
$this->assertTrue($uglyQueue->getInit());
|
|
||||||
|
|
||||||
return $uglyQueue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::processQueue
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanInitializeNewUglyQueue
|
|
||||||
* @expectedException \RuntimeException
|
* @expectedException \RuntimeException
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
@@ -214,7 +65,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::keyExistsInQueue
|
* @covers \DCarbone\UglyQueue::keyExistsInQueue
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeNewUglyQueue
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testKeyExistsInQueueReturnsFalseWithEmptyQueueAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
public function testKeyExistsInQueueReturnsFalseWithEmptyQueueAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
||||||
@@ -227,60 +78,73 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::addToQueue
|
* @covers \DCarbone\UglyQueue::addToQueue
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeNewUglyQueue
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
* @expectedException \RuntimeException
|
* @expectedException \RuntimeException
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLockAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLock(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$addToQueue = $uglyQueue->addToQueue('test', 'value');
|
$addToQueue = $uglyQueue->addToQueue('test', 'value');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::getInit
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeNewUglyQueue
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testGetInitReturnsTrueAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
public function testCanGetQueueDirectory(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$init = $uglyQueue->getInit();
|
$queuePath = $uglyQueue->path;
|
||||||
$this->assertTrue($init);
|
|
||||||
|
$this->assertFileExists($queuePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::getQueueGroupDirPath
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeNewUglyQueue
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testCanGetQueueGroupDirectoryAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
public function testCanGetQueueName(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$queueGroupDir = $uglyQueue->getQueueGroupDirPath();
|
$queueName = $uglyQueue->name;
|
||||||
|
|
||||||
$this->assertFileExists($queueGroupDir);
|
$this->assertEquals('tasty-sandwich', $queueName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::getQueueGroup
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeNewUglyQueue
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testCanGetQueueGroupAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
public function testCanGetQueueLockedStatus(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$queueGroup = $uglyQueue->getQueueGroup();
|
$locked = $uglyQueue->locked;
|
||||||
|
|
||||||
$this->assertEquals('tasty-sandwich', $queueGroup);
|
$this->assertFalse($locked);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @expectedException \OutOfBoundsException
|
||||||
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownWhenAttemptingToGetInvalidProperty(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$sandwich = $uglyQueue->sandwich;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::isLocked
|
* @covers \DCarbone\UglyQueue::isLocked
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeNewUglyQueue
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testIsLockedReturnsFalseBeforeLockingAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
public function testIsLockedReturnsFalseBeforeLocking(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$isLocked = $uglyQueue->isLocked();
|
$isLocked = $uglyQueue->isLocked();
|
||||||
|
|
||||||
@@ -290,45 +154,33 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::getQueueItemCount
|
* @covers \DCarbone\UglyQueue::getQueueItemCount
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeNewUglyQueue
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testGetQueueItemCountReturnsZeroAfterInitializingEmptyQueue(\DCarbone\UglyQueue $uglyQueue)
|
public function testGetQueueItemCountReturnsZeroWithEmptyQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$itemCount = $uglyQueue->getQueueItemCount();
|
$itemCount = $uglyQueue->getQueueItemCount();
|
||||||
$this->assertEquals(0, $itemCount);
|
$this->assertEquals(0, $itemCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::initialize
|
* @covers \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers
|
||||||
* @covers \DCarbone\UglyQueue::__construct
|
|
||||||
* @covers \DCarbone\UglyQueue::getInit
|
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @return \DCarbone\UglyQueue
|
* @return \DCarbone\UglyQueue
|
||||||
*/
|
*/
|
||||||
public function testCanInitializeExistingQueue()
|
public function testCanInitializeExistingQueue()
|
||||||
{
|
{
|
||||||
$conf = array(
|
$uglyQueue = \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers(dirname(__DIR__).'/misc/queues/tasty-sandwich');
|
||||||
'queue-base-dir' => dirname(__DIR__).'/misc/',
|
|
||||||
);
|
|
||||||
|
|
||||||
$uglyQueue = new \DCarbone\UglyQueue($conf);
|
|
||||||
|
|
||||||
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
||||||
|
|
||||||
$this->assertFalse($uglyQueue->getInit());
|
|
||||||
|
|
||||||
$uglyQueue->initialize('tasty-sandwich');
|
|
||||||
|
|
||||||
$this->assertTrue($uglyQueue->getInit());
|
|
||||||
|
|
||||||
return $uglyQueue;
|
return $uglyQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::lock
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
@@ -340,7 +192,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::lock
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
@@ -352,9 +204,9 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::lock
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
* @covers \DCarbone\UglyQueue::isLocked
|
* @covers \DCarbone\UglyQueue::isLocked
|
||||||
* @covers \DCarbone\UglyQueue::createQueueLock
|
* @covers \DCarbone\UglyQueue::createLockFile
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeNewUglyQueue
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
* @return \DCarbone\UglyQueue
|
* @return \DCarbone\UglyQueue
|
||||||
*/
|
*/
|
||||||
@@ -364,7 +216,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertTrue($locked);
|
$this->assertTrue($locked);
|
||||||
|
|
||||||
$queueDir = $uglyQueue->getQueueBaseDir().$uglyQueue->getQueueGroup().'/';
|
$queueDir = $uglyQueue->path;
|
||||||
|
|
||||||
$this->assertFileExists($queueDir.'queue.lock');
|
$this->assertFileExists($queueDir.'queue.lock');
|
||||||
|
|
||||||
@@ -385,7 +237,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
* @depends testCanInitializeExistingQueue
|
* @depends testCanInitializeExistingQueue
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testCannotLockInitializedQueueThatIsAlreadyLocked(\DCarbone\UglyQueue $uglyQueue)
|
public function testCannotLockQueueThatIsAlreadyLocked(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$lock = $uglyQueue->lock();
|
$lock = $uglyQueue->lock();
|
||||||
|
|
||||||
@@ -398,7 +250,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
* @depends testCanLockUglyQueueWithDefaultTTL
|
* @depends testCanLockUglyQueueWithDefaultTTL
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testIsLockedReturnsTrueAfterLockingInitializedQueue(\DCarbone\UglyQueue $uglyQueue)
|
public function testIsLockedReturnsTrueAfterLocking(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$isLocked = $uglyQueue->isLocked();
|
$isLocked = $uglyQueue->isLocked();
|
||||||
$this->assertTrue($isLocked);
|
$this->assertTrue($isLocked);
|
||||||
@@ -416,7 +268,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$uglyQueue->unlock();
|
$uglyQueue->unlock();
|
||||||
|
|
||||||
$queueGroupDir = $uglyQueue->getQueueGroupDirPath();
|
$queueGroupDir = $uglyQueue->path;
|
||||||
|
|
||||||
$this->assertFileNotExists($queueGroupDir.'queue.lock');
|
$this->assertFileNotExists($queueGroupDir.'queue.lock');
|
||||||
|
|
||||||
@@ -459,7 +311,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::lock
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
* @covers \DCarbone\UglyQueue::isLocked
|
* @covers \DCarbone\UglyQueue::isLocked
|
||||||
* @covers \DCarbone\UglyQueue::getQueueGroupDirPath
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanUnlockLockedQueue
|
* @depends testCanUnlockLockedQueue
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
@@ -471,7 +323,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertTrue($locked);
|
$this->assertTrue($locked);
|
||||||
|
|
||||||
$queueDir = $uglyQueue->getQueueGroupDirPath();
|
$queueDir = $uglyQueue->path;
|
||||||
|
|
||||||
$this->assertFileExists($queueDir.'queue.lock');
|
$this->assertFileExists($queueDir.'queue.lock');
|
||||||
|
|
||||||
@@ -501,7 +353,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertTrue($added);
|
$this->assertTrue($added);
|
||||||
}
|
}
|
||||||
|
|
||||||
$groupDir = $uglyQueue->getQueueGroupDirPath();
|
$groupDir = $uglyQueue->path;
|
||||||
|
|
||||||
$this->assertFileExists(
|
$this->assertFileExists(
|
||||||
$groupDir.'queue.tmp',
|
$groupDir.'queue.tmp',
|
||||||
@@ -525,7 +377,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$uglyQueue->_populateQueue();
|
$uglyQueue->_populateQueue();
|
||||||
|
|
||||||
$groupDir = $uglyQueue->getQueueGroupDirPath();
|
$groupDir = $uglyQueue->path;
|
||||||
|
|
||||||
$this->assertFileNotExists($groupDir.'queue.tmp');
|
$this->assertFileNotExists($groupDir.'queue.tmp');
|
||||||
|
|
||||||
|
|||||||
233
tests/UglyQueueManager/UglyQueueManagerTest.php
Normal file
233
tests/UglyQueueManager/UglyQueueManagerTest.php
Normal file
@@ -0,0 +1,233 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UglyQueueManagerTest
|
||||||
|
*/
|
||||||
|
class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected $reallyTastySandwich = array(
|
||||||
|
'0' => 'beef broth',
|
||||||
|
'1' => 'barbeque sauce',
|
||||||
|
'2' => 'boneless pork ribs',
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::__construct
|
||||||
|
* @covers \DCarbone\UglyQueueManager::init
|
||||||
|
* @covers \DCarbone\UglyQueue::unserialize
|
||||||
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
|
* @covers \DCarbone\UglyQueueManager::addQueue
|
||||||
|
* @covers \DCarbone\UglyQueueManager::containsQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function testCanInitializeManagerWithConfigAndNoObservers()
|
||||||
|
{
|
||||||
|
$config = array(
|
||||||
|
'queue-base-dir' => __DIR__.'/../misc/queues'
|
||||||
|
);
|
||||||
|
|
||||||
|
$manager = \DCarbone\UglyQueueManager::init($config);
|
||||||
|
|
||||||
|
$this->assertInstanceOf('\\DCarbone\\UglyQueueManager', $manager);
|
||||||
|
|
||||||
|
return $manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::init
|
||||||
|
* @covers \DCarbone\UglyQueueManager::__construct
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownDuringConstructionWithInvalidBasePathValue()
|
||||||
|
{
|
||||||
|
$config = array(
|
||||||
|
'queue-base-dir' => 'i do not exist!'
|
||||||
|
);
|
||||||
|
|
||||||
|
$manager = \DCarbone\UglyQueueManager::init($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::init
|
||||||
|
* @covers \DCarbone\UglyQueueManager::__construct
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownDuringConstructionWithInvalidConfArray()
|
||||||
|
{
|
||||||
|
$config = array(
|
||||||
|
'wrong-key' => 'wrong value'
|
||||||
|
);
|
||||||
|
|
||||||
|
$manager = \DCarbone\UglyQueueManager::init($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::containsQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanDetermineIfValidQueueExistsInManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$shouldBeTrue = $manager->containsQueueWithName('tasty-sandwich');
|
||||||
|
|
||||||
|
$this->assertTrue($shouldBeTrue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::containsQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanDetermineQueueDoesNotExistInManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$shouldBeFalse = $manager->containsQueueWithName('i should not exist');
|
||||||
|
|
||||||
|
$this->assertFalse($shouldBeFalse);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueWithName
|
||||||
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanGetUglyQueueObjectFromManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$uglyQueue = $manager->getQueueWithName('tasty-sandwich');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
||||||
|
$this->assertEquals('tasty-sandwich', $uglyQueue->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownWhenTryingToGetNonExistentQueueFromManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$shouldNotExist = $manager->getQueueWithName('sandwiches');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueList
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanGetListOfQueuesInManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$queueList = $manager->getQueueList();
|
||||||
|
|
||||||
|
$this->assertInternalType('array', $queueList);
|
||||||
|
$this->assertCount(1, $queueList);
|
||||||
|
$this->assertContains('tasty-sandwich', $queueList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueWithName
|
||||||
|
* @covers \DCarbone\UglyQueueManager::addQueue
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownWhenReAddingQueueToManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$uglyQueue = $manager->getQueueWithName('tasty-sandwich');
|
||||||
|
|
||||||
|
$manager->addQueue($uglyQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::addQueueAtPath
|
||||||
|
* @covers \DCarbone\UglyQueueManager::addQueue
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanInitializeNewQueueAndAddToManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$manager->addQueueAtPath(__DIR__.'/../misc/queues/really-tasty-sandwich');
|
||||||
|
|
||||||
|
$uglyQueue = $manager->getQueueWithName('really-tasty-sandwich');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
||||||
|
$this->assertEquals('really-tasty-sandwich', $uglyQueue->name);
|
||||||
|
|
||||||
|
$queueList = $manager->getQueueList();
|
||||||
|
|
||||||
|
$this->assertInternalType('array', $queueList);
|
||||||
|
$this->assertCount(2, $queueList);
|
||||||
|
$this->assertContains('really-tasty-sandwich', $queueList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::removeQueueByName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanRemoveQueueFromManagerByName(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$manager->removeQueueByName('really-tasty-sandwich');
|
||||||
|
|
||||||
|
$queueList = $manager->getQueueList();
|
||||||
|
|
||||||
|
$this->assertCount(1, $queueList);
|
||||||
|
$this->assertNotContains('really-tasty-sandwich', $queueList);
|
||||||
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * @covers \DCarbone\UglyQueue::queueExists
|
||||||
|
// * @uses \DCarbone\UglyQueue
|
||||||
|
// * @depends testCanInitializeNewUglyQueue
|
||||||
|
// * @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
// */
|
||||||
|
// public function testCanDetermineExistenceOfExistingQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
// {
|
||||||
|
// $exists = $uglyQueue->queueExists('tasty-sandwich');
|
||||||
|
//
|
||||||
|
// $this->assertTrue($exists);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * @covers \DCarbone\UglyQueue::queueExists
|
||||||
|
// * @uses \DCarbone\UglyQueue
|
||||||
|
// * @depends testCanInitializeNewUglyQueue
|
||||||
|
// * @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
// */
|
||||||
|
// public function testCanDetermineExistenceOfNonExistingQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
// {
|
||||||
|
// $exists = $uglyQueue->queueExists('nasty-sandwich');
|
||||||
|
//
|
||||||
|
// $this->assertFalse($exists);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * @covers \DCarbone\UglyQueue::getInitializedQueueList
|
||||||
|
// * @uses \DCarbone\UglyQueue
|
||||||
|
// * @depends testCanInitializeNewUglyQueue
|
||||||
|
// * @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
// */
|
||||||
|
// public function testCanGetListOfInitializedQueues(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
// {
|
||||||
|
// $queueList = $uglyQueue->getInitializedQueueList();
|
||||||
|
//
|
||||||
|
// $this->assertEquals(1, count($queueList));
|
||||||
|
// $this->assertContains('tasty-sandwich', $queueList);
|
||||||
|
// }
|
||||||
|
}
|
||||||
@@ -1,13 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
if (is_dir(__DIR__.'/tasty-sandwich'))
|
if (is_dir(__DIR__.'/queues/'))
|
||||||
{
|
{
|
||||||
foreach(glob(__DIR__.'/tasty-sandwich/*') as $file)
|
foreach(glob(__DIR__.'/queues/*', GLOB_ONLYDIR) as $queueDir)
|
||||||
{
|
{
|
||||||
if (substr($file, -1) === '.')
|
foreach(glob($queueDir.'/*') as $file)
|
||||||
|
{
|
||||||
|
$split = preg_split('#[/\\\]+#', $file);
|
||||||
|
if (strpos(end($split), '.') === 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
unlink($file);
|
unlink($file);
|
||||||
}
|
}
|
||||||
rmdir(__DIR__.'/tasty-sandwich');
|
|
||||||
|
rmdir($queueDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mkdir(__DIR__.'/queues');
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user