From d935032ec309cdfae01086acd2a7fc08ac7a6229 Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Tue, 29 Sep 2015 12:08:32 -0500 Subject: [PATCH] Devil is in the details. --- src/UglyQueue.php | 21 ++++++++------- tests/UglyQueue/UglyQueueTest.php | 26 +++++++++++++++++-- .../UglyQueueManager/UglyQueueManagerTest.php | 4 +-- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/UglyQueue.php b/src/UglyQueue.php index 9040803..ea19b57 100644 --- a/src/UglyQueue.php +++ b/src/UglyQueue.php @@ -46,23 +46,23 @@ class UglyQueue implements \Serializable, \SplSubject, \Countable /** * @param string $baseDir - * @param string $queueName + * @param string $name * @param \SplObserver[] $observers */ - public function __construct($baseDir, $queueName, array $observers = array()) + public function __construct($baseDir, $name, array $observers = array()) { $this->baseDir = trim($baseDir, "/\\"); - $this->name = $queueName; + $this->name = $name; $this->_observers = $observers; - $path = sprintf('%s/%s', $baseDir, $queueName); + $path = sprintf('%s/%s', $baseDir, $name); if (!file_exists($path) && !@mkdir($path)) throw new \RuntimeException('Unable to initialize queue directory "'.$path.'". Please check permissions.'); $this->path = $path; - $this->lockFile = sprintf('%s/queue.lock', $this->path); - $this->queueFile = sprintf('%s/queue.txt', $this->path); - $this->queueTmpFile = sprintf('%s/queue.tmp', $this->path); + $this->lockFile = sprintf('%s/queue.lock', $path); + $this->queueFile = sprintf('%s/queue.txt', $path); + $this->queueTmpFile = sprintf('%s/queue.tmp', $path); $this->_initialize(); } @@ -77,7 +77,7 @@ class UglyQueue implements \Serializable, \SplSubject, \Countable else if (is_readable($this->path)) $this->mode = self::QUEUE_READONLY; - if (!file_exists($this->path.'index.html')) + 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.'); @@ -92,7 +92,7 @@ class UglyQueue implements \Serializable, \SplSubject, \Countable HTML; - file_put_contents($this->path.'index.html', $html); + file_put_contents($this->path.'/index.html', $html); } if (!file_exists($this->queueFile)) @@ -301,7 +301,7 @@ HTML; } // Find number of lines in the queue file - $lineCount = FileHelper::getLineCount($this->queueFile); + $lineCount = count($this); // If queue line count is 0, assume empty if ($lineCount === 0) @@ -450,6 +450,7 @@ HTML; */ public function count() { + var_dump($this->queueFile); return (int)FileHelper::getLineCount($this->queueFile); } diff --git a/tests/UglyQueue/UglyQueueTest.php b/tests/UglyQueue/UglyQueueTest.php index e47d33e..dddf2a1 100644 --- a/tests/UglyQueue/UglyQueueTest.php +++ b/tests/UglyQueue/UglyQueueTest.php @@ -30,7 +30,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase protected function setUp() { - $this->baseDir = __DIR__.'/../misc/queues'; + $this->baseDir = realpath(__DIR__.'/../misc/queues'); } /** @@ -40,6 +40,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testCanInitializeObjectWithValidParameters() { + echo __FUNCTION__."\n"; $uglyQueue = new \DCarbone\UglyQueue($this->baseDir, 'tasty-sandwich'); $this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue); @@ -56,6 +57,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testExceptionThrownWhenTryingToProcessQueueAfterInitializationBeforeLock(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $uglyQueue->retrieveItems(); } @@ -67,6 +69,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testKeyExistsInQueueReturnsFalseWithEmptyQueueAfterInitialization(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $exists = $uglyQueue->keyExistsInQueue(0); $this->assertFalse($exists); @@ -92,6 +95,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testCanGetQueueDirectory(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $queuePath = $uglyQueue->getPath(); $this->assertFileExists($queuePath); @@ -105,6 +109,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testCanGetQueueName(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $queueName = $uglyQueue->getName(); $this->assertEquals('tasty-sandwich', $queueName); @@ -118,6 +123,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testCanGetQueueLockedStatus(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $locked = $uglyQueue->isLocked(); $this->assertFalse($locked); @@ -131,6 +137,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testGetQueueItemCountReturnsZeroWithEmptyQueue(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $itemCount = count($uglyQueue); $this->assertEquals(0, $itemCount); } @@ -142,6 +149,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testCanInitializeExistingQueue() { + echo __FUNCTION__."\n"; $uglyQueue = new \DCarbone\UglyQueue($this->baseDir, 'tasty-sandwich'); $this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue); @@ -158,6 +166,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testExceptionThrownWhenPassingNonIntegerValueToLock(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $uglyQueue->lock('7 billion'); } @@ -170,6 +179,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testExceptionThrownWhenPassingNegativeIntegerValueToLock(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $uglyQueue->lock(-73); } @@ -184,6 +194,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testCanLockUglyQueueWithDefaultTTL(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $locked = $uglyQueue->lock(); $this->assertTrue($locked); @@ -209,6 +220,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testCannotLockQueueThatIsAlreadyLocked(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $lock = $uglyQueue->lock(); $this->assertFalse($lock); @@ -222,6 +234,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testIsLockedReturnsTrueAfterLocking(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $isLocked = $uglyQueue->isLocked(); $this->assertTrue($isLocked); } @@ -236,6 +249,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testCanUnlockLockedQueue(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $uglyQueue->unlock(); $this->assertFileNotExists($uglyQueue->getLockFile()); @@ -251,6 +265,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testIsLockedReturnsFalseAfterUnlockingQueue(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $isLocked = $uglyQueue->isLocked(); $this->assertFalse($isLocked); @@ -266,6 +281,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testIsLockedReturnsFalseWithStaleQueueLockFile(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $uglyQueue->lock(2); $isLocked = $uglyQueue->isLocked(); $this->assertTrue($isLocked); @@ -279,7 +295,6 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase /** * @covers \DCarbone\UglyQueue::lock * @covers \DCarbone\UglyQueue::isLocked - * @covers \DCarbone\UglyQueue::__get * @uses \DCarbone\UglyQueue * @depends testCanUnlockLockedQueue * @param \DCarbone\UglyQueue $uglyQueue @@ -287,6 +302,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testCanLockQueueWithValidIntegerValue(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $locked = $uglyQueue->lock(200); $this->assertTrue($locked); @@ -313,6 +329,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; foreach(array_reverse($this->tastySandwich, true) as $k=>$v) { $added = $uglyQueue->addItem($k, $v); @@ -339,6 +356,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testCanForciblyUpdateQueueFileFromTempFile(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $uglyQueue->_populateQueue(); $this->assertFileNotExists($uglyQueue->getQueueTmpFile()); @@ -370,6 +388,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testKeyExistsReturnsTrueWithPopulatedQueue(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $exists = $uglyQueue->keyExistsInQueue(5); $this->assertTrue($exists); @@ -384,6 +403,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testExceptionThrownWhenTryingToProcessLockedQueueWithNonInteger(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $uglyQueue->retrieveItems('Eleventy Billion'); } @@ -396,6 +416,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testExceptionThrownWhenTryingToProcessLockedQueueWithIntegerLessThan1(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $uglyQueue->retrieveItems(0); } @@ -410,6 +431,7 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase */ public function testCanGetPartialQueueContents(\DCarbone\UglyQueue $uglyQueue) { + echo __FUNCTION__."\n"; $process = $uglyQueue->retrieveItems(5); $this->assertEquals(5, count($process)); diff --git a/tests/UglyQueueManager/UglyQueueManagerTest.php b/tests/UglyQueueManager/UglyQueueManagerTest.php index 789ae19..7f5bbb8 100644 --- a/tests/UglyQueueManager/UglyQueueManagerTest.php +++ b/tests/UglyQueueManager/UglyQueueManagerTest.php @@ -15,7 +15,7 @@ class UglyQueueManagerTest extends PHPUnit_Framework_TestCase protected function setUp() { - $this->baseDir = __DIR__.'/../misc/queues'; + $this->baseDir = realpath(__DIR__.'/../misc/queues'); } /** @@ -142,7 +142,7 @@ class UglyQueueManagerTest extends PHPUnit_Framework_TestCase */ public function testCanInitializeNewQueueAndAddToManager(\DCarbone\UglyQueueManager $manager) { - $manager->addQueueAtPath(__DIR__.'/../misc/queues/really-tasty-sandwich'); + $manager->addQueueAtPath($this->baseDir.'/really-tasty-sandwich'); $uglyQueue = $manager->getQueueWithName('really-tasty-sandwich');