]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Tests/Phergie/Plugin/PingTest.php
b9c2dde3d40ba5a99bedf0222c9f3f9e6224fad5
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Tests / Phergie / Plugin / PingTest.php
1 <?php
2 /**
3  * Phergie
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://phergie.org/license
13  *
14  * @category  Phergie
15  * @package   Phergie
16  * @author    Phergie Development Team <team@phergie.org>
17  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18  * @license   http://phergie.org/license New BSD License
19  * @link      http://pear.phergie.org/package/Phergie
20  */
21
22 require_once(dirname(__FILE__) . '/TestCase.php');
23
24 /**
25  * Unit test suite for Pherge_Plugin_Ping.
26  *
27  * @category Phergie
28  * @package  Phergie_Tests
29  * @author   Phergie Development Team <team@phergie.org>
30  * @license  http://phergie.org/license New BSD License
31  * @link     http://pear.phergie.org/package/Phergie
32  */
33 class Phergie_Plugin_PingTest extends Phergie_Plugin_TestCase
34 {
35     protected $config = array('ping.ping'  => 10,
36                               'ping.event' => 300);
37     
38     /**
39      * Sets up the fixture, for example, opens a network connection.
40      * This method is called before a test is executed.
41      */
42     protected function setUp()
43     {
44         $this->setPlugin(new Phergie_Plugin_Ping);
45     }
46
47     /**
48      * Test the lastEvent setter and getter
49      */
50     public function testSetGetLastEvent()
51     {
52         $expected = rand(100000,200000);
53         $this->plugin->setLastEvent($expected);
54         $this->assertEquals($expected,
55                             $this->plugin->getLastEvent(),
56                             'Assert that the last event was set and gotten ' .
57                             'correctly');
58     }
59
60     /**
61      * Test the lastPing setter and getter
62      */
63     public function testSetGetLastPing()
64     {
65
66         $expected = rand(100000,200000);
67         $this->plugin->setLastPing($expected);
68         $this->assertEquals($expected,
69                             $this->plugin->getLastPing(),
70                             'Assert that the last ping was set and gotten ' .
71                             'correctly');
72     }
73
74     /**
75      * Tests the onConnect hook
76      */
77     public function testOnConnect()
78     {
79         $time = time() - 1;
80         // We need to make sure time() is going to be creater next time it is called
81         
82         $this->plugin->onConnect();
83         $this->assertNull($this->plugin->getLastPing(), 
84                           'onConnect should set last ping to null');
85         $this->assertGreaterThan($time,
86                                  $this->plugin->getLastEvent(),
87                                  'onConnect should update lastEvent with the ' .
88                                  'current timestamp');
89         $this->assertLessThan($time + 2,
90                               $this->plugin->getLastEvent(),
91                               'onConnect should update lastEvent with the ' .
92                               'current timestamp');
93     }
94
95     /**
96      * Test that the preEvent method updates the lastEvent with the current time
97      */
98     public function testPreEvent()
99     {
100         $time = time() -1;
101         $this->plugin->preEvent();
102         $this->assertGreaterThan($time,
103                                  $this->plugin->getLastEvent(),
104                                  'Last event time was set properly on preEvent');
105         $this->assertLessThan($time +2,
106                               $this->plugin->getLastEvent(),
107                               'Last Event time was set properly on preEvent');
108     }
109
110     /**
111      * @todo Implement testOnPingResponse().
112      */
113     public function testOnPingResponse()
114     {
115         $this->plugin->setLastPing(time());
116         $this->plugin->onPingResponse();
117         $this->assertNull($this->plugin->getLastPing(),
118                           'Last ping time should be null after onPingResponse');
119
120     }
121
122     /**
123      * Test that the plugin issues a quit when the ping threashold
124      * has been exceeded
125      */
126     public function testOnTickExceededPingThresholdQuits()
127     {
128         $this->plugin->setLastPing(1);
129         $this->plugin->onTick();
130         $this->assertHasEvent(Phergie_Event_Command::TYPE_QUIT);
131     }
132     
133     /**
134      * Test that the plugin issues a quit when the ping threashold
135      * has been exceeded
136      */
137     public function testOnTickPingWithinThresholdDoesNotQuits()
138     {
139         $this->plugin->setLastPing(time());
140         $this->plugin->onTick();
141         $this->assertDoesNotHaveEvent(Phergie_Event_Command::TYPE_QUIT);
142     }
143
144     /**
145      * Test that a ping is emitted when the event threashold is exceeded
146      */
147     public function testPingEmittedAfterThresholdExceeded()
148     {
149         $this->plugin->setLastEvent(time() - $this->config['ping.event'] - 1);
150         $this->plugin->onTick();
151         $this->assertHasEvent(Phergie_Event_Command::TYPE_PING);
152         $events = $this->getResponseEvents(Phergie_Event_Command::TYPE_PING);
153         foreach ($events as $event) {
154             $this->assertEventEmitter($event,
155                                       $this->plugin,
156                     'Assert that the event was emitted by the tested plugin');
157         }
158     }
159
160     /**
161      * Test that no ping is emitted when the event thresthold is not exceeded
162      */
163     public function testNoPingEmittedWhenThresholdNotExceeded()
164     {
165         $this->plugin->setLastEvent(time() - $this->config['ping.event'] +1);
166         $this->plugin->onTick();
167         $this->assertDoesNotHaveEvent(Phergie_Event_Command::TYPE_PING);
168     }
169
170     public function tearDown()
171     {
172         $this->handler->clearEvents();
173     }
174
175 }