From b166fd34975f64b9aa3cfed26006e6e1d365fac3 Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Fri, 8 Aug 2014 18:09:45 -0500 Subject: [PATCH] Adding 2 methods and beginning work on test cases --- src/UglyQueue.php | 44 ++++++++++++++++++++++++++++ tests/UglyQueue/UglyQueueTest.php | 44 +++++++++++++++++++++++++++- tests/misc/tasty-sandwich/index.html | 8 +++++ tests/misc/tasty-sandwich/queue.txt | 0 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 tests/misc/tasty-sandwich/index.html create mode 100644 tests/misc/tasty-sandwich/queue.txt diff --git a/src/UglyQueue.php b/src/UglyQueue.php index 318b1d2..270618e 100644 --- a/src/UglyQueue.php +++ b/src/UglyQueue.php @@ -308,4 +308,48 @@ HTML; { return $this->queueBaseDir; } + + /** + * @return int|null + * @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 + * @return bool + * @throws \RuntimeException + */ + public function keyExistsInQueue($key) + { + if ($this->init === false) + throw new \RuntimeException('UglyQueue::keyExistsInQueue - Must first initialize queue'); + + // If we don't have a lock, assume issue and move on. + if ($this->haveLock === false) + throw new \RuntimeException('UglyQueue::keyExistsInQueue - Must first acquire queue lock'); + + $key = (string)$key; + + // Try to open the file for reading / writing. + $queue_file_handle = fopen($this->queueGroupDirPath.'queue.txt', 'r'); + + while(($line = fscanf($queue_file_handle, "%s\t")) !== false) + { + if (strpos($line, $key) === 0) + { + fclose($queue_file_handle); + return true; + } + } + + fclose($queue_file_handle); + return false; + } } \ No newline at end of file diff --git a/tests/UglyQueue/UglyQueueTest.php b/tests/UglyQueue/UglyQueueTest.php index 7fd3725..d40b1e7 100644 --- a/tests/UglyQueue/UglyQueueTest.php +++ b/tests/UglyQueue/UglyQueueTest.php @@ -5,6 +5,23 @@ */ class UglyQueueTest extends PHPUnit_Framework_TestCase { + /** + * @var array + */ + protected $tastySandwich = array( + '0' => 'unsalted butter', + '1' => 'all-purpose flour', + '2' => 'hot milk', + '3' => 'kosher salt', + '4' => 'freshly ground black pepper', + '5' => 'nutmeg', + '6' => 'grated Gruyere', + '7' => 'freshly grated Parmesan', + '8' => 'white sandwich bread, crust removed', + '9' => 'Dijon mustard', + '10' => 'Virginia baked ham, sliced', + ); + /** * @covers \DCarbone\UglyQueue::__construct * @uses \DCarbone\UglyQueue @@ -18,6 +35,8 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase $uglyQueue = new \DCarbone\UglyQueue($conf); + $this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue); + return $uglyQueue; } @@ -45,4 +64,27 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase $uglyQueue = new \DCarbone\UglyQueue($conf); } -} + + /** + * @covers \DCarbone\UglyQueue::initialize + * @uses \DCarbone\UglyQueue + * @depends testCanConstructUglyQueueWithValidParameter + * @param \DCarbone\UglyQueue $uglyQueue + */ + public function testCanInitializeExistingQueue(\DCarbone\UglyQueue $uglyQueue) + { + $uglyQueue->initialize('tasty-sandwich'); + + $this->assertTrue( + is_dir($uglyQueue->getQueueBaseDir().$uglyQueue->getQueueGroup().'/'), + 'Could not verify existence of queue group dir'); + + $this->assertTrue( + file_exists($uglyQueue->getQueueBaseDir().$uglyQueue->getQueueGroup().'/'.'index.html'), + 'Could not verify existence of index.html in queue group dir'); + + $this->assertTrue( + file_exists($uglyQueue->getQueueBaseDir().$uglyQueue->getQueueGroup().'/'.'queue.txt'), + 'Could not verify existence of queue.txt in queue group dir'); + } +} \ No newline at end of file diff --git a/tests/misc/tasty-sandwich/index.html b/tests/misc/tasty-sandwich/index.html new file mode 100644 index 0000000..8c42028 --- /dev/null +++ b/tests/misc/tasty-sandwich/index.html @@ -0,0 +1,8 @@ + + + 403 Forbidden + + +

Directory access is forbidden.

+ + \ No newline at end of file diff --git a/tests/misc/tasty-sandwich/queue.txt b/tests/misc/tasty-sandwich/queue.txt new file mode 100644 index 0000000..e69de29