PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Phergie/Event/RequestTest.php

https://github.com/markizano/phergie
PHP | 582 lines | 281 code | 34 blank | 267 comment | 12 complexity | b828ca7620dcaa323a1554fb344f4eae MD5 | raw file
Possible License(s): BSD-3-Clause
  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_Tests
  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_Tests
  20. */
  21. /**
  22. * Unit test suite for Phergie_Event_Request.
  23. *
  24. * @category Phergie
  25. * @package Phergie_Tests
  26. * @author Phergie Development Team <team@phergie.org>
  27. * @license http://phergie.org/license New BSD License
  28. * @link http://pear.phergie.org/package/Phergie_Tests
  29. */
  30. class Phergie_Event_RequestTest extends PHPUnit_Framework_TestCase
  31. {
  32. /**
  33. * Instance of the class to test
  34. *
  35. * @var Phergie_Event_Request
  36. */
  37. private $event;
  38. /**
  39. * Instantiates the class to test.
  40. *
  41. * @return void
  42. */
  43. public function setUp()
  44. {
  45. $this->event = new Phergie_Event_Request;
  46. }
  47. /**
  48. * Tests that an attempt to retrieve the event hostmask when none has
  49. * been set results in an exception.
  50. *
  51. * @return void
  52. */
  53. public function testGetHostmask()
  54. {
  55. try {
  56. $this->event->getHostmask();
  57. $this->fail('Expected exception not thrown');
  58. } catch (Phergie_Event_Exception $e) {
  59. if ($e->getCode() != Phergie_Event_Exception::ERR_MISSING_HOSTMASK) {
  60. $this->fail('Unexpected exception code ' . $e->getCode());
  61. }
  62. }
  63. }
  64. /**
  65. * Tests that the hostmask associated with the event can be changed.
  66. *
  67. * @return void
  68. */
  69. public function testSetHostmask()
  70. {
  71. $hostmask = $this->getMock(
  72. 'Phergie_Hostmask',
  73. array(),
  74. array('nick', 'username', 'host')
  75. );
  76. $this->event->setHostmask($hostmask);
  77. $this->assertSame($hostmask, $this->event->getHostmask());
  78. }
  79. /**
  80. * Tests that the event contains no arguments by default.
  81. *
  82. * @return void
  83. */
  84. public function testGetArguments()
  85. {
  86. $expected = array();
  87. $actual = $this->event->getArguments();
  88. $this->assertSame($expected, $actual);
  89. }
  90. /**
  91. * Tests that event arguments cannot be retrieved before an event type
  92. * is set.
  93. *
  94. * @return void
  95. */
  96. public function testGetArgumentWithoutEventType()
  97. {
  98. try {
  99. $this->event->getArgument('text');
  100. $this->fail('Expected exception not thrown');
  101. } catch (Phergie_Event_Exception $e) {
  102. if ($e->getCode() != Phergie_Event_Exception::ERR_INVALID_ARGUMENT) {
  103. $this->fail('Unexpected exception code ' . $e->getCode());
  104. }
  105. }
  106. }
  107. /**
  108. * Tests that an event argument cannot be retrieved if it does not
  109. * correspond to the specified event type.
  110. *
  111. * @return void
  112. */
  113. public function testGetArgumentWithInvalidArgument()
  114. {
  115. $this->event->setType('privmsg');
  116. try {
  117. $this->event->getArgument('message');
  118. $this->fail('Expected exception not thrown');
  119. } catch (Phergie_Event_Exception $e) {
  120. if ($e->getCode() != Phergie_Event_Exception::ERR_INVALID_ARGUMENT) {
  121. $this->fail('Unexpected exception code ' . $e->getCode());
  122. }
  123. }
  124. }
  125. /**
  126. * Tests that a default value is returned if an event argument has not
  127. * been assigned one.
  128. *
  129. * @return void
  130. */
  131. public function testGetArgumentWithUnsetArgument()
  132. {
  133. $this->event->setType('privmsg');
  134. $this->assertNull($this->event->getArgument('text'));
  135. }
  136. /**
  137. * Data provider for testSetArgumentWithValidArgument().
  138. *
  139. * @return array Enumerated array of enumerated arrays each containing
  140. * parameter values for a single call to
  141. * testSetArgumentWithValidArgument()
  142. */
  143. public function dataProviderTestSetArgumentWithValidArgument()
  144. {
  145. return array(
  146. array(0, 0, '#channel'),
  147. array('receiver', 0, '#channel'),
  148. array(1, 1, '#channel'),
  149. array('text', 1, '#channel'),
  150. );
  151. }
  152. /**
  153. * Tests that a single valid event argument can be changed.
  154. *
  155. * @param mixed $argument Positional integer or string identifying the
  156. * argument to set
  157. * @param int $index Positional integer corresponding to $argument
  158. * @param string $value Argument value to assign
  159. *
  160. * @return void
  161. * @dataProvider dataProviderTestSetArgumentWithValidArgument
  162. */
  163. public function testSetArgumentWithValidArgument($argument,
  164. $index, $value
  165. ) {
  166. $this->event->setType('privmsg');
  167. $this->event->setArgument($argument, $value);
  168. $this->assertSame($value, $this->event->getArgument($argument));
  169. }
  170. /**
  171. * Tests that an event argument cannot be set without first setting a
  172. * valid event type.
  173. *
  174. * @return void
  175. */
  176. public function testSetArgumentWithoutEventType()
  177. {
  178. try {
  179. $this->event->setArgument('receiver', '#channel');
  180. $this->fail('Expected exception not thrown');
  181. } catch (Phergie_Event_Exception $e) {
  182. if ($e->getCode() != Phergie_Event_Exception::ERR_INVALID_ARGUMENT) {
  183. $this->fail('Unexpected exception code ' . $e->getCode());
  184. }
  185. }
  186. }
  187. /**
  188. * Tests that an event argument cannot be set if it does not correspond
  189. * to the specified event type.
  190. *
  191. * @return void
  192. */
  193. public function testSetArgumentWithInvalidArgument()
  194. {
  195. $this->event->setType('privmsg');
  196. try {
  197. $this->event->setArgument('message', 'foo');
  198. $this->fail('Expected exception not thrown');
  199. } catch (Phergie_Event_Exception $e) {
  200. if ($e->getCode() != Phergie_Event_Exception::ERR_INVALID_ARGUMENT) {
  201. $this->fail('Unexpected exception code ' . $e->getCode());
  202. }
  203. }
  204. }
  205. /**
  206. * Tests that the event arguments can be changed.
  207. *
  208. * @return void
  209. * @depends testSetArgumentWithValidArgument
  210. */
  211. public function testSetArguments()
  212. {
  213. $this->event->setType('privmsg');
  214. $expected = array('#channel', 'text');
  215. $this->event->setArguments($expected);
  216. $actual = $this->event->getArguments();
  217. $this->assertSame($expected, $actual);
  218. }
  219. /**
  220. * Tests that event arguments can be removed.
  221. *
  222. * @return void
  223. * @depends testSetArgumentWithValidArgument
  224. */
  225. public function testRemoveArgument()
  226. {
  227. $this->event->setType('privmsg');
  228. $this->event->setArgument('receiver', '#channel');
  229. $this->event->removeArgument('receiver');
  230. $this->assertNull($this->event->getArgument('receiver'));
  231. }
  232. /**
  233. * Tests that no raw data is associated with the event by default.
  234. *
  235. * @return void
  236. */
  237. public function testGetRawData()
  238. {
  239. $this->assertNull($this->event->getRawData());
  240. }
  241. /**
  242. * Tests that the raw data associated with the event can be changed.
  243. *
  244. * @return void
  245. */
  246. public function testSetRawData()
  247. {
  248. $expected = 'foo';
  249. $this->event->setRawData($expected);
  250. $actual = $this->event->getRawData();
  251. $this->assertSame($expected, $actual);
  252. }
  253. /**
  254. * Returns a hostmask instance configured for a specified nick.
  255. *
  256. * @param string $nick Optional user nick to associate with the
  257. * hostmask, defaults to 'nick'
  258. * @param string $username Optional username to associate with the
  259. * hostmask, defaults to 'username'
  260. *
  261. * @return Phergie_Hostmask
  262. */
  263. private function getMockHostmask($nick = 'nick', $username = 'username')
  264. {
  265. $hostmask = $this->getMock(
  266. 'Phergie_Hostmask',
  267. array('getNick', 'getUsername'),
  268. array($nick, 'username', 'example.com')
  269. );
  270. $hostmask
  271. ->expects($this->any())
  272. ->method('getNick')
  273. ->will($this->returnValue($nick));
  274. $hostmask
  275. ->expects($this->any())
  276. ->method('getUsername')
  277. ->will($this->returnValue($username));
  278. return $hostmask;
  279. }
  280. /**
  281. * Tests that the nick of the user who originated the event can be
  282. * retrieved.
  283. *
  284. * @return void
  285. * @depends testSetHostmask
  286. */
  287. public function testGetNick()
  288. {
  289. $expected = 'nick';
  290. $hostmask = $this->getMockHostmask($expected);
  291. $this->event->setHostmask($hostmask);
  292. $actual = $this->event->getNick();
  293. $this->assertSame($expected, $actual);
  294. }
  295. /**
  296. * Tests that the channel name is returned as the event source if the
  297. * event occurs in a channel.
  298. *
  299. * @return void
  300. */
  301. public function testGetSourceWithChannel()
  302. {
  303. $expected = '#channel';
  304. $this->event->setType('privmsg');
  305. $this->event->setArguments(array($expected, 'text'));
  306. $actual = $this->event->getSource();
  307. $this->assertSame($expected, $actual);
  308. }
  309. /**
  310. * Tests that the user nick is returned as the event source if the event
  311. * is a private message to the bot from a user.
  312. *
  313. * @return void
  314. */
  315. public function testGetSourceWithUser()
  316. {
  317. $expected = 'nick';
  318. $hostmask = $this->getMockHostmask($expected);
  319. $this->event->setHostmask($hostmask);
  320. $this->event->setType('privmsg');
  321. $this->event->setArguments(array($expected, 'text'));
  322. $actual = $this->event->getSource();
  323. $this->assertSame($expected, $actual);
  324. }
  325. /**
  326. * Data provider for testIsInChannel().
  327. *
  328. * @return array Enumerated array of enumerated arrays each containing
  329. * parameter values for a single call to testIsInChannel()
  330. */
  331. public function dataProviderTestIsInChannel()
  332. {
  333. return array(
  334. array('#channel', true),
  335. array('&channel', true),
  336. array('+channel', true),
  337. array('!channel', true),
  338. array('nick', false),
  339. );
  340. }
  341. /**
  342. * Tests that the event properly detects whether it occurred within a
  343. * channel.
  344. *
  345. * @param string $source Event source
  346. * @param boolean $isInChannel TRUE if the event occurred in a channel,
  347. * FALSE otherwise
  348. *
  349. * @return void
  350. * @depends testGetSourceWithChannel
  351. * @depends testGetSourceWithUser
  352. * @dataProvider dataProviderTestIsInChannel
  353. */
  354. public function testIsInChannel($source, $isInChannel)
  355. {
  356. $hostmask = $this->getMockHostmask('nick');
  357. $this->event->setHostmask($hostmask);
  358. $this->event->setType('privmsg');
  359. $this->event->setArguments(array($source, 'text'));
  360. $this->assertSame($isInChannel, $this->event->isInChannel());
  361. }
  362. /**
  363. * Data provider for testIsFromUser().
  364. *
  365. * @return array Enumerated array of enumerated arrays each containing
  366. * parameter values for a single call to testIsFromUser()
  367. */
  368. public function dataProviderTestIsFromUser()
  369. {
  370. return array(
  371. array(null, null, false),
  372. array('nick', 'username', true),
  373. );
  374. }
  375. /**
  376. * Tests that the event properly detects whether it occurred within a
  377. * private message from a user.
  378. *
  379. * @param mixed $nick String containing the user's nick or NULL
  380. * if the event is not from a user
  381. * @param mixed $username String containing the user's username or
  382. * NULL if the event is not from a user
  383. * @param boolean $isFromUser TRUE if the event is from a user, FALSE
  384. * otherwise
  385. *
  386. * @return void
  387. * @dataProvider dataProviderTestIsFromUser
  388. */
  389. public function testIsFromUser($nick, $username, $isFromUser)
  390. {
  391. $hostmask = $this->getMockHostmask($nick, $username);
  392. $this->event->setHostmask($hostmask);
  393. $this->assertSame($isFromUser, $this->event->isFromUser());
  394. }
  395. /**
  396. * Data provider for testIsFromServer().
  397. *
  398. * @return array Enumerated array of enumerated arrays each containing
  399. * parameter values for a single call to testIsFromServer()
  400. */
  401. public function dataProviderTestIsFromServer()
  402. {
  403. return array(
  404. array(null, null, true),
  405. array('nick', 'username', false),
  406. );
  407. }
  408. /**
  409. * Tests that the event properly detects whether the server (versus a
  410. * user) originated it.
  411. *
  412. * @param mixed $nick String containing the user's nick or NULL
  413. * if the event is not from a user
  414. * @param mixed $username String containing the user's username or
  415. * NULL if the event is not from a user
  416. * @param boolean $isFromServer TRUE if the event is from the server,
  417. * FALSE otherwise
  418. *
  419. * @return void
  420. * @dataProvider dataProviderTestIsFromServer
  421. */
  422. public function testIsFromServer($nick, $username, $isFromServer)
  423. {
  424. $hostmask = $this->getMockHostmask($nick, $username);
  425. $this->event->setHostmask($hostmask);
  426. $this->assertSame($isFromServer, $this->event->isFromServer());
  427. }
  428. /**
  429. * Data provider for testProvidesVirtualGetterMethods().
  430. *
  431. * @return array Enumerated array of enumerated arrays each containing
  432. * parameter values for a single call to
  433. * testProvidesVirtualGetterMethods()
  434. */
  435. public function dataProviderTestProvidesVirtualGetterMethods()
  436. {
  437. $map = Phergie_Event_Request::getArgumentMapping();
  438. $data = array();
  439. foreach ($map as $command => $params) {
  440. $data[] = array($command, $params);
  441. }
  442. return $data;
  443. }
  444. /**
  445. * Tests that the event makes virtual "getter" methods available based
  446. * on the parameters associated with the event type.
  447. *
  448. * @param string $type Event type corresponding to the arguments for
  449. * which to test virtual "getter" methods
  450. * @param array $args Associative array mapping argument name to its
  451. * position starting from 0
  452. *
  453. * @return void
  454. * @dataProvider dataProviderTestProvidesVirtualGetterMethods
  455. */
  456. public function testProvidesVirtualGetterMethods($type, array $args)
  457. {
  458. $this->event->setType($type);
  459. $this->event->setArguments(array_keys($args));
  460. foreach ($args as $name => $position) {
  461. $this->assertSame($name, $this->event->{'get' . ucfirst($name)}());
  462. }
  463. }
  464. /**
  465. * Tests that all event types have a corresponding argument mapping
  466. * defined.
  467. *
  468. * @return void
  469. */
  470. public function testAllCommandsHaveArgumentMapping()
  471. {
  472. $reflector = new ReflectionClass('Phergie_Event_Request');
  473. $map = array_keys(Phergie_Event_Request::getArgumentMapping());
  474. foreach ($reflector->getConstants() as $constant) {
  475. $this->assertContains($constant, $map);
  476. }
  477. }
  478. /**
  479. * Tests that calling an undefined method on the event results in an
  480. * exception.
  481. *
  482. * @return void
  483. */
  484. public function testInvalidMethodCallRaisesException()
  485. {
  486. try {
  487. $this->event->foo();
  488. $this->fail('Expected exception not thrown');
  489. } catch (Phergie_Event_Exception $e) {
  490. if ($e->getCode() != Phergie_Event_Exception::ERR_INVALID_METHOD_CALL) {
  491. $this->fail('Unexpected exception code ' . $e->getCode());
  492. }
  493. }
  494. }
  495. /**
  496. * Tests that the presence of event arguments can be checked using array
  497. * syntax.
  498. *
  499. * @return void
  500. */
  501. public function testImplementsOffsetExists()
  502. {
  503. $this->event->setType('privmsg');
  504. $this->assertFalse(isset($this->event['message']));
  505. $this->assertFalse(isset($this->event['text']));
  506. $this->event->setArgument('text', 'text');
  507. $this->assertTrue(isset($this->event['text']));
  508. }
  509. /**
  510. * Tests that event argument values can be retrieved using array syntax.
  511. *
  512. * @return void
  513. * @depends testSetArgumentWithValidArgument
  514. */
  515. public function testImplementsOffsetGet()
  516. {
  517. $receiver = '#channel';
  518. $this->event->setType('privmsg');
  519. $this->event->setArgument('receiver', $receiver);
  520. $this->assertSame($receiver, $this->event['receiver']);
  521. }
  522. /**
  523. * Tests that event argument values can be set using array syntax.
  524. *
  525. * @return void
  526. * @depends testSetArgumentWithValidArgument
  527. */
  528. public function testImplementsOffsetSet()
  529. {
  530. $receiver = '#channel';
  531. $this->event->setType('privmsg');
  532. $this->event['receiver'] = $receiver;
  533. $this->assertSame($receiver, $this->event->getArgument('receiver'));
  534. }
  535. /**
  536. * Tests that event argument values can be removed using array syntax.
  537. *
  538. * @return void
  539. * @depends testSetArgumentWithValidArgument
  540. */
  541. public function testImplementsOffsetUnset()
  542. {
  543. $this->event->setType('privmsg');
  544. $this->event->setArgument('receiver', '#channel');
  545. unset($this->event['receiver']);
  546. $this->assertNull($this->event->getArgument('receiver'));
  547. }
  548. }