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