Lots of cleanup, removing of dumb code. More to do.

This commit is contained in:
2015-09-29 11:35:48 -05:00
parent 4020de5223
commit 232e228475
5 changed files with 328 additions and 408 deletions

View File

@@ -7,4 +7,28 @@ Build statuses:
- master: [![Build Status](https://travis-ci.org/dcarbone/ugly-queue.svg?branch=master)](https://travis-ci.org/dcarbone/ugly-queue) - master: [![Build Status](https://travis-ci.org/dcarbone/ugly-queue.svg?branch=master)](https://travis-ci.org/dcarbone/ugly-queue)
- 0.3.1: [![Build Status](https://travis-ci.org/dcarbone/ugly-queue.svg?branch=0.3.1)](https://travis-ci.org/dcarbone/ugly-queue) - 0.3.1: [![Build Status](https://travis-ci.org/dcarbone/ugly-queue.svg?branch=0.3.1)](https://travis-ci.org/dcarbone/ugly-queue)
Documentation and Test suites forthcoming. ## Installation
This library is designed to be installed into your app using [https://getcomposer.org/](Composer).
Simply copy-paste the following line into your `"requires:"` hash:
```json
"dcarbone/ugly-queue": "0.3.*"
```
## Basic Usage
Once installed, you must first initialize an instance of [src/UglyQueueManager.php](UglyQueueManager).
This is done as such:
```php
$config = array(
'queue-base-dir' => 'path to where you would like queue files and directories to be stored'
);
$manager = new UglyQueueManager($config);
```
Once initialized, you can start adding queues!
```php
$manager
```

View File

@@ -5,80 +5,82 @@ use DCarbone\Helpers\FileHelper;
/** /**
* Class UglyQueue * Class UglyQueue
* @package DCarbone * @package DCarbone
*
* @property string name
* @property string path
* @property bool locked
*/ */
class UglyQueue implements \Serializable, \SplSubject, \Countable class UglyQueue implements \Serializable, \SplSubject, \Countable
{ {
/** @var int */
public $notifyStatus;
const QUEUE_READONLY = 0; const QUEUE_READONLY = 0;
const QUEUE_READWRITE = 1; const QUEUE_READWRITE = 1;
/** @var array */ /** @var int */
private $observers = array(); private $_notifyStatus;
/** @var \SplObserver[] */
private $_observers = array();
/** @var int */ /** @var int */
protected $mode = null; protected $mode = null;
/** @var string */ /** @var string */
protected $_name; protected $baseDir;
/** @var string */ /** @var string */
protected $_path; protected $name;
/** @var string */
protected $path;
/** @var bool */ /** @var bool */
protected $_locked = false; protected $locked = false;
/** @var resource */ /** @var resource */
protected $_tmpHandle; protected $tmpHandle;
/** @var string */
protected $queueFile;
/** @var string */
protected $queueTmpFile;
/** @var string */
protected $lockFile;
/** /**
* @param string $directoryPath * @param string $baseDir
* @param array $observers * @param string $queueName
* @throws \RuntimeException * @param \SplObserver[] $observers
* @throws \InvalidArgumentException
* @return UglyQueue
*/ */
public static function queueWithDirectoryPathAndObservers($directoryPath, array $observers = array()) public function __construct($baseDir, $queueName, array $observers = array())
{ {
if (!is_string($directoryPath)) $this->baseDir = trim($baseDir, "/\\");
throw new \InvalidArgumentException('Argument 1 expected to be string, '.gettype($directoryPath).' seen'); $this->name = $queueName;
$this->_observers = $observers;
if (($directoryPath = trim($directoryPath)) === '') $path = sprintf('%s/%s', $baseDir, $queueName);
throw new \InvalidArgumentException('Empty string passed for argument 1'); if (!file_exists($path) && !@mkdir($path))
throw new \RuntimeException('Unable to initialize queue directory "'.$path.'". Please check permissions.');
if (file_exists($directoryPath)) $this->path = $path;
$this->lockFile = sprintf('%s/queue.lock', $this->path);
$this->queueFile = sprintf('%s/queue.txt', $this->path);
$this->queueTmpFile = sprintf('%s/queue.tmp', $this->path);
$this->_initialize();
}
/**
* Initialize queue if needed.
*/
private function _initialize()
{
if (is_readable($this->path) && is_writable($this->path))
$this->mode = self::QUEUE_READWRITE;
else if (is_readable($this->path))
$this->mode = self::QUEUE_READONLY;
if (!file_exists($this->path.'index.html'))
{ {
if (!is_dir($directoryPath)) if ($this->mode === self::QUEUE_READONLY)
throw new \RuntimeException('Argument 1 expected to be path to directory, path to non-directory seen'); throw new \RuntimeException('Cannot initialize queue with name "'.$this->name.'", the user lacks permission to create files.');
}
else if (!@mkdir($directoryPath))
{
throw new \RuntimeException('Unable to create queue directory at path: "'.$directoryPath.'".');
}
$uglyQueue = new UglyQueue();
$uglyQueue->observers = $observers;
$split = preg_split('#[/\\\]+#', $directoryPath);
$uglyQueue->_name = end($split);
$uglyQueue->_path = rtrim(realpath(implode(DIRECTORY_SEPARATOR, $split)), "/\\").DIRECTORY_SEPARATOR;
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($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>
@@ -90,44 +92,19 @@ class UglyQueue implements \Serializable, \SplSubject, \Countable
</body> </body>
</html> </html>
HTML; HTML;
file_put_contents($uglyQueue->_path.'index.html', $html); file_put_contents($this->path.'index.html', $html);
} }
if (!file_exists($uglyQueue->_path.'queue.txt')) if (!file_exists($this->queueFile))
{ {
if ($uglyQueue->mode === self::QUEUE_READONLY) if ($this->mode === self::QUEUE_READONLY)
throw new \RuntimeException('Cannot initialize queue with name "'.$uglyQueue->_name.'", the user lacks permission to create files.'); throw new \RuntimeException('Cannot initialize queue with name "'.$this->name.'", the user lacks permission to create files.');
file_put_contents($uglyQueue->_path.'queue.txt', ''); file_put_contents($this->queueFile, '');
} }
$uglyQueue->notifyStatus = UglyQueueEnum::QUEUE_INITIALIZED; $this->_notifyStatus = UglyQueueEnum::QUEUE_INITIALIZED;
$uglyQueue->notify(); $this->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.'".');
}
} }
/** /**
@@ -137,7 +114,63 @@ HTML;
{ {
$this->_populateQueue(); $this->_populateQueue();
$this->unlock(); $this->unlock();
file_put_contents($this->_path.UglyQueueManager::UGLY_QUEUE_SERIALIZED_NAME, serialize($this)); file_put_contents($this->path.'/ugly-queue.obj', serialize($this));
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return int
*/
public function getMode()
{
return $this->mode;
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @return string
*/
public function getBaseDir()
{
return $this->baseDir;
}
/**
* @return string
*/
public function getQueueFile()
{
return $this->queueFile;
}
/**
* @return string
*/
public function getQueueTmpFile()
{
return $this->queueTmpFile;
}
/**
* @return string
*/
public function getLockFile()
{
return $this->lockFile;
} }
/** /**
@@ -150,8 +183,8 @@ HTML;
if (!is_int($ttl)) if (!is_int($ttl))
throw new \InvalidArgumentException('Argument 1 expected to be integer, "'.gettype($ttl).'" seen'); throw new \InvalidArgumentException('Argument 1 expected to be integer, "'.gettype($ttl).'" seen');
if ($ttl < 0) if ($ttl < 1)
throw new \InvalidArgumentException('Argument 1 expected to be positive integer, "'.$ttl.'" seen'); throw new \InvalidArgumentException('Argument 1 expected to be greater than 0 "'.$ttl.'" seen');
$alreadyLocked = $this->isLocked(); $alreadyLocked = $this->isLocked();
@@ -160,8 +193,8 @@ HTML;
return $this->createLockFile($ttl); return $this->createLockFile($ttl);
// If we make it this far, there is already a lock in place. // If we make it this far, there is already a lock in place.
$this->_locked = false; $this->locked = false;
$this->notifyStatus = UglyQueueEnum::QUEUE_LOCKED_BY_OTHER_PROCESS; $this->_notifyStatus = UglyQueueEnum::QUEUE_LOCKED_BY_OTHER_PROCESS;
$this->notify(); $this->notify();
return false; return false;
@@ -174,19 +207,19 @@ HTML;
protected function createLockFile($ttl) protected function createLockFile($ttl)
{ {
$ok = (bool)@file_put_contents( $ok = (bool)@file_put_contents(
$this->_path.'queue.lock', $this->lockFile,
json_encode(array('ttl' => $ttl, 'born' => time()))); json_encode(array('ttl' => $ttl, 'born' => time())));
if ($ok !== true) if ($ok !== true)
{ {
$this->notifyStatus = UglyQueueEnum::QUEUE_FAILED_TO_LOCK; $this->_notifyStatus = UglyQueueEnum::QUEUE_FAILED_TO_LOCK;
$this->notify(); $this->notify();
return $this->_locked = false; return $this->locked = false;
} }
$this->_locked = true; $this->locked = true;
$this->notifyStatus = UglyQueueEnum::QUEUE_LOCKED; $this->_notifyStatus = UglyQueueEnum::QUEUE_LOCKED;
$this->notify(); $this->notify();
return true; return true;
@@ -197,12 +230,12 @@ HTML;
*/ */
public function unlock() public function unlock()
{ {
if ($this->_locked === true) if ($this->locked === true)
{ {
unlink($this->_path.'queue.lock'); unlink($this->lockFile);
$this->_locked = false; $this->locked = false;
$this->notifyStatus = UglyQueueEnum::QUEUE_UNLOCKED; $this->_notifyStatus = UglyQueueEnum::QUEUE_UNLOCKED;
$this->notify(); $this->notify();
} }
} }
@@ -214,24 +247,23 @@ HTML;
public function isLocked() public function isLocked()
{ {
// First check for lock file // First check for lock file
if (is_file($this->_path.'queue.lock')) if (is_file($this->lockFile))
{ {
$lock = json_decode(file_get_contents($this->_path.'queue.lock'), true); $lock = json_decode(file_get_contents($this->lockFile), true);
// If we have an invalid lock structure. // If the decoded lock file contains a ttl and born value...
if (!isset($lock['ttl']) || !isset($lock['born'])) if (isset($lock['ttl']) && isset($lock['born']))
throw new \RuntimeException('Invalid "queue.lock" file structure seen at "'.$this->_path.'queue.lock".'); {
$lock_ttl = ((int)$lock['born'] + (int)$lock['ttl']);
// Otherwise... // If we're within the TTL of the lock, assume another thread is already processing.
$lock_ttl = ((int)$lock['born'] + (int)$lock['ttl']); // We'll pick it up on the next go around.
if ($lock_ttl > time())
// If we're within the TTL of the lock, assume another thread is already processing. return true;
// 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! // Else, remove lock file and assume we're good to go!
unlink($this->_path.'queue.lock'); unlink($this->lockFile);
return false; return false;
} }
@@ -245,14 +277,14 @@ HTML;
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @return bool|array * @return bool|array
*/ */
public function processQueue($count = 1) public function retrieveItems($count = 1)
{ {
if ($this->mode === self::QUEUE_READONLY) 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).'); 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->_locked === false) if ($this->isLocked() === false)
throw new \RuntimeException('Cannot process queue named "'.$this->_name.'". It is 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))
@@ -262,21 +294,21 @@ HTML;
if ($count <= 0) if ($count <= 0)
throw new \InvalidArgumentException('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) if ($this->_notifyStatus !== UglyQueueEnum::QUEUE_PROCESSING)
{ {
$this->notifyStatus = UglyQueueEnum::QUEUE_PROCESSING; $this->_notifyStatus = UglyQueueEnum::QUEUE_PROCESSING;
$this->notify(); $this->notify();
} }
// Find number of lines in the queue file // Find number of lines in the queue file
$lineCount = FileHelper::getLineCount($this->_path.'queue.txt'); $lineCount = FileHelper::getLineCount($this->queueFile);
// 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->_path.'queue.txt', 'r+'); $queueFileHandle = fopen($this->queueFile, 'r+');
if ($queueFileHandle === false) if ($queueFileHandle === false)
$this->unlock(); $this->unlock();
@@ -300,13 +332,13 @@ HTML;
ftruncate($queueFileHandle, 0); ftruncate($queueFileHandle, 0);
fclose($queueFileHandle); fclose($queueFileHandle);
$this->notifyStatus = UglyQueueEnum::QUEUE_REACHED_END; $this->_notifyStatus = UglyQueueEnum::QUEUE_REACHED_END;
$this->notify(); $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->_path.'queue.tmp', 'w+'); $tmp = fopen($this->queueTmpFile, '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)
@@ -319,8 +351,8 @@ HTML;
fclose($queueFileHandle); fclose($queueFileHandle);
fclose($tmp); fclose($tmp);
unlink($this->_path.'queue.txt'); unlink($this->queueFile);
rename($this->_path.'queue.tmp', $this->_path.'queue.txt'); rename($this->queueTmpFile, $this->queueFile);
} }
return $data; return $data;
@@ -332,19 +364,19 @@ HTML;
* @return bool * @return bool
* @throws \RuntimeException * @throws \RuntimeException
*/ */
public function addToQueue($key, $value) public function addItem($key, $value)
{ {
if ($this->mode === self::QUEUE_READONLY) if ($this->mode === self::QUEUE_READONLY)
throw new \RuntimeException('Cannot add items to queue "'.$this->_name.'" as it is in read-only mode'); throw new \RuntimeException('Cannot add item 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->_locked === false) if ($this->locked === false)
throw new \RuntimeException('Cannot add items to queue "'.$this->_name.'". Queue is already locked by another process'); throw new \RuntimeException('Cannot add item 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->_path.'queue.tmp', 'w+'); $this->tmpHandle = fopen($this->queueTmpFile, 'w+');
if ($this->_tmpHandle === false) if ($this->tmpHandle === false)
throw new \RuntimeException('Unable to create "queue.tmp" file.'); throw new \RuntimeException('Unable to create "queue.tmp" file.');
} }
@@ -352,7 +384,7 @@ HTML;
$value = json_encode($value); $value = json_encode($value);
return (bool)fwrite( return (bool)fwrite(
$this->_tmpHandle, $this->tmpHandle,
$key."\t".str_replace(array("\r\n", "\n"), ' ', $value) $key."\t".str_replace(array("\r\n", "\n"), ' ', $value)
."\n"); ."\n");
} }
@@ -364,35 +396,26 @@ HTML;
*/ */
public function _populateQueue() public function _populateQueue()
{ {
if (is_resource($this->_tmpHandle)) if (is_resource($this->tmpHandle))
{ {
if (file_exists($this->_path.'queue.txt')) if (file_exists($this->queueFile))
{ {
$queueFileHandle = fopen($this->_path.'queue.txt', 'r+'); $queueFileHandle = fopen($this->queueFile, 'r+');
while (($line = fgets($queueFileHandle)) !== false) while (($line = fgets($queueFileHandle)) !== false)
{ {
if ($line !== "\n" && $line !== "") if ($line !== "\n" && $line !== "")
fwrite($this->_tmpHandle, $line); fwrite($this->tmpHandle, $line);
} }
fclose($queueFileHandle); fclose($queueFileHandle);
unlink($this->_path.'queue.txt'); unlink($this->queueFile);
} }
fclose($this->_tmpHandle); fclose($this->tmpHandle);
rename($this->_path.'queue.tmp', $this->_path.'queue.txt'); rename($this->queueTmpFile, $this->queueFile);
} }
} }
/**
* @return int
* @throws \RuntimeException
*/
public function getQueueItemCount()
{
return FileHelper::getLineCount($this->_path.'queue.txt');
}
/** /**
* @param string $key * @param string $key
* @return bool * @return bool
@@ -403,13 +426,11 @@ HTML;
$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->_path.'queue.txt', 'r'); $queueFileHandle = fopen($this->queueFile, 'r');
while(($line = fscanf($queueFileHandle, "%s\t%s\n")) !== false) while(($line = fscanf($queueFileHandle, "%s\t%s\n")) !== false)
{ {
list ($lineKey, $lineValue) = $line; if ($key === $line[0])
if ($key === $lineKey)
{ {
fclose($queueFileHandle); fclose($queueFileHandle);
return true; return true;
@@ -429,7 +450,7 @@ HTML;
*/ */
public function count() public function count()
{ {
return $this->getQueueItemCount(); return (int)FileHelper::getLineCount($this->queueFile);
} }
/** /**
@@ -441,7 +462,7 @@ HTML;
*/ */
public function serialize() public function serialize()
{ {
return serialize(array($this->_name, $this->_path)); return serialize(array($this->name, $this->path));
} }
/** /**
@@ -456,8 +477,8 @@ HTML;
{ {
/** @var \DCarbone\UglyQueue $uglyQueue */ /** @var \DCarbone\UglyQueue $uglyQueue */
$data = unserialize($serialized); $data = unserialize($serialized);
$this->_name = $data[0]; $this->name = $data[0];
$this->_path = $data[1]; $this->path = $data[1];
} }
/** /**
@@ -470,8 +491,8 @@ HTML;
*/ */
public function attach(\SplObserver $observer) public function attach(\SplObserver $observer)
{ {
if (!in_array($observer, $this->observers)) if (!in_array($observer, $this->_observers))
$this->observers[] = $observer; $this->_observers[] = $observer;
} }
/** /**
@@ -484,9 +505,9 @@ HTML;
*/ */
public function detach(\SplObserver $observer) public function detach(\SplObserver $observer)
{ {
$idx = array_search($observer, $this->observers, true); $idx = array_search($observer, $this->_observers, true);
if ($idx !== false) if ($idx !== false)
unset($this->observers[$idx]); unset($this->_observers[$idx]);
} }
/** /**
@@ -498,9 +519,9 @@ HTML;
*/ */
public function notify() public function notify()
{ {
for ($i = 0, $count = count($this->observers); $i < $count; $i++) foreach($this->_observers as $observer)
{ {
$this->observers[$i]->notify($this); $observer->update($this);
} }
} }
} }

View File

@@ -4,77 +4,38 @@
* Class UglyQueueManager * Class UglyQueueManager
* @package DCarbone * @package DCarbone
*/ */
class UglyQueueManager implements \SplObserver, \SplSubject, \Countable class UglyQueueManager implements \SplObserver, \Countable
{ {
/** @var int */ /** @var UglyQueue[] */
public $notifyStatus;
const UGLY_QUEUE_SERIALIZED_NAME = 'ugly-queue.obj';
/** @var array */
private $observers = array();
/** @var array */
protected $queues = array(); protected $queues = array();
/** @var array */
protected $config = array();
/** @var string */ /** @var string */
protected $queueBaseDir; protected $baseDir;
/** /**
* Constructor * Constructor
* *
* @param array $config * @param string $baseDir
* @param array $observers
* @throws \RuntimeException * @throws \RuntimeException
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
protected function __construct(array $config, array $observers = array()) public function __construct($baseDir)
{ {
if (!isset($config['queue-base-dir'])) if (false === is_string($baseDir))
throw new \InvalidArgumentException('"$config" parameter "queue-base-dir" not seen.'); throw new \InvalidArgumentException('Argument 1 expected to be string, "'.gettype($baseDir).'" seen.');
if (!is_dir($config['queue-base-dir'])) if (false === is_dir($baseDir))
throw new \RuntimeException('"queue-base-dir" points to a directory that does not exist.'); throw new \RuntimeException('"'.$baseDir.'" points to a directory that does not exist.');
$this->config = $config; if (false === is_readable($baseDir))
$this->queueBaseDir = rtrim(realpath($this->config['queue-base-dir']), "/\\").DIRECTORY_SEPARATOR; throw new \RuntimeException('"'.$baseDir.'" is not readable and/or writable .');
$this->observers = $observers;
}
/** $this->baseDir = rtrim($baseDir, "/\\");
* @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(sprintf('%s/*', $this->baseDir), GLOB_ONLYDIR) as $queueDir)
foreach(glob($manager->queueBaseDir.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $queueDir)
{ {
// Try to avoid looking at hidden directories or magic dirs such as '.' and '..' $this->addQueueAtPath($queueDir);
$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;
} }
/** /**
@@ -84,24 +45,45 @@ class UglyQueueManager implements \SplObserver, \SplSubject, \Countable
*/ */
public function addQueue(UglyQueue $uglyQueue) public function addQueue(UglyQueue $uglyQueue)
{ {
if ($this->containsQueueWithName($uglyQueue->name)) $name = $uglyQueue->getName();
throw new \RuntimeException('Queue named "'.$uglyQueue->name.'" already exists in this manager.');
$this->queues[$uglyQueue->name] = $uglyQueue; if ($this->containsQueueWithName($name))
throw new \RuntimeException('Queue named "'.$name.'" already exists in this manager.');
$this->notifyStatus = UglyQueueEnum::QUEUE_ADDED; $this->queues[$name] = $uglyQueue;
$this->notify();
return $this; return $this;
} }
/** /**
* @param $path * @param string $name
* @return UglyQueue
*/
public function createQueue($name)
{
$this->addQueue(new UglyQueue($this->baseDir, $name, array($this)));
return end($this->queues);
}
/**
* @param string $path
* @return \DCarbone\UglyQueueManager * @return \DCarbone\UglyQueueManager
*/ */
public function addQueueAtPath($path) public function addQueueAtPath($path)
{ {
$uglyQueue = UglyQueue::queueWithDirectoryPathAndObservers($path, $this->observers); // Try to avoid looking at hidden directories or magic dirs such as '.' and '..'
$split = preg_split('#[/\\\]+#', $path);
$queueName = end($split);
if (0 === strpos($queueName, '.'))
return null;
$serializedFile = sprintf('%s/%s/ugly-queue.obj', $this->baseDir, $queueName);
if (file_exists($serializedFile))
$uglyQueue = unserialize(file_get_contents($serializedFile));
else
$uglyQueue = new UglyQueue($this->baseDir, $queueName, array($this));
return $this->addQueue($uglyQueue); return $this->addQueue($uglyQueue);
} }
@@ -112,8 +94,9 @@ class UglyQueueManager implements \SplObserver, \SplSubject, \Countable
*/ */
public function removeQueue(UglyQueue $uglyQueue) public function removeQueue(UglyQueue $uglyQueue)
{ {
if ($this->containsQueueWithName($uglyQueue->name)) $name = $uglyQueue->getName();
unset($this->queues[$uglyQueue->name]); if ($this->containsQueueWithName($name))
unset($this->queues[$name]);
return $this; return $this;
} }
@@ -125,11 +108,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject, \Countable
public function removeQueueByName($name) public function removeQueueByName($name)
{ {
if ($this->containsQueueWithName($name)) if ($this->containsQueueWithName($name))
{
unset($this->queues[$name]); unset($this->queues[$name]);
$this->notifyStatus = UglyQueueEnum::QUEUE_REMOVED;
$this->notify();
}
return $this; return $this;
} }
@@ -139,7 +118,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject, \Countable
* @return \DCarbone\UglyQueue * @return \DCarbone\UglyQueue
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function &getQueueWithName($name) public function getQueueWithName($name)
{ {
if (isset($this->queues[$name])) if (isset($this->queues[$name]))
return $this->queues[$name]; return $this->queues[$name];
@@ -186,53 +165,6 @@ class UglyQueueManager implements \SplObserver, \SplSubject, \Countable
*/ */
public function update(\SplSubject $subject) public function update(\SplSubject $subject)
{ {
for ($i = 0, $count = count($this->observers); $i < $count; $i++) // Nothing for now...
{
$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

@@ -9,6 +9,8 @@ require_once __DIR__.'/../misc/cleanup.php';
*/ */
class UglyQueueTest extends PHPUnit_Framework_TestCase class UglyQueueTest extends PHPUnit_Framework_TestCase
{ {
protected $baseDir;
/** /**
* @var array * @var array
*/ */
@@ -26,14 +28,19 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
'10' => 'Virginia baked ham, sliced', '10' => 'Virginia baked ham, sliced',
); );
protected function setUp()
{
$this->baseDir = __DIR__.'/../misc/queues';
}
/** /**
* @covers \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers * @covers \DCarbone\UglyQueue::__construct
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @return \DCarbone\UglyQueue * @return \DCarbone\UglyQueue
*/ */
public function testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers() public function testCanInitializeObjectWithValidParameters()
{ {
$uglyQueue = \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers(dirname(__DIR__).'/misc/queues/tasty-sandwich'); $uglyQueue = new \DCarbone\UglyQueue($this->baseDir, 'tasty-sandwich');
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue); $this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
@@ -41,31 +48,21 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
} }
/** /**
* @covers \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers * @covers \DCarbone\UglyQueue::retrieveItems
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @expectedException \InvalidArgumentException * @depends testCanInitializeObjectWithValidParameters
*/
public function testExceptionThrownWhenInitializingUglyQueueWithEmptyOrInvalidConf()
{
$uglyQueue = \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers(array());
}
/**
* @covers \DCarbone\UglyQueue::processQueue
* @uses \DCarbone\UglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @expectedException \RuntimeException * @expectedException \RuntimeException
* @param \DCarbone\UglyQueue $uglyQueue * @param \DCarbone\UglyQueue $uglyQueue
*/ */
public function testExceptionThrownWhenTryingToProcessQueueAfterInitializationBeforeLock(\DCarbone\UglyQueue $uglyQueue) public function testExceptionThrownWhenTryingToProcessQueueAfterInitializationBeforeLock(\DCarbone\UglyQueue $uglyQueue)
{ {
$process = $uglyQueue->processQueue(); $uglyQueue->retrieveItems();
} }
/** /**
* @covers \DCarbone\UglyQueue::keyExistsInQueue * @covers \DCarbone\UglyQueue::keyExistsInQueue
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers * @depends testCanInitializeObjectWithValidParameters
* @param \DCarbone\UglyQueue $uglyQueue * @param \DCarbone\UglyQueue $uglyQueue
*/ */
public function testKeyExistsInQueueReturnsFalseWithEmptyQueueAfterInitialization(\DCarbone\UglyQueue $uglyQueue) public function testKeyExistsInQueueReturnsFalseWithEmptyQueueAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
@@ -76,101 +73,76 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
} }
/** /**
* @covers \DCarbone\UglyQueue::addToQueue * @covers \DCarbone\UglyQueue::addItem
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers * @depends testCanInitializeObjectWithValidParameters
* @expectedException \RuntimeException * @expectedException \RuntimeException
* @param \DCarbone\UglyQueue $uglyQueue * @param \DCarbone\UglyQueue $uglyQueue
*/ */
public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLock(\DCarbone\UglyQueue $uglyQueue) public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLock(\DCarbone\UglyQueue $uglyQueue)
{ {
$addToQueue = $uglyQueue->addToQueue('test', 'value'); $uglyQueue->addItem('test', 'value');
} }
/** /**
* @covers \DCarbone\UglyQueue::__get * @covers \DCarbone\UglyQueue::getPath
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers * @depends testCanInitializeObjectWithValidParameters
* @param \DCarbone\UglyQueue $uglyQueue * @param \DCarbone\UglyQueue $uglyQueue
*/ */
public function testCanGetQueueDirectory(\DCarbone\UglyQueue $uglyQueue) public function testCanGetQueueDirectory(\DCarbone\UglyQueue $uglyQueue)
{ {
$queuePath = $uglyQueue->path; $queuePath = $uglyQueue->getPath();
$this->assertFileExists($queuePath); $this->assertFileExists($queuePath);
} }
/** /**
* @covers \DCarbone\UglyQueue::__get * @covers \DCarbone\UglyQueue::getName
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers * @depends testCanInitializeObjectWithValidParameters
* @param \DCarbone\UglyQueue $uglyQueue * @param \DCarbone\UglyQueue $uglyQueue
*/ */
public function testCanGetQueueName(\DCarbone\UglyQueue $uglyQueue) public function testCanGetQueueName(\DCarbone\UglyQueue $uglyQueue)
{ {
$queueName = $uglyQueue->name; $queueName = $uglyQueue->getName();
$this->assertEquals('tasty-sandwich', $queueName); $this->assertEquals('tasty-sandwich', $queueName);
} }
/** /**
* @covers \DCarbone\UglyQueue::__get * @covers \DCarbone\UglyQueue::isLocked
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers * @depends testCanInitializeObjectWithValidParameters
* @param \DCarbone\UglyQueue $uglyQueue * @param \DCarbone\UglyQueue $uglyQueue
*/ */
public function testCanGetQueueLockedStatus(\DCarbone\UglyQueue $uglyQueue) public function testCanGetQueueLockedStatus(\DCarbone\UglyQueue $uglyQueue)
{ {
$locked = $uglyQueue->locked; $locked = $uglyQueue->isLocked();
$this->assertFalse($locked); $this->assertFalse($locked);
} }
/** /**
* @covers \DCarbone\UglyQueue::__get * @covers \DCarbone\UglyQueue::count
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @expectedException \OutOfBoundsException * @depends testCanInitializeObjectWithValidParameters
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @param \DCarbone\UglyQueue $uglyQueue
*/
public function testExceptionThrownWhenAttemptingToGetInvalidProperty(\DCarbone\UglyQueue $uglyQueue)
{
$sandwich = $uglyQueue->sandwich;
}
/**
* @covers \DCarbone\UglyQueue::isLocked
* @uses \DCarbone\UglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @param \DCarbone\UglyQueue $uglyQueue
*/
public function testIsLockedReturnsFalseBeforeLocking(\DCarbone\UglyQueue $uglyQueue)
{
$isLocked = $uglyQueue->isLocked();
$this->assertFalse($isLocked);
}
/**
* @covers \DCarbone\UglyQueue::getQueueItemCount
* @uses \DCarbone\UglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
* @param \DCarbone\UglyQueue $uglyQueue * @param \DCarbone\UglyQueue $uglyQueue
*/ */
public function testGetQueueItemCountReturnsZeroWithEmptyQueue(\DCarbone\UglyQueue $uglyQueue) public function testGetQueueItemCountReturnsZeroWithEmptyQueue(\DCarbone\UglyQueue $uglyQueue)
{ {
$itemCount = $uglyQueue->getQueueItemCount(); $itemCount = count($uglyQueue);
$this->assertEquals(0, $itemCount); $this->assertEquals(0, $itemCount);
} }
/** /**
* @covers \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers * @covers \DCarbone\UglyQueue::__construct
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @return \DCarbone\UglyQueue * @return \DCarbone\UglyQueue
*/ */
public function testCanInitializeExistingQueue() public function testCanInitializeExistingQueue()
{ {
$uglyQueue = \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers(dirname(__DIR__).'/misc/queues/tasty-sandwich'); $uglyQueue = new \DCarbone\UglyQueue($this->baseDir, 'tasty-sandwich');
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue); $this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
@@ -180,7 +152,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
/** /**
* @covers \DCarbone\UglyQueue::lock * @covers \DCarbone\UglyQueue::lock
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers * @depends testCanInitializeObjectWithValidParameters
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
* @param \DCarbone\UglyQueue $uglyQueue * @param \DCarbone\UglyQueue $uglyQueue
*/ */
@@ -192,7 +164,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
/** /**
* @covers \DCarbone\UglyQueue::lock * @covers \DCarbone\UglyQueue::lock
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers * @depends testCanInitializeObjectWithValidParameters
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
* @param \DCarbone\UglyQueue $uglyQueue * @param \DCarbone\UglyQueue $uglyQueue
*/ */
@@ -206,7 +178,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
* @covers \DCarbone\UglyQueue::isLocked * @covers \DCarbone\UglyQueue::isLocked
* @covers \DCarbone\UglyQueue::createLockFile * @covers \DCarbone\UglyQueue::createLockFile
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers * @depends testCanInitializeObjectWithValidParameters
* @param \DCarbone\UglyQueue $uglyQueue * @param \DCarbone\UglyQueue $uglyQueue
* @return \DCarbone\UglyQueue * @return \DCarbone\UglyQueue
*/ */
@@ -216,11 +188,9 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
$this->assertTrue($locked); $this->assertTrue($locked);
$queueDir = $uglyQueue->path; $this->assertFileExists($uglyQueue->getLockFile());
$this->assertFileExists($queueDir.'queue.lock'); $decode = @json_decode(file_get_contents($uglyQueue->getLockFile()));
$decode = @json_decode(file_get_contents($queueDir.'queue.lock'));
$this->assertTrue((json_last_error() === JSON_ERROR_NONE)); $this->assertTrue((json_last_error() === JSON_ERROR_NONE));
$this->assertObjectHasAttribute('ttl', $decode); $this->assertObjectHasAttribute('ttl', $decode);
@@ -268,9 +238,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
{ {
$uglyQueue->unlock(); $uglyQueue->unlock();
$queueGroupDir = $uglyQueue->path; $this->assertFileNotExists($uglyQueue->getLockFile());
$this->assertFileNotExists($queueGroupDir.'queue.lock');
return $uglyQueue; return $uglyQueue;
} }
@@ -323,11 +291,9 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
$this->assertTrue($locked); $this->assertTrue($locked);
$queueDir = $uglyQueue->path; $this->assertFileExists($uglyQueue->getLockFile());
$this->assertFileExists($queueDir.'queue.lock'); $decode = @json_decode(file_get_contents($uglyQueue->getLockFile()));
$decode = @json_decode(file_get_contents($queueDir.'queue.lock'));
$this->assertTrue((json_last_error() === JSON_ERROR_NONE)); $this->assertTrue((json_last_error() === JSON_ERROR_NONE));
$this->assertObjectHasAttribute('ttl', $decode); $this->assertObjectHasAttribute('ttl', $decode);
@@ -338,7 +304,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
} }
/** /**
* @covers \DCarbone\UglyQueue::addToQueue * @covers \DCarbone\UglyQueue::addItem
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @uses \DCarbone\Helpers\FileHelper * @uses \DCarbone\Helpers\FileHelper
* @depends testCanLockQueueWithValidIntegerValue * @depends testCanLockQueueWithValidIntegerValue
@@ -349,17 +315,15 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
{ {
foreach(array_reverse($this->tastySandwich, true) as $k=>$v) foreach(array_reverse($this->tastySandwich, true) as $k=>$v)
{ {
$added = $uglyQueue->addToQueue($k, $v); $added = $uglyQueue->addItem($k, $v);
$this->assertTrue($added); $this->assertTrue($added);
} }
$groupDir = $uglyQueue->path;
$this->assertFileExists( $this->assertFileExists(
$groupDir.'queue.tmp', $uglyQueue->getQueueTmpFile(),
'queue.tmp file was not created!'); 'queue.tmp file was not created!');
$lineCount = \DCarbone\Helpers\FileHelper::getLineCount($groupDir.'queue.tmp'); $lineCount = \DCarbone\Helpers\FileHelper::getLineCount($uglyQueue->getQueueTmpFile());
$this->assertEquals(11, $lineCount); $this->assertEquals(11, $lineCount);
@@ -377,9 +341,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
{ {
$uglyQueue->_populateQueue(); $uglyQueue->_populateQueue();
$groupDir = $uglyQueue->path; $this->assertFileNotExists($uglyQueue->getQueueTmpFile());
$this->assertFileNotExists($groupDir.'queue.tmp');
$uglyQueue->_populateQueue(); $uglyQueue->_populateQueue();
@@ -395,7 +357,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
*/ */
public function testCanGetCountOfItemsInPopulatedQueue(\DCarbone\UglyQueue $uglyQueue) public function testCanGetCountOfItemsInPopulatedQueue(\DCarbone\UglyQueue $uglyQueue)
{ {
$itemCount = $uglyQueue->getQueueItemCount(); $itemCount = count($uglyQueue);
$this->assertEquals(11, $itemCount); $this->assertEquals(11, $itemCount);
} }
@@ -414,7 +376,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
} }
/** /**
* @covers \DCarbone\UglyQueue::processQueue * @covers \DCarbone\UglyQueue::retrieveItems
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock * @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
@@ -422,11 +384,11 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
*/ */
public function testExceptionThrownWhenTryingToProcessLockedQueueWithNonInteger(\DCarbone\UglyQueue $uglyQueue) public function testExceptionThrownWhenTryingToProcessLockedQueueWithNonInteger(\DCarbone\UglyQueue $uglyQueue)
{ {
$process = $uglyQueue->processQueue('Eleventy Billion'); $uglyQueue->retrieveItems('Eleventy Billion');
} }
/** /**
* @covers \DCarbone\UglyQueue::processQueue * @covers \DCarbone\UglyQueue::retrieveItems
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock * @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
@@ -434,11 +396,11 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
*/ */
public function testExceptionThrownWhenTryingToProcessLockedQueueWithIntegerLessThan1(\DCarbone\UglyQueue $uglyQueue) public function testExceptionThrownWhenTryingToProcessLockedQueueWithIntegerLessThan1(\DCarbone\UglyQueue $uglyQueue)
{ {
$process = $uglyQueue->processQueue(0); $uglyQueue->retrieveItems(0);
} }
/** /**
* @covers \DCarbone\UglyQueue::processQueue * @covers \DCarbone\UglyQueue::retrieveItems
* @covers \DCarbone\UglyQueue::getQueueItemCount * @covers \DCarbone\UglyQueue::getQueueItemCount
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @uses \DCarbone\Helpers\FileHelper * @uses \DCarbone\Helpers\FileHelper
@@ -448,20 +410,20 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
*/ */
public function testCanGetPartialQueueContents(\DCarbone\UglyQueue $uglyQueue) public function testCanGetPartialQueueContents(\DCarbone\UglyQueue $uglyQueue)
{ {
$process = $uglyQueue->processQueue(5); $process = $uglyQueue->retrieveItems(5);
$this->assertEquals(5, count($process)); $this->assertEquals(5, count($process));
$this->assertArrayHasKey('0', $process); $this->assertArrayHasKey('0', $process);
$this->assertArrayHasKey('4', $process); $this->assertArrayHasKey('4', $process);
$this->assertEquals(6, $uglyQueue->getQueueItemCount()); $this->assertEquals(6, count($uglyQueue));
return $uglyQueue; return $uglyQueue;
} }
/** /**
* @covers \DCarbone\UglyQueue::processQueue * @covers \DCarbone\UglyQueue::retrieveItems
* @covers \DCarbone\UglyQueue::getQueueItemCount * @covers \DCarbone\UglyQueue::getQueueItemCount
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @uses \DCarbone\Helpers\FileHelper * @uses \DCarbone\Helpers\FileHelper
@@ -471,14 +433,14 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
*/ */
public function testCanGetFullQueueContents(\DCarbone\UglyQueue $uglyQueue) public function testCanGetFullQueueContents(\DCarbone\UglyQueue $uglyQueue)
{ {
$process = $uglyQueue->processQueue(6); $process = $uglyQueue->retrieveItems(6);
$this->assertEquals(6, count($process)); $this->assertEquals(6, count($process));
$this->assertArrayHasKey('10', $process); $this->assertArrayHasKey('10', $process);
$this->assertArrayHasKey('5', $process); $this->assertArrayHasKey('5', $process);
$this->assertEquals(0, $uglyQueue->getQueueItemCount()); $this->assertEquals(0, count($uglyQueue));
return $uglyQueue; return $uglyQueue;
} }

View File

@@ -5,30 +5,31 @@
*/ */
class UglyQueueManagerTest extends PHPUnit_Framework_TestCase class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
{ {
protected $baseDir;
protected $reallyTastySandwich = array( protected $reallyTastySandwich = array(
'0' => 'beef broth', '0' => 'beef broth',
'1' => 'barbeque sauce', '1' => 'barbeque sauce',
'2' => 'boneless pork ribs', '2' => 'boneless pork ribs',
); );
protected function setUp()
{
$this->baseDir = __DIR__.'/../misc/queues';
}
/** /**
* @covers \DCarbone\UglyQueueManager::__construct * @covers \DCarbone\UglyQueueManager::__construct
* @covers \DCarbone\UglyQueueManager::init
* @covers \DCarbone\UglyQueue::unserialize * @covers \DCarbone\UglyQueue::unserialize
* @covers \DCarbone\UglyQueue::__get
* @covers \DCarbone\UglyQueueManager::addQueue * @covers \DCarbone\UglyQueueManager::addQueue
* @covers \DCarbone\UglyQueueManager::containsQueueWithName * @covers \DCarbone\UglyQueueManager::containsQueueWithName
* @uses \DCarbone\UglyQueueManager * @uses \DCarbone\UglyQueueManager
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @return \DCarbone\UglyQueueManager * @return \DCarbone\UglyQueueManager
*/ */
public function testCanInitializeManagerWithConfigAndNoObservers() public function testCanInitializeObjectWithValidPath()
{ {
$config = array( $manager = new \DCarbone\UglyQueueManager($this->baseDir);
'queue-base-dir' => __DIR__.'/../misc/queues'
);
$manager = \DCarbone\UglyQueueManager::init($config);
$this->assertInstanceOf('\\DCarbone\\UglyQueueManager', $manager); $this->assertInstanceOf('\\DCarbone\\UglyQueueManager', $manager);
@@ -36,39 +37,20 @@ class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
} }
/** /**
* @covers \DCarbone\UglyQueueManager::init
* @covers \DCarbone\UglyQueueManager::__construct * @covers \DCarbone\UglyQueueManager::__construct
* @uses \DCarbone\UglyQueueManager * @uses \DCarbone\UglyQueueManager
* @expectedException \RuntimeException * @expectedException \RuntimeException
*/ */
public function testExceptionThrownDuringConstructionWithInvalidBasePathValue() public function testExceptionThrownDuringConstructionWithInvalidBasePathValue()
{ {
$config = array( new \DCarbone\UglyQueueManager('i do not exist!');
'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 * @covers \DCarbone\UglyQueueManager::containsQueueWithName
* @uses \DCarbone\UglyQueueManager * @uses \DCarbone\UglyQueueManager
* @depends testCanInitializeManagerWithConfigAndNoObservers * @depends testCanInitializeObjectWithValidPath
* @param \DCarbone\UglyQueueManager $manager * @param \DCarbone\UglyQueueManager $manager
*/ */
public function testCanDetermineIfValidQueueExistsInManager(\DCarbone\UglyQueueManager $manager) public function testCanDetermineIfValidQueueExistsInManager(\DCarbone\UglyQueueManager $manager)
@@ -81,7 +63,7 @@ class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
/** /**
* @covers \DCarbone\UglyQueueManager::containsQueueWithName * @covers \DCarbone\UglyQueueManager::containsQueueWithName
* @uses \DCarbone\UglyQueueManager * @uses \DCarbone\UglyQueueManager
* @depends testCanInitializeManagerWithConfigAndNoObservers * @depends testCanInitializeObjectWithValidPath
* @param \DCarbone\UglyQueueManager $manager * @param \DCarbone\UglyQueueManager $manager
*/ */
public function testCanDetermineQueueDoesNotExistInManager(\DCarbone\UglyQueueManager $manager) public function testCanDetermineQueueDoesNotExistInManager(\DCarbone\UglyQueueManager $manager)
@@ -93,10 +75,9 @@ class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
/** /**
* @covers \DCarbone\UglyQueueManager::getQueueWithName * @covers \DCarbone\UglyQueueManager::getQueueWithName
* @covers \DCarbone\UglyQueue::__get
* @uses \DCarbone\UglyQueueManager * @uses \DCarbone\UglyQueueManager
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @depends testCanInitializeManagerWithConfigAndNoObservers * @depends testCanInitializeObjectWithValidPath
* @param \DCarbone\UglyQueueManager $manager * @param \DCarbone\UglyQueueManager $manager
*/ */
public function testCanGetUglyQueueObjectFromManager(\DCarbone\UglyQueueManager $manager) public function testCanGetUglyQueueObjectFromManager(\DCarbone\UglyQueueManager $manager)
@@ -104,25 +85,25 @@ class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
$uglyQueue = $manager->getQueueWithName('tasty-sandwich'); $uglyQueue = $manager->getQueueWithName('tasty-sandwich');
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue); $this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
$this->assertEquals('tasty-sandwich', $uglyQueue->name); $this->assertEquals('tasty-sandwich', $uglyQueue->getName());
} }
/** /**
* @covers \DCarbone\UglyQueueManager::getQueueWithName * @covers \DCarbone\UglyQueueManager::getQueueWithName
* @uses \DCarbone\UglyQueueManager * @uses \DCarbone\UglyQueueManager
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
* @depends testCanInitializeManagerWithConfigAndNoObservers * @depends testCanInitializeObjectWithValidPath
* @param \DCarbone\UglyQueueManager $manager * @param \DCarbone\UglyQueueManager $manager
*/ */
public function testExceptionThrownWhenTryingToGetNonExistentQueueFromManager(\DCarbone\UglyQueueManager $manager) public function testExceptionThrownWhenTryingToGetNonExistentQueueFromManager(\DCarbone\UglyQueueManager $manager)
{ {
$shouldNotExist = $manager->getQueueWithName('sandwiches'); $manager->getQueueWithName('sandwiches');
} }
/** /**
* @covers \DCarbone\UglyQueueManager::getQueueList * @covers \DCarbone\UglyQueueManager::getQueueList
* @uses \DCarbone\UglyQueueManager * @uses \DCarbone\UglyQueueManager
* @depends testCanInitializeManagerWithConfigAndNoObservers * @depends testCanInitializeObjectWithValidPath
* @param \DCarbone\UglyQueueManager $manager * @param \DCarbone\UglyQueueManager $manager
*/ */
public function testCanGetListOfQueuesInManager(\DCarbone\UglyQueueManager $manager) public function testCanGetListOfQueuesInManager(\DCarbone\UglyQueueManager $manager)
@@ -140,7 +121,7 @@ class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
* @uses \DCarbone\UglyQueueManager * @uses \DCarbone\UglyQueueManager
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @expectedException \RuntimeException * @expectedException \RuntimeException
* @depends testCanInitializeManagerWithConfigAndNoObservers * @depends testCanInitializeObjectWithValidPath
* @param \DCarbone\UglyQueueManager $manager * @param \DCarbone\UglyQueueManager $manager
*/ */
public function testExceptionThrownWhenReAddingQueueToManager(\DCarbone\UglyQueueManager $manager) public function testExceptionThrownWhenReAddingQueueToManager(\DCarbone\UglyQueueManager $manager)
@@ -156,7 +137,7 @@ class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
* @covers \DCarbone\UglyQueueManager::getQueueWithName * @covers \DCarbone\UglyQueueManager::getQueueWithName
* @uses \DCarbone\UglyQueueManager * @uses \DCarbone\UglyQueueManager
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue
* @depends testCanInitializeManagerWithConfigAndNoObservers * @depends testCanInitializeObjectWithValidPath
* @param \DCarbone\UglyQueueManager $manager * @param \DCarbone\UglyQueueManager $manager
*/ */
public function testCanInitializeNewQueueAndAddToManager(\DCarbone\UglyQueueManager $manager) public function testCanInitializeNewQueueAndAddToManager(\DCarbone\UglyQueueManager $manager)
@@ -166,7 +147,7 @@ class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
$uglyQueue = $manager->getQueueWithName('really-tasty-sandwich'); $uglyQueue = $manager->getQueueWithName('really-tasty-sandwich');
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue); $this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
$this->assertEquals('really-tasty-sandwich', $uglyQueue->name); $this->assertEquals('really-tasty-sandwich', $uglyQueue->getName());
$queueList = $manager->getQueueList(); $queueList = $manager->getQueueList();
@@ -178,7 +159,7 @@ class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
/** /**
* @covers \DCarbone\UglyQueueManager::removeQueueByName * @covers \DCarbone\UglyQueueManager::removeQueueByName
* @uses \DCarbone\UglyQueueManager * @uses \DCarbone\UglyQueueManager
* @depends testCanInitializeManagerWithConfigAndNoObservers * @depends testCanInitializeObjectWithValidPath
* @param \DCarbone\UglyQueueManager $manager * @param \DCarbone\UglyQueueManager $manager
*/ */
public function testCanRemoveQueueFromManagerByName(\DCarbone\UglyQueueManager $manager) public function testCanRemoveQueueFromManagerByName(\DCarbone\UglyQueueManager $manager)