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
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
23 * Unit test suite for Pherge_Connection.
26 * @package Phergie_Tests
27 * @author Phergie Development Team <team@phergie.org>
28 * @license http://phergie.org/license New BSD License
29 * @link http://pear.phergie.org/package/Phergie_Tests
31 class Phergie_ConnectionTest extends PHPUnit_Framework_TestCase
34 * Associative array containing an option-to-value mapping
38 private $options = array(
39 'host' => 'example.com',
42 'encoding' => 'ASCII',
44 'username' => 'MyUsername',
45 'realname' => 'MyRealName',
46 'password' => 'MyPassword',
50 * Data provider for testGetOptionReturnsDefault().
52 * @return array Enumerated array of enumerated arrays each containing a
53 * set of parameters for a single call to
54 * testGetOptionReturnsDefault()
56 public function dataProviderTestGetOptionReturnsDefault()
59 array('transport', 'tcp'),
60 array('encoding', 'ISO-8859-1'),
62 array('password', null),
67 * Tests that a default values are used for some options.
69 * @param string $option Name of the option with a default value
70 * @param mixed $value Default value of the option
73 * @dataProvider dataProviderTestGetOptionReturnsDefault
75 public function testGetOptionReturnsDefault($option, $value)
77 $connection = new Phergie_Connection;
78 $this->assertEquals($value, $connection->{'get' . ucfirst($option)}());
82 * Tests that a default encoding is used if one isn't specified.
86 public function testGetEncodingReturnsDefault()
88 $connection = new Phergie_Connection;
89 $this->assertEquals('ISO-8859-1', $connection->getEncoding());
93 * Tests that options can be set via the constructor.
97 public function testSetOptionsViaConstructor()
99 $connection = new Phergie_Connection($this->options);
100 foreach ($this->options as $key => $value) {
101 $this->assertEquals($value, $connection->{'get' . ucfirst($key)}());
106 * Data provider for testGetHostmaskMissingDataGeneratesException().
108 * @return array Enumerated array of enumerated arrays each containing a
109 * set of parameters for a single call to
110 * testGetHostmaskMissingDataGeneratesException()
112 public function dataProviderTestGetHostmaskMissingDataGeneratesException()
115 array(null, $this->options['username'], $this->options['host']),
116 array($this->options['nick'], null, $this->options['host']),
117 array($this->options['nick'], $this->options['username'], null),
122 * Tests that attempting to retrieve a hostmask without option values
123 * for all of its constituents generates an exception.
125 * @param string $nick Bot nick
126 * @param string $username Bot username
127 * @param string $host Server hostname
130 * @dataProvider dataProviderTestGetHostmaskMissingDataGeneratesException
132 public function testGetHostmaskMissingDataGeneratesException($nick, $username, $host)
136 'username' => $username,
140 $connection = new Phergie_Connection($options);
143 $hostmask = $connection->getHostmask();
144 $this->fail('Expected exception was not thrown');
145 } catch (Phergie_Connection_Exception $e) {
147 } catch (Exception $e) {
148 $this->fail('Unexpected exception was thrown');
153 * Tests that attempting to retrieve a hostmask with all required
154 * options is successful.
158 public function testGetHostmaskWithValidData()
162 'username' => 'MyUsername',
163 'host' => 'example.com'
166 $connection = new Phergie_Connection($options);
167 $hostmask = $connection->getHostmask();
168 $this->assertType('Phergie_Hostmask', $hostmask);
172 * Data provider for testGetRequiredOptionsWithoutValuesSet().
174 * @return array Enumerated array of enumerated arrays each containing a
175 * set of parameters for a single call to
176 * testGetRequiredOptionsWithoutValuesSet()
178 public function dataProviderTestGetRequiredOptionsWithoutValuesSet()
189 * Tests that attempting to retrieve values of required options when no
190 * values are set results in an exception.
192 * @param string $option Option name
195 * @dataProvider dataProviderTestGetRequiredOptionsWithoutValuesSet
197 public function testGetRequiredOptionsWithoutValuesSet($option)
200 $connection = new Phergie_Connection;
201 $value = $connection->{'get' . ucfirst($option)}();
202 $this->fail('Expected exception was not thrown');
203 } catch (Phergie_Connection_Exception $e) {
205 } catch (Exception $e) {
206 $this->fail('Unexpected exception was thrown');
211 * Tests that attempting to set an invalid value for the transport
212 * results in an exception.
216 public function testSetTransportWithInvalidValue()
218 $connection = new Phergie_Connection;
220 $connection->setTransport('blah');
221 $this->fail('Expected exception was not thrown');
222 } catch (Phergie_Connection_Exception $e) {
224 } catch (Exception $e) {
225 $this->fail('Unexpected exception was thrown');
230 * Tests that attempting to set an invalid value for the encoding
231 * results in an exception.
235 public function testSetEncodingWithInvalidValue()
237 $connection = new Phergie_Connection;
239 $connection->setEncoding('blah');
240 $this->fail('Expected exception was not thrown');
241 } catch (Phergie_Connection_Exception $e) {
243 } catch (Exception $e) {
244 $this->fail('Unexpected exception was thrown');
249 * Tests that options can be set collectively after the connection is
254 public function testSetOptions()
256 $connection = new Phergie_Connection;
257 $connection->setOptions($this->options);
258 foreach ($this->options as $key => $value) {
259 $this->assertEquals($value, $connection->{'get' . ucfirst($key)}());