diff --git a/src/UglyQueue.php b/src/UglyQueue.php index fd39aa3..a0fe956 100644 --- a/src/UglyQueue.php +++ b/src/UglyQueue.php @@ -256,8 +256,7 @@ HTML; public function processQueue($count = 1) { if ($this->mode === self::QUEUE_READONLY) - throw new \RuntimeException('Queue "'.$this->_name.'" cannot be processed.'. - ' It was started in Read-Only mode (the user running this process does not have permission to write to the queue directory).'); + throw new \RuntimeException('Queue "'.$this->_name.'" cannot be processed. It was started in Read-Only mode (the user running this process does not have permission to write to the queue directory).'); // If we don't have a lock, assume issue and move on. if ($this->_locked === false) diff --git a/src/UglyQueueManager.php b/src/UglyQueueManager.php index 12939ab..5a42ccc 100644 --- a/src/UglyQueueManager.php +++ b/src/UglyQueueManager.php @@ -43,8 +43,6 @@ class UglyQueueManager implements \SplObserver, \SplSubject if (!is_dir($config['queue-base-dir'])) throw new \RuntimeException('"queue-base-dir" points to a directory that does not exist.'); - $this->notifyStatus = self::NOTIFY_MANAGER_INITIALIZED; - $this->config = $config; $this->queueBaseDir = rtrim(realpath($this->config['queue-base-dir']), "/\\").DIRECTORY_SEPARATOR; $this->observers = $observers; @@ -58,18 +56,14 @@ class UglyQueueManager implements \SplObserver, \SplSubject public static function init(array $config, array $observers = array()) { /** @var \DCarbone\UglyQueueManager $manager */ - $manager = new static($config, array()); - - $manager->observers = $observers; - - $manager->notify(); + $manager = new static($config, $observers); /** @var \DCarbone\UglyQueue $uglyQueue */ foreach(glob($manager->queueBaseDir.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $queueDir) { // Try to avoid looking at hidden directories or magic dirs such as '.' and '..' - $split = preg_split('#[/\\]+#', $queueDir); + $split = preg_split('#[/\\\]+#', $queueDir); if (strpos(end($split), '.') === 0) continue; @@ -81,6 +75,9 @@ class UglyQueueManager implements \SplObserver, \SplSubject $manager->addQueue($uglyQueue); } + $manager->notifyStatus = self::NOTIFY_MANAGER_INITIALIZED; + $manager->notify(); + return $manager; } @@ -91,10 +88,9 @@ class UglyQueueManager implements \SplObserver, \SplSubject */ 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.'); - $this->queues[$uglyQueue->name] = $uglyQueue; $this->notifyStatus = self::NOTIFY_QUEUE_ADDED; @@ -120,7 +116,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject */ public function removeQueue(UglyQueue $uglyQueue) { - if ($this->queueExistsByName($uglyQueue->name)) + if ($this->containsQueueWithName($uglyQueue->name)) unset($this->queues[$uglyQueue->name]); return $this; @@ -132,7 +128,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject */ public function removeQueueByName($name) { - if ($this->queueExistsByName($name)) + if ($this->containsQueueWithName($name)) { unset($this->queues[$name]); $this->notifyStatus = self::NOTIFY_QUEUE_REMOVED; @@ -159,7 +155,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject * @param string $name * @return bool */ - public function queueExistsByName($name) + public function containsQueueWithName($name) { return isset($this->queues[$name]); } diff --git a/tests/UglyQueue/UglyQueueTest.php b/tests/UglyQueue/UglyQueueTest.php index 7a001ca..883fea7 100644 --- a/tests/UglyQueue/UglyQueueTest.php +++ b/tests/UglyQueue/UglyQueueTest.php @@ -113,6 +113,31 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase $this->assertEquals('tasty-sandwich', $queueName); } + /** + * @covers \DCarbone\UglyQueue::__get + * @uses \DCarbone\UglyQueue + * @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers + * @param \DCarbone\UglyQueue $uglyQueue + */ + public function testCanGetQueueLockedStatus(\DCarbone\UglyQueue $uglyQueue) + { + $locked = $uglyQueue->locked; + + $this->assertFalse($locked); + } + + /** + * @covers \DCarbone\UglyQueue::__get + * @uses \DCarbone\UglyQueue + * @expectedException \OutOfBoundsException + * @depends testCanInitializeUglyQueueWithValidConfigArrayAndNoObservers + * @param \DCarbone\UglyQueue $uglyQueue + */ + public function testExceptionThrownWhenAttemptingToGetInvalidProperty(\DCarbone\UglyQueue $uglyQueue) + { + $sandwich = $uglyQueue->sandwich; + } + /** * @covers \DCarbone\UglyQueue::isLocked * @uses \DCarbone\UglyQueue diff --git a/tests/UglyQueueManager/UglyQueueManagerTest.php b/tests/UglyQueueManager/UglyQueueManagerTest.php index 6ca65c6..88ec794 100644 --- a/tests/UglyQueueManager/UglyQueueManagerTest.php +++ b/tests/UglyQueueManager/UglyQueueManagerTest.php @@ -5,6 +5,59 @@ */ 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 // * @uses \DCarbone\UglyQueue