summaryrefslogtreecommitdiff
path: root/plugins/Irc/extlib/phergie/Tests/Phergie/Plugin/PingTest.php
blob: b9c2dde3d40ba5a99bedf0222c9f3f9e6224fad5 (plain)
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
<?php
/**
 * Phergie
 *
 * 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://phergie.org/license
 *
 * @category  Phergie
 * @package   Phergie
 * @author    Phergie Development Team <team@phergie.org>
 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
 * @license   http://phergie.org/license New BSD License
 * @link      http://pear.phergie.org/package/Phergie
 */

require_once(dirname(__FILE__) . '/TestCase.php');

/**
 * Unit test suite for Pherge_Plugin_Ping.
 *
 * @category Phergie
 * @package  Phergie_Tests
 * @author   Phergie Development Team <team@phergie.org>
 * @license  http://phergie.org/license New BSD License
 * @link     http://pear.phergie.org/package/Phergie
 */
class Phergie_Plugin_PingTest extends Phergie_Plugin_TestCase
{
    protected $config = array('ping.ping'  => 10,
                              'ping.event' => 300);
    
    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->setPlugin(new Phergie_Plugin_Ping);
    }

    /**
     * Test the lastEvent setter and getter
     */
    public function testSetGetLastEvent()
    {
        $expected = rand(100000,200000);
        $this->plugin->setLastEvent($expected);
        $this->assertEquals($expected,
                            $this->plugin->getLastEvent(),
                            'Assert that the last event was set and gotten ' .
                            'correctly');
    }

    /**
     * Test the lastPing setter and getter
     */
    public function testSetGetLastPing()
    {

        $expected = rand(100000,200000);
        $this->plugin->setLastPing($expected);
        $this->assertEquals($expected,
                            $this->plugin->getLastPing(),
                            'Assert that the last ping was set and gotten ' .
                            'correctly');
    }

    /**
     * Tests the onConnect hook
     */
    public function testOnConnect()
    {
        $time = time() - 1;
        // We need to make sure time() is going to be creater next time it is called
        
        $this->plugin->onConnect();
        $this->assertNull($this->plugin->getLastPing(), 
                          'onConnect should set last ping to null');
        $this->assertGreaterThan($time,
                                 $this->plugin->getLastEvent(),
                                 'onConnect should update lastEvent with the ' .
                                 'current timestamp');
        $this->assertLessThan($time + 2,
                              $this->plugin->getLastEvent(),
                              'onConnect should update lastEvent with the ' .
                              'current timestamp');
    }

    /**
     * Test that the preEvent method updates the lastEvent with the current time
     */
    public function testPreEvent()
    {
        $time = time() -1;
        $this->plugin->preEvent();
        $this->assertGreaterThan($time,
                                 $this->plugin->getLastEvent(),
                                 'Last event time was set properly on preEvent');
        $this->assertLessThan($time +2,
                              $this->plugin->getLastEvent(),
                              'Last Event time was set properly on preEvent');
    }

    /**
     * @todo Implement testOnPingResponse().
     */
    public function testOnPingResponse()
    {
        $this->plugin->setLastPing(time());
        $this->plugin->onPingResponse();
        $this->assertNull($this->plugin->getLastPing(),
                          'Last ping time should be null after onPingResponse');

    }

    /**
     * Test that the plugin issues a quit when the ping threashold
     * has been exceeded
     */
    public function testOnTickExceededPingThresholdQuits()
    {
        $this->plugin->setLastPing(1);
        $this->plugin->onTick();
        $this->assertHasEvent(Phergie_Event_Command::TYPE_QUIT);
    }
    
    /**
     * Test that the plugin issues a quit when the ping threashold
     * has been exceeded
     */
    public function testOnTickPingWithinThresholdDoesNotQuits()
    {
        $this->plugin->setLastPing(time());
        $this->plugin->onTick();
        $this->assertDoesNotHaveEvent(Phergie_Event_Command::TYPE_QUIT);
    }

    /**
     * Test that a ping is emitted when the event threashold is exceeded
     */
    public function testPingEmittedAfterThresholdExceeded()
    {
        $this->plugin->setLastEvent(time() - $this->config['ping.event'] - 1);
        $this->plugin->onTick();
        $this->assertHasEvent(Phergie_Event_Command::TYPE_PING);
        $events = $this->getResponseEvents(Phergie_Event_Command::TYPE_PING);
        foreach ($events as $event) {
            $this->assertEventEmitter($event,
                                      $this->plugin,
                    'Assert that the event was emitted by the tested plugin');
        }
    }

    /**
     * Test that no ping is emitted when the event thresthold is not exceeded
     */
    public function testNoPingEmittedWhenThresholdNotExceeded()
    {
        $this->plugin->setLastEvent(time() - $this->config['ping.event'] +1);
        $this->plugin->onTick();
        $this->assertDoesNotHaveEvent(Phergie_Event_Command::TYPE_PING);
    }

    public function tearDown()
    {
        $this->handler->clearEvents();
    }

}