Xmpp /tests/Xmpp/ConnectionTest.php

Language PHP Lines 186
MD5 Hash ad9712b9570b958d95cd872c98bbed85
Repository https://github.com/donglinkang/Xmpp.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
 * PHP XMPP Library
 *
 * PHP version 5
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.
 * It is also available through the world-wide-web at this URL:
 * http://m.me.uk/xmpp/license/
 *
 * @category  Xmpp
 * @package   Xmpp
 * @author    Alex Mace <a@m.me.uk>
 * @copyright 2010-2011 Alex Mace (http://m.me.uk)
 * @license   http://m.me.uk/xmpp/license/ New BSD License
 * @link      http://pear.m.me.uk/package/Xmpp
 */

require_once 'Stream.php';
require_once 'Xmpp/Connection.php';

/**
 * Tests for the XMPP class. Each tests need to mock the Stream object and stub the 
 * _getStream method so that the XMPP class doesn't actually have to connect to 
 * anything.
 *
 * @category  XMPP
 * @package   XMPP
 * @author    Alex Mace <a@m.me.uk>
 * @copyright 2010-2011 Alex Mace (http://m.me.uk)
 * @license   http://m.me.uk/license New BSD License
 * @link      http://m.me.uk/xmpp
 */
class Xmpp_ConnectionTest extends PHPUnit_Framework_TestCase
{

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     *
     * @return void
     */
    public function setUp()
    {
        
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     *
     * @return void
     */
    public function tearDown()
    {
        
    }

    /**
     * Gets a mock instance of Stream for use in tests.
     * 
     * @return type 
     */
    public function getMockStream()
    {
        return $this->getMock('Stream', array(), array(), '', false);
    }

    /**
     * Gets a mock instance of Xmpp_Connection for use in tests.
     * 
     * @param Stream $stream A stream to connect with.
     * 
     * @return type 
     */
    public function getMockXmppConnection(Stream $stream)
    {
        $xmpp = $this->getMock(
            'Xmpp_Connection', array('_getStream'), 
            array('test@test.server.com', 'testPass', 'test.xmpp.com')
        );
        $xmpp->expects($this->once())
            ->method('_getStream')
            ->with($this->equalTo('tcp://test.xmpp.com:5222'))
            ->will($this->returnValue($stream));

        return $xmpp;
    }

    /**
     * Test for the connect function
     *
     * @return void
     */
    public function testConnect()
    {
        $stream = $this->getMockStream();

        // Set up what we expect the XMPP class to send to the server
        $message = '<stream:stream to="test.xmpp.com" '
                . 'xmlns:stream="http://etherx.jabber.org/streams" '
                . 'xmlns="jabber:client" version="1.0">';
        $stream->expects($this->at(1))
            ->method('send')
            ->with($this->equalTo($message));

        // Next we expect to tell the stream to wait for a response from the
        // server
        $stream->expects($this->at(2))
            ->method('select')
            ->will($this->returnValue(1));

        // Set up what expect the reponse of the server to be to this
        $message = "<?xml version='1.0' encoding='UTF-8'?>"
                . '<stream:stream '
                . 'xmlns:stream="http://etherx.jabber.org/streams" '
                . 'xmlns="jabber:client" from="test.xmpp.com" '
                . 'id="e674e243" xml:lang="en" version="1.0">';
        $stream->expects($this->at(3))
            ->method('read')
            ->with($this->equalTo(4096))
            ->will($this->returnValue($message));

        // Next we expect to tell the stream to wait for a response from the
        // server
        $stream->expects($this->at(4))
            ->method('select')
            ->will($this->returnValue(1));

        // Next the server should report what features it supports
        $message = '<stream:features>'
                . '<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"></starttls>'
                . '<mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">'
                . '<mechanism>DIGEST-MD5</mechanism>'
                . '<mechanism>PLAIN</mechanism>'
                . '<mechanism>ANONYMOUS</mechanism>'
                . '<mechanism>CRAM-MD5</mechanism>'
                . '</mechanisms>'
                . '<compression xmlns="http://jabber.org/features/compress">'
                . '<method>zlib</method>'
                . '</compression>'
                . '<auth xmlns="http://jabber.org/features/iq-auth"/>'
                . '<register xmlns="http://jabber.org/features/iq-register"/>'
                . '</stream:features>';
        $stream->expects($this->at(5))
            ->method('read')
            ->with($this->equalTo(4096))
            ->will($this->returnValue($message));

        // Now get a mock of XMPP and replace the _getStream function with a
        // stub that will return our stream mock.
        $xmpp = $this->getMockXmppConnection($stream);
        $xmpp->connect();
    }

    /**
     * Tests disconnecting from an XMPP server.
     * 
     * @return void
     */
    public function testDisconnect()
    {
        $stream = $this->getMockStream();

        // Setup what we expect the Xmpp_Connection class to send to the server.
        // Disconnecting from the server should basically just be sending a
        // closing stream tag followed by disconnection.
        $message = '</stream:stream>';
        $stream->expects($this->at(0))
            ->method('send')
            ->with($this->equalTo($message));

        // Next we expect the Stream to be told to close to the connect
        $stream->expects($this->at(1))
            ->method('disconnect')
            ->will($this->returnValue(true));

        $xmpp = $this->getMockXmppConnection($stream);
        $xmpp->disconnect();
    }

}
Back to Top