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
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
22 require_once(dirname(__FILE__) . '/TestCase.php');
25 * Unit test suite for Pherge_Plugin_Ping.
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
33 class Phergie_Plugin_PingTest extends Phergie_Plugin_TestCase
35 protected $config = array('ping.ping' => 10,
39 * Sets up the fixture, for example, opens a network connection.
40 * This method is called before a test is executed.
42 protected function setUp()
44 $this->setPlugin(new Phergie_Plugin_Ping);
48 * Test the lastEvent setter and getter
50 public function testSetGetLastEvent()
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 ' .
61 * Test the lastPing setter and getter
63 public function testSetGetLastPing()
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 ' .
75 * Tests the onConnect hook
77 public function testOnConnect()
80 // We need to make sure time() is going to be creater next time it is called
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 ' .
89 $this->assertLessThan($time + 2,
90 $this->plugin->getLastEvent(),
91 'onConnect should update lastEvent with the ' .
96 * Test that the preEvent method updates the lastEvent with the current time
98 public function testPreEvent()
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');
111 * @todo Implement testOnPingResponse().
113 public function testOnPingResponse()
115 $this->plugin->setLastPing(time());
116 $this->plugin->onPingResponse();
117 $this->assertNull($this->plugin->getLastPing(),
118 'Last ping time should be null after onPingResponse');
123 * Test that the plugin issues a quit when the ping threashold
126 public function testOnTickExceededPingThresholdQuits()
128 $this->plugin->setLastPing(1);
129 $this->plugin->onTick();
130 $this->assertHasEvent(Phergie_Event_Command::TYPE_QUIT);
134 * Test that the plugin issues a quit when the ping threashold
137 public function testOnTickPingWithinThresholdDoesNotQuits()
139 $this->plugin->setLastPing(time());
140 $this->plugin->onTick();
141 $this->assertDoesNotHaveEvent(Phergie_Event_Command::TYPE_QUIT);
145 * Test that a ping is emitted when the event threashold is exceeded
147 public function testPingEmittedAfterThresholdExceeded()
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,
156 'Assert that the event was emitted by the tested plugin');
161 * Test that no ping is emitted when the event thresthold is not exceeded
163 public function testNoPingEmittedWhenThresholdNotExceeded()
165 $this->plugin->setLastEvent(time() - $this->config['ping.event'] +1);
166 $this->plugin->onTick();
167 $this->assertDoesNotHaveEvent(Phergie_Event_Command::TYPE_PING);
170 public function tearDown()
172 $this->handler->clearEvents();