8 Commits
0.2.0 ... 0.3.1

Author SHA1 Message Date
b840f40a89 - Moving Notify things into "enum" class
- Adding \Countable interfaces to both UglyQueue and UglyQueueManager
2014-10-30 10:54:06 -05:00
d20106a021 Some more tests and such. 2014-10-01 08:10:39 -05:00
97dba637d9 Adding some test cases for UglyQueueManager 2014-09-29 17:23:43 -05:00
793edc2d60 Adding some test cases for UglyQueueManager 2014-09-29 17:18:14 -05:00
1e98b0cf7e Fixing some issues noticed in test cases. 2014-09-29 17:02:28 -05:00
89c51468eb Huge update. 2014-09-29 16:26:53 -05:00
d33fd6bcba Adding ability to get list of initialized queues. 2014-08-11 12:42:17 -05:00
89cb1ae4b1 Adding ability to check for queue group existence 2014-08-11 12:24:05 -05:00
9 changed files with 864 additions and 384 deletions

View File

@@ -3,6 +3,8 @@ ugly-queue
A simple file-based queue system for PHP 5.3.3+
Build status: [![Build Status](https://travis-ci.org/dcarbone/ugly-queue.svg)](https://travis-ci.org/dcarbone/ugly-queue)
Build statuses:
- master: [![Build Status](https://travis-ci.org/dcarbone/ugly-queue.svg?branch=master)](https://travis-ci.org/dcarbone/ugly-queue)
- 0.3.0: [![Build Status](https://travis-ci.org/dcarbone/ugly-queue.svg?branch=0.3.0)](https://travis-ci.org/dcarbone/ugly-queue)
Documentation and Test suites forthcoming.

View File

@@ -21,11 +21,11 @@
"require" : {
"php" : ">=5.3.3",
"dcarbone/helpers" : "6.1.*"
"dcarbone/helpers" : "~6.1"
},
"require-dev" : {
"phpunit/phpunit": "4.1.*"
"phpunit/phpunit": "~4.1"
},
"autoload" : {

View File

@@ -17,6 +17,11 @@
<exclude>./tests/misc</exclude>
</testsuite>
<testsuite name="UglyQueueManager">
<directory>./tests/UglyQueueManager</directory>
<exclude>./tests/misc</exclude>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>

View File

@@ -5,159 +5,81 @@ use DCarbone\Helpers\FileHelper;
/**
* Class UglyQueue
* @package DCarbone
*
* @property string name
* @property string path
* @property bool locked
*/
class UglyQueue
class UglyQueue implements \Serializable, \SplSubject, \Countable
{
/** @var int */
public $notifyStatus;
const QUEUE_READONLY = 0;
const QUEUE_READWRITE = 1;
/** @var array */
protected $config;
private $observers = array();
/** @var int */
protected $mode = null;
/** @var string */
protected $queueBaseDir;
protected $_name;
/** @var string */
protected $queueGroup = null;
/** @var string */
protected $queueGroupDirPath = null;
protected $_path;
/** @var bool */
protected $haveLock = false;
/** @var bool */
protected $init = false;
protected $_locked = false;
/** @var resource */
protected $_tmpHandle;
/**
* @param array $config
* @param string $directoryPath
* @param array $observers
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @return UglyQueue
*/
public function __construct(array $config)
public static function queueWithDirectoryPathAndObservers($directoryPath, array $observers = array())
{
if (!isset($config['queue-base-dir']))
throw new \InvalidArgumentException('UglyQueue::__construct - "$config" parameter "queue-base-dir" not seen.');
if (!is_string($directoryPath))
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']))
throw new \RuntimeException('UglyQueue::__construct - "$config[\'queue-base-dir\']" points to a directory that either doesn\'t exist or is not writable');
if (($directoryPath = trim($directoryPath)) === '')
throw new \InvalidArgumentException('Empty string passed for argument 1');
$this->config = $config;
$this->queueBaseDir = $this->config['queue-base-dir'];
if (file_exists($directoryPath))
{
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.'".');
}
/**
* Destructor
*/
public function __destruct()
{
$this->unlock();
$this->_populateQueue();
}
$uglyQueue = new UglyQueue();
$uglyQueue->observers = $observers;
/**
* @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');
$split = preg_split('#[/\\\]+#', $directoryPath);
if ($ttl < 0)
throw new \InvalidArgumentException('UglyQueue::lock - Argument 1 expected to be positive integer, "'.$ttl.'" seen');
$uglyQueue->_name = end($split);
$uglyQueue->_path = rtrim(realpath(implode(DIRECTORY_SEPARATOR, $split)), "/\\").DIRECTORY_SEPARATOR;
$already_locked = $this->isLocked();
// If there is no lock, currently
if ($already_locked === false)
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);
if (is_writable($uglyQueue->_path))
$uglyQueue->mode = self::QUEUE_READWRITE;
else if (is_readable($uglyQueue->_path))
$uglyQueue->mode = self::QUEUE_READONLY;
// 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>
<head>
@@ -168,13 +90,153 @@ class UglyQueue
</body>
</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'))
file_put_contents($this->queueGroupDirPath.'queue.txt', '');
if (!file_exists($uglyQueue->_path.'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 = UglyQueueEnum::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 = UglyQueueEnum::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 = UglyQueueEnum::QUEUE_FAILED_TO_LOCK;
$this->notify();
return $this->_locked = false;
}
$this->_locked = true;
$this->notifyStatus = UglyQueueEnum::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 = UglyQueueEnum::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 +247,36 @@ HTML;
*/
public function processQueue($count = 1)
{
if ($this->init === false)
throw new \RuntimeException('UglyQueue::processQueue - Must first initialize queue!');
if ($this->mode === self::QUEUE_READONLY)
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 ($this->haveLock === false)
throw new \RuntimeException('UglyQueue::processQueue - Cannot process queue locked by another process');
if ($this->_locked === false)
throw new \RuntimeException('Cannot process queue named "'.$this->_name.'". It is locked by another process.');
// If non-int valid is passed
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 ($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 !== UglyQueueEnum::QUEUE_PROCESSING)
{
$this->notifyStatus = UglyQueueEnum::QUEUE_PROCESSING;
$this->notify();
}
// 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 ($lineCount === 0)
return false;
// 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)
$this->unlock();
@@ -231,11 +299,14 @@ HTML;
rewind($queueFileHandle);
ftruncate($queueFileHandle, 0);
fclose($queueFileHandle);
$this->notifyStatus = UglyQueueEnum::QUEUE_REACHED_END;
$this->notify();
}
// Otherwise, create new queue file minus the processed lines.
else
{
$tmp = fopen($this->queueGroupDirPath.'queue.tmp', 'w+');
$tmp = fopen($this->_path.'queue.tmp', 'w+');
rewind($queueFileHandle);
$i = 0;
while (($line = fgets($queueFileHandle)) !== false && $i < $start_line)
@@ -248,8 +319,8 @@ HTML;
fclose($queueFileHandle);
fclose($tmp);
unlink($this->queueGroupDirPath.'queue.txt');
rename($this->queueGroupDirPath.'queue.tmp', $this->queueGroupDirPath.'queue.txt');
unlink($this->_path.'queue.txt');
rename($this->_path.'queue.tmp', $this->_path.'queue.txt');
}
return $data;
@@ -263,18 +334,18 @@ HTML;
*/
public function addToQueue($key, $value)
{
if ($this->init === false)
throw new \RuntimeException('UglyQueue::addToQueue - Must first initialize queue!');
if ($this->mode === self::QUEUE_READONLY)
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 ($this->haveLock === false)
throw new \RuntimeException('UglyQueue::addToQueue - You do not have a lock on this queue');
if ($this->_locked === false)
throw new \RuntimeException('Cannot add items to queue "'.$this->_name.'". Queue is already locked by another process');
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)
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)
@@ -295,9 +366,9 @@ HTML;
{
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)
{
if ($line !== "\n" && $line !== "")
@@ -305,11 +376,11 @@ HTML;
}
fclose($queueFileHandle);
unlink($this->queueGroupDirPath.'queue.txt');
unlink($this->_path.'queue.txt');
}
fclose($this->_tmpHandle);
rename($this->queueGroupDirPath.'queue.tmp', $this->queueGroupDirPath.'queue.txt');
rename($this->_path.'queue.tmp', $this->_path.'queue.txt');
}
}
@@ -319,10 +390,7 @@ HTML;
*/
public function getQueueItemCount()
{
if ($this->init === false)
throw new \RuntimeException('UglyQueue::getQueueItemCount - Must first initialize queue');
return FileHelper::getLineCount($this->queueGroupDirPath.'queue.txt');
return FileHelper::getLineCount($this->_path.'queue.txt');
}
/**
@@ -332,19 +400,16 @@ HTML;
*/
public function keyExistsInQueue($key)
{
if ($this->init === false)
throw new \RuntimeException('UglyQueue::keyExistsInQueue - Must first initialize queue');
$key = (string)$key;
// 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);
return true;
@@ -356,34 +421,86 @@ HTML;
}
/**
* @return boolean
* (PHP 5 >= 5.1.0)
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
*
* @return int The custom count as an integer.
*/
public function getInit()
public function count()
{
return $this->init;
return $this->getQueueItemCount();
}
/**
* @return string
* (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 getQueueBaseDir()
public function serialize()
{
return $this->queueBaseDir;
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 getQueueGroupDirPath()
public function unserialize($serialized)
{
return $this->queueGroupDirPath;
/** @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 getQueueGroup()
public function attach(\SplObserver $observer)
{
return $this->queueGroup;
if (!in_array($observer, $this->observers))
$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);
}
}
}

24
src/UglyQueueEnum.php Normal file
View File

@@ -0,0 +1,24 @@
<?php namespace DCarbone;
/**
* Class UglyQueueEnum
* @package DCarbone
*
* Pseudo-enum thing.
*/
abstract class UglyQueueEnum
{
// Typically used by UglyQueueManager
const MANAGER_INITIALIZED = 1;
const QUEUE_ADDED = 2;
const QUEUE_REMOVED = 3;
// Typically used by UglyQueues
const QUEUE_INITIALIZED = 100;
const QUEUE_LOCKED = 101;
const QUEUE_FAILED_TO_LOCK = 102;
const QUEUE_LOCKED_BY_OTHER_PROCESS = 103;
const QUEUE_UNLOCKED = 104;
const QUEUE_PROCESSING = 105;
const QUEUE_REACHED_END = 106;
}

238
src/UglyQueueManager.php Normal file
View File

@@ -0,0 +1,238 @@
<?php namespace DCarbone;
/**
* Class UglyQueueManager
* @package DCarbone
*/
class UglyQueueManager implements \SplObserver, \SplSubject, \Countable
{
/** @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 = UglyQueueEnum::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 = UglyQueueEnum::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 = UglyQueueEnum::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)
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
*
* @return int The custom count as an integer. The return value is cast to an integer.
*/
public function count()
{
return count($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);
}
}
}

View File

@@ -27,17 +27,13 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
);
/**
* @covers \DCarbone\UglyQueue::__construct
* @covers \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers
* @uses \DCarbone\UglyQueue
* @return \DCarbone\UglyQueue
*/
public function testCanConstructUglyQueueWithValidParameter()
public function testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers()
{
$conf = array(
'queue-base-dir' => dirname(__DIR__).'/misc/',
);
$uglyQueue = new \DCarbone\UglyQueue($conf);
$uglyQueue = \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers(dirname(__DIR__).'/misc/queues/tasty-sandwich');
$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
* @expectedException \InvalidArgumentException
*/
public function testExceptionThrownWhenConstructingUglyQueueWithEmptyOrInvalidConf()
public function testExceptionThrownWhenInitializingUglyQueueWithEmptyOrInvalidConf()
{
$conf = 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);
$uglyQueue = \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers(array());
}
/**
* @covers \DCarbone\UglyQueue::processQueue
* @uses \DCarbone\UglyQueue
* @depends testCanConstructUglyQueueWithValidParameter
* @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
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @expectedException \RuntimeException
* @param \DCarbone\UglyQueue $uglyQueue
*/
@@ -214,7 +65,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
/**
* @covers \DCarbone\UglyQueue::keyExistsInQueue
* @uses \DCarbone\UglyQueue
* @depends testCanInitializeNewUglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @param \DCarbone\UglyQueue $uglyQueue
*/
public function testKeyExistsInQueueReturnsFalseWithEmptyQueueAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
@@ -227,60 +78,73 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
/**
* @covers \DCarbone\UglyQueue::addToQueue
* @uses \DCarbone\UglyQueue
* @depends testCanInitializeNewUglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @expectedException \RuntimeException
* @param \DCarbone\UglyQueue $uglyQueue
*/
public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLockAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLock(\DCarbone\UglyQueue $uglyQueue)
{
$addToQueue = $uglyQueue->addToQueue('test', 'value');
}
/**
* @covers \DCarbone\UglyQueue::getInit
* @covers \DCarbone\UglyQueue::__get
* @uses \DCarbone\UglyQueue
* @depends testCanInitializeNewUglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @param \DCarbone\UglyQueue $uglyQueue
*/
public function testGetInitReturnsTrueAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
public function testCanGetQueueDirectory(\DCarbone\UglyQueue $uglyQueue)
{
$init = $uglyQueue->getInit();
$this->assertTrue($init);
$queuePath = $uglyQueue->path;
$this->assertFileExists($queuePath);
}
/**
* @covers \DCarbone\UglyQueue::getQueueGroupDirPath
* @covers \DCarbone\UglyQueue::__get
* @uses \DCarbone\UglyQueue
* @depends testCanInitializeNewUglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @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
* @depends testCanInitializeNewUglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @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
* @uses \DCarbone\UglyQueue
* @depends testCanInitializeNewUglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @param \DCarbone\UglyQueue $uglyQueue
*/
public function testIsLockedReturnsFalseBeforeLockingAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
public function testIsLockedReturnsFalseBeforeLocking(\DCarbone\UglyQueue $uglyQueue)
{
$isLocked = $uglyQueue->isLocked();
@@ -290,45 +154,33 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
/**
* @covers \DCarbone\UglyQueue::getQueueItemCount
* @uses \DCarbone\UglyQueue
* @depends testCanInitializeNewUglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @param \DCarbone\UglyQueue $uglyQueue
*/
public function testGetQueueItemCountReturnsZeroAfterInitializingEmptyQueue(\DCarbone\UglyQueue $uglyQueue)
public function testGetQueueItemCountReturnsZeroWithEmptyQueue(\DCarbone\UglyQueue $uglyQueue)
{
$itemCount = $uglyQueue->getQueueItemCount();
$this->assertEquals(0, $itemCount);
}
/**
* @covers \DCarbone\UglyQueue::initialize
* @covers \DCarbone\UglyQueue::__construct
* @covers \DCarbone\UglyQueue::getInit
* @covers \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers
* @uses \DCarbone\UglyQueue
* @return \DCarbone\UglyQueue
*/
public function testCanInitializeExistingQueue()
{
$conf = array(
'queue-base-dir' => dirname(__DIR__).'/misc/',
);
$uglyQueue = new \DCarbone\UglyQueue($conf);
$uglyQueue = \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers(dirname(__DIR__).'/misc/queues/tasty-sandwich');
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
$this->assertFalse($uglyQueue->getInit());
$uglyQueue->initialize('tasty-sandwich');
$this->assertTrue($uglyQueue->getInit());
return $uglyQueue;
}
/**
* @covers \DCarbone\UglyQueue::lock
* @uses \DCarbone\UglyQueue
* @depends testCanConstructUglyQueueWithValidParameter
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @expectedException \InvalidArgumentException
* @param \DCarbone\UglyQueue $uglyQueue
*/
@@ -340,7 +192,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
/**
* @covers \DCarbone\UglyQueue::lock
* @uses \DCarbone\UglyQueue
* @depends testCanConstructUglyQueueWithValidParameter
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @expectedException \InvalidArgumentException
* @param \DCarbone\UglyQueue $uglyQueue
*/
@@ -352,9 +204,9 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
/**
* @covers \DCarbone\UglyQueue::lock
* @covers \DCarbone\UglyQueue::isLocked
* @covers \DCarbone\UglyQueue::createQueueLock
* @covers \DCarbone\UglyQueue::createLockFile
* @uses \DCarbone\UglyQueue
* @depends testCanInitializeNewUglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @param \DCarbone\UglyQueue $uglyQueue
* @return \DCarbone\UglyQueue
*/
@@ -364,7 +216,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
$this->assertTrue($locked);
$queueDir = $uglyQueue->getQueueBaseDir().$uglyQueue->getQueueGroup().'/';
$queueDir = $uglyQueue->path;
$this->assertFileExists($queueDir.'queue.lock');
@@ -385,7 +237,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
* @depends testCanInitializeExistingQueue
* @param \DCarbone\UglyQueue $uglyQueue
*/
public function testCannotLockInitializedQueueThatIsAlreadyLocked(\DCarbone\UglyQueue $uglyQueue)
public function testCannotLockQueueThatIsAlreadyLocked(\DCarbone\UglyQueue $uglyQueue)
{
$lock = $uglyQueue->lock();
@@ -398,7 +250,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
* @depends testCanLockUglyQueueWithDefaultTTL
* @param \DCarbone\UglyQueue $uglyQueue
*/
public function testIsLockedReturnsTrueAfterLockingInitializedQueue(\DCarbone\UglyQueue $uglyQueue)
public function testIsLockedReturnsTrueAfterLocking(\DCarbone\UglyQueue $uglyQueue)
{
$isLocked = $uglyQueue->isLocked();
$this->assertTrue($isLocked);
@@ -416,7 +268,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
{
$uglyQueue->unlock();
$queueGroupDir = $uglyQueue->getQueueGroupDirPath();
$queueGroupDir = $uglyQueue->path;
$this->assertFileNotExists($queueGroupDir.'queue.lock');
@@ -459,7 +311,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
/**
* @covers \DCarbone\UglyQueue::lock
* @covers \DCarbone\UglyQueue::isLocked
* @covers \DCarbone\UglyQueue::getQueueGroupDirPath
* @covers \DCarbone\UglyQueue::__get
* @uses \DCarbone\UglyQueue
* @depends testCanUnlockLockedQueue
* @param \DCarbone\UglyQueue $uglyQueue
@@ -471,7 +323,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
$this->assertTrue($locked);
$queueDir = $uglyQueue->getQueueGroupDirPath();
$queueDir = $uglyQueue->path;
$this->assertFileExists($queueDir.'queue.lock');
@@ -501,7 +353,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
$this->assertTrue($added);
}
$groupDir = $uglyQueue->getQueueGroupDirPath();
$groupDir = $uglyQueue->path;
$this->assertFileExists(
$groupDir.'queue.tmp',
@@ -525,7 +377,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
{
$uglyQueue->_populateQueue();
$groupDir = $uglyQueue->getQueueGroupDirPath();
$groupDir = $uglyQueue->path;
$this->assertFileNotExists($groupDir.'queue.tmp');

View 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);
// }
}

View File

@@ -1,13 +1,22 @@
<?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;
unlink($file);
}
rmdir(__DIR__.'/tasty-sandwich');
rmdir($queueDir);
}
}
else
{
mkdir(__DIR__.'/queues');
}