ConnectionTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 Pherge_Connection.
  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_ConnectionTest extends PHPUnit_Framework_TestCase
  31. {
  32. /**
  33. * Associative array containing an option-to-value mapping
  34. *
  35. * @var array
  36. */
  37. private $options = array(
  38. 'host' => 'example.com',
  39. 'port' => 4080,
  40. 'transport' => 'udp',
  41. 'encoding' => 'ASCII',
  42. 'nick' => 'MyNick',
  43. 'username' => 'MyUsername',
  44. 'realname' => 'MyRealName',
  45. 'password' => 'MyPassword',
  46. );
  47. /**
  48. * Data provider for testGetOptionReturnsDefault().
  49. *
  50. * @return array Enumerated array of enumerated arrays each containing a
  51. * set of parameters for a single call to
  52. * testGetOptionReturnsDefault()
  53. */
  54. public function dataProviderTestGetOptionReturnsDefault()
  55. {
  56. return array(
  57. array('transport', 'tcp'),
  58. array('encoding', 'ISO-8859-1'),
  59. array('port', 6667),
  60. array('password', null),
  61. );
  62. }
  63. /**
  64. * Tests that a default values are used for some options.
  65. *
  66. * @param string $option Name of the option with a default value
  67. * @param mixed $value Default value of the option
  68. *
  69. * @return void
  70. * @dataProvider dataProviderTestGetOptionReturnsDefault
  71. */
  72. public function testGetOptionReturnsDefault($option, $value)
  73. {
  74. $connection = new Phergie_Connection;
  75. $this->assertEquals($value, $connection->{'get' . ucfirst($option)}());
  76. }
  77. /**
  78. * Tests that a default encoding is used if one isn't specified.
  79. *
  80. * @return void
  81. */
  82. public function testGetEncodingReturnsDefault()
  83. {
  84. $connection = new Phergie_Connection;
  85. $this->assertEquals('ISO-8859-1', $connection->getEncoding());
  86. }
  87. /**
  88. * Tests that options can be set via the constructor.
  89. *
  90. * @return void
  91. */
  92. public function testSetOptionsViaConstructor()
  93. {
  94. $connection = new Phergie_Connection($this->options);
  95. foreach ($this->options as $key => $value) {
  96. $this->assertEquals($value, $connection->{'get' . ucfirst($key)}());
  97. }
  98. }
  99. /**
  100. * Data provider for testGetHostmaskMissingDataGeneratesException().
  101. *
  102. * @return array Enumerated array of enumerated arrays each containing a
  103. * set of parameters for a single call to
  104. * testGetHostmaskMissingDataGeneratesException()
  105. */
  106. public function dataProviderTestGetHostmaskMissingDataGeneratesException()
  107. {
  108. return array(
  109. array(null, $this->options['username'], $this->options['host']),
  110. array($this->options['nick'], null, $this->options['host']),
  111. array($this->options['nick'], $this->options['username'], null),
  112. );
  113. }
  114. /**
  115. * Tests that attempting to retrieve a hostmask without option values
  116. * for all of its constituents generates an exception.
  117. *
  118. * @param string $nick Bot nick
  119. * @param string $username Bot username
  120. * @param string $host Server hostname
  121. *
  122. * @return void
  123. * @dataProvider dataProviderTestGetHostmaskMissingDataGeneratesException
  124. */
  125. public function testGetHostmaskMissingDataGeneratesException($nick, $username, $host)
  126. {
  127. $options = array(
  128. 'nick' => $nick,
  129. 'username' => $username,
  130. 'host' => $host,
  131. );
  132. $connection = new Phergie_Connection($options);
  133. try {
  134. $hostmask = $connection->getHostmask();
  135. $this->fail('Expected exception was not thrown');
  136. } catch (Phergie_Connection_Exception $e) {
  137. return;
  138. } catch (Exception $e) {
  139. $this->fail('Unexpected exception was thrown');
  140. }
  141. }
  142. /**
  143. * Tests that attempting to retrieve a hostmask with all required
  144. * options is successful.
  145. *
  146. * @return void
  147. */
  148. public function testGetHostmaskWithValidData()
  149. {
  150. $options = array(
  151. 'nick' => 'MyNick',
  152. 'username' => 'MyUsername',
  153. 'host' => 'example.com'
  154. );
  155. $connection = new Phergie_Connection($options);
  156. $hostmask = $connection->getHostmask();
  157. $this->assertType('Phergie_Hostmask', $hostmask);
  158. }
  159. /**
  160. * Data provider for testGetRequiredOptionsWithoutValuesSet().
  161. *
  162. * @return array Enumerated array of enumerated arrays each containing a
  163. * set of parameters for a single call to
  164. * testGetRequiredOptionsWithoutValuesSet()
  165. */
  166. public function dataProviderTestGetRequiredOptionsWithoutValuesSet()
  167. {
  168. return array(
  169. array('host'),
  170. array('nick'),
  171. array('username'),
  172. array('realname'),
  173. );
  174. }
  175. /**
  176. * Tests that attempting to retrieve values of required options when no
  177. * values are set results in an exception.
  178. *
  179. * @param string $option Option name
  180. *
  181. * @return void
  182. * @dataProvider dataProviderTestGetRequiredOptionsWithoutValuesSet
  183. */
  184. public function testGetRequiredOptionsWithoutValuesSet($option)
  185. {
  186. try {
  187. $connection = new Phergie_Connection;
  188. $value = $connection->{'get' . ucfirst($option)}();
  189. $this->fail('Expected exception was not thrown');
  190. } catch (Phergie_Connection_Exception $e) {
  191. return;
  192. } catch (Exception $e) {
  193. $this->fail('Unexpected exception was thrown');
  194. }
  195. }
  196. /**
  197. * Tests that attempting to set an invalid value for the transport
  198. * results in an exception.
  199. *
  200. * @return void
  201. */
  202. public function testSetTransportWithInvalidValue()
  203. {
  204. $connection = new Phergie_Connection;
  205. try {
  206. $connection->setTransport('blah');
  207. $this->fail('Expected exception was not thrown');
  208. } catch (Phergie_Connection_Exception $e) {
  209. return;
  210. } catch (Exception $e) {
  211. $this->fail('Unexpected exception was thrown');
  212. }
  213. }
  214. /**
  215. * Tests that attempting to set an invalid value for the encoding
  216. * results in an exception.
  217. *
  218. * @return void
  219. */
  220. public function testSetEncodingWithInvalidValue()
  221. {
  222. $connection = new Phergie_Connection;
  223. try {
  224. $connection->setEncoding('blah');
  225. $this->fail('Expected exception was not thrown');
  226. } catch (Phergie_Connection_Exception $e) {
  227. return;
  228. } catch (Exception $e) {
  229. $this->fail('Unexpected exception was thrown');
  230. }
  231. }
  232. /**
  233. * Tests that options can be set collectively after the connection is
  234. * instantiated.
  235. *
  236. * @return void
  237. */
  238. public function testSetOptions()
  239. {
  240. $connection = new Phergie_Connection;
  241. $connection->setOptions($this->options);
  242. foreach ($this->options as $key => $value) {
  243. $this->assertEquals($value, $connection->{'get' . ucfirst($key)}());
  244. }
  245. }
  246. }