You've already forked ugly-queue
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f03508bf3e | |||
| 49677e8bf2 | |||
| 50601bc3c8 | |||
| d935032ec3 | |||
| 232e228475 | |||
| d40357df73 | |||
| 4020de5223 | |||
| 13df907166 | |||
| b840f40a89 | |||
| d20106a021 | |||
| 97dba637d9 | |||
| 793edc2d60 | |||
| 1e98b0cf7e | |||
| 89c51468eb | |||
| d33fd6bcba | |||
| 89cb1ae4b1 |
@@ -1,5 +1,7 @@
|
|||||||
language: php
|
language: php
|
||||||
|
|
||||||
|
sudo: false
|
||||||
|
|
||||||
php:
|
php:
|
||||||
- 5.3.3
|
- 5.3.3
|
||||||
- 5.3
|
- 5.3
|
||||||
@@ -8,9 +10,8 @@ php:
|
|||||||
- 5.6
|
- 5.6
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- composer self-update
|
- composer self-update 1.0.0-alpha10
|
||||||
- composer update --prefer-source
|
- composer install --no-interaction --prefer-source
|
||||||
- composer install --no-interaction --dev --prefer-source
|
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- ./vendor/bin/phpunit
|
- ./vendor/bin/phpunit
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -652,7 +652,7 @@ Also add information on how to contact you by electronic and paper mail.
|
|||||||
If the program does terminal interaction, make it output a short
|
If the program does terminal interaction, make it output a short
|
||||||
notice like this when it starts in an interactive mode:
|
notice like this when it starts in an interactive mode:
|
||||||
|
|
||||||
{project} Copyright (C) {year} {fullname}
|
dcarbone/ugly-queue Copyright (C) 2015 Daniel Paul Carbone
|
||||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
This is free software, and you are welcome to redistribute it
|
This is free software, and you are welcome to redistribute it
|
||||||
under certain conditions; type `show c' for details.
|
under certain conditions; type `show c' for details.
|
||||||
|
|||||||
91
README.md
91
README.md
@@ -1,8 +1,93 @@
|
|||||||
ugly-queue
|
ugly-queue
|
||||||
==========
|
==========
|
||||||
|
|
||||||
A simple file-based queue system for PHP 5.3.3+
|
A simple file-based FIFO queue system for PHP 5.3.3+
|
||||||
|
|
||||||
Build status: [](https://travis-ci.org/dcarbone/ugly-queue)
|
Build statuses:
|
||||||
|
- master: [](https://travis-ci.org/dcarbone/ugly-queue)
|
||||||
|
- 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
|
||||||
|
$queueBaseDir = 'path to where you would like queue files and directories to be stored';
|
||||||
|
|
||||||
|
$manager = new UglyQueueManager($queueBaseDir);
|
||||||
|
```
|
||||||
|
|
||||||
|
Once initialized, you can start adding queues!
|
||||||
|
|
||||||
|
```php
|
||||||
|
$sandwichQueue = $manager->createQueue('sandwiches');
|
||||||
|
|
||||||
|
$sandwichQueue->lock();
|
||||||
|
|
||||||
|
$sandwichQueue->addItems(array(
|
||||||
|
'bread',
|
||||||
|
'meat',
|
||||||
|
'cheese',
|
||||||
|
'lettuce',
|
||||||
|
'bread'
|
||||||
|
));
|
||||||
|
|
||||||
|
$sandwichQueue->unlock();
|
||||||
|
```
|
||||||
|
|
||||||
|
Once you have items added to the queue, you can either pull items out ad-hoc or set up some sort of cron
|
||||||
|
or schedule task to process items regularly.
|
||||||
|
|
||||||
|
If the base directory for all of your queues remains the same, each initialization
|
||||||
|
of `UglyQueueManager` will automatically find and initialize instances of pre-existing
|
||||||
|
UglyQueues.
|
||||||
|
|
||||||
|
In a subsequent request, simply do the following:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$queueBaseDir = 'path to where you would like queue files and directories to be stored';
|
||||||
|
|
||||||
|
$manager = new UglyQueueManager($queueBaseDir);
|
||||||
|
|
||||||
|
// 'tasty-sandwich' queue will exist now
|
||||||
|
|
||||||
|
$tastySandwich = $manager->getQueue('sandwiches');
|
||||||
|
|
||||||
|
$tastySandwich->lock();
|
||||||
|
|
||||||
|
// Specify the number of items you wish to retrieve from the queue
|
||||||
|
|
||||||
|
$items = $tastySandwich->retrieveItems(4);
|
||||||
|
|
||||||
|
// $items is now an array...
|
||||||
|
|
||||||
|
var_export($items);
|
||||||
|
|
||||||
|
/*
|
||||||
|
array (
|
||||||
|
4 => 'bread',
|
||||||
|
3 => 'lettuce',
|
||||||
|
2 => 'cheese',
|
||||||
|
1 => 'meat',
|
||||||
|
)
|
||||||
|
*/
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
The queue will then retain a single item, `0 => 'bread'` as the 5th item left in the queue.
|
||||||
|
|
||||||
|
At any time you can determine how many items remain in a queue by executing `count($queueObj);`
|
||||||
|
|
||||||
|
There are a few limitations currently:
|
||||||
|
|
||||||
|
1. This lib is designed for small values without much in the way of formatting or line breaks
|
||||||
|
2. It is designed to be atomic, meaning that only one process can be adding / retrieving items from
|
||||||
|
a queue at a time. Reading actions (count, searching, etc) are NOT atomic, however.
|
||||||
|
|||||||
@@ -21,11 +21,11 @@
|
|||||||
|
|
||||||
"require" : {
|
"require" : {
|
||||||
"php" : ">=5.3.3",
|
"php" : ">=5.3.3",
|
||||||
"dcarbone/helpers" : "6.1.*"
|
"dcarbone/helpers" : "~6.1"
|
||||||
},
|
},
|
||||||
|
|
||||||
"require-dev" : {
|
"require-dev" : {
|
||||||
"phpunit/phpunit": "4.1.*"
|
"phpunit/phpunit": "~4.1"
|
||||||
},
|
},
|
||||||
|
|
||||||
"autoload" : {
|
"autoload" : {
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
@@ -6,45 +6,69 @@ use DCarbone\Helpers\FileHelper;
|
|||||||
* Class UglyQueue
|
* Class UglyQueue
|
||||||
* @package DCarbone
|
* @package DCarbone
|
||||||
*/
|
*/
|
||||||
class UglyQueue
|
class UglyQueue implements \Serializable, \SplSubject, \Countable
|
||||||
{
|
{
|
||||||
/** @var array */
|
const QUEUE_READONLY = 0;
|
||||||
protected $config;
|
const QUEUE_READWRITE = 1;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
private $_notifyStatus;
|
||||||
|
|
||||||
|
/** @var \SplObserver[] */
|
||||||
|
private $_observers = array();
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
protected $mode = null;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $queueBaseDir;
|
protected $baseDir;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $queueGroup = null;
|
protected $name;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
protected $queueGroupDirPath = null;
|
protected $path;
|
||||||
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
protected $haveLock = false;
|
protected $locked = false;
|
||||||
|
|
||||||
/** @var bool */
|
|
||||||
protected $init = false;
|
|
||||||
|
|
||||||
/** @var resource */
|
/** @var resource */
|
||||||
protected $_tmpHandle;
|
protected $tmpHandle;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $queueFile;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $queueTmpFile;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $lockFile;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $serializeFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $config
|
* @param string $baseDir
|
||||||
* @throws \RuntimeException
|
* @param string $name
|
||||||
* @throws \InvalidArgumentException
|
* @param \SplObserver[] $observers
|
||||||
*/
|
*/
|
||||||
public function __construct(array $config)
|
public function __construct($baseDir, $name, array $observers = array())
|
||||||
{
|
{
|
||||||
if (!isset($config['queue-base-dir']))
|
$this->baseDir = realpath($baseDir);
|
||||||
throw new \InvalidArgumentException('UglyQueue::__construct - "$config" parameter "queue-base-dir" not seen.');
|
$this->name = $name;
|
||||||
|
$this->_observers = $observers;
|
||||||
|
|
||||||
if (!is_dir($config['queue-base-dir']) || !is_writable($config['queue-base-dir']))
|
$path = sprintf('%s%s%s', $baseDir, DIRECTORY_SEPARATOR, $name);
|
||||||
throw new \RuntimeException('UglyQueue::__construct - "$config[\'queue-base-dir\']" points to a directory that either doesn\'t exist or is not writable');
|
if (!file_exists($path) && !@mkdir($path))
|
||||||
|
throw new \RuntimeException('Unable to initialize queue directory "'.$path.'". Please check permissions.');
|
||||||
|
|
||||||
$this->config = $config;
|
$this->path = $path;
|
||||||
|
$this->lockFile = sprintf('%s%squeue.lock', $path, DIRECTORY_SEPARATOR);
|
||||||
|
$this->queueFile = sprintf('%s%squeue.txt', $path, DIRECTORY_SEPARATOR);
|
||||||
|
$this->queueTmpFile = sprintf('%s%squeue.tmp', $path, DIRECTORY_SEPARATOR);
|
||||||
|
$this->serializeFile = sprintf('%s%sugly-queue.obj', $path, DIRECTORY_SEPARATOR);
|
||||||
|
|
||||||
$this->queueBaseDir = $this->config['queue-base-dir'];
|
$this->initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -52,8 +76,81 @@ class UglyQueue
|
|||||||
*/
|
*/
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
{
|
{
|
||||||
$this->unlock();
|
|
||||||
$this->_populateQueue();
|
$this->_populateQueue();
|
||||||
|
$this->unlock();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSerializeFile()
|
||||||
|
{
|
||||||
|
return $this->serializeFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function isLocked()
|
||||||
|
{
|
||||||
|
return $this->locked;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,36 +161,21 @@ class UglyQueue
|
|||||||
public function lock($ttl = 250)
|
public function lock($ttl = 250)
|
||||||
{
|
{
|
||||||
if (!is_int($ttl))
|
if (!is_int($ttl))
|
||||||
throw new \InvalidArgumentException('UglyQueue::lock - Argument 1 expected to be positive 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('UglyQueue::lock - Argument 1 expected to be positive integer, "'.$ttl.'" seen');
|
throw new \InvalidArgumentException('Argument 1 expected to be greater than 0 "'.$ttl.'" seen');
|
||||||
|
|
||||||
$already_locked = $this->isLocked();
|
// If there is currently no lock
|
||||||
|
if ($this->isAlreadyLocked() === false)
|
||||||
// If there is no lock, currently
|
return $this->createLockFile($ttl);
|
||||||
if ($already_locked === false)
|
|
||||||
return $this->haveLock = $this->createQueueLock($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.
|
||||||
return $this->haveLock = false;
|
$this->locked = false;
|
||||||
}
|
$this->_notifyStatus = UglyQueueEnum::QUEUE_LOCKED_BY_OTHER_PROCESS;
|
||||||
|
$this->notify();
|
||||||
|
|
||||||
/**
|
|
||||||
* @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;
|
return false;
|
||||||
|
|
||||||
$this->haveLock = true;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -101,10 +183,13 @@ class UglyQueue
|
|||||||
*/
|
*/
|
||||||
public function unlock()
|
public function unlock()
|
||||||
{
|
{
|
||||||
if ($this->haveLock === true)
|
if ($this->isLocked() === true)
|
||||||
{
|
{
|
||||||
unlink($this->queueGroupDirPath.'queue.lock');
|
unlink($this->lockFile);
|
||||||
$this->haveLock = false;
|
$this->locked = false;
|
||||||
|
|
||||||
|
$this->_notifyStatus = UglyQueueEnum::QUEUE_UNLOCKED;
|
||||||
|
$this->notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,30 +197,26 @@ class UglyQueue
|
|||||||
* @throws \RuntimeException
|
* @throws \RuntimeException
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isLocked()
|
public function isAlreadyLocked()
|
||||||
{
|
{
|
||||||
if ($this->init === false)
|
|
||||||
throw new \RuntimeException('UglyQueue::isLocked - Must first initialize queue');
|
|
||||||
|
|
||||||
// First check for lock file
|
// First check for lock file
|
||||||
if (is_file($this->queueGroupDirPath.'queue.lock'))
|
if (is_file($this->lockFile))
|
||||||
{
|
{
|
||||||
$lock = json_decode(file_get_contents($this->queueGroupDirPath.'queue.lock'), true);
|
$lock = json_decode(file_get_contents($this->lockFile), true);
|
||||||
|
|
||||||
// If we have an invalid lock structure, THIS IS BAD.
|
// 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('UglyQueue::isLocked - Invalid "queue.lock" file structure seen at "'.$this->queueGroupDirPath.'queue.lock'.'"');
|
{
|
||||||
|
|
||||||
// Otherwise...
|
|
||||||
$lock_ttl = ((int)$lock['born'] + (int)$lock['ttl']);
|
$lock_ttl = ((int)$lock['born'] + (int)$lock['ttl']);
|
||||||
|
|
||||||
// If we're within the TTL of the lock, assume another thread is already processing.
|
// 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.
|
// We'll pick it up on the next go around.
|
||||||
if ($lock_ttl > time())
|
if ($lock_ttl > time())
|
||||||
return true;
|
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->queueGroupDirPath.'queue.lock');
|
unlink($this->lockFile);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,72 +224,44 @@ class UglyQueue
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $queueGroup
|
|
||||||
*/
|
|
||||||
public function initialize($queueGroup)
|
|
||||||
{
|
|
||||||
$this->queueGroup = $queueGroup;
|
|
||||||
$this->queueGroupDirPath = $this->queueBaseDir.$queueGroup.DIRECTORY_SEPARATOR;
|
|
||||||
|
|
||||||
// Create directory for this queue group
|
|
||||||
if (!is_dir($this->queueGroupDirPath))
|
|
||||||
mkdir($this->queueGroupDirPath);
|
|
||||||
|
|
||||||
// Insert "don't look here" index.html file
|
|
||||||
if (!file_exists($this->queueGroupDirPath.'index.html'))
|
|
||||||
{
|
|
||||||
$html = <<<HTML
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>403 Forbidden</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<p>Directory access is forbidden.</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
HTML;
|
|
||||||
file_put_contents($this->queueGroupDirPath.'index.html', $html);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!file_exists($this->queueGroupDirPath.'queue.txt'))
|
|
||||||
file_put_contents($this->queueGroupDirPath.'queue.txt', '');
|
|
||||||
|
|
||||||
$this->init = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $count
|
* @param int $count
|
||||||
* @throws \RuntimeException
|
* @throws \RuntimeException
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
* @return bool|array
|
* @return bool|array
|
||||||
*/
|
*/
|
||||||
public function processQueue($count = 1)
|
public function retrieveItems($count = 1)
|
||||||
{
|
{
|
||||||
if ($this->init === false)
|
if ($this->mode === self::QUEUE_READONLY)
|
||||||
throw new \RuntimeException('UglyQueue::processQueue - Must first initialize queue!');
|
throw new \RuntimeException('Queue "'.$this->name.'" cannot be processed. It was started in Read-Only mode (the user running this process does not have permission to write to the queue directory).');
|
||||||
|
|
||||||
// If we don't have a lock, assume issue and move on.
|
// If we don't have a lock, assume issue and move on.
|
||||||
if ($this->haveLock === false)
|
if ($this->isLocked() === false)
|
||||||
throw new \RuntimeException('UglyQueue::processQueue - Cannot process queue locked by another process');
|
throw new \RuntimeException('Cannot process queue named "'.$this->name.'". It is locked by another process.');
|
||||||
|
|
||||||
// If non-int valid is passed
|
// If non-int valid is passed
|
||||||
if (!is_int($count))
|
if (!is_int($count))
|
||||||
throw new \InvalidArgumentException('UglyQueue::processQueue - Argument 1 expected to be integer greater than 0, "'.gettype($count).'" seen');
|
throw new \InvalidArgumentException('Argument 1 expected to be integer greater than 0, "'.gettype($count).'" seen');
|
||||||
|
|
||||||
// If negative integer passed
|
// If negative integer passed
|
||||||
if ($count <= 0)
|
if ($count <= 0)
|
||||||
throw new \InvalidArgumentException('UglyQueue::processQueue - Argument 1 expected to be integer greater than 0, "'.$count.'" seen');
|
throw new \InvalidArgumentException('Argument 1 expected to be integer greater than 0, "'.$count.'" seen');
|
||||||
|
|
||||||
|
if ($this->_notifyStatus !== UglyQueueEnum::QUEUE_PROCESSING)
|
||||||
|
{
|
||||||
|
$this->_notifyStatus = UglyQueueEnum::QUEUE_PROCESSING;
|
||||||
|
$this->notify();
|
||||||
|
}
|
||||||
|
|
||||||
// Find number of lines in the queue file
|
// Find number of lines in the queue file
|
||||||
$lineCount = FileHelper::getLineCount($this->queueGroupDirPath.'queue.txt');
|
$lineCount = count($this);
|
||||||
|
|
||||||
// If queue line count is 0, assume empty
|
// If queue line count is 0, assume empty
|
||||||
if ($lineCount === 0)
|
if ($lineCount === 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Try to open the file for reading / writing.
|
// Try to open the file for reading / writing.
|
||||||
$queueFileHandle = fopen($this->queueGroupDirPath.'queue.txt', 'r+');
|
$queueFileHandle = fopen($this->queueFile, 'r+');
|
||||||
if ($queueFileHandle === false)
|
if ($queueFileHandle === false)
|
||||||
$this->unlock();
|
$this->unlock();
|
||||||
|
|
||||||
@@ -221,7 +274,7 @@ HTML;
|
|||||||
if ($i++ >= $start_line)
|
if ($i++ >= $start_line)
|
||||||
{
|
{
|
||||||
list ($key, $value) = $line;
|
list ($key, $value) = $line;
|
||||||
$data[$key] = $value;
|
$data = array($key => $value) + $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,11 +284,14 @@ HTML;
|
|||||||
rewind($queueFileHandle);
|
rewind($queueFileHandle);
|
||||||
ftruncate($queueFileHandle, 0);
|
ftruncate($queueFileHandle, 0);
|
||||||
fclose($queueFileHandle);
|
fclose($queueFileHandle);
|
||||||
|
|
||||||
|
$this->_notifyStatus = UglyQueueEnum::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->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)
|
||||||
@@ -248,8 +304,8 @@ HTML;
|
|||||||
|
|
||||||
fclose($queueFileHandle);
|
fclose($queueFileHandle);
|
||||||
fclose($tmp);
|
fclose($tmp);
|
||||||
unlink($this->queueGroupDirPath.'queue.txt');
|
unlink($this->queueFile);
|
||||||
rename($this->queueGroupDirPath.'queue.tmp', $this->queueGroupDirPath.'queue.txt');
|
rename($this->queueTmpFile, $this->queueFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
@@ -261,31 +317,42 @@ HTML;
|
|||||||
* @return bool
|
* @return bool
|
||||||
* @throws \RuntimeException
|
* @throws \RuntimeException
|
||||||
*/
|
*/
|
||||||
public function addToQueue($key, $value)
|
public function addItem($key, $value)
|
||||||
{
|
{
|
||||||
if ($this->init === false)
|
if ($this->mode === self::QUEUE_READONLY)
|
||||||
throw new \RuntimeException('UglyQueue::addToQueue - Must first initialize queue!');
|
throw new \RuntimeException('Cannot add 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->haveLock === false)
|
if ($this->isLocked() === false)
|
||||||
throw new \RuntimeException('UglyQueue::addToQueue - You do not have a lock on this queue');
|
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->queueGroupDirPath.'queue.tmp', 'w+');
|
$this->tmpHandle = fopen($this->queueTmpFile, 'w+');
|
||||||
if ($this->_tmpHandle === false)
|
if ($this->tmpHandle === false)
|
||||||
throw new \RuntimeException('UglyQueue::addToQueue - Unable to create "queue.tmp" file');
|
throw new \RuntimeException('Unable to create "queue.tmp" file.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_array($value) || $value instanceof \stdClass)
|
if (is_array($value) || $value instanceof \stdClass)
|
||||||
$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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $items
|
||||||
|
*/
|
||||||
|
public function addItems(array $items)
|
||||||
|
{
|
||||||
|
foreach($items as $k=>$v)
|
||||||
|
{
|
||||||
|
$this->addItem($k, $v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If there is a tmp queue file, add it's contents to the beginning of a new queue file
|
* If there is a tmp queue file, add it's contents to the beginning of a new queue file
|
||||||
*
|
*
|
||||||
@@ -293,38 +360,26 @@ HTML;
|
|||||||
*/
|
*/
|
||||||
public 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->queueFile))
|
||||||
{
|
{
|
||||||
$queueFileHandle = fopen($this->queueGroupDirPath.'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->queueGroupDirPath.'queue.txt');
|
unlink($this->queueFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose($this->_tmpHandle);
|
fclose($this->tmpHandle);
|
||||||
rename($this->queueGroupDirPath.'queue.tmp', $this->queueGroupDirPath.'queue.txt');
|
rename($this->queueTmpFile, $this->queueFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
* @throws \RuntimeException
|
|
||||||
*/
|
|
||||||
public function getQueueItemCount()
|
|
||||||
{
|
|
||||||
if ($this->init === false)
|
|
||||||
throw new \RuntimeException('UglyQueue::getQueueItemCount - Must first initialize queue');
|
|
||||||
|
|
||||||
return FileHelper::getLineCount($this->queueGroupDirPath.'queue.txt');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @return bool
|
* @return bool
|
||||||
@@ -332,19 +387,14 @@ HTML;
|
|||||||
*/
|
*/
|
||||||
public function keyExistsInQueue($key)
|
public function keyExistsInQueue($key)
|
||||||
{
|
{
|
||||||
if ($this->init === false)
|
|
||||||
throw new \RuntimeException('UglyQueue::keyExistsInQueue - Must first initialize queue');
|
|
||||||
|
|
||||||
$key = (string)$key;
|
$key = (string)$key;
|
||||||
|
|
||||||
// Try to open the file for reading / writing.
|
// Try to open the file for reading / writing.
|
||||||
$queueFileHandle = fopen($this->queueGroupDirPath.'queue.txt', 'r');
|
$queueFileHandle = fopen($this->queueFile, 'r');
|
||||||
|
|
||||||
while(($line = fscanf($queueFileHandle, "%s\t")) !== false)
|
while(($line = fscanf($queueFileHandle, "%s\t%s\n")) !== false)
|
||||||
{
|
{
|
||||||
list($queueKey) = $line;
|
if ($key === $line[0])
|
||||||
|
|
||||||
if ($key === $queueKey)
|
|
||||||
{
|
{
|
||||||
fclose($queueFileHandle);
|
fclose($queueFileHandle);
|
||||||
return true;
|
return true;
|
||||||
@@ -356,34 +406,189 @@ HTML;
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return boolean
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Count elements of an object
|
||||||
|
* @link http://php.net/manual/en/countable.count.php
|
||||||
|
*
|
||||||
|
* @return int The custom count as an integer.
|
||||||
*/
|
*/
|
||||||
public function getInit()
|
public function count()
|
||||||
{
|
{
|
||||||
return $this->init;
|
return (int)FileHelper::getLineCount($this->queueFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* String representation of object
|
||||||
|
* @link http://php.net/manual/en/serializable.serialize.php
|
||||||
|
*
|
||||||
|
* @return string the string representation of the object or null
|
||||||
*/
|
*/
|
||||||
public function getQueueBaseDir()
|
public function serialize()
|
||||||
{
|
{
|
||||||
return $this->queueBaseDir;
|
return serialize(
|
||||||
|
array(
|
||||||
|
$this->baseDir,
|
||||||
|
$this->name,
|
||||||
|
$this->path,
|
||||||
|
$this->queueFile,
|
||||||
|
$this->queueTmpFile,
|
||||||
|
$this->lockFile,
|
||||||
|
$this->serializeFile,
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Constructs the object
|
||||||
|
* @link http://php.net/manual/en/serializable.unserialize.php
|
||||||
|
*
|
||||||
|
* @param string $serialized The string representation of the object.
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function getQueueGroupDirPath()
|
public function unserialize($serialized)
|
||||||
{
|
{
|
||||||
return $this->queueGroupDirPath;
|
$data = unserialize($serialized);
|
||||||
|
$this->baseDir = $data[0];
|
||||||
|
$this->name = $data[1];
|
||||||
|
$this->path = $data[2];
|
||||||
|
$this->queueFile = $data[3];
|
||||||
|
$this->queueTmpFile = $data[4];
|
||||||
|
$this->lockFile = $data[5];
|
||||||
|
$this->serializeFile = $data[6];
|
||||||
|
$this->initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Attach an SplObserver
|
||||||
|
* @link http://php.net/manual/en/splsubject.attach.php
|
||||||
|
*
|
||||||
|
* @param \SplObserver $observer The SplObserver to attach.
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function getQueueGroup()
|
public function attach(\SplObserver $observer)
|
||||||
{
|
{
|
||||||
return $this->queueGroup;
|
if (!in_array($observer, $this->_observers))
|
||||||
|
$this->_observers[] = $observer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Detach an observer
|
||||||
|
* @link http://php.net/manual/en/splsubject.detach.php
|
||||||
|
*
|
||||||
|
* @param \SplObserver $observer The SplObserver to detach.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function detach(\SplObserver $observer)
|
||||||
|
{
|
||||||
|
$idx = array_search($observer, $this->_observers, true);
|
||||||
|
if ($idx !== false)
|
||||||
|
unset($this->_observers[$idx]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Notify an observer
|
||||||
|
* @link http://php.net/manual/en/splsubject.notify.php
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function notify()
|
||||||
|
{
|
||||||
|
foreach($this->_observers as $observer)
|
||||||
|
{
|
||||||
|
$observer->update($this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is mostly intended to check the "validity" of a re-initialized queue
|
||||||
|
*
|
||||||
|
* Could probably stand to be improved.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function _valid()
|
||||||
|
{
|
||||||
|
return (
|
||||||
|
$this->baseDir !== null &&
|
||||||
|
$this->name !== null &&
|
||||||
|
$this->path !== null &&
|
||||||
|
$this->queueFile !== null &&
|
||||||
|
$this->queueTmpFile !== null &&
|
||||||
|
$this->lockFile !== null &&
|
||||||
|
$this->serializeFile !== null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $ttl seconds to live
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function createLockFile($ttl)
|
||||||
|
{
|
||||||
|
$ok = (bool)@file_put_contents(
|
||||||
|
$this->lockFile,
|
||||||
|
json_encode(array('ttl' => $ttl, 'born' => time())));
|
||||||
|
|
||||||
|
if ($ok !== true)
|
||||||
|
{
|
||||||
|
$this->_notifyStatus = UglyQueueEnum::QUEUE_FAILED_TO_LOCK;
|
||||||
|
$this->notify();
|
||||||
|
return $this->locked = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->locked = true;
|
||||||
|
$this->_notifyStatus = UglyQueueEnum::QUEUE_LOCKED;
|
||||||
|
$this->notify();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post-construct initialization method.
|
||||||
|
*
|
||||||
|
* Also used post-un-serialization
|
||||||
|
*/
|
||||||
|
protected 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 ($this->mode === self::QUEUE_READONLY)
|
||||||
|
throw new \RuntimeException('Cannot initialize queue with name "'.$this->name.'", the user lacks permission to create files.');
|
||||||
|
|
||||||
|
$html = <<<HTML
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>403 Forbidden</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>Directory access is forbidden.</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
HTML;
|
||||||
|
file_put_contents($this->path.'/index.html', $html);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file_exists($this->queueFile))
|
||||||
|
{
|
||||||
|
if ($this->mode === self::QUEUE_READONLY)
|
||||||
|
throw new \RuntimeException('Cannot initialize queue with name "'.$this->name.'", the user lacks permission to create files.');
|
||||||
|
|
||||||
|
file_put_contents($this->queueFile, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->_notifyStatus = UglyQueueEnum::QUEUE_INITIALIZED;
|
||||||
|
$this->notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
24
src/UglyQueueEnum.php
Normal file
24
src/UglyQueueEnum.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php namespace DCarbone;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UglyQueueEnum
|
||||||
|
* @package DCarbone
|
||||||
|
*
|
||||||
|
* Pseudo-enum thing.
|
||||||
|
*/
|
||||||
|
abstract class UglyQueueEnum
|
||||||
|
{
|
||||||
|
// Typically used by UglyQueueManager
|
||||||
|
const MANAGER_INITIALIZED = 1;
|
||||||
|
const QUEUE_ADDED = 2;
|
||||||
|
const QUEUE_REMOVED = 3;
|
||||||
|
|
||||||
|
// Typically used by UglyQueues
|
||||||
|
const QUEUE_INITIALIZED = 100;
|
||||||
|
const QUEUE_LOCKED = 101;
|
||||||
|
const QUEUE_FAILED_TO_LOCK = 102;
|
||||||
|
const QUEUE_LOCKED_BY_OTHER_PROCESS = 103;
|
||||||
|
const QUEUE_UNLOCKED = 104;
|
||||||
|
const QUEUE_PROCESSING = 105;
|
||||||
|
const QUEUE_REACHED_END = 106;
|
||||||
|
}
|
||||||
191
src/UglyQueueManager.php
Normal file
191
src/UglyQueueManager.php
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
<?php namespace DCarbone;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UglyQueueManager
|
||||||
|
* @package DCarbone
|
||||||
|
*/
|
||||||
|
class UglyQueueManager implements \SplObserver, \Countable
|
||||||
|
{
|
||||||
|
/** @var UglyQueue[] */
|
||||||
|
protected $queues = array();
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $baseDir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param string $baseDir
|
||||||
|
* @throws \RuntimeException
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function __construct($baseDir)
|
||||||
|
{
|
||||||
|
if (false === is_string($baseDir))
|
||||||
|
throw new \InvalidArgumentException('Argument 1 expected to be string, "'.gettype($baseDir).'" seen.');
|
||||||
|
|
||||||
|
if (false === is_dir($baseDir))
|
||||||
|
throw new \RuntimeException('"'.$baseDir.'" points to a directory that does not exist.');
|
||||||
|
|
||||||
|
if (false === is_readable($baseDir))
|
||||||
|
throw new \RuntimeException('"'.$baseDir.'" is not readable and/or writable .');
|
||||||
|
|
||||||
|
$this->baseDir = rtrim($baseDir, "/\\");
|
||||||
|
|
||||||
|
foreach(glob(sprintf('%s/*', $this->baseDir), GLOB_ONLYDIR) as $queueDir)
|
||||||
|
{
|
||||||
|
$this->addQueueAtPath($queueDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return UglyQueue|UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function getQueue($name)
|
||||||
|
{
|
||||||
|
if (isset($this->queues[$name]))
|
||||||
|
return $this->queues[$name];
|
||||||
|
|
||||||
|
$path = sprintf('%s/%s', $this->baseDir, $name);
|
||||||
|
if (file_exists($path))
|
||||||
|
return $this->addQueueAtPath($path);
|
||||||
|
|
||||||
|
return $this->createQueue($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
* @throws \RuntimeException
|
||||||
|
*/
|
||||||
|
public function addQueue(UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$name = $uglyQueue->getName();
|
||||||
|
|
||||||
|
if ($this->containsQueueWithName($name))
|
||||||
|
throw new \RuntimeException('Queue named "'.$name.'" already exists in this manager.');
|
||||||
|
|
||||||
|
$this->queues[$name] = $uglyQueue;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return UglyQueue
|
||||||
|
*/
|
||||||
|
public function createQueue($name)
|
||||||
|
{
|
||||||
|
$queue = new UglyQueue($this->baseDir, $name, array($this));
|
||||||
|
$this->addQueue($queue);
|
||||||
|
return end($this->queues);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function addQueueAtPath($path)
|
||||||
|
{
|
||||||
|
// 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);
|
||||||
|
/** @var \DCarbone\UglyQueue $uglyQueue */
|
||||||
|
if (file_exists($serializedFile))
|
||||||
|
$uglyQueue = unserialize(file_get_contents($serializedFile));
|
||||||
|
|
||||||
|
if (!isset($uglyQueue) || $uglyQueue->_valid() === false)
|
||||||
|
$uglyQueue = new UglyQueue($this->baseDir, $queueName, array($this));
|
||||||
|
|
||||||
|
$uglyQueue->attach($this);
|
||||||
|
|
||||||
|
return $this->addQueue($uglyQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param UglyQueue $uglyQueue
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function removeQueue(UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$name = $uglyQueue->getName();
|
||||||
|
if ($this->containsQueueWithName($name))
|
||||||
|
unset($this->queues[$name]);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function removeQueueByName($name)
|
||||||
|
{
|
||||||
|
if ($this->containsQueueWithName($name))
|
||||||
|
unset($this->queues[$name]);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return \DCarbone\UglyQueue
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function getQueueWithName($name)
|
||||||
|
{
|
||||||
|
if (isset($this->queues[$name]))
|
||||||
|
return $this->queues[$name];
|
||||||
|
|
||||||
|
throw new \InvalidArgumentException('Argument 1 expected to be valid queue name.');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function containsQueueWithName($name)
|
||||||
|
{
|
||||||
|
return isset($this->queues[$name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getQueueList()
|
||||||
|
{
|
||||||
|
return array_keys($this->queues);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Count elements of an object
|
||||||
|
* @link http://php.net/manual/en/countable.count.php
|
||||||
|
*
|
||||||
|
* @return int The custom count as an integer. The return value is cast to an integer.
|
||||||
|
*/
|
||||||
|
public function count()
|
||||||
|
{
|
||||||
|
return count($this->queues);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (PHP 5 >= 5.1.0)
|
||||||
|
* Receive update from subject
|
||||||
|
* @link http://php.net/manual/en/splobserver.update.php
|
||||||
|
*
|
||||||
|
* @param \SplSubject $subject The SplSubject notifying the observer of an update.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function update(\SplSubject $subject)
|
||||||
|
{
|
||||||
|
// Nothing for now...
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,18 +28,19 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
'10' => 'Virginia baked ham, sliced',
|
'10' => 'Virginia baked ham, sliced',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->baseDir = realpath(__DIR__.'/../misc/queues');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::__construct
|
* @covers \DCarbone\UglyQueue::__construct
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @return \DCarbone\UglyQueue
|
* @return \DCarbone\UglyQueue
|
||||||
*/
|
*/
|
||||||
public function testCanConstructUglyQueueWithValidParameter()
|
public function testCanInitializeObjectWithValidParameters()
|
||||||
{
|
{
|
||||||
$conf = array(
|
$uglyQueue = new \DCarbone\UglyQueue($this->baseDir, 'tasty-sandwich');
|
||||||
'queue-base-dir' => dirname(__DIR__).'/misc/',
|
|
||||||
);
|
|
||||||
|
|
||||||
$uglyQueue = new \DCarbone\UglyQueue($conf);
|
|
||||||
|
|
||||||
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
||||||
|
|
||||||
@@ -45,290 +48,208 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::__construct
|
* @covers \DCarbone\UglyQueue::retrieveItems
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @expectedException \InvalidArgumentException
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenConstructingUglyQueueWithEmptyOrInvalidConf()
|
|
||||||
{
|
|
||||||
$conf = array();
|
|
||||||
$uglyQueue = new \DCarbone\UglyQueue($conf);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::__construct
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @expectedException \RuntimeException
|
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenConstructingUglyQueueWithInvalidQueueBaseDirPath()
|
|
||||||
{
|
|
||||||
$conf = array(
|
|
||||||
'queue-base-dir' => 'sandwiches',
|
|
||||||
);
|
|
||||||
|
|
||||||
$uglyQueue = new \DCarbone\UglyQueue($conf);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::getQueueBaseDir
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testCanGetQueueBaseDir(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$queueBaseDir = $uglyQueue->getQueueBaseDir();
|
|
||||||
|
|
||||||
$this->assertFileExists(
|
|
||||||
$queueBaseDir,
|
|
||||||
'Could not verify that Queue Base Dir exists');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::getQueueGroup
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testGetQueueGroupReturnsNullBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$queueGroup = $uglyQueue->getQueueGroup();
|
|
||||||
|
|
||||||
$this->assertNull($queueGroup);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::getInit
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testGetInitReturnsFalseBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$init = $uglyQueue->getInit();
|
|
||||||
$this->assertFalse($init);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::getQueueGroupDirPath
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testGetQueueGroupDirPathReturnsNullBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$queueGroupDir = $uglyQueue->getQueueGroupDirPath();
|
|
||||||
|
|
||||||
$this->assertNull($queueGroupDir);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::isLocked
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @expectedException \RuntimeException
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenCallingIsLockedOnUninitializedQueue(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$isLocked = $uglyQueue->isLocked();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::addToQueue
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @expectedException \RuntimeException
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenTryingToAddItemsToUninitializedQueue(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$addToQueue = $uglyQueue->addToQueue('test', 'value');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::getQueueItemCount
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @expectedException \RuntimeException
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenTryingToGetCountOfItemsInQueueBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$itemCount = $uglyQueue->getQueueItemCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::keyExistsInQueue
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @expectedException \RuntimeException
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenTryingToFindKeyBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$keyExists = $uglyQueue->keyExistsInQueue(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::processQueue
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @expectedException \RuntimeException
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
*/
|
|
||||||
public function testExceptionThrownWhenTryingToProcessQueueBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$process = $uglyQueue->processQueue();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::initialize
|
|
||||||
* @covers \DCarbone\UglyQueue::getInit
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
|
||||||
* @return \DCarbone\UglyQueue
|
|
||||||
*/
|
|
||||||
public function testCanInitializeNewUglyQueue(\DCarbone\UglyQueue $uglyQueue)
|
|
||||||
{
|
|
||||||
$uglyQueue->initialize('tasty-sandwich');
|
|
||||||
|
|
||||||
$this->assertTrue($uglyQueue->getInit());
|
|
||||||
|
|
||||||
return $uglyQueue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers \DCarbone\UglyQueue::processQueue
|
|
||||||
* @uses \DCarbone\UglyQueue
|
|
||||||
* @depends testCanInitializeNewUglyQueue
|
|
||||||
* @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 testCanInitializeNewUglyQueue
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testKeyExistsInQueueReturnsFalseWithEmptyQueueAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
public function testKeyExistsInQueueReturnsFalseWithEmptyQueueAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
|
|
||||||
$exists = $uglyQueue->keyExistsInQueue(0);
|
$exists = $uglyQueue->keyExistsInQueue(0);
|
||||||
|
|
||||||
$this->assertFalse($exists);
|
$this->assertFalse($exists);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::addToQueue
|
* @covers \DCarbone\UglyQueue::addItem
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeNewUglyQueue
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
* @expectedException \RuntimeException
|
* @expectedException \RuntimeException
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLockAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLock(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$addToQueue = $uglyQueue->addToQueue('test', 'value');
|
$uglyQueue->addItem('test', 'value');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::getInit
|
* @covers \DCarbone\UglyQueue::getPath
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeNewUglyQueue
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testGetInitReturnsTrueAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
public function testCanGetQueueDirectory(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$init = $uglyQueue->getInit();
|
$queuePath = $uglyQueue->getPath();
|
||||||
$this->assertTrue($init);
|
|
||||||
|
$this->assertFileExists($queuePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::getQueueGroupDirPath
|
* @covers \DCarbone\UglyQueue::getName
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeNewUglyQueue
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testCanGetQueueGroupDirectoryAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
public function testCanGetQueueName(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$queueGroupDir = $uglyQueue->getQueueGroupDirPath();
|
$queueName = $uglyQueue->getName();
|
||||||
|
|
||||||
$this->assertFileExists($queueGroupDir);
|
$this->assertEquals('tasty-sandwich', $queueName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::getQueueGroup
|
* @covers \DCarbone\UglyQueue::getMode
|
||||||
* @uses \DCarbone\UglyQueue
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
* @depends testCanInitializeNewUglyQueue
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testCanGetQueueGroupAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
public function testCanGetQueueMode(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$queueGroup = $uglyQueue->getQueueGroup();
|
$mode = $uglyQueue->getMode();
|
||||||
|
$this->assertEquals(\DCarbone\UglyQueue::QUEUE_READWRITE, $mode);
|
||||||
$this->assertEquals('tasty-sandwich', $queueGroup);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::isLocked
|
* @covers \DCarbone\UglyQueue::getBaseDir
|
||||||
* @uses \DCarbone\UglyQueue
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
* @depends testCanInitializeNewUglyQueue
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testIsLockedReturnsFalseBeforeLockingAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
public function testCanGetBaseDirectory(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$isLocked = $uglyQueue->isLocked();
|
$baseDir = $uglyQueue->getBaseDir();
|
||||||
|
$this->assertEquals($this->baseDir, $baseDir);
|
||||||
$this->assertFalse($isLocked);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::getQueueItemCount
|
* @covers \DCarbone\UglyQueue::getLockFile
|
||||||
* @uses \DCarbone\UglyQueue
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
* @depends testCanInitializeNewUglyQueue
|
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testGetQueueItemCountReturnsZeroAfterInitializingEmptyQueue(\DCarbone\UglyQueue $uglyQueue)
|
public function testCanGetLockFilePath(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$itemCount = $uglyQueue->getQueueItemCount();
|
$lockFile = $uglyQueue->getLockFile();
|
||||||
|
$this->assertEquals(
|
||||||
|
sprintf('%s%s%s%squeue.lock',
|
||||||
|
$this->baseDir,
|
||||||
|
DIRECTORY_SEPARATOR,
|
||||||
|
$uglyQueue->getName(),
|
||||||
|
DIRECTORY_SEPARATOR),
|
||||||
|
$lockFile
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::getQueueFile
|
||||||
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanGetQueueFilePath(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$queueFile = $uglyQueue->getQueueFile();
|
||||||
|
$this->assertEquals(
|
||||||
|
sprintf('%s%s%s%squeue.txt',
|
||||||
|
$this->baseDir,
|
||||||
|
DIRECTORY_SEPARATOR,
|
||||||
|
$uglyQueue->getName(),
|
||||||
|
DIRECTORY_SEPARATOR),
|
||||||
|
$queueFile
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::getQueueTmpFile
|
||||||
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanGetQueueTmpFilePath(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$queueTmpFile = $uglyQueue->getQueueTmpFile();
|
||||||
|
$this->assertEquals(
|
||||||
|
sprintf(
|
||||||
|
'%s%s%s%squeue.tmp',
|
||||||
|
$this->baseDir,
|
||||||
|
DIRECTORY_SEPARATOR,
|
||||||
|
$uglyQueue->getName(),
|
||||||
|
DIRECTORY_SEPARATOR),
|
||||||
|
$queueTmpFile
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::getSerializeFile
|
||||||
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanGetSerializeFilePath(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$serializeFile = $uglyQueue->getSerializeFile();
|
||||||
|
$this->assertEquals(
|
||||||
|
sprintf(
|
||||||
|
'%s%s%s%sugly-queue.obj',
|
||||||
|
$this->baseDir,
|
||||||
|
DIRECTORY_SEPARATOR,
|
||||||
|
$uglyQueue->getName(),
|
||||||
|
DIRECTORY_SEPARATOR),
|
||||||
|
$serializeFile
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testCanGetQueueLockedStatus(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$locked = $uglyQueue->isAlreadyLocked();
|
||||||
|
$this->assertFalse($locked);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueue::count
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
*/
|
||||||
|
public function testGetQueueItemCountReturnsZeroWithEmptyQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
{
|
||||||
|
$itemCount = count($uglyQueue);
|
||||||
$this->assertEquals(0, $itemCount);
|
$this->assertEquals(0, $itemCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::initialize
|
|
||||||
* @covers \DCarbone\UglyQueue::__construct
|
* @covers \DCarbone\UglyQueue::__construct
|
||||||
* @covers \DCarbone\UglyQueue::getInit
|
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @return \DCarbone\UglyQueue
|
* @return \DCarbone\UglyQueue
|
||||||
*/
|
*/
|
||||||
public function testCanInitializeExistingQueue()
|
public function testCanInitializeExistingQueue()
|
||||||
{
|
{
|
||||||
$conf = array(
|
$uglyQueue = new \DCarbone\UglyQueue($this->baseDir, 'tasty-sandwich');
|
||||||
'queue-base-dir' => dirname(__DIR__).'/misc/',
|
|
||||||
);
|
|
||||||
|
|
||||||
$uglyQueue = new \DCarbone\UglyQueue($conf);
|
|
||||||
|
|
||||||
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
||||||
|
|
||||||
$this->assertFalse($uglyQueue->getInit());
|
|
||||||
|
|
||||||
$uglyQueue->initialize('tasty-sandwich');
|
|
||||||
|
|
||||||
$this->assertTrue($uglyQueue->getInit());
|
|
||||||
|
|
||||||
return $uglyQueue;
|
return $uglyQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::lock
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
@@ -340,7 +261,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::lock
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanConstructUglyQueueWithValidParameter
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
@@ -351,10 +272,10 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::lock
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
* @covers \DCarbone\UglyQueue::isLocked
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
||||||
* @covers \DCarbone\UglyQueue::createQueueLock
|
* @covers \DCarbone\UglyQueue::createLockFile
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeNewUglyQueue
|
* @depends testCanInitializeObjectWithValidParameters
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
* @return \DCarbone\UglyQueue
|
* @return \DCarbone\UglyQueue
|
||||||
*/
|
*/
|
||||||
@@ -364,11 +285,9 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertTrue($locked);
|
$this->assertTrue($locked);
|
||||||
|
|
||||||
$queueDir = $uglyQueue->getQueueBaseDir().$uglyQueue->getQueueGroup().'/';
|
$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);
|
||||||
@@ -380,27 +299,29 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::lock
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
* @covers \DCarbone\UglyQueue::isLocked
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanInitializeExistingQueue
|
* @depends testCanInitializeExistingQueue
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testCannotLockInitializedQueueThatIsAlreadyLocked(\DCarbone\UglyQueue $uglyQueue)
|
public function testCannotLockQueueThatIsAlreadyLocked(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
|
|
||||||
$lock = $uglyQueue->lock();
|
$lock = $uglyQueue->lock();
|
||||||
|
|
||||||
$this->assertFalse($lock);
|
$this->assertFalse($lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::isLocked
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanLockUglyQueueWithDefaultTTL
|
* @depends testCanLockUglyQueueWithDefaultTTL
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testIsLockedReturnsTrueAfterLockingInitializedQueue(\DCarbone\UglyQueue $uglyQueue)
|
public function testIsLockedReturnsTrueAfterLocking(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$isLocked = $uglyQueue->isLocked();
|
|
||||||
|
$isLocked = $uglyQueue->isAlreadyLocked();
|
||||||
$this->assertTrue($isLocked);
|
$this->assertTrue($isLocked);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -414,31 +335,31 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function testCanUnlockLockedQueue(\DCarbone\UglyQueue $uglyQueue)
|
public function testCanUnlockLockedQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
|
|
||||||
$uglyQueue->unlock();
|
$uglyQueue->unlock();
|
||||||
|
|
||||||
$queueGroupDir = $uglyQueue->getQueueGroupDirPath();
|
$this->assertFileNotExists($uglyQueue->getLockFile());
|
||||||
|
|
||||||
$this->assertFileNotExists($queueGroupDir.'queue.lock');
|
|
||||||
|
|
||||||
return $uglyQueue;
|
return $uglyQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::isLocked
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanUnlockLockedQueue
|
* @depends testCanUnlockLockedQueue
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
*/
|
*/
|
||||||
public function testIsLockedReturnsFalseAfterUnlockingQueue(\DCarbone\UglyQueue $uglyQueue)
|
public function testIsLockedReturnsFalseAfterUnlockingQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
$isLocked = $uglyQueue->isLocked();
|
|
||||||
|
$isLocked = $uglyQueue->isAlreadyLocked();
|
||||||
|
|
||||||
$this->assertFalse($isLocked);
|
$this->assertFalse($isLocked);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::lock
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
* @covers \DCarbone\UglyQueue::isLocked
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @uses \DCarbone\Helpers\FileHelper
|
* @uses \DCarbone\Helpers\FileHelper
|
||||||
* @depends testCanUnlockLockedQueue
|
* @depends testCanUnlockLockedQueue
|
||||||
@@ -446,20 +367,20 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function testIsLockedReturnsFalseWithStaleQueueLockFile(\DCarbone\UglyQueue $uglyQueue)
|
public function testIsLockedReturnsFalseWithStaleQueueLockFile(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
|
|
||||||
$uglyQueue->lock(2);
|
$uglyQueue->lock(2);
|
||||||
$isLocked = $uglyQueue->isLocked();
|
$isLocked = $uglyQueue->isAlreadyLocked();
|
||||||
$this->assertTrue($isLocked);
|
$this->assertTrue($isLocked);
|
||||||
|
|
||||||
sleep(3);
|
sleep(3);
|
||||||
|
|
||||||
$isLocked = $uglyQueue->isLocked();
|
$isLocked = $uglyQueue->isAlreadyLocked();
|
||||||
$this->assertFalse($isLocked);
|
$this->assertFalse($isLocked);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::lock
|
* @covers \DCarbone\UglyQueue::lock
|
||||||
* @covers \DCarbone\UglyQueue::isLocked
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
||||||
* @covers \DCarbone\UglyQueue::getQueueGroupDirPath
|
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanUnlockLockedQueue
|
* @depends testCanUnlockLockedQueue
|
||||||
* @param \DCarbone\UglyQueue $uglyQueue
|
* @param \DCarbone\UglyQueue $uglyQueue
|
||||||
@@ -467,15 +388,14 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function testCanLockQueueWithValidIntegerValue(\DCarbone\UglyQueue $uglyQueue)
|
public function testCanLockQueueWithValidIntegerValue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
|
|
||||||
$locked = $uglyQueue->lock(200);
|
$locked = $uglyQueue->lock(200);
|
||||||
|
|
||||||
$this->assertTrue($locked);
|
$this->assertTrue($locked);
|
||||||
|
|
||||||
$queueDir = $uglyQueue->getQueueGroupDirPath();
|
$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);
|
||||||
@@ -486,7 +406,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
|
||||||
@@ -495,19 +415,18 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock(\DCarbone\UglyQueue $uglyQueue)
|
public function testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
|
|
||||||
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->getQueueGroupDirPath();
|
|
||||||
|
|
||||||
$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);
|
||||||
|
|
||||||
@@ -523,11 +442,10 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function testCanForciblyUpdateQueueFileFromTempFile(\DCarbone\UglyQueue $uglyQueue)
|
public function testCanForciblyUpdateQueueFileFromTempFile(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
|
|
||||||
$uglyQueue->_populateQueue();
|
$uglyQueue->_populateQueue();
|
||||||
|
|
||||||
$groupDir = $uglyQueue->getQueueGroupDirPath();
|
$this->assertFileNotExists($uglyQueue->getQueueTmpFile());
|
||||||
|
|
||||||
$this->assertFileNotExists($groupDir.'queue.tmp');
|
|
||||||
|
|
||||||
$uglyQueue->_populateQueue();
|
$uglyQueue->_populateQueue();
|
||||||
|
|
||||||
@@ -535,7 +453,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::getQueueItemCount
|
* @covers \DCarbone\UglyQueue::count
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @uses \DCarbone\Helpers\FileHelper
|
* @uses \DCarbone\Helpers\FileHelper
|
||||||
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
||||||
@@ -543,7 +461,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);
|
||||||
}
|
}
|
||||||
@@ -556,13 +474,14 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function testKeyExistsReturnsTrueWithPopulatedQueue(\DCarbone\UglyQueue $uglyQueue)
|
public function testKeyExistsReturnsTrueWithPopulatedQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
{
|
{
|
||||||
|
|
||||||
$exists = $uglyQueue->keyExistsInQueue(5);
|
$exists = $uglyQueue->keyExistsInQueue(5);
|
||||||
|
|
||||||
$this->assertTrue($exists);
|
$this->assertTrue($exists);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \DCarbone\UglyQueue::processQueue
|
* @covers \DCarbone\UglyQueue::retrieveItems
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
@@ -570,11 +489,12 @@ 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
|
||||||
@@ -582,12 +502,13 @@ 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::count
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @uses \DCarbone\Helpers\FileHelper
|
* @uses \DCarbone\Helpers\FileHelper
|
||||||
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
||||||
@@ -596,21 +517,22 @@ 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::count
|
||||||
* @uses \DCarbone\UglyQueue
|
* @uses \DCarbone\UglyQueue
|
||||||
* @uses \DCarbone\Helpers\FileHelper
|
* @uses \DCarbone\Helpers\FileHelper
|
||||||
* @depends testCanGetPartialQueueContents
|
* @depends testCanGetPartialQueueContents
|
||||||
@@ -619,14 +541,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;
|
||||||
}
|
}
|
||||||
|
|||||||
214
tests/UglyQueueManager/UglyQueueManagerTest.php
Normal file
214
tests/UglyQueueManager/UglyQueueManagerTest.php
Normal file
@@ -0,0 +1,214 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UglyQueueManagerTest
|
||||||
|
*/
|
||||||
|
class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected $baseDir;
|
||||||
|
|
||||||
|
protected $reallyTastySandwich = array(
|
||||||
|
'0' => 'beef broth',
|
||||||
|
'1' => 'barbeque sauce',
|
||||||
|
'2' => 'boneless pork ribs',
|
||||||
|
);
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->baseDir = realpath(__DIR__.'/../misc/queues');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::__construct
|
||||||
|
* @covers \DCarbone\UglyQueue::unserialize
|
||||||
|
* @covers \DCarbone\UglyQueueManager::addQueue
|
||||||
|
* @covers \DCarbone\UglyQueueManager::containsQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @return \DCarbone\UglyQueueManager
|
||||||
|
*/
|
||||||
|
public function testCanInitializeObjectWithValidPath()
|
||||||
|
{
|
||||||
|
$manager = new \DCarbone\UglyQueueManager($this->baseDir);
|
||||||
|
|
||||||
|
$this->assertInstanceOf('\\DCarbone\\UglyQueueManager', $manager);
|
||||||
|
|
||||||
|
return $manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::__construct
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @expectedException \RuntimeException
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownDuringConstructionWithInvalidBasePathValue()
|
||||||
|
{
|
||||||
|
new \DCarbone\UglyQueueManager('i do not exist!');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::containsQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @depends testCanInitializeObjectWithValidPath
|
||||||
|
* @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 testCanInitializeObjectWithValidPath
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanDetermineQueueDoesNotExistInManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$shouldBeFalse = $manager->containsQueueWithName('i should not exist');
|
||||||
|
|
||||||
|
$this->assertFalse($shouldBeFalse);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @uses \DCarbone\UglyQueue
|
||||||
|
* @depends testCanInitializeObjectWithValidPath
|
||||||
|
* @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->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueWithName
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @depends testCanInitializeObjectWithValidPath
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testExceptionThrownWhenTryingToGetNonExistentQueueFromManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$manager->getQueueWithName('sandwiches');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \DCarbone\UglyQueueManager::getQueueList
|
||||||
|
* @uses \DCarbone\UglyQueueManager
|
||||||
|
* @depends testCanInitializeObjectWithValidPath
|
||||||
|
* @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 testCanInitializeObjectWithValidPath
|
||||||
|
* @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 testCanInitializeObjectWithValidPath
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanInitializeNewQueueAndAddToManager(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$manager->addQueueAtPath($this->baseDir.'/really-tasty-sandwich');
|
||||||
|
|
||||||
|
$uglyQueue = $manager->getQueueWithName('really-tasty-sandwich');
|
||||||
|
|
||||||
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
||||||
|
$this->assertEquals('really-tasty-sandwich', $uglyQueue->getName());
|
||||||
|
|
||||||
|
$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 testCanInitializeObjectWithValidPath
|
||||||
|
* @param \DCarbone\UglyQueueManager $manager
|
||||||
|
*/
|
||||||
|
public function testCanRemoveQueueFromManagerByName(\DCarbone\UglyQueueManager $manager)
|
||||||
|
{
|
||||||
|
$manager->removeQueueByName('really-tasty-sandwich');
|
||||||
|
|
||||||
|
$queueList = $manager->getQueueList();
|
||||||
|
|
||||||
|
$this->assertCount(1, $queueList);
|
||||||
|
$this->assertNotContains('really-tasty-sandwich', $queueList);
|
||||||
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * @covers \DCarbone\UglyQueue::queueExists
|
||||||
|
// * @uses \DCarbone\UglyQueue
|
||||||
|
// * @depends testCanInitializeNewUglyQueue
|
||||||
|
// * @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
// */
|
||||||
|
// public function testCanDetermineExistenceOfExistingQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
// {
|
||||||
|
// $exists = $uglyQueue->queueExists('tasty-sandwich');
|
||||||
|
//
|
||||||
|
// $this->assertTrue($exists);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * @covers \DCarbone\UglyQueue::queueExists
|
||||||
|
// * @uses \DCarbone\UglyQueue
|
||||||
|
// * @depends testCanInitializeNewUglyQueue
|
||||||
|
// * @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
// */
|
||||||
|
// public function testCanDetermineExistenceOfNonExistingQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
// {
|
||||||
|
// $exists = $uglyQueue->queueExists('nasty-sandwich');
|
||||||
|
//
|
||||||
|
// $this->assertFalse($exists);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * @covers \DCarbone\UglyQueue::getInitializedQueueList
|
||||||
|
// * @uses \DCarbone\UglyQueue
|
||||||
|
// * @depends testCanInitializeNewUglyQueue
|
||||||
|
// * @param \DCarbone\UglyQueue $uglyQueue
|
||||||
|
// */
|
||||||
|
// public function testCanGetListOfInitializedQueues(\DCarbone\UglyQueue $uglyQueue)
|
||||||
|
// {
|
||||||
|
// $queueList = $uglyQueue->getInitializedQueueList();
|
||||||
|
//
|
||||||
|
// $this->assertEquals(1, count($queueList));
|
||||||
|
// $this->assertContains('tasty-sandwich', $queueList);
|
||||||
|
// }
|
||||||
|
}
|
||||||
@@ -1,13 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
if (is_dir(__DIR__.'/tasty-sandwich'))
|
if (is_dir(__DIR__.'/queues/'))
|
||||||
{
|
{
|
||||||
foreach(glob(__DIR__.'/tasty-sandwich/*') as $file)
|
foreach(glob(__DIR__.'/queues/*', GLOB_ONLYDIR) as $queueDir)
|
||||||
{
|
{
|
||||||
if (substr($file, -1) === '.')
|
foreach(glob($queueDir.'/*') as $file)
|
||||||
|
{
|
||||||
|
$split = preg_split('#[/\\\]+#', $file);
|
||||||
|
if (strpos(end($split), '.') === 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
unlink($file);
|
unlink($file);
|
||||||
}
|
}
|
||||||
rmdir(__DIR__.'/tasty-sandwich');
|
|
||||||
|
rmdir($queueDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mkdir(__DIR__.'/queues');
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user