Adding some test cases for UglyQueueManager

This commit is contained in:
2014-09-29 17:18:14 -05:00
parent 1e98b0cf7e
commit 793edc2d60
4 changed files with 88 additions and 15 deletions

View File

@@ -256,8 +256,7 @@ HTML;
public function processQueue($count = 1) public function processQueue($count = 1)
{ {
if ($this->mode === self::QUEUE_READONLY) if ($this->mode === self::QUEUE_READONLY)
throw new \RuntimeException('Queue "'.$this->_name.'" cannot be processed.'. 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).');
' It was started in Read-Only mode (the user running this process does not have permission to write to the queue directory).');
// If we don't have a lock, assume issue and move on. // If we don't have a lock, assume issue and move on.
if ($this->_locked === false) if ($this->_locked === false)

View File

@@ -43,8 +43,6 @@ class UglyQueueManager implements \SplObserver, \SplSubject
if (!is_dir($config['queue-base-dir'])) if (!is_dir($config['queue-base-dir']))
throw new \RuntimeException('"queue-base-dir" points to a directory that does not exist.'); throw new \RuntimeException('"queue-base-dir" points to a directory that does not exist.');
$this->notifyStatus = self::NOTIFY_MANAGER_INITIALIZED;
$this->config = $config; $this->config = $config;
$this->queueBaseDir = rtrim(realpath($this->config['queue-base-dir']), "/\\").DIRECTORY_SEPARATOR; $this->queueBaseDir = rtrim(realpath($this->config['queue-base-dir']), "/\\").DIRECTORY_SEPARATOR;
$this->observers = $observers; $this->observers = $observers;
@@ -58,18 +56,14 @@ class UglyQueueManager implements \SplObserver, \SplSubject
public static function init(array $config, array $observers = array()) public static function init(array $config, array $observers = array())
{ {
/** @var \DCarbone\UglyQueueManager $manager */ /** @var \DCarbone\UglyQueueManager $manager */
$manager = new static($config, array()); $manager = new static($config, $observers);
$manager->observers = $observers;
$manager->notify();
/** @var \DCarbone\UglyQueue $uglyQueue */ /** @var \DCarbone\UglyQueue $uglyQueue */
foreach(glob($manager->queueBaseDir.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $queueDir) foreach(glob($manager->queueBaseDir.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $queueDir)
{ {
// Try to avoid looking at hidden directories or magic dirs such as '.' and '..' // Try to avoid looking at hidden directories or magic dirs such as '.' and '..'
$split = preg_split('#[/\\]+#', $queueDir); $split = preg_split('#[/\\\]+#', $queueDir);
if (strpos(end($split), '.') === 0) if (strpos(end($split), '.') === 0)
continue; continue;
@@ -81,6 +75,9 @@ class UglyQueueManager implements \SplObserver, \SplSubject
$manager->addQueue($uglyQueue); $manager->addQueue($uglyQueue);
} }
$manager->notifyStatus = self::NOTIFY_MANAGER_INITIALIZED;
$manager->notify();
return $manager; return $manager;
} }
@@ -91,10 +88,9 @@ class UglyQueueManager implements \SplObserver, \SplSubject
*/ */
public function addQueue(UglyQueue $uglyQueue) public function addQueue(UglyQueue $uglyQueue)
{ {
if ($this->queueExistsByName($uglyQueue->name)) if ($this->containsQueueWithName($uglyQueue->name))
throw new \InvalidArgumentException('Queue named "'.$uglyQueue->name.'" already exists in this manager.'); throw new \InvalidArgumentException('Queue named "'.$uglyQueue->name.'" already exists in this manager.');
$this->queues[$uglyQueue->name] = $uglyQueue; $this->queues[$uglyQueue->name] = $uglyQueue;
$this->notifyStatus = self::NOTIFY_QUEUE_ADDED; $this->notifyStatus = self::NOTIFY_QUEUE_ADDED;
@@ -120,7 +116,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject
*/ */
public function removeQueue(UglyQueue $uglyQueue) public function removeQueue(UglyQueue $uglyQueue)
{ {
if ($this->queueExistsByName($uglyQueue->name)) if ($this->containsQueueWithName($uglyQueue->name))
unset($this->queues[$uglyQueue->name]); unset($this->queues[$uglyQueue->name]);
return $this; return $this;
@@ -132,7 +128,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject
*/ */
public function removeQueueByName($name) public function removeQueueByName($name)
{ {
if ($this->queueExistsByName($name)) if ($this->containsQueueWithName($name))
{ {
unset($this->queues[$name]); unset($this->queues[$name]);
$this->notifyStatus = self::NOTIFY_QUEUE_REMOVED; $this->notifyStatus = self::NOTIFY_QUEUE_REMOVED;
@@ -159,7 +155,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject
* @param string $name * @param string $name
* @return bool * @return bool
*/ */
public function queueExistsByName($name) public function containsQueueWithName($name)
{ {
return isset($this->queues[$name]); return isset($this->queues[$name]);
} }

View File

@@ -113,6 +113,31 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
$this->assertEquals('tasty-sandwich', $queueName); $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 * @covers \DCarbone\UglyQueue::isLocked
* @uses \DCarbone\UglyQueue * @uses \DCarbone\UglyQueue

View File

@@ -5,6 +5,59 @@
*/ */
class UglyQueueManagerTest extends PHPUnit_Framework_TestCase class UglyQueueManagerTest extends PHPUnit_Framework_TestCase
{ {
/**
* @covers \DCarbone\UglyQueueManager::__construct
* @covers \DCarbone\UglyQueueManager::init
* @covers \DCarbone\UglyQueue::unserialize
* @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/'
);
$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\UglyQueue::queueExists // * @covers \DCarbone\UglyQueue::queueExists
// * @uses \DCarbone\UglyQueue // * @uses \DCarbone\UglyQueue