PageRenderTime 103ms CodeModel.GetById 60ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 1ms

/tests/Xmpp/ConnectionTest.php

https://github.com/donglinkang/Xmpp
PHP | 185 lines | 86 code | 21 blank | 78 comment | 0 complexity | ad9712b9570b958d95cd872c98bbed85 MD5 | raw file
  1<?php
  2/**
  3 * PHP XMPP Library
  4 *
  5 * PHP version 5
  6 *
  7 * LICENSE
  8 *
  9 * This source file is subject to the new BSD license that is bundled
 10 * with this package in the file LICENSE.
 11 * It is also available through the world-wide-web at this URL:
 12 * http://m.me.uk/xmpp/license/
 13 *
 14 * @category  Xmpp
 15 * @package   Xmpp
 16 * @author    Alex Mace <a@m.me.uk>
 17 * @copyright 2010-2011 Alex Mace (http://m.me.uk)
 18 * @license   http://m.me.uk/xmpp/license/ New BSD License
 19 * @link      http://pear.m.me.uk/package/Xmpp
 20 */
 21
 22require_once 'Stream.php';
 23require_once 'Xmpp/Connection.php';
 24
 25/**
 26 * Tests for the XMPP class. Each tests need to mock the Stream object and stub the 
 27 * _getStream method so that the XMPP class doesn't actually have to connect to 
 28 * anything.
 29 *
 30 * @category  XMPP
 31 * @package   XMPP
 32 * @author    Alex Mace <a@m.me.uk>
 33 * @copyright 2010-2011 Alex Mace (http://m.me.uk)
 34 * @license   http://m.me.uk/license New BSD License
 35 * @link      http://m.me.uk/xmpp
 36 */
 37class Xmpp_ConnectionTest extends PHPUnit_Framework_TestCase
 38{
 39
 40    /**
 41     * Sets up the fixture, for example, opens a network connection.
 42     * This method is called before a test is executed.
 43     *
 44     * @return void
 45     */
 46    public function setUp()
 47    {
 48        
 49    }
 50
 51    /**
 52     * Tears down the fixture, for example, closes a network connection.
 53     * This method is called after a test is executed.
 54     *
 55     * @return void
 56     */
 57    public function tearDown()
 58    {
 59        
 60    }
 61
 62    /**
 63     * Gets a mock instance of Stream for use in tests.
 64     * 
 65     * @return type 
 66     */
 67    public function getMockStream()
 68    {
 69        return $this->getMock('Stream', array(), array(), '', false);
 70    }
 71
 72    /**
 73     * Gets a mock instance of Xmpp_Connection for use in tests.
 74     * 
 75     * @param Stream $stream A stream to connect with.
 76     * 
 77     * @return type 
 78     */
 79    public function getMockXmppConnection(Stream $stream)
 80    {
 81        $xmpp = $this->getMock(
 82            'Xmpp_Connection', array('_getStream'), 
 83            array('test@test.server.com', 'testPass', 'test.xmpp.com')
 84        );
 85        $xmpp->expects($this->once())
 86            ->method('_getStream')
 87            ->with($this->equalTo('tcp://test.xmpp.com:5222'))
 88            ->will($this->returnValue($stream));
 89
 90        return $xmpp;
 91    }
 92
 93    /**
 94     * Test for the connect function
 95     *
 96     * @return void
 97     */
 98    public function testConnect()
 99    {
100        $stream = $this->getMockStream();
101
102        // Set up what we expect the XMPP class to send to the server
103        $message = '<stream:stream to="test.xmpp.com" '
104                . 'xmlns:stream="http://etherx.jabber.org/streams" '
105                . 'xmlns="jabber:client" version="1.0">';
106        $stream->expects($this->at(1))
107            ->method('send')
108            ->with($this->equalTo($message));
109
110        // Next we expect to tell the stream to wait for a response from the
111        // server
112        $stream->expects($this->at(2))
113            ->method('select')
114            ->will($this->returnValue(1));
115
116        // Set up what expect the reponse of the server to be to this
117        $message = "<?xml version='1.0' encoding='UTF-8'?>"
118                . '<stream:stream '
119                . 'xmlns:stream="http://etherx.jabber.org/streams" '
120                . 'xmlns="jabber:client" from="test.xmpp.com" '
121                . 'id="e674e243" xml:lang="en" version="1.0">';
122        $stream->expects($this->at(3))
123            ->method('read')
124            ->with($this->equalTo(4096))
125            ->will($this->returnValue($message));
126
127        // Next we expect to tell the stream to wait for a response from the
128        // server
129        $stream->expects($this->at(4))
130            ->method('select')
131            ->will($this->returnValue(1));
132
133        // Next the server should report what features it supports
134        $message = '<stream:features>'
135                . '<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"></starttls>'
136                . '<mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">'
137                . '<mechanism>DIGEST-MD5</mechanism>'
138                . '<mechanism>PLAIN</mechanism>'
139                . '<mechanism>ANONYMOUS</mechanism>'
140                . '<mechanism>CRAM-MD5</mechanism>'
141                . '</mechanisms>'
142                . '<compression xmlns="http://jabber.org/features/compress">'
143                . '<method>zlib</method>'
144                . '</compression>'
145                . '<auth xmlns="http://jabber.org/features/iq-auth"/>'
146                . '<register xmlns="http://jabber.org/features/iq-register"/>'
147                . '</stream:features>';
148        $stream->expects($this->at(5))
149            ->method('read')
150            ->with($this->equalTo(4096))
151            ->will($this->returnValue($message));
152
153        // Now get a mock of XMPP and replace the _getStream function with a
154        // stub that will return our stream mock.
155        $xmpp = $this->getMockXmppConnection($stream);
156        $xmpp->connect();
157    }
158
159    /**
160     * Tests disconnecting from an XMPP server.
161     * 
162     * @return void
163     */
164    public function testDisconnect()
165    {
166        $stream = $this->getMockStream();
167
168        // Setup what we expect the Xmpp_Connection class to send to the server.
169        // Disconnecting from the server should basically just be sending a
170        // closing stream tag followed by disconnection.
171        $message = '</stream:stream>';
172        $stream->expects($this->at(0))
173            ->method('send')
174            ->with($this->equalTo($message));
175
176        // Next we expect the Stream to be told to close to the connect
177        $stream->expects($this->at(1))
178            ->method('disconnect')
179            ->will($this->returnValue(true));
180
181        $xmpp = $this->getMockXmppConnection($stream);
182        $xmpp->disconnect();
183    }
184
185}