diff options
author | LEDfan <tobia@ledfan.be> | 2016-01-17 13:18:41 +0300 |
---|---|---|
committer | LEDfan <tobia@ledfan.be> | 2016-01-17 13:18:41 +0300 |
commit | edca62d2fb1c4824aaaa07d56c799e8ff7f0c1ba (patch) | |
tree | e80c96bc80e83f7b9d59727543a52869c220ce4a /tests/unit | |
parent | e9c2976d2af317f25c7945ba84d74519318f72f5 (diff) |
More unit and integration tests
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/controller/HttpBindControllerTest.php | 113 |
1 files changed, 107 insertions, 6 deletions
diff --git a/tests/unit/controller/HttpBindControllerTest.php b/tests/unit/controller/HttpBindControllerTest.php index 9366da7..f6b3dc2 100644 --- a/tests/unit/controller/HttpBindControllerTest.php +++ b/tests/unit/controller/HttpBindControllerTest.php @@ -1,11 +1,14 @@ <?php namespace OCA\OJSXC\Controller; +use OCA\OJSXC\Db\Message; +use OCA\OJSXC\Db\Stanza; use OCA\OJSXC\Db\StanzaMapper; use OCA\OJSXC\Http\XMPPResponse; use OCA\OJSXC\StanzaHandlers\IQ; use OCP\AppFramework\Db\DoesNotExistException; use PHPUnit_Framework_TestCase; +use Sabre\Xml\Writer; class HttpBindControllerTest extends PHPUnit_Framework_TestCase { @@ -24,6 +27,11 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { */ private $iqHandler; + /** + * @var PHPUnit_Framework_MockObject_MockObject + */ + private $messageHandler; + private $userId = 'john'; public function setUp() { @@ -40,7 +48,7 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $this->stanzaMapper = $this->getMockBuilder('OCA\OJSXC\Db\StanzaMapper')->getMock(); $this->iqHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\IQ')->getMock(); - $messageHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\Message')->getMock(); + $this->messageHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\Message')->getMock(); $this->controller = new HttpBindController( 'ojsxc', @@ -49,7 +57,7 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $session, $this->stanzaMapper, $this->iqHandler, - $messageHandler, + $this->messageHandler, 'localhost', $requestBody, 0, @@ -61,7 +69,7 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { * When invalid XML, just start long polling. */ public function testInvalidXML() { - $ex = new DoesNotExistException(); + $ex = new DoesNotExistException(''); $expResponse = new XMPPResponse(); $this->setUpController('<x>'); @@ -78,12 +86,14 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { return [ [ '<body rid=\'897878733\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><iq from=\'admin@localhost\' to=\'localhost\' type=\'get\' xmlns=\'jabber:client\' id=\'1:sendIQ\'><query xmlns=\'http://jabber.org/protocol/disco#info\' node=\'undefined#undefined\'/></iq><iq type=\'get\' xmlns=\'jabber:client\' id=\'2:sendIQ\'><query xmlns=\'jabber:iq:roster\'/></iq><iq type=\'get\' to=\'admin@localhost\' xmlns=\'jabber:client\' id=\'3:sendIQ\'><vCard xmlns=\'vcard-temp\'/></iq></body>', - '<body xmlns="http://jabber.org/protocol/httpbind"><iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq></body>', + '<iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq>', + '<iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq><iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq><iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq>', // we ask for 3 IQ's thus return 3 values $this->once() ], [ '<body rid=\'897878734\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><iq from=\'admin@localhost\' to=\'localhost\' type=\'get\' xmlns=\'jabber:client\' id=\'1:sendIQ\'><query xmlns=\'http://jabber.org/protocol/disco#info\' node=\'undefined#undefined\'/></iq><iq type=\'get\' xmlns=\'jabber:client\' id=\'2:sendIQ\'><query xmlns=\'jabber:iq:roster\'/></iq><iq type=\'get\' to=\'admin@localhost\' xmlns=\'jabber:client\' id=\'3:sendIQ\'><vCard xmlns=\'vcard-temp\'/></iq></body>', null, + null, $this->exactly(10) ] ]; @@ -92,12 +102,12 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { /** * @dataProvider IQProvider */ - public function testIQHandler($body, $result, $pollCount) { + public function testIQHandlerWhenNoDbResults($body, $result, $expected, $pollCount) { $ex = new DoesNotExistException(); $this->setUpController($body); $expResponse = new XMPPResponse(); - $expResponse->write($result); + $expResponse->write($expected); $this->iqHandler->expects($this->any()) // FIXME ->method('handle') @@ -111,7 +121,98 @@ class HttpBindControllerTest extends PHPUnit_Framework_TestCase { $response = $this->controller->index(); $this->assertEquals($expResponse, $response); + $this->assertEquals($expResponse->render(), $response->render()); + + } + + public function testDbResults() { + $result = 'test'; + $this->setUpController('<body rid=\'897878797\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>'); + + $expResponse = new XMPPResponse(); + $expResponse->write($result); + + $this->iqHandler->expects($this->any()) // FIXME + ->method('handle') + ->will($this->returnValue($result)); + + $r1 = $this->getMockBuilder('Sabre\XML\XmlSerializable')->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@localhost') + ->will($this->returnValue([$r1])); + + + $response = $this->controller->index(); + $this->assertEquals($expResponse, $response); + $this->assertEquals($expResponse->render(), $response->render()); + } + + 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->setUpController($body); + + $expResponse = new XMPPResponse(); + + $this->messageHandler->expects($this->any()) // FIXME + ->method('handle'); + + $this->stanzaMapper->expects($this->exactly(10)) + ->method('findByTo') + ->with('john@localhost') + ->will($this->throwException($ex)); + + $response = $this->controller->index(); + $this->assertEquals($expResponse, $response); + $this->assertEquals($expResponse->render(), $response->render()); + } + + 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->setUpController($body); + + $expResponse = new XMPPResponse(); + $expResponse->write('test'); + + $this->messageHandler->expects($this->any()) // FIXME + ->method('handle'); + + $r1 = $this->getMockBuilder('Sabre\XML\XmlSerializable')->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@localhost') + ->will($this->returnValue([$r1])); + + $response = $this->controller->index(); + $this->assertEquals($expResponse, $response); + $this->assertEquals($expResponse->render(), $response->render()); + } + + 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->setUpController($body); + + $this->controller->index(); + } + + public function testBodyHandler() { + $body = '<body rid=\'897878985\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'/>'; + $this->setUpController($body); + $this->controller->index(); } }
\ No newline at end of file |