diff options
author | LEDfan <tobia@ledfan.be> | 2016-02-11 10:07:01 +0300 |
---|---|---|
committer | LEDfan <tobia@ledfan.be> | 2016-02-11 10:07:01 +0300 |
commit | 653d612e1a6d09567b58ff530caa2a61aa4d2dd0 (patch) | |
tree | ea2d76f7c1c86e35e77c6794af5cd5a7a2509537 /tests/unit | |
parent | 9dd41c3241942f5de0040daa6f86d73075f5d46b (diff) |
Add presence
- automatically go offline
- don't use memcache for now
- broadcast presence when someone changes his
- add unit and integration tests
- add newContentContainer to easily add extra stanzas to the response
- do not return the presence of other users to a user which set his
presence to unavailable
- improve README's in code
- add some caches to PresenceMapper to prevent that presence is fetched
multiple times from the DB in the same request
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/NewContentContainerTest.php | 53 | ||||
-rw-r--r-- | tests/unit/controller/HttpBindControllerTest.php | 242 | ||||
-rw-r--r-- | tests/unit/db/README.md | 4 | ||||
-rw-r--r-- | tests/unit/stanzahandlers/PresenceTest.php | 154 |
4 files changed, 444 insertions, 9 deletions
diff --git a/tests/unit/NewContentContainerTest.php b/tests/unit/NewContentContainerTest.php new file mode 100644 index 0000000..e2caf39 --- /dev/null +++ b/tests/unit/NewContentContainerTest.php @@ -0,0 +1,53 @@ +<?php + +namespace OCA\OJSXC; + +use OCA\OJSXC\Db\Message; +use OCA\OJSXC\Db\Stanza; +use PHPUnit_Framework_TestCase; + +class NewContentContainerTest extends PHPUnit_Framework_TestCase { + + /** + * @var NewContentContainer $newContentContainer + */ + private $newContentContainer; + + public function setUp() { + $this->newContentContainer = new NewContentContainer(); + } + + public function testProvider() { + $stanza1 = new Stanza(); + $stanza1->setFrom('test@own.dev'); + $stanza1->setTo('adsffdsst@own.dev'); + + $stanza2 = new Message(); + $stanza2->setFrom('test@own.dev'); + $stanza2->setTo('addsf@own.dev'); + $stanza2->setType('chat'); + $stanza2->setValue('abc'); + return [ + [ + [$stanza1, $stanza2], + 2 + ] + ]; + } + + /** + * @dataProvider testProvider + */ + public function test($stanzas, $count) { + foreach ($stanzas as $stanza) { + $this->newContentContainer->addStanza($stanza); + } + $this->assertEquals($count, $this->newContentContainer->getCount()); + + $result = $this->newContentContainer->getStanzas(); + $this->assertEquals(sort($stanzas), sort($result)); + + } + + +}
\ No newline at end of file diff --git a/tests/unit/controller/HttpBindControllerTest.php b/tests/unit/controller/HttpBindControllerTest.php index 7ae6931..ca14d10 100644 --- a/tests/unit/controller/HttpBindControllerTest.php +++ b/tests/unit/controller/HttpBindControllerTest.php @@ -2,6 +2,7 @@ namespace OCA\OJSXC\Controller; use OCA\OJSXC\Db\Message; +use OCA\OJSXC\Db\Presence; use OCA\OJSXC\Db\Stanza; use OCA\OJSXC\Db\StanzaMapper; use OCA\OJSXC\Http\XMPPResponse; @@ -39,8 +40,22 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { */ private $lock; + /** + * @var PHPUnit_Framework_MockObject_MockObject + */ + private $presenceHandler; + + /** + * @var PHPUnit_Framework_MockObject_MockObject + */ + private $presenceMapper; + private $userId = 'john'; + /** + * @var PHPUnit_Framework_MockObject_MockObject + */ + private $newContentContainer; public function setUp() { } @@ -63,10 +78,15 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { private function setUpController($requestBody) { $request = $this->getMockBuilder('OCP\IRequest')->disableOriginalConstructor()->getMock(); $this->stanzaMapper = $this->getMockBuilder('OCA\OJSXC\Db\StanzaMapper')->disableOriginalConstructor()->getMock(); + $this->presenceMapper = $this->getMockBuilder('OCA\OJSXC\Db\PresenceMapper')->disableOriginalConstructor()->getMock(); $this->iqHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\IQ')->disableOriginalConstructor()->getMock(); $this->messageHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\Message')->disableOriginalConstructor()->getMock(); + $this->presenceHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\Presence')->disableOriginalConstructor()->getMock(); $this->lock = $this->getMockBuilder('OCA\OJSXC\ILock')->disableOriginalConstructor()->getMock(); + $this->newContentContainer = $this->getMockBuilder('OCA\OJSXC\NewContentContainer')->disableOriginalConstructor()->getMock(); + + $logger = \OC::$server->getLogger(); $this->controller = new HttpBindController( 'ojsxc', @@ -77,12 +97,134 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $this->messageHandler, 'localhost', $this->lock, + $logger, + $this->presenceHandler, + $this->presenceMapper, $requestBody, 0, - 10 + 10, + $this->newContentContainer ); } + public function testNewContentContainerNoNew() { + $this->setUpController('<body xmlns=\'http://jabber.org/protocol/httpbind\'/>'); + $this->mockLock(); + $ex = new DoesNotExistException(''); + $expResponse = new XMPPResponse(); + + $this->newContentContainer->expects($this->once()) + ->method('getCount') + ->will($this->returnValue(0)); + + $this->newContentContainer->expects($this->never()) + ->method('getStanzas'); + + $this->stanzaMapper->expects($this->exactly(10)) + ->method('findByTo') + ->with('john') + ->will($this->throwException($ex)); + + $this->presenceMapper->expects($this->once()) + ->method('setActive') + ->with('john'); + + $response = $this->controller->index(); + $this->assertEquals($expResponse, $response); + + } + + public function testNewContentContainerNoNewWithDbResults() { + $result = new Stanza('test'); + $this->setUpController('<body rid=\'897878797\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>'); + $this->mockLock(); + + $expResponse = new XMPPResponse($result); + + $this->iqHandler->expects($this->never()) + ->method('handle') + ->will($this->returnValue($result)); + + $r1 = $this->getMockBuilder('OCA\OJSXC\Db\Stanza')->disableOriginalConstructor()->getMock(); + $r1->expects($this->once()) + ->method('xmlSerialize') + ->will($this->returnCallback(function(Writer $writer){ + $writer->write('test'); + })); + + $this->stanzaMapper->expects($this->once()) + ->method('findByTo') + ->with('john') + ->will($this->returnValue([$r1])); + + $this->presenceMapper->expects($this->once()) + ->method('setActive') + ->with('john'); + + $this->newContentContainer->expects($this->once()) + ->method('getCount') + ->will($this->returnValue(0)); + + $this->newContentContainer->expects($this->never()) + ->method('getStanzas'); + + $response = $this->controller->index(); + $this->assertEquals($expResponse, $response); + $this->assertEquals($expResponse->render(), $response->render()); + } + + + public function testNewContentContainerWithNewWithDbResults() { + $result = new Stanza('test'); + $this->setUpController('<body rid=\'897878797\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>'); + $this->mockLock(); + + $expResponse = new XMPPResponse($result); + + $this->iqHandler->expects($this->never()) + ->method('handle') + ->will($this->returnValue($result)); + + $r1 = $this->getMockBuilder('OCA\OJSXC\Db\Stanza')->disableOriginalConstructor()->getMock(); + $r1->expects($this->once()) + ->method('xmlSerialize') + ->will($this->returnCallback(function(Writer $writer){ + $writer->write('test'); + })); + + $this->stanzaMapper->expects($this->once()) + ->method('findByTo') + ->with('john') + ->will($this->returnValue([$r1])); + + $this->presenceMapper->expects($this->once()) + ->method('setActive') + ->with('john'); + + $this->newContentContainer->expects($this->once()) + ->method('getCount') + ->will($this->returnValue(5)); + + $testStanza = new Stanza(); + $testStanza->setFrom('derp@own.dev'); + $testStanza->setTo('admin@own.dev'); + + $this->newContentContainer->expects($this->once()) + ->method('getStanzas') + ->will($this->returnValue([$testStanza,$testStanza, $testStanza, $testStanza, $testStanza ])); + + $expResponse->write($testStanza); + $expResponse->write($testStanza); + $expResponse->write($testStanza); + $expResponse->write($testStanza); + $expResponse->write($testStanza); + + $response = $this->controller->index(); + $this->assertEquals($expResponse, $response); + $this->assertEquals($expResponse->render(), $response->render()); + } + + /** * When invalid XML, just start long polling. * Note: this test will cause some errors in the owncloud.log: @@ -100,6 +242,10 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { ->with('john') ->will($this->throwException($ex)); + $this->presenceMapper->expects($this->once()) + ->method('setActive') + ->with('john'); + $response = $this->controller->index(); $this->assertEquals($expResponse, $response); } @@ -152,6 +298,9 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { ->with('john') ->will($this->throwException($ex)); + $this->presenceMapper->expects($this->once()) + ->method('setActive') + ->with('john'); $response = $this->controller->index(); $this->assertEquals($expResponse, $response); @@ -182,6 +331,9 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { ->with('john') ->will($this->returnValue([$r1])); + $this->presenceMapper->expects($this->once()) + ->method('setActive') + ->with('john'); $response = $this->controller->index(); $this->assertEquals($expResponse, $response); @@ -204,6 +356,10 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { ->with('john') ->will($this->throwException($ex)); + $this->presenceMapper->expects($this->once()) + ->method('setActive') + ->with('john'); + $response = $this->controller->index(); $this->assertEquals($expResponse, $response); $this->assertEquals($expResponse->render(), $response->render()); @@ -263,6 +419,10 @@ XML; ->with('john') ->will($this->throwException($ex)); + $this->presenceMapper->expects($this->once()) + ->method('setActive') + ->with('john'); + $response = $this->controller->index(); $this->assertEquals($expResponse, $response); $this->assertEquals($expResponse->render(), $response->render()); @@ -290,22 +450,82 @@ XML; ->with('john') ->will($this->returnValue([$r1])); + $this->presenceMapper->expects($this->once()) + ->method('setActive') + ->with('john'); + $response = $this->controller->index(); $this->assertEquals($expResponse, $response); $this->assertEquals($expResponse->render(), $response->render()); } - /** - * @TODO implement tests - */ - public function testPresenceHandler() { - $this->markTestSkipped(); - $this->markTestIncomplete(); - $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>'; + public function testPresenceReturnNothingHandler() { + $body = "<body xmlns='http://jabber.org/protocol/httpbind'><presence xmlns='jabber:client'><show>chat</show></presence></body>"; + $ex = new DoesNotExistException(''); + $expResponse = new XMPPResponse(); + $this->setUpController($body); $this->mockLock(); - $this->controller->index(); + $this->presenceHandler->expects($this->once()) + ->method('handle') + ->will($this->returnValue(null)); + + $this->stanzaMapper->expects($this->exactly(10)) + ->method('findByTo') + ->with('john') + ->will($this->throwException($ex)); + + $this->presenceMapper->expects($this->once()) + ->method('setActive') + ->with('john'); + + $response = $this->controller->index(); + $this->assertEquals($expResponse, $response); + $this->assertEquals($expResponse->render(), $response->render()); + + } + + public function testPresenceHandler() { + $body = "<body xmlns='http://jabber.org/protocol/httpbind'><presence xmlns='jabber:client'><show>chat</show></presence></body>"; + $ex = new DoesNotExistException(''); + $expResponse = new XMPPResponse(); + + $pres1 = new Presence(); + $pres1->setPresence('online'); + $pres1->setUserid('admin'); + $pres1->setTo('admin@localhost'); + $pres1->setFrom('derp@localhot'); + + $pres2 = new Presence(); + $pres2->setPresence('unavailable'); + $pres2->setUserid('herp'); + $pres2->setTo('admin@localhost'); + $pres2->setFrom('herp@localhot'); + + $expResponse->write($pres1); + $expResponse->write($pres2); + + $this->setUpController($body); + $this->mockLock(); + + $this->presenceHandler->expects($this->once()) + ->method('handle') + ->will($this->returnValue([$pres1, $pres2])); + + $this->stanzaMapper->expects($this->never()) + ->method('findByTo') + ->with('john') + ->will($this->throwException($ex)); + + $this->presenceMapper->expects($this->once()) + ->method('setActive') + ->with('john'); + + $response = $this->controller->index(); + $this->assertEquals($expResponse, $response); + $this->assertEquals($expResponse->render(), $response->render()); + } public function testBodyHandler() { @@ -320,6 +540,10 @@ XML; ->with('john') ->will($this->throwException($ex)); + $this->presenceMapper->expects($this->once()) + ->method('setActive') + ->with('john'); + $response = $this->controller->index(); $this->assertEquals($expResponse, $response); $this->assertEquals($expResponse->render(), $response->render()); diff --git a/tests/unit/db/README.md b/tests/unit/db/README.md new file mode 100644 index 0000000..35902a8 --- /dev/null +++ b/tests/unit/db/README.md @@ -0,0 +1,4 @@ +tests/unit/db +=== +Note that in this directory only Entity can be tests since this are unit tests. +Mapper tests should go into the integraion tests directory.
\ No newline at end of file diff --git a/tests/unit/stanzahandlers/PresenceTest.php b/tests/unit/stanzahandlers/PresenceTest.php new file mode 100644 index 0000000..1c4e6aa --- /dev/null +++ b/tests/unit/stanzahandlers/PresenceTest.php @@ -0,0 +1,154 @@ +<?php + +namespace OCA\OJSXC\StanzaHandlers; + +use OCA\OJSXC\StanzaHandlers\Presence; +use OCA\OJSXC\Db\Presence as PresenceEntity; +use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_TestCase; + + +class PresenceTest extends PHPUnit_Framework_TestCase { + + private $host; + + private $userId; + + /** + * @var PHPUnit_Framework_MockObject_MockObject $presenceMapper + */ + private $presenceMapper; + + /** + * @var PHPUnit_Framework_MockObject_MockObject $presenceMapper + */ + private $messageMapper; + + /** + * @var Presence + */ + private $presence; + + public function setUp() { + $this->host = 'localhost'; + $this->userId = 'john'; + $this->presenceMapper = $this->getMockBuilder('OCA\OJSXC\Db\PresenceMapper')->disableOriginalConstructor()->getMock(); + $this->messageMapper = $this->getMockBuilder('OCA\OJSXC\Db\MessageMapper')->disableOriginalConstructor()->getMock(); + + $this->presence = new Presence($this->userId, $this->host + , $this->presenceMapper, $this->messageMapper); + } + + public function handleProvider() { + $presence = new PresenceEntity(); + $presence->setPresence('online'); + $presence->setUserid('john'); + $presence->setLastActive(time()); + + // broadcast presence + $insert1 = new PresenceEntity(); + $insert1->setPresence('online'); + $insert1->setFrom('john'); + $insert1->setTo('derp'); + + $insert2 = new PresenceEntity(); + $insert2->setPresence('online'); + $insert2->setFrom('john'); + $insert2->setTo('herp'); + return [ + [ + $presence, + ['derp', 'herp'], + 'testValue', + [$insert1, $insert2] + ] + ]; + } + + /** + * @dataProvider handleProvider + */ + public function testHandle($presenceEntity, $connectedUsers, $presences, $insert) { + + $this->presenceMapper->expects($this->once()) + ->method('setPresence') + ->with($presenceEntity); + + + $this->presenceMapper->expects($this->once()) + ->method('getConnectedUsers') + ->will($this->returnValue($connectedUsers)); + + $this->messageMapper->expects($this->exactly(2)) + ->method('insert') + ->withConsecutive( + $this->equalTo($insert[0]), + $this->equalTo($insert[1]) + ); + + + $this->presenceMapper->expects($this->once()) + ->method('getPresences') + ->will($this->returnValue($presences)); + + + $result = $this->presence->handle($presenceEntity); + $this->assertEquals($presences, $result); + } + + + public function unavailableHandleProvider() { + $presence = new PresenceEntity(); + $presence->setPresence('unavailable'); + $presence->setUserid('john'); + $presence->setLastActive(time()); + + // broadcast presence + $insert1 = new PresenceEntity(); + $insert1->setPresence('online'); + $insert1->setFrom('john'); + $insert1->setTo('derp'); + + $insert2 = new PresenceEntity(); + $insert2->setPresence('online'); + $insert2->setFrom('john'); + $insert2->setTo('herp'); + + return [ + [ + $presence, + ['derp', 'herp'], + [], + [$insert1, $insert2] + ] + ]; + } + + /** + * @dataProvider UnavailableHandleProvider + */ + public function testUnavailableHandle($presenceEntity, $connectedUsers, $presences, $insert) { + + $this->presenceMapper->expects($this->once()) + ->method('setPresence') + ->with($presenceEntity); + + + $this->presenceMapper->expects($this->once()) + ->method('getConnectedUsers') + ->will($this->returnValue($connectedUsers)); + + $this->messageMapper->expects($this->exactly(2)) + ->method('insert') + ->withConsecutive( + $this->equalTo($insert[0]), + $this->equalTo($insert[1]) + ); + + $this->presenceMapper->expects($this->never()) + ->method('getPresences'); + + $result = $this->presence->handle($presenceEntity); + $this->assertEquals($presences, $result); + } +}
\ No newline at end of file |