diff options
author | LEDfan <tobia@ledfan.be> | 2016-01-20 12:08:47 +0300 |
---|---|---|
committer | LEDfan <tobia@ledfan.be> | 2016-01-20 12:08:47 +0300 |
commit | 5fc9eb463f29351ab448a51978b6e06c5e7528e5 (patch) | |
tree | 5c44dcd334d6be4f95102b7dddc444fdee3691be | |
parent | 53ea997ee3180e1e3c9f03a889334c7fee6e665c (diff) |
Refactor httpbindcontrollertest + add tests for dblock
-rw-r--r-- | lib/controller/httpbindcontroller.php | 2 | ||||
-rw-r--r-- | phpunit.integration.xml | 2 | ||||
-rw-r--r-- | tests/integration/DbLockTest.php | 97 | ||||
-rw-r--r-- | tests/unit/controller/HttpBindControllerTest.php | 45 |
4 files changed, 128 insertions, 18 deletions
diff --git a/lib/controller/httpbindcontroller.php b/lib/controller/httpbindcontroller.php index b0f8838..e75b568 100644 --- a/lib/controller/httpbindcontroller.php +++ b/lib/controller/httpbindcontroller.php @@ -172,4 +172,4 @@ class HttpBindController extends Controller { return self::PRESENCE; } } -}
\ No newline at end of file +} diff --git a/phpunit.integration.xml b/phpunit.integration.xml index ada7e30..bd6a3e5 100644 --- a/phpunit.integration.xml +++ b/phpunit.integration.xml @@ -7,6 +7,8 @@ <filter> <whitelist processUncoveredFilesFromWhitelist="true"> <directory suffix=".php">lib/db</directory> + <file>lib/dblock.php</file> + <file>lib/memlock.php</file> </whitelist> </filter> </phpunit>
\ No newline at end of file diff --git a/tests/integration/DbLockTest.php b/tests/integration/DbLockTest.php new file mode 100644 index 0000000..61ce3ca --- /dev/null +++ b/tests/integration/DbLockTest.php @@ -0,0 +1,97 @@ +<?php +namespace OCA\OJSXC; + +use OCP\AppFramework\Db\DoesNotExistException; +use Test\TestCase; +use OCA\OJSXC\AppInfo\Application; +use OCA\OJSXC\DbLock; + +function time() { + return DbLockTest::$time; +} + +/** + * @group DB + */ +class DbLockTest extends TestCase { + + /** + * @var \OCA\OJSXC\DbLock + */ + private $dbLock; + + /** + * @var \OCA\OJSXC\DbLock + */ + private $dbLock2; + + /** + * @var \OCP\IDb + */ + private $con; + + public static $time; + + public function setUp() { + parent::setUp(); + $app = new Application(); + $this->container = $app->getContainer(); + $this->con = $this->container->getServer()->getDb(); + $this->con->executeQuery("DELETE FROM `*PREFIX*preferences` WHERE `appid`='ojsxc' AND `configkey`='longpolling'"); + } + + /** + * Tests the setLock and stillLocked function by setting up and lock + * and then setting a new lock. + */ + public function testLock() { + self::$time = 4; + $this->dbLock = new DbLock( + 'john', + $this->container->getServer()->getDb(), + $this->container->getServer()->getConfig() + ); + $this->dbLock->setLock(); + $result = $this->fetchLocks(); + $this->assertCount(1, $result); + $this->assertEquals($result[0]['userid'], 'john'); + $this->assertEquals($result[0]['appid'], 'ojsxc'); + $this->assertEquals($result[0]['configkey'], 'longpolling'); + $this->assertEquals($result[0]['configvalue'], '4'); + $this->assertTrue($this->dbLock->stillLocked()); + + + self::$time = 5; + $this->dbLock2 = new DbLock( + 'john', + $this->container->getServer()->getDb(), + $this->container->getServer()->getConfig() + ); // simulate new lock/request + $this->dbLock2->setLock(); + + $this->assertFalse($this->dbLock->stillLocked()); + $this->assertTrue($this->dbLock2->stillLocked()); + $result = $this->fetchLocks(); + $this->assertCount(1, $result); + $this->assertEquals($result[0]['userid'], 'john'); + $this->assertEquals($result[0]['appid'], 'ojsxc'); + $this->assertEquals($result[0]['configkey'], 'longpolling'); + $this->assertEquals($result[0]['configvalue'], '5'); + $this->assertTrue($this->dbLock2->stillLocked()); + + } + + private function fetchLocks() { + $stmt = $this->con->executeQuery("SELECT * FROM `*PREFIX*preferences` WHERE `appid`='ojsxc' AND `configkey`='longpolling'"); + + $reuslt = []; + + while($row = $stmt->fetch()){ + $result[] = $row; + } + + + return $result; + } + +}
\ No newline at end of file diff --git a/tests/unit/controller/HttpBindControllerTest.php b/tests/unit/controller/HttpBindControllerTest.php index c06a42f..fa9ec29 100644 --- a/tests/unit/controller/HttpBindControllerTest.php +++ b/tests/unit/controller/HttpBindControllerTest.php @@ -43,12 +43,22 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { public function setUp() { } + private function mockLock() { + $this->lock->expects($this->any()) + ->method('setLock') + ->will($this->returnValue(null)); + + $this->lock->expects($this->any()) + ->method('stillLocked') + ->will($this->returnValue(true)); + } + /** * Helper function to set up the controller. This can't be done in the setUp, * since the requestBody is different for every test. * @param $requestBody */ - private function setUpControllerWithoutLock($requestBody) { + private function setUpController($requestBody) { $request = $this->getMockBuilder('OCP\IRequest')->disableOriginalConstructor()->getMock(); $session = $this->getMockBuilder('OCP\ISession')->disableOriginalConstructor()->getMock(); $this->stanzaMapper = $this->getMockBuilder('OCA\OJSXC\Db\StanzaMapper')->disableOriginalConstructor()->getMock(); @@ -56,13 +66,6 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $this->iqHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\IQ')->disableOriginalConstructor()->getMock(); $this->messageHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\Message')->disableOriginalConstructor()->getMock(); $this->lock = $this->getMockBuilder('OCA\OJSXC\ILock')->disableOriginalConstructor()->getMock(); - $this->lock->expects($this->any()) - ->method('setLock') - ->will($this->returnValue(null)); - - $this->lock->expects($this->any()) - ->method('stillLocked') - ->will($this->returnValue(true)); $this->controller = new HttpBindController( 'ojsxc', @@ -87,7 +90,8 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $ex = new DoesNotExistException(''); $expResponse = new XMPPResponse(); - $this->setUpControllerWithoutLock('<x>'); + $this->setUpController('<x>'); + $this->mockLock(); $this->stanzaMapper->expects($this->exactly(10)) ->method('findByTo') ->with('john@localhost') @@ -119,8 +123,8 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { */ public function testIQHandlerWhenNoDbResults($body, $result, $expected, $pollCount) { $ex = new DoesNotExistException(); - $this->setUpControllerWithoutLock($body); - + $this->setUpController($body); + $this->mockLock(); $expResponse = new XMPPResponse(); $expResponse->write($expected); @@ -142,7 +146,8 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { public function testDbResults() { $result = 'test'; - $this->setUpControllerWithoutLock('<body rid=\'897878797\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>'); + $this->setUpController('<body rid=\'897878797\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>'); + $this->mockLock(); $expResponse = new XMPPResponse(); $expResponse->write($result); @@ -172,7 +177,8 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { public function testMessageNoDbHandler() { $body = '<body rid=\'897878959\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><message to=\'derp@own.dev\' type=\'chat\' id=\'1452960296859-msg\' xmlns=\'jabber:client\'><body>abc</body><request xmlns=\'urn:xmpp:receipts\'/></message></body>'; $ex = new DoesNotExistException(); - $this->setUpControllerWithoutLock($body); + $this->setUpController($body); + $this->mockLock(); $expResponse = new XMPPResponse(); @@ -191,7 +197,8 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { public function testMessageDbHandler() { $body = '<body rid=\'897878959\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><message to=\'derp@own.dev\' type=\'chat\' id=\'1452960296859-msg\' xmlns=\'jabber:client\'><body>abc</body><request xmlns=\'urn:xmpp:receipts\'/></message></body>'; - $this->setUpControllerWithoutLock($body); + $this->setUpController($body); + $this->mockLock(); $expResponse = new XMPPResponse(); $expResponse->write('test'); @@ -218,16 +225,20 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { public function testPresenceHandler() { $body = '<body rid=\'897878985\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><presence xmlns=\'jabber:client\'><c xmlns=\'http://jabber.org/protocol/caps\' hash=\'sha-1\' node=\'http://jsxc.org/\' ver=\'u2kAg/CbVmVZhsu+lZrkuLLdO+0=\'/><show>chat</show></presence></body>'; - $this->setUpControllerWithoutLock($body); + $this->setUpController($body); + $this->mockLock(); $this->controller->index(); } public function testBodyHandler() { $body = '<body rid=\'897878985\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>'; - $this->setUpControllerWithoutLock($body); + $this->setUpController($body); + $this->mockLock(); $this->controller->index(); } + + -}
\ No newline at end of file +} |