Files
ugly-queue/tests/UglyQueue/UglyQueueTest.php

553 lines
17 KiB
PHP

<?php
date_default_timezone_set('UTC');
require_once __DIR__.'/../misc/cleanup.php';
/**
* Class UglyQueueTest
*/
class UglyQueueTest extends PHPUnit\Framework\TestCase
{
protected $baseDir;
/**
* @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',
);
protected function setUp(): void
{
$this->baseDir = realpath(__DIR__.'/../misc/queues');
}
/**
* @covers \Cybercinch\UglyQueue::__construct
* @uses \Cybercinch\UglyQueue
* @return \Cybercinch\UglyQueue
*/
public function testCanInitializeObjectWithValidParameters()
{
$uglyQueue = new \Cybercinch\UglyQueue($this->baseDir, 'tasty-sandwich');
$this->assertInstanceOf('\\Cybercinch\\UglyQueue', $uglyQueue);
return $uglyQueue;
}
/**
* @covers \Cybercinch\UglyQueue::retrieveItems
* @uses \Cybercinch\UglyQueue
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testExceptionThrownWhenTryingToProcessQueueAfterInitializationBeforeLock(\Cybercinch\UglyQueue $uglyQueue)
{
$this->expectException(\RuntimeException::class);
$uglyQueue->retrieveItems();
}
/**
* @covers \Cybercinch\UglyQueue::keyExistsInQueue
* @uses \Cybercinch\UglyQueue
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testKeyExistsInQueueReturnsFalseWithEmptyQueueAfterInitialization(\Cybercinch\UglyQueue $uglyQueue)
{
$exists = $uglyQueue->keyExistsInQueue(0);
$this->assertFalse($exists);
}
/**
* @covers \Cybercinch\UglyQueue::addItem
* @uses \Cybercinch\UglyQueue
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLock(\Cybercinch\UglyQueue $uglyQueue)
{
$this->expectException(\RuntimeException::class);
$uglyQueue->addItem('test', 'value');
}
/**
* @covers \Cybercinch\UglyQueue::getPath
* @uses \Cybercinch\UglyQueue
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testCanGetQueueDirectory(\Cybercinch\UglyQueue $uglyQueue)
{
$queuePath = $uglyQueue->getPath();
$this->assertFileExists($queuePath);
}
/**
* @covers \Cybercinch\UglyQueue::getName
* @uses \Cybercinch\UglyQueue
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testCanGetQueueName(\Cybercinch\UglyQueue $uglyQueue)
{
$queueName = $uglyQueue->getName();
$this->assertEquals('tasty-sandwich', $queueName);
}
/**
* @covers \Cybercinch\UglyQueue::getMode
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testCanGetQueueMode(\Cybercinch\UglyQueue $uglyQueue)
{
$mode = $uglyQueue->getMode();
$this->assertEquals(\Cybercinch\UglyQueue::QUEUE_READWRITE, $mode);
}
/**
* @covers \Cybercinch\UglyQueue::getBaseDir
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testCanGetBaseDirectory(\Cybercinch\UglyQueue $uglyQueue)
{
$baseDir = $uglyQueue->getBaseDir();
$this->assertEquals($this->baseDir, $baseDir);
}
/**
* @covers \Cybercinch\UglyQueue::getLockFile
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testCanGetLockFilePath(\Cybercinch\UglyQueue $uglyQueue)
{
$lockFile = $uglyQueue->getLockFile();
$this->assertEquals(
sprintf('%s%s%s%squeue.lock',
$this->baseDir,
DIRECTORY_SEPARATOR,
$uglyQueue->getName(),
DIRECTORY_SEPARATOR),
$lockFile
);
}
/**
* @covers \Cybercinch\UglyQueue::getQueueFile
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testCanGetQueueFilePath(\Cybercinch\UglyQueue $uglyQueue)
{
$queueFile = $uglyQueue->getQueueFile();
$this->assertEquals(
sprintf('%s%s%s%squeue.txt',
$this->baseDir,
DIRECTORY_SEPARATOR,
$uglyQueue->getName(),
DIRECTORY_SEPARATOR),
$queueFile
);
}
/**
* @covers \Cybercinch\UglyQueue::getQueueTmpFile
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testCanGetQueueTmpFilePath(\Cybercinch\UglyQueue $uglyQueue)
{
$queueTmpFile = $uglyQueue->getQueueTmpFile();
$this->assertEquals(
sprintf(
'%s%s%s%squeue.tmp',
$this->baseDir,
DIRECTORY_SEPARATOR,
$uglyQueue->getName(),
DIRECTORY_SEPARATOR),
$queueTmpFile
);
}
/**
* @covers \Cybercinch\UglyQueue::getSerializeFile
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testCanGetSerializeFilePath(\Cybercinch\UglyQueue $uglyQueue)
{
$serializeFile = $uglyQueue->getSerializeFile();
$this->assertEquals(
sprintf(
'%s%s%s%sugly-queue.obj',
$this->baseDir,
DIRECTORY_SEPARATOR,
$uglyQueue->getName(),
DIRECTORY_SEPARATOR),
$serializeFile
);
}
/**
* @covers \Cybercinch\isAlreadyLocked::isAlreadyLocked
* @uses \Cybercinch\UglyQueue
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testCanGetQueueLockedStatus(\Cybercinch\UglyQueue $uglyQueue)
{
$locked = $uglyQueue->isAlreadyLocked();
$this->assertFalse($locked);
}
/**
* @covers \Cybercinch\UglyQueue::count
* @uses \Cybercinch\UglyQueue
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testGetQueueItemCountReturnsZeroWithEmptyQueue(\Cybercinch\UglyQueue $uglyQueue)
{
$itemCount = count($uglyQueue);
$this->assertEquals(0, $itemCount);
}
/**
* @covers \Cybercinch\UglyQueue::__construct
* @uses \Cybercinch\UglyQueue
* @return \Cybercinch\UglyQueue
*/
public function testCanInitializeExistingQueue()
{
$uglyQueue = new \Cybercinch\UglyQueue($this->baseDir, 'tasty-sandwich');
$this->assertInstanceOf('\\Cybercinch\\UglyQueue', $uglyQueue);
return $uglyQueue;
}
/**
* @covers \Cybercinch\UglyQueue::lock
* @uses \Cybercinch\UglyQueue
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testExceptionThrownWhenPassingNonIntegerValueToLock(\Cybercinch\UglyQueue $uglyQueue)
{
$this->expectException(\InvalidArgumentException::class);
$uglyQueue->lock('7 billion');
}
/**
* @covers \Cybercinch\UglyQueue::lock
* @uses \Cybercinch\UglyQueue
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testExceptionThrownWhenPassingNegativeIntegerValueToLock(\Cybercinch\UglyQueue $uglyQueue)
{
$this->expectException(\InvalidArgumentException::class);
$uglyQueue->lock(-73);
}
/**
* @covers \Cybercinch\UglyQueue::lock
* @covers \Cybercinch\isAlreadyLocked::isAlreadyLocked
* @covers \Cybercinch\UglyQueue::createLockFile
* @uses \Cybercinch\UglyQueue
* @depends testCanInitializeObjectWithValidParameters
* @param \Cybercinch\UglyQueue $uglyQueue
* @return \Cybercinch\UglyQueue
*/
public function testCanLockUglyQueueWithDefaultTTL(\Cybercinch\UglyQueue $uglyQueue)
{
$locked = $uglyQueue->lock();
$this->assertTrue($locked);
$this->assertFileExists($uglyQueue->getLockFile());
$decode = @json_decode(file_get_contents($uglyQueue->getLockFile()));
$this->assertTrue((json_last_error() === JSON_ERROR_NONE));
$this->assertObjectHasAttribute('ttl', $decode);
$this->assertObjectHasAttribute('born', $decode);
$this->assertEquals(250, $decode->ttl);
return $uglyQueue;
}
/**
* @covers \Cybercinch\UglyQueue::lock
* @covers \Cybercinch\isAlreadyLocked::isAlreadyLocked
* @uses \Cybercinch\UglyQueue
* @depends testCanInitializeExistingQueue
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testCannotLockQueueThatIsAlreadyLocked(\Cybercinch\UglyQueue $uglyQueue)
{
$lock = $uglyQueue->lock();
$this->assertFalse($lock);
}
/**
* @covers \Cybercinch\isAlreadyLocked::isAlreadyLocked
* @uses \Cybercinch\UglyQueue
* @depends testCanLockUglyQueueWithDefaultTTL
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testIsLockedReturnsTrueAfterLocking(\Cybercinch\UglyQueue $uglyQueue)
{
$isLocked = $uglyQueue->isAlreadyLocked();
$this->assertTrue($isLocked);
}
/**
* @covers \Cybercinch\UglyQueue::unlock
* @uses \Cybercinch\UglyQueue
* @uses \Cybercinch\Helpers\FileHelper
* @depends testCanLockUglyQueueWithDefaultTTL
* @param \Cybercinch\UglyQueue $uglyQueue
* @return \Cybercinch\UglyQueue
*/
public function testCanUnlockLockedQueue(\Cybercinch\UglyQueue $uglyQueue)
{
$uglyQueue->unlock();
$this->assertFileNotExists($uglyQueue->getLockFile());
return $uglyQueue;
}
/**
* @covers \Cybercinch\isAlreadyLocked::isAlreadyLocked
* @uses \Cybercinch\UglyQueue
* @depends testCanUnlockLockedQueue
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testIsLockedReturnsFalseAfterUnlockingQueue(\Cybercinch\UglyQueue $uglyQueue)
{
$isLocked = $uglyQueue->isAlreadyLocked();
$this->assertFalse($isLocked);
}
/**
* @covers \Cybercinch\UglyQueue::lock
* @covers \Cybercinch\isAlreadyLocked::isAlreadyLocked
* @uses \Cybercinch\UglyQueue
* @uses \Cybercinch\Helpers\FileHelper
* @depends testCanUnlockLockedQueue
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testIsLockedReturnsFalseWithStaleQueueLockFile(\Cybercinch\UglyQueue $uglyQueue)
{
$uglyQueue->lock(2);
$isLocked = $uglyQueue->isAlreadyLocked();
$this->assertTrue($isLocked);
sleep(3);
$isLocked = $uglyQueue->isAlreadyLocked();
$this->assertFalse($isLocked);
}
/**
* @covers \Cybercinch\UglyQueue::lock
* @covers \Cybercinch\isAlreadyLocked::isAlreadyLocked
* @uses \Cybercinch\UglyQueue
* @depends testCanUnlockLockedQueue
* @param \Cybercinch\UglyQueue $uglyQueue
* @return \Cybercinch\UglyQueue
*/
public function testCanLockQueueWithValidIntegerValue(\Cybercinch\UglyQueue $uglyQueue)
{
$locked = $uglyQueue->lock(200);
$this->assertTrue($locked);
$this->assertFileExists($uglyQueue->getLockFile());
$decode = @json_decode(file_get_contents($uglyQueue->getLockFile()));
$this->assertTrue((json_last_error() === JSON_ERROR_NONE));
$this->assertObjectHasAttribute('ttl', $decode);
$this->assertObjectHasAttribute('born', $decode);
$this->assertEquals(200, $decode->ttl);
return $uglyQueue;
}
/**
* @covers \Cybercinch\UglyQueue::addItem
* @uses \Cybercinch\UglyQueue
* @uses \Cybercinch\Helpers\FileHelper
* @depends testCanLockQueueWithValidIntegerValue
* @param \Cybercinch\UglyQueue $uglyQueue
* @return \Cybercinch\UglyQueue
*/
public function testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock(\Cybercinch\UglyQueue $uglyQueue)
{
foreach(array_reverse($this->tastySandwich, true) as $k=>$v)
{
$added = $uglyQueue->addItem($k, $v);
$this->assertTrue($added);
}
$this->assertFileExists(
$uglyQueue->getQueueTmpFile(),
'queue.tmp file was not created!');
$lineCount = \Dcarbone\Helpers\FileHelper::getLineCount($uglyQueue->getQueueTmpFile());
$this->assertEquals(11, $lineCount);
return $uglyQueue;
}
/**
* @covers \Cybercinch\UglyQueue::_populateQueue
* @uses \Cybercinch\UglyQueue
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
* @param \Cybercinch\UglyQueue $uglyQueue
* @return \Cybercinch\UglyQueue
*/
public function testCanForciblyUpdateQueueFileFromTempFile(\Cybercinch\UglyQueue $uglyQueue)
{
$uglyQueue->_populateQueue();
$this->assertFileNotExists($uglyQueue->getQueueTmpFile());
$uglyQueue->_populateQueue();
return $uglyQueue;
}
/**
* @covers \Cybercinch\UglyQueue::count
* @uses \Cybercinch\UglyQueue
* @uses \Cybercinch\Helpers\FileHelper
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testCanGetCountOfItemsInPopulatedQueue(\Cybercinch\UglyQueue $uglyQueue)
{
$itemCount = count($uglyQueue);
$this->assertEquals(11, $itemCount);
}
/**
* @covers \Cybercinch\UglyQueue::keyExistsInQueue
* @uses \Cybercinch\UglyQueue
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testKeyExistsReturnsTrueWithPopulatedQueue(\Cybercinch\UglyQueue $uglyQueue)
{
$exists = $uglyQueue->keyExistsInQueue(5);
$this->assertTrue($exists);
}
/**
* @covers \Cybercinch\UglyQueue::retrieveItems
* @uses \Cybercinch\UglyQueue
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testExceptionThrownWhenTryingToProcessLockedQueueWithNonInteger(\Cybercinch\UglyQueue $uglyQueue)
{
$this->expectException(\InvalidArgumentException::class);
$uglyQueue->retrieveItems('Eleventy Billion');
}
/**
* @covers \Cybercinch\UglyQueue::retrieveItems
* @uses \Cybercinch\UglyQueue
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
* @expectedException
* @param \Cybercinch\UglyQueue $uglyQueue
*/
public function testExceptionThrownWhenTryingToProcessLockedQueueWithIntegerLessThan1(\Cybercinch\UglyQueue $uglyQueue)
{
$this->expectException(\InvalidArgumentException::class);
$uglyQueue->retrieveItems(0);
}
/**
* @covers \Cybercinch\UglyQueue::retrieveItems
* @covers \Cybercinch\UglyQueue::count
* @uses \Cybercinch\UglyQueue
* @uses \Cybercinch\Helpers\FileHelper
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
* @param \Cybercinch\UglyQueue $uglyQueue
* @return \Cybercinch\UglyQueue
*/
public function testCanGetPartialQueueContents(\Cybercinch\UglyQueue $uglyQueue)
{
$process = $uglyQueue->retrieveItems(5);
$this->assertEquals(5, count($process));
$this->assertArrayHasKey('0', $process);
$this->assertArrayHasKey('4', $process);
$this->assertEquals(6, count($uglyQueue));
return $uglyQueue;
}
/**
* @covers \Cybercinch\UglyQueue::retrieveItems
* @covers \Cybercinch\UglyQueue::count
* @uses \Cybercinch\UglyQueue
* @uses \Cybercinch\Helpers\FileHelper
* @depends testCanGetPartialQueueContents
* @param \Cybercinch\UglyQueue $uglyQueue
* @return \Cybercinch\UglyQueue
*/
public function testCanGetFullQueueContents(\Cybercinch\UglyQueue $uglyQueue)
{
$process = $uglyQueue->retrieveItems(6);
$this->assertEquals(6, count($process));
$this->assertArrayHasKey('10', $process);
$this->assertArrayHasKey('5', $process);
$this->assertEquals(0, count($uglyQueue));
return $uglyQueue;
}
}