You've already forked ugly-queue
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d20106a021 | |||
| 97dba637d9 | |||
| 793edc2d60 | |||
| 1e98b0cf7e | |||
| 89c51468eb | |||
| d33fd6bcba | |||
| 89cb1ae4b1 | |||
| 12f6edf269 | |||
| 0b3b876b64 | |||
| e07cb21821 | |||
| 9387bb8843 | |||
| 1825c09123 | |||
| 728c6a5a7d | |||
| d63950b5f5 | |||
| 6dcb89742e | |||
| b166fd3497 | |||
| 734ec3d5c4 |
16
.travis.yml
Normal file
16
.travis.yml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
language: php
|
||||||
|
|
||||||
|
php:
|
||||||
|
- 5.3.3
|
||||||
|
- 5.3
|
||||||
|
- 5.4
|
||||||
|
- 5.5
|
||||||
|
- 5.6
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- composer self-update
|
||||||
|
- composer update --prefer-source
|
||||||
|
- composer install --no-interaction --dev --prefer-source
|
||||||
|
|
||||||
|
script:
|
||||||
|
- ./vendor/bin/phpunit
|
||||||
@@ -3,4 +3,6 @@ ugly-queue
|
|||||||
|
|
||||||
A simple file-based queue system for PHP 5.3.3+
|
A simple file-based queue system for PHP 5.3.3+
|
||||||
|
|
||||||
Documentation and Test suites forthcoming.
|
Build status: [](https://travis-ci.org/dcarbone/ugly-queue)
|
||||||
|
|
||||||
|
Documentation and Test suites forthcoming.
|
||||||
|
|||||||
@@ -17,6 +17,11 @@
|
|||||||
<exclude>./tests/misc</exclude>
|
<exclude>./tests/misc</exclude>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
|
|
||||||
|
<testsuite name="UglyQueueManager">
|
||||||
|
<directory>./tests/UglyQueueManager</directory>
|
||||||
|
<exclude>./tests/misc</exclude>
|
||||||
|
</testsuite>
|
||||||
|
|
||||||
<filter>
|
<filter>
|
||||||
<whitelist addUncoveredFilesFromWhitelist="true">
|
<whitelist addUncoveredFilesFromWhitelist="true">
|
||||||
<directory suffix=".php">./src</directory>
|
<directory suffix=".php">./src</directory>
|
||||||
|
|||||||
@@ -5,147 +5,89 @@ use DCarbone\Helpers\FileHelper;
|
|||||||
/**
|
/**
|
||||||
* Class UglyQueue
|
* Class UglyQueue
|
||||||
* @package DCarbone
|
* @package DCarbone
|
||||||
|
*
|
||||||
|
* @property string name
|
||||||
|
* @property string path
|
||||||
|
* @property bool locked
|
||||||
*/
|
*/
|
||||||
class UglyQueue
|
class UglyQueue implements \Serializable, \SplSubject
|
||||||
{
|
{
|
||||||
|
const NOTIFY_QUEUE_INITIALIZED = 0;
|
||||||
|
const NOTIFY_QUEUE_LOCKED = 1;
|
||||||
|
const NOTIFY_QUEUE_FAILED_TO_LOCK = 2;
|
||||||
|
const NOTIFY_QUEUE_LOCKED_BY_OTHER_PROCESS = 3;
|
||||||
|
const NOTIFY_QUEUE_UNLOCKED = 4;
|
||||||
|
const NOTIFY_QUEUE_PROCESSING = 5;
|
||||||
|
const NOTIFY_QUEUE_REACHED_END = 6;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
public $notifyStatus;
|
||||||
|
|
||||||
|
const QUEUE_READONLY = 0;
|
||||||
|
const QUEUE_READWRITE = 1;
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
protected $config;
|
private $observers = array();
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
protected $mode = null;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $queueBaseDir;
|
protected $_name;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $queueGroup = null;
|
protected $_path;
|
||||||
|
|
||||||
/** @var string */
|
|
||||||
protected $queueGroupDirPath = null;
|
|
||||||
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
protected $haveLock = false;
|
protected $_locked = false;
|
||||||
|
|
||||||
/** @var bool */
|
|
||||||
protected $init = false;
|
|
||||||
|
|
||||||
/** @var resource */
|
/** @var resource */
|
||||||
protected $_tmpHandle;
|
protected $_tmpHandle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $config
|
* @param string $directoryPath
|
||||||
|
* @param array $observers
|
||||||
* @throws \RuntimeException
|
* @throws \RuntimeException
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
|
* @return UglyQueue
|
||||||
*/
|
*/
|
||||||
public function __construct(array $config)
|
public static function queueWithDirectoryPathAndObservers($directoryPath, array $observers = array())
|
||||||
{
|
{
|
||||||
if (!isset($config['queue-base-dir']))
|
if (!is_string($directoryPath))
|
||||||
throw new \InvalidArgumentException('UglyQueue::__construct - "$config" parameter "queue-base-dir" not seen.');
|
throw new \InvalidArgumentException('Argument 1 expected to be string, '.gettype($directoryPath).' seen');
|
||||||
|
|
||||||
if (!is_dir($config['queue-base-dir']) || !is_writable($config['queue-base-dir']))
|
if (($directoryPath = trim($directoryPath)) === '')
|
||||||
throw new \RuntimeException('UglyQueue::__construct - "$config[\'queue-base-dir\']" points to a directory that either doesn\'t exist or is not writable');
|
throw new \InvalidArgumentException('Empty string passed for argument 1');
|
||||||
|
|
||||||
$this->config = $config;
|
if (file_exists($directoryPath))
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Destructor
|
|
||||||
*/
|
|
||||||
public function __destruct()
|
|
||||||
{
|
|
||||||
$this->unlock();
|
|
||||||
$this->_populateQueue();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $ttl Time to live in seconds
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function lock($ttl = 250)
|
|
||||||
{
|
|
||||||
$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 file_queue, writing out contents to file.
|
|
||||||
*/
|
|
||||||
public function unlock()
|
|
||||||
{
|
|
||||||
if ($this->haveLock === true)
|
|
||||||
{
|
{
|
||||||
@FileHelper::superUnlink($this->queueGroupDirPath.'queue.lock');
|
if (!is_dir($directoryPath))
|
||||||
$this->haveLock = false;
|
throw new \RuntimeException('Argument 1 expected to be path to directory, path to non-directory seen');
|
||||||
}
|
}
|
||||||
}
|
else if (!@mkdir($directoryPath))
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function isLocked()
|
|
||||||
{
|
|
||||||
// First check for lock file
|
|
||||||
if (is_file($this->queueGroupDirPath.'queue.lock'))
|
|
||||||
{
|
{
|
||||||
$lock = json_decode(file_get_contents($this->queueGroupDirPath.'queue.lock'), true);
|
throw new \RuntimeException('Unable to create queue directory at path: "'.$directoryPath.'".');
|
||||||
|
|
||||||
// If we have an invalid lock structure, THIS IS BAD.
|
|
||||||
if (!isset($lock['ttl']) || !isset($lock['born']))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
$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!
|
|
||||||
@FileHelper::superUnlink($this->queueGroupDirPath.'queue.lock');
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no file, assume not locked.
|
$uglyQueue = new UglyQueue();
|
||||||
return false;
|
$uglyQueue->observers = $observers;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
$split = preg_split('#[/\\\]+#', $directoryPath);
|
||||||
* @param string $queue_group
|
|
||||||
*/
|
|
||||||
public function initialize($queue_group)
|
|
||||||
{
|
|
||||||
$this->queueBaseDir = $this->config['queue-base-dir'];
|
|
||||||
|
|
||||||
$this->queueGroup = $queue_group;
|
$uglyQueue->_name = end($split);
|
||||||
$this->queueGroupDirPath = $this->queueBaseDir.$queue_group.DIRECTORY_SEPARATOR;
|
$uglyQueue->_path = rtrim(realpath(implode(DIRECTORY_SEPARATOR, $split)), "/\\").DIRECTORY_SEPARATOR;
|
||||||
|
|
||||||
// Create directory for this queue group
|
if (is_writable($uglyQueue->_path))
|
||||||
if (!is_dir($this->queueGroupDirPath))
|
$uglyQueue->mode = self::QUEUE_READWRITE;
|
||||||
mkdir($this->queueGroupDirPath);
|
else if (is_readable($uglyQueue->_path))
|
||||||
|
$uglyQueue->mode = self::QUEUE_READONLY;
|
||||||
|
|
||||||
// Insert "don't look here" index.html file
|
// Insert "don't look here" index.html file
|
||||||
if (!file_exists($this->queueGroupDirPath.'index.html'))
|
if (!file_exists($uglyQueue->_path.'index.html'))
|
||||||
{
|
{
|
||||||
|
if ($uglyQueue->mode === self::QUEUE_READONLY)
|
||||||
|
throw new \RuntimeException('Cannot initialize queue with name "'.$uglyQueue->_name.'", the user lacks permission to create files.');
|
||||||
|
|
||||||
$html = <<<HTML
|
$html = <<<HTML
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
@@ -156,46 +98,201 @@ class UglyQueue
|
|||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
HTML;
|
HTML;
|
||||||
file_put_contents($this->queueGroupDirPath.'index.html', $html);
|
file_put_contents($uglyQueue->_path.'index.html', $html);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!file_exists($this->queueGroupDirPath.'queue.txt'))
|
if (!file_exists($uglyQueue->_path.'queue.txt'))
|
||||||
file_put_contents($this->queueGroupDirPath.'queue.txt', '');
|
{
|
||||||
|
if ($uglyQueue->mode === self::QUEUE_READONLY)
|
||||||
|
throw new \RuntimeException('Cannot initialize queue with name "'.$uglyQueue->_name.'", the user lacks permission to create files.');
|
||||||
|
|
||||||
$this->init = true;
|
file_put_contents($uglyQueue->_path.'queue.txt', '');
|
||||||
|
}
|
||||||
|
|
||||||
|
$uglyQueue->notifyStatus = self::NOTIFY_QUEUE_INITIALIZED;
|
||||||
|
$uglyQueue->notify();
|
||||||
|
|
||||||
|
return $uglyQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $param
|
||||||
|
* @return string
|
||||||
|
* @throws \OutOfBoundsException
|
||||||
|
*/
|
||||||
|
public function __get($param)
|
||||||
|
{
|
||||||
|
switch($param)
|
||||||
|
{
|
||||||
|
case 'name' :
|
||||||
|
return $this->_name;
|
||||||
|
|
||||||
|
case 'path':
|
||||||
|
return $this->_path;
|
||||||
|
|
||||||
|
case 'locked':
|
||||||
|
return $this->_locked;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new \OutOfBoundsException(get_class($this).' does not have a property named "'.$param.'".');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destructor
|
||||||
|
*/
|
||||||
|
public function __destruct()
|
||||||
|
{
|
||||||
|
$this->_populateQueue();
|
||||||
|
$this->unlock();
|
||||||
|
file_put_contents($this->_path.UglyQueueManager::UGLY_QUEUE_SERIALIZED_NAME, serialize($this));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $ttl Time to live in seconds
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function lock($ttl = 250)
|
||||||
|
{
|
||||||
|
if (!is_int($ttl))
|
||||||
|
throw new \InvalidArgumentException('Argument 1 expected to be integer, "'.gettype($ttl).'" seen');
|
||||||
|
|
||||||
|
if ($ttl < 0)
|
||||||
|
throw new \InvalidArgumentException('Argument 1 expected to be positive integer, "'.$ttl.'" seen');
|
||||||
|
|
||||||
|
$alreadyLocked = $this->isLocked();
|
||||||
|
|
||||||
|
// If there is currently no lock
|
||||||
|
if ($alreadyLocked === false)
|
||||||
|
return $this->createLockFile($ttl);
|
||||||
|
|
||||||
|
// If we make it this far, there is already a lock in place.
|
||||||
|
$this->_locked = false;
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_LOCKED_BY_OTHER_PROCESS;
|
||||||
|
$this->notify();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $ttl seconds to live
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function createLockFile($ttl)
|
||||||
|
{
|
||||||
|
$ok = (bool)@file_put_contents(
|
||||||
|
$this->_path.'queue.lock',
|
||||||
|
json_encode(array('ttl' => $ttl, 'born' => time())));
|
||||||
|
|
||||||
|
if ($ok !== true)
|
||||||
|
{
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_FAILED_TO_LOCK;
|
||||||
|
$this->notify();
|
||||||
|
return $this->_locked = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->_locked = true;
|
||||||
|
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_LOCKED;
|
||||||
|
$this->notify();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close this ugly queue, writing out contents to file.
|
||||||
|
*/
|
||||||
|
public function unlock()
|
||||||
|
{
|
||||||
|
if ($this->_locked === true)
|
||||||
|
{
|
||||||
|
unlink($this->_path.'queue.lock');
|
||||||
|
$this->_locked = false;
|
||||||
|
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_UNLOCKED;
|
||||||
|
$this->notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \RuntimeException
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isLocked()
|
||||||
|
{
|
||||||
|
// First check for lock file
|
||||||
|
if (is_file($this->_path.'queue.lock'))
|
||||||
|
{
|
||||||
|
$lock = json_decode(file_get_contents($this->_path.'queue.lock'), true);
|
||||||
|
|
||||||
|
// If we have an invalid lock structure.
|
||||||
|
if (!isset($lock['ttl']) || !isset($lock['born']))
|
||||||
|
throw new \RuntimeException('Invalid "queue.lock" file structure seen at "'.$this->_path.'queue.lock".');
|
||||||
|
|
||||||
|
// Otherwise...
|
||||||
|
$lock_ttl = ((int)$lock['born'] + (int)$lock['ttl']);
|
||||||
|
|
||||||
|
// If we're within the TTL of the lock, assume another thread is already processing.
|
||||||
|
// We'll pick it up on the next go around.
|
||||||
|
if ($lock_ttl > time())
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// Else, remove lock file and assume we're good to go!
|
||||||
|
unlink($this->_path.'queue.lock');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no file, assume not locked.
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $count
|
* @param int $count
|
||||||
* @throws \RuntimeException
|
* @throws \RuntimeException
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
* @return bool|array
|
* @return bool|array
|
||||||
*/
|
*/
|
||||||
public function processQueue($count = 1)
|
public function processQueue($count = 1)
|
||||||
{
|
{
|
||||||
if ($this->init === false)
|
if ($this->mode === self::QUEUE_READONLY)
|
||||||
throw new \RuntimeException('file_queue::load_queue_data - Must first initialize queue!');
|
throw new \RuntimeException('Queue "'.$this->_name.'" cannot be processed. It was started in Read-Only mode (the user running this process does not have permission to write to the queue directory).');
|
||||||
|
|
||||||
// If we don't have a lock, assume issue and move on.
|
// If we don't have a lock, assume issue and move on.
|
||||||
if ($this->haveLock === false || !file_exists($this->queueGroupDirPath.'queue.txt'))
|
if ($this->_locked === false)
|
||||||
return 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('Argument 1 expected to be integer greater than 0, "'.gettype($count).'" seen');
|
||||||
|
|
||||||
|
// If negative integer passed
|
||||||
|
if ($count <= 0)
|
||||||
|
throw new \InvalidArgumentException('Argument 1 expected to be integer greater than 0, "'.$count.'" seen');
|
||||||
|
|
||||||
|
if ($this->notifyStatus !== self::NOTIFY_QUEUE_PROCESSING)
|
||||||
|
{
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_PROCESSING;
|
||||||
|
$this->notify();
|
||||||
|
}
|
||||||
|
|
||||||
// Find number of lines in the queue file
|
// Find number of lines in the queue file
|
||||||
$line_count = FileHelper::getLineCount($this->queueGroupDirPath.'queue.txt');
|
$lineCount = FileHelper::getLineCount($this->_path.'queue.txt');
|
||||||
|
|
||||||
// If queue line count is 0, assume empty
|
// If queue line count is 0, assume empty
|
||||||
if ($line_count === 0)
|
if ($lineCount === 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Try to open the file for reading / writing.
|
// Try to open the file for reading / writing.
|
||||||
$queue_file_handle = fopen($this->queueGroupDirPath.'queue.txt', 'r+');
|
$queueFileHandle = fopen($this->_path.'queue.txt', 'r+');
|
||||||
if ($queue_file_handle === false)
|
if ($queueFileHandle === false)
|
||||||
$this->unlock();
|
$this->unlock();
|
||||||
|
|
||||||
// Get an array of the oldest $count data in the queue
|
// Get an array of the oldest $count data in the queue
|
||||||
$data = array();
|
$data = array();
|
||||||
$start_line = $line_count - $count;
|
$start_line = $lineCount - $count;
|
||||||
$i = 0;
|
$i = 0;
|
||||||
while (($line = fscanf($queue_file_handle, "%s\t%s\n")) !== false && $i < $line_count)
|
while (($line = fscanf($queueFileHandle, "%s\t%s\n")) !== false && $i < $lineCount)
|
||||||
{
|
{
|
||||||
if ($i++ >= $start_line)
|
if ($i++ >= $start_line)
|
||||||
{
|
{
|
||||||
@@ -205,20 +302,22 @@ HTML;
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If we have consumed the rest of the file
|
// If we have consumed the rest of the file
|
||||||
if ($count >= $line_count)
|
if ($count >= $lineCount)
|
||||||
{
|
{
|
||||||
rewind($queue_file_handle);
|
rewind($queueFileHandle);
|
||||||
ftruncate($queue_file_handle, 0);
|
ftruncate($queueFileHandle, 0);
|
||||||
fclose($queue_file_handle);
|
fclose($queueFileHandle);
|
||||||
$this->unlock();
|
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_REACHED_END;
|
||||||
|
$this->notify();
|
||||||
}
|
}
|
||||||
// Otherwise, create new queue file minus the processed lines.
|
// Otherwise, create new queue file minus the processed lines.
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$tmp = fopen($this->queueGroupDirPath.'queue.tmp', 'w+');
|
$tmp = fopen($this->_path.'queue.tmp', 'w+');
|
||||||
rewind($queue_file_handle);
|
rewind($queueFileHandle);
|
||||||
$i = 0;
|
$i = 0;
|
||||||
while (($line = fgets($queue_file_handle)) !== false && $i < $start_line)
|
while (($line = fgets($queueFileHandle)) !== false && $i < $start_line)
|
||||||
{
|
{
|
||||||
if ($line !== "\n" || $line !== "")
|
if ($line !== "\n" || $line !== "")
|
||||||
fwrite($tmp, $line);
|
fwrite($tmp, $line);
|
||||||
@@ -226,10 +325,10 @@ HTML;
|
|||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose($queue_file_handle);
|
fclose($queueFileHandle);
|
||||||
fclose($tmp);
|
fclose($tmp);
|
||||||
FileHelper::superUnlink($this->queueGroupDirPath.'queue.txt');
|
unlink($this->_path.'queue.txt');
|
||||||
rename($this->queueGroupDirPath.'queue.tmp', $this->queueGroupDirPath.'queue.txt');
|
rename($this->_path.'queue.tmp', $this->_path.'queue.txt');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
@@ -243,18 +342,18 @@ HTML;
|
|||||||
*/
|
*/
|
||||||
public function addToQueue($key, $value)
|
public function addToQueue($key, $value)
|
||||||
{
|
{
|
||||||
if ($this->init === false)
|
if ($this->mode === self::QUEUE_READONLY)
|
||||||
throw new \RuntimeException('file_queue::add_to_queue - Must first initialize queue!');
|
throw new \RuntimeException('Cannot add items to queue "'.$this->_name.'" as it is in read-only mode');
|
||||||
|
|
||||||
// If we don't have a lock, assume issue and move on.
|
// If we don't have a lock, assume issue and move on.
|
||||||
if ($this->haveLock === false)
|
if ($this->_locked === false)
|
||||||
return false;
|
throw new \RuntimeException('Cannot add items to queue "'.$this->_name.'". Queue is already locked by another process');
|
||||||
|
|
||||||
if (!is_resource($this->_tmpHandle))
|
if (!is_resource($this->_tmpHandle))
|
||||||
{
|
{
|
||||||
$this->_tmpHandle = fopen($this->queueGroupDirPath.'queue.tmp', 'w+');
|
$this->_tmpHandle = fopen($this->_path.'queue.tmp', 'w+');
|
||||||
if ($this->_tmpHandle === false)
|
if ($this->_tmpHandle === false)
|
||||||
return false;
|
throw new \RuntimeException('Unable to create "queue.tmp" file.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_array($value) || $value instanceof \stdClass)
|
if (is_array($value) || $value instanceof \stdClass)
|
||||||
@@ -271,41 +370,133 @@ HTML;
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function _populateQueue()
|
public function _populateQueue()
|
||||||
{
|
{
|
||||||
if (is_resource($this->_tmpHandle))
|
if (is_resource($this->_tmpHandle))
|
||||||
{
|
{
|
||||||
if (file_exists($this->queueGroupDirPath.'queue.txt'))
|
if (file_exists($this->_path.'queue.txt'))
|
||||||
{
|
{
|
||||||
$queue_file_handle = fopen($this->queueGroupDirPath.'queue.txt', 'r+');
|
$queueFileHandle = fopen($this->_path.'queue.txt', 'r+');
|
||||||
while (($line = fgets($queue_file_handle)) !== false)
|
while (($line = fgets($queueFileHandle)) !== false)
|
||||||
{
|
{
|
||||||
if ($line !== "\n" && $line !== "")
|
if ($line !== "\n" && $line !== "")
|
||||||
fwrite($this->_tmpHandle, $line);
|
fwrite($this->_tmpHandle, $line);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose($queue_file_handle);
|
fclose($queueFileHandle);
|
||||||
FileHelper::superUnlink($this->queueGroupDirPath.'queue.txt');
|
unlink($this->_path.'queue.txt');
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose($this->_tmpHandle);
|
fclose($this->_tmpHandle);
|
||||||
rename($this->queueGroupDirPath.'queue.tmp', $this->queueGroupDirPath.'queue.txt');
|
rename($this->_path.'queue.tmp', $this->_path.'queue.txt');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return int
|
||||||
|
* @throws \RuntimeException
|
||||||
*/
|
*/
|
||||||
public function getQueueGroup()
|
public function getQueueItemCount()
|
||||||
{
|
{
|
||||||
return $this->queueGroup;
|
return FileHelper::getLineCount($this->_path.'queue.txt');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @param string $key
|
||||||
|
* @return bool
|
||||||
|
* @throws \RuntimeException
|
||||||
*/
|
*/
|
||||||
public function getQueueBaseDir()
|
public function keyExistsInQueue($key)
|
||||||
{
|
{
|
||||||
return $this->queueBaseDir;
|
$key = (string)$key;
|
||||||
|
|
||||||
|
// Try to open the file for reading / writing.
|
||||||
|
$queueFileHandle = fopen($this->_path.'queue.txt', 'r');
|
||||||
|
|
||||||
|
while(($line = fscanf($queueFileHandle, "%s\t%s\n")) !== false)
|
||||||
|
{
|
||||||
|
list ($lineKey, $lineValue) = $line;
|
||||||
|
|
||||||
|
if ($key === $lineKey)
|
||||||
|
{
|
||||||
|
fclose($queueFileHandle);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose($queueFileHandle);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (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 serialize()
|
||||||
|
{
|
||||||
|
return serialize(array($this->_name, $this->_path));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (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 unserialize($serialized)
|
||||||
|
{
|
||||||
|
/** @var \DCarbone\UglyQueue $uglyQueue */
|
||||||
|
$data = unserialize($serialized);
|
||||||
|
$this->_name = $data[0];
|
||||||
|
$this->_path = $data[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (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))
|
||||||
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
230
src/UglyQueueManager.php
Normal file
230
src/UglyQueueManager.php
Normal file
@@ -0,0 +1,230 @@
|
|||||||
|
<?php namespace DCarbone;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UglyQueueManager
|
||||||
|
* @package DCarbone
|
||||||
|
*/
|
||||||
|
class UglyQueueManager implements \SplObserver, \SplSubject
|
||||||
|
{
|
||||||
|
const NOTIFY_MANAGER_INITIALIZED = 0;
|
||||||
|
const NOTIFY_QUEUE_ADDED = 1;
|
||||||
|
const NOTIFY_QUEUE_REMOVED = 2;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
public $notifyStatus;
|
||||||
|
|
||||||
|
const UGLY_QUEUE_SERIALIZED_NAME = 'ugly-queue.obj';
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
private $observers = array();
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
protected $queues = array();
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
protected $config = array();
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $queueBaseDir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param array $config
|
||||||
|
* @param array $observers
|
||||||
|
* @throws \RuntimeException
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
protected function __construct(array $config, array $observers = array())
|
||||||
|
{
|
||||||
|
if (!isset($config['queue-base-dir']))
|
||||||
|
throw new \InvalidArgumentException('"$config" parameter "queue-base-dir" not seen.');
|
||||||
|
|
||||||
|
if (!is_dir($config['queue-base-dir']))
|
||||||
|
throw new \RuntimeException('"queue-base-dir" points to a directory that does not exist.');
|
||||||
|
|
||||||
|
$this->config = $config;
|
||||||
|
$this->queueBaseDir = rtrim(realpath($this->config['queue-base-dir']), "/\\").DIRECTORY_SEPARATOR;
|
||||||
|
$this->observers = $observers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $config
|
||||||
|
* @param array $observers
|
||||||
|
* @return UglyQueueManager
|
||||||
|
*/
|
||||||
|
public static function init(array $config, array $observers = array())
|
||||||
|
{
|
||||||
|
/** @var \DCarbone\UglyQueueManager $manager */
|
||||||
|
$manager = new static($config, $observers);
|
||||||
|
|
||||||
|
/** @var \DCarbone\UglyQueue $uglyQueue */
|
||||||
|
|
||||||
|
foreach(glob($manager->queueBaseDir.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $queueDir)
|
||||||
|
{
|
||||||
|
// Try to avoid looking at hidden directories or magic dirs such as '.' and '..'
|
||||||
|
$split = preg_split('#[/\\\]+#', $queueDir);
|
||||||
|
if (strpos(end($split), '.') === 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (file_exists($queueDir.DIRECTORY_SEPARATOR.self::UGLY_QUEUE_SERIALIZED_NAME))
|
||||||
|
$uglyQueue = unserialize(file_get_contents($queueDir.DIRECTORY_SEPARATOR.self::UGLY_QUEUE_SERIALIZED_NAME));
|
||||||
|
else
|
||||||
|
$uglyQueue = UglyQueue::queueWithDirectoryPathAndObservers($queueDir, $manager->observers);
|
||||||
|
|
||||||
|
$manager->addQueue($uglyQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
$manager->notifyStatus = self::NOTIFY_MANAGER_INITIALIZED;
|
||||||
|
$manager->notify();
|
||||||
|
|
||||||
|
return $manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
* @throws \RuntimeException
|
||||||
|
*/
|
||||||
|
public function addQueue(UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
if ($this->containsQueueWithName($uglyQueue->name))
|
||||||
|
throw new \RuntimeException('Queue named "'.$uglyQueue->name.'" already exists in this manager.');
|
||||||
|
|
||||||
|
$this->queues[$uglyQueue->name] = $uglyQueue;
|
||||||
|
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_ADDED;
|
||||||
|
$this->notify();
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function addQueueAtPath($path)
|
||||||
|
{
|
||||||
|
$uglyQueue = UglyQueue::queueWithDirectoryPathAndObservers($path, $this->observers);
|
||||||
|
|
||||||
|
return $this->addQueue($uglyQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function removeQueue(UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
if ($this->containsQueueWithName($uglyQueue->name))
|
||||||
|
unset($this->queues[$uglyQueue->name]);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function removeQueueByName($name)
|
||||||
|
{
|
||||||
|
if ($this->containsQueueWithName($name))
|
||||||
|
{
|
||||||
|
unset($this->queues[$name]);
|
||||||
|
$this->notifyStatus = self::NOTIFY_QUEUE_REMOVED;
|
||||||
|
$this->notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return \DCarbone\UglyQueue
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function &getQueueWithName($name)
|
||||||
|
{
|
||||||
|
if (isset($this->queues[$name]))
|
||||||
|
return $this->queues[$name];
|
||||||
|
|
||||||
|
throw new \InvalidArgumentException('Argument 1 expected to be valid queue name.');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function containsQueueWithName($name)
|
||||||
|
{
|
||||||
|
return isset($this->queues[$name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getQueueList()
|
||||||
|
{
|
||||||
|
return array_keys($this->queues);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Receive update from subject
|
||||||
|
* @link http://php.net/manual/en/splobserver.update.php
|
||||||
|
*
|
||||||
|
* @param \SplSubject $subject The SplSubject notifying the observer of an update.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function update(\SplSubject $subject)
|
||||||
|
{
|
||||||
|
for ($i = 0, $count = count($this->observers); $i < $count; $i++)
|
||||||
|
{
|
||||||
|
$this->observers[$i]->notify($subject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Attach an SplObserver
|
||||||
|
* @link http://php.net/manual/en/splsubject.attach.php
|
||||||
|
*
|
||||||
|
* @param \SplObserver $observer The SplObserver to attach.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function attach(\SplObserver $observer)
|
||||||
|
{
|
||||||
|
if (!in_array($observer, $this->observers, true))
|
||||||
|
$this->observers[] = $observer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Detach an observer
|
||||||
|
* @link http://php.net/manual/en/splsubject.detach.php
|
||||||
|
*
|
||||||
|
* @param \SplObserver $observer The SplObserver to detach.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function detach(\SplObserver $observer)
|
||||||
|
{
|
||||||
|
$idx = array_search($observer, $this->observers, true);
|
||||||
|
if ($idx !== false)
|
||||||
|
unset($this->observers[$idx]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Notify an observer
|
||||||
|
* @link http://php.net/manual/en/splsubject.notify.php
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function notify()
|
||||||
|
{
|
||||||
|
for ($i = 0, $count = count($this->observers); $i < $count; $i++)
|
||||||
|
{
|
||||||
|
$this->observers[$i]->notify($this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,48 +1,485 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
date_default_timezone_set('UTC');
|
||||||
|
|
||||||
|
require_once __DIR__.'/../misc/cleanup.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class UglyQueueTest
|
* Class UglyQueueTest
|
||||||
*/
|
*/
|
||||||
class UglyQueueTest extends PHPUnit_Framework_TestCase
|
class UglyQueueTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::__construct
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $tastySandwich = array(
|
||||||
|
'0' => 'unsalted butter',
|
||||||
|
'1' => 'all-purpose flour',
|
||||||
|
'2' => 'hot milk',
|
||||||
|
'3' => 'kosher salt',
|
||||||
|
'4' => 'freshly ground black pepper',
|
||||||
|
'5' => 'nutmeg',
|
||||||
|
'6' => 'grated Gruyere',
|
||||||
|
'7' => 'freshly grated Parmesan',
|
||||||
|
'8' => 'white sandwich bread, crust removed',
|
||||||
|
'9' => 'Dijon mustard',
|
||||||
|
'10' => 'Virginia baked ham, sliced',
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @return \DCarbone\UglyQueue
|
* @return \DCarbone\UglyQueue
|
||||||
*/
|
*/
|
||||||
public function testCanConstructUglyQueueWithValidParameter()
|
public function testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers()
|
||||||
{
|
{
|
||||||
$conf = array(
|
$uglyQueue = \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers(dirname(__DIR__).'/misc/queues/tasty-sandwich');
|
||||||
'queue-base-dir' => dirname(__DIR__).'/misc/',
|
|
||||||
);
|
|
||||||
|
|
||||||
$uglyQueue = new \DCarbone\UglyQueue($conf);
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
||||||
|
|
||||||
return $uglyQueue;
|
return $uglyQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::__construct
|
* @covers \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function testExceptionThrownWhenConstructingUglyQueueWithEmptyOrInvalidConf()
|
public function testExceptionThrownWhenInitializingUglyQueueWithEmptyOrInvalidConf()
|
||||||
{
|
{
|
||||||
$conf = array();
|
$uglyQueue = \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers(array());
|
||||||
$uglyQueue = new \DCarbone\UglyQueue($conf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::__construct
|
* @covers \DCarbone\UglyQueue::processQueue
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
* @expectedException \RuntimeException
|
* @expectedException \RuntimeException
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testExceptionThrownWhenConstructingUglyQueueWithInvalidQueueBaseDirPath()
|
public function testExceptionThrownWhenTryingToProcessQueueAfterInitializationBeforeLock(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$conf = array(
|
$process = $uglyQueue->processQueue();
|
||||||
'queue-base-dir' => 'sandwiches',
|
|
||||||
);
|
|
||||||
|
|
||||||
$uglyQueue = new \DCarbone\UglyQueue($conf);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::keyExistsInQueue
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testKeyExistsInQueueReturnsFalseWithEmptyQueueAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$exists = $uglyQueue->keyExistsInQueue(0);
|
||||||
|
|
||||||
|
$this->assertFalse($exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::addToQueue
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLock(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$addToQueue = $uglyQueue->addToQueue('test', 'value');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanGetQueueDirectory(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$queuePath = $uglyQueue->path;
|
||||||
|
|
||||||
|
$this->assertFileExists($queuePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanGetQueueName(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$queueName = $uglyQueue->name;
|
||||||
|
|
||||||
|
$this->assertEquals('tasty-sandwich', $queueName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanGetQueueLockedStatus(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$locked = $uglyQueue->locked;
|
||||||
|
|
||||||
|
$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 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
|
||||||
|
*/
|
||||||
|
public function testGetQueueItemCountReturnsZeroWithEmptyQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$itemCount = $uglyQueue->getQueueItemCount();
|
||||||
|
$this->assertEquals(0, $itemCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @return \DCarbone\UglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanInitializeExistingQueue()
|
||||||
|
{
|
||||||
|
$uglyQueue = \DCarbone\UglyQueue::queueWithDirectoryPathAndObservers(dirname(__DIR__).'/misc/queues/tasty-sandwich');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
||||||
|
|
||||||
|
return $uglyQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownWhenPassingNonIntegerValueToLock(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$uglyQueue->lock('7 billion');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownWhenPassingNegativeIntegerValueToLock(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$uglyQueue->lock(-73);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
|
* @covers \DCarbone\UglyQueue::isLocked
|
||||||
|
* @covers \DCarbone\UglyQueue::createLockFile
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanLockUglyQueueWithDefaultTTL(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$locked = $uglyQueue->lock();
|
||||||
|
|
||||||
|
$this->assertTrue($locked);
|
||||||
|
|
||||||
|
$queueDir = $uglyQueue->path;
|
||||||
|
|
||||||
|
$this->assertFileExists($queueDir.'queue.lock');
|
||||||
|
|
||||||
|
$decode = @json_decode(file_get_contents($queueDir.'queue.lock'));
|
||||||
|
|
||||||
|
$this->assertTrue((json_last_error() === JSON_ERROR_NONE));
|
||||||
|
$this->assertObjectHasAttribute('ttl', $decode);
|
||||||
|
$this->assertObjectHasAttribute('born', $decode);
|
||||||
|
$this->assertEquals(250, $decode->ttl);
|
||||||
|
|
||||||
|
return $uglyQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
|
* @covers \DCarbone\UglyQueue::isLocked
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeExistingQueue
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testCannotLockQueueThatIsAlreadyLocked(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$lock = $uglyQueue->lock();
|
||||||
|
|
||||||
|
$this->assertFalse($lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::isLocked
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanLockUglyQueueWithDefaultTTL
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testIsLockedReturnsTrueAfterLocking(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$isLocked = $uglyQueue->isLocked();
|
||||||
|
$this->assertTrue($isLocked);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::unlock
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @uses \DCarbone\Helpers\FileHelper
|
||||||
|
* @depends testCanLockUglyQueueWithDefaultTTL
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanUnlockLockedQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$uglyQueue->unlock();
|
||||||
|
|
||||||
|
$queueGroupDir = $uglyQueue->path;
|
||||||
|
|
||||||
|
$this->assertFileNotExists($queueGroupDir.'queue.lock');
|
||||||
|
|
||||||
|
return $uglyQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::isLocked
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanUnlockLockedQueue
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testIsLockedReturnsFalseAfterUnlockingQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$isLocked = $uglyQueue->isLocked();
|
||||||
|
|
||||||
|
$this->assertFalse($isLocked);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
|
* @covers \DCarbone\UglyQueue::isLocked
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @uses \DCarbone\Helpers\FileHelper
|
||||||
|
* @depends testCanUnlockLockedQueue
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testIsLockedReturnsFalseWithStaleQueueLockFile(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$uglyQueue->lock(2);
|
||||||
|
$isLocked = $uglyQueue->isLocked();
|
||||||
|
$this->assertTrue($isLocked);
|
||||||
|
|
||||||
|
sleep(3);
|
||||||
|
|
||||||
|
$isLocked = $uglyQueue->isLocked();
|
||||||
|
$this->assertFalse($isLocked);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
|
* @covers \DCarbone\UglyQueue::isLocked
|
||||||
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanUnlockLockedQueue
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanLockQueueWithValidIntegerValue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$locked = $uglyQueue->lock(200);
|
||||||
|
|
||||||
|
$this->assertTrue($locked);
|
||||||
|
|
||||||
|
$queueDir = $uglyQueue->path;
|
||||||
|
|
||||||
|
$this->assertFileExists($queueDir.'queue.lock');
|
||||||
|
|
||||||
|
$decode = @json_decode(file_get_contents($queueDir.'queue.lock'));
|
||||||
|
|
||||||
|
$this->assertTrue((json_last_error() === JSON_ERROR_NONE));
|
||||||
|
$this->assertObjectHasAttribute('ttl', $decode);
|
||||||
|
$this->assertObjectHasAttribute('born', $decode);
|
||||||
|
$this->assertEquals(200, $decode->ttl);
|
||||||
|
|
||||||
|
return $uglyQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::addToQueue
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @uses \DCarbone\Helpers\FileHelper
|
||||||
|
* @depends testCanLockQueueWithValidIntegerValue
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
foreach(array_reverse($this->tastySandwich, true) as $k=>$v)
|
||||||
|
{
|
||||||
|
$added = $uglyQueue->addToQueue($k, $v);
|
||||||
|
$this->assertTrue($added);
|
||||||
|
}
|
||||||
|
|
||||||
|
$groupDir = $uglyQueue->path;
|
||||||
|
|
||||||
|
$this->assertFileExists(
|
||||||
|
$groupDir.'queue.tmp',
|
||||||
|
'queue.tmp file was not created!');
|
||||||
|
|
||||||
|
$lineCount = \DCarbone\Helpers\FileHelper::getLineCount($groupDir.'queue.tmp');
|
||||||
|
|
||||||
|
$this->assertEquals(11, $lineCount);
|
||||||
|
|
||||||
|
return $uglyQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::_populateQueue
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanForciblyUpdateQueueFileFromTempFile(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$uglyQueue->_populateQueue();
|
||||||
|
|
||||||
|
$groupDir = $uglyQueue->path;
|
||||||
|
|
||||||
|
$this->assertFileNotExists($groupDir.'queue.tmp');
|
||||||
|
|
||||||
|
$uglyQueue->_populateQueue();
|
||||||
|
|
||||||
|
return $uglyQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::getQueueItemCount
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @uses \DCarbone\Helpers\FileHelper
|
||||||
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanGetCountOfItemsInPopulatedQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$itemCount = $uglyQueue->getQueueItemCount();
|
||||||
|
|
||||||
|
$this->assertEquals(11, $itemCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::keyExistsInQueue
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testKeyExistsReturnsTrueWithPopulatedQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$exists = $uglyQueue->keyExistsInQueue(5);
|
||||||
|
|
||||||
|
$this->assertTrue($exists);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::processQueue
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownWhenTryingToProcessLockedQueueWithNonInteger(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$process = $uglyQueue->processQueue('Eleventy Billion');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::processQueue
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownWhenTryingToProcessLockedQueueWithIntegerLessThan1(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$process = $uglyQueue->processQueue(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::processQueue
|
||||||
|
* @covers \DCarbone\UglyQueue::getQueueItemCount
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @uses \DCarbone\Helpers\FileHelper
|
||||||
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanGetPartialQueueContents(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$process = $uglyQueue->processQueue(5);
|
||||||
|
|
||||||
|
$this->assertEquals(5, count($process));
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('0', $process);
|
||||||
|
$this->assertArrayHasKey('4', $process);
|
||||||
|
|
||||||
|
$this->assertEquals(6, $uglyQueue->getQueueItemCount());
|
||||||
|
|
||||||
|
return $uglyQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::processQueue
|
||||||
|
* @covers \DCarbone\UglyQueue::getQueueItemCount
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @uses \DCarbone\Helpers\FileHelper
|
||||||
|
* @depends testCanGetPartialQueueContents
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanGetFullQueueContents(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$process = $uglyQueue->processQueue(6);
|
||||||
|
|
||||||
|
$this->assertEquals(6, count($process));
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('10', $process);
|
||||||
|
$this->assertArrayHasKey('5', $process);
|
||||||
|
|
||||||
|
$this->assertEquals(0, $uglyQueue->getQueueItemCount());
|
||||||
|
|
||||||
|
return $uglyQueue;
|
||||||
|
}
|
||||||
|
}
|
||||||
233
tests/UglyQueueManager/UglyQueueManagerTest.php
Normal file
233
tests/UglyQueueManager/UglyQueueManagerTest.php
Normal file
@@ -0,0 +1,233 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UglyQueueManagerTest
|
||||||
|
*/
|
||||||
|
class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected $reallyTastySandwich = array(
|
||||||
|
'0' => 'beef broth',
|
||||||
|
'1' => 'barbeque sauce',
|
||||||
|
'2' => 'boneless pork ribs',
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::__construct
|
||||||
|
* @covers \DCarbone\UglyQueueManager::init
|
||||||
|
* @covers \DCarbone\UglyQueue::unserialize
|
||||||
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
|
* @covers \DCarbone\UglyQueueManager::addQueue
|
||||||
|
* @covers \DCarbone\UglyQueueManager::containsQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function testCanInitializeManagerWithConfigAndNoObservers()
|
||||||
|
{
|
||||||
|
$config = array(
|
||||||
|
'queue-base-dir' => __DIR__.'/../misc/queues'
|
||||||
|
);
|
||||||
|
|
||||||
|
$manager = \DCarbone\UglyQueueManager::init($config);
|
||||||
|
|
||||||
|
$this->assertInstanceOf('\\DCarbone\\UglyQueueManager', $manager);
|
||||||
|
|
||||||
|
return $manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::init
|
||||||
|
* @covers \DCarbone\UglyQueueManager::__construct
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownDuringConstructionWithInvalidBasePathValue()
|
||||||
|
{
|
||||||
|
$config = array(
|
||||||
|
'queue-base-dir' => 'i do not exist!'
|
||||||
|
);
|
||||||
|
|
||||||
|
$manager = \DCarbone\UglyQueueManager::init($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::init
|
||||||
|
* @covers \DCarbone\UglyQueueManager::__construct
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownDuringConstructionWithInvalidConfArray()
|
||||||
|
{
|
||||||
|
$config = array(
|
||||||
|
'wrong-key' => 'wrong value'
|
||||||
|
);
|
||||||
|
|
||||||
|
$manager = \DCarbone\UglyQueueManager::init($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::containsQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanDetermineIfValidQueueExistsInManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$shouldBeTrue = $manager->containsQueueWithName('tasty-sandwich');
|
||||||
|
|
||||||
|
$this->assertTrue($shouldBeTrue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::containsQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanDetermineQueueDoesNotExistInManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$shouldBeFalse = $manager->containsQueueWithName('i should not exist');
|
||||||
|
|
||||||
|
$this->assertFalse($shouldBeFalse);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueWithName
|
||||||
|
* @covers \DCarbone\UglyQueue::__get
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanGetUglyQueueObjectFromManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$uglyQueue = $manager->getQueueWithName('tasty-sandwich');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
||||||
|
$this->assertEquals('tasty-sandwich', $uglyQueue->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownWhenTryingToGetNonExistentQueueFromManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$shouldNotExist = $manager->getQueueWithName('sandwiches');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueList
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanGetListOfQueuesInManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$queueList = $manager->getQueueList();
|
||||||
|
|
||||||
|
$this->assertInternalType('array', $queueList);
|
||||||
|
$this->assertCount(1, $queueList);
|
||||||
|
$this->assertContains('tasty-sandwich', $queueList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueWithName
|
||||||
|
* @covers \DCarbone\UglyQueueManager::addQueue
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownWhenReAddingQueueToManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$uglyQueue = $manager->getQueueWithName('tasty-sandwich');
|
||||||
|
|
||||||
|
$manager->addQueue($uglyQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::addQueueAtPath
|
||||||
|
* @covers \DCarbone\UglyQueueManager::addQueue
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanInitializeNewQueueAndAddToManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$manager->addQueueAtPath(__DIR__.'/../misc/queues/really-tasty-sandwich');
|
||||||
|
|
||||||
|
$uglyQueue = $manager->getQueueWithName('really-tasty-sandwich');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
||||||
|
$this->assertEquals('really-tasty-sandwich', $uglyQueue->name);
|
||||||
|
|
||||||
|
$queueList = $manager->getQueueList();
|
||||||
|
|
||||||
|
$this->assertInternalType('array', $queueList);
|
||||||
|
$this->assertCount(2, $queueList);
|
||||||
|
$this->assertContains('really-tasty-sandwich', $queueList);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::removeQueueByName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @depends testCanInitializeManagerWithConfigAndNoObservers
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanRemoveQueueFromManagerByName(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$manager->removeQueueByName('really-tasty-sandwich');
|
||||||
|
|
||||||
|
$queueList = $manager->getQueueList();
|
||||||
|
|
||||||
|
$this->assertCount(1, $queueList);
|
||||||
|
$this->assertNotContains('really-tasty-sandwich', $queueList);
|
||||||
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * @covers \DCarbone\UglyQueue::queueExists
|
||||||
|
// * @uses \DCarbone\UglyQueue
|
||||||
|
// * @depends testCanInitializeNewUglyQueue
|
||||||
|
// * @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
// */
|
||||||
|
// public function testCanDetermineExistenceOfExistingQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
// {
|
||||||
|
// $exists = $uglyQueue->queueExists('tasty-sandwich');
|
||||||
|
//
|
||||||
|
// $this->assertTrue($exists);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * @covers \DCarbone\UglyQueue::queueExists
|
||||||
|
// * @uses \DCarbone\UglyQueue
|
||||||
|
// * @depends testCanInitializeNewUglyQueue
|
||||||
|
// * @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
// */
|
||||||
|
// public function testCanDetermineExistenceOfNonExistingQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
// {
|
||||||
|
// $exists = $uglyQueue->queueExists('nasty-sandwich');
|
||||||
|
//
|
||||||
|
// $this->assertFalse($exists);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * @covers \DCarbone\UglyQueue::getInitializedQueueList
|
||||||
|
// * @uses \DCarbone\UglyQueue
|
||||||
|
// * @depends testCanInitializeNewUglyQueue
|
||||||
|
// * @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
// */
|
||||||
|
// public function testCanGetListOfInitializedQueues(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
// {
|
||||||
|
// $queueList = $uglyQueue->getInitializedQueueList();
|
||||||
|
//
|
||||||
|
// $this->assertEquals(1, count($queueList));
|
||||||
|
// $this->assertContains('tasty-sandwich', $queueList);
|
||||||
|
// }
|
||||||
|
}
|
||||||
22
tests/misc/cleanup.php
Normal file
22
tests/misc/cleanup.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
if (is_dir(__DIR__.'/queues/'))
|
||||||
|
{
|
||||||
|
foreach(glob(__DIR__.'/queues/*', GLOB_ONLYDIR) as $queueDir)
|
||||||
|
{
|
||||||
|
foreach(glob($queueDir.'/*') as $file)
|
||||||
|
{
|
||||||
|
$split = preg_split('#[/\\\]+#', $file);
|
||||||
|
if (strpos(end($split), '.') === 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
unlink($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
rmdir($queueDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mkdir(__DIR__.'/queues');
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user