]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Tests/Phergie/Plugin/KarmaTest.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Tests / Phergie / Plugin / KarmaTest.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_Plugin_Karma.
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_Plugin_KarmaTest extends Phergie_Plugin_TestCase
32 {
33     /**
34      * Skips tests if the SQLite PDO driver is not available.
35      *
36      * @return void
37      */
38     public function setUp()
39     {
40         if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
41             $this->markTestSkipped('PDO or pdo_sqlite extension is required');
42         }
43
44         parent::setUp();
45     }
46
47     /**
48      * Configures the plugin to use a temporary copy of the database.
49      *
50      * @return PDO Connection to the temporary database
51      */
52     private function createMockDatabase()
53     {
54         $dbPath = $this->getPluginsPath('Karma/karma.db');
55         $db = $this->getMockDatabase($dbPath);
56         $this->plugin->setDb($db);
57         return $db;
58     }
59
60     /**
61      * Tests the requirement of the Command plugin.
62      *
63      * @return void
64      */
65     public function testRequiresCommandPlugin()
66     {
67         $this->assertRequiresPlugin('Command');
68         $this->plugin->onLoad();
69     }
70
71     /**
72      * Initiates a karma event with a specified term.
73      *
74      * @param string $term Karma term
75      *
76      * @return Phergie_Event_Request Initiated mock event
77      */
78     private function initiateKarmaEvent($term)
79     {
80         $args = array(
81             'receiver' => $this->source,
82             'text' => 'karma ' . $term
83         );
84         $event = $this->getMockEvent('privmsg', $args);
85         $this->plugin->setEvent($event);
86         return $event;
87     }
88
89     /**
90      * Checks for an expected karma response.
91      *
92      * @param Phergie_Event_Request $event    Event containing the karma
93      *                                        request
94      * @param string                $term     Karma term
95      * @param string                $response Portion of the response
96      *                                        message following the term
97      *                                        from the original event
98      *
99      * @return void
100      */
101     private function checkForKarmaResponse($event, $term, $response)
102     {
103         $text = $event->getNick() . ': ' . $response;
104         $this->assertEmitsEvent('privmsg', array($event->getSource(), $text));
105         $this->plugin->onCommandKarma($term);
106     }
107
108     /**
109      * Tests that a default database is used when none is specified.
110      *
111      * @return void
112      */
113     public function testGetDb()
114     {
115         $db = $this->plugin->getDb();
116         $this->assertType('PDO', $db);
117     }
118
119     /**
120      * Tests specifying a custom database for the plugin to use.
121      *
122      * @return void
123      */
124     public function testSetDb()
125     {
126         $db = $this->createMockDatabase();
127         $this->assertSame($db, $this->plugin->getDb());
128     }
129
130     /**
131      * Tests that issuing the karma command with an unknown term returns a
132      * neutral rating.
133      *
134      * @return void
135      */
136     public function testKarmaCommandOnUnknownTerm()
137     {
138         $term = 'foo';
139         $this->createMockDatabase();
140         $event = $this->initiateKarmaEvent($term);
141         $this->checkForKarmaResponse($event, $term, $term . ' has neutral karma.');
142     }
143
144     /**
145      * Tests that issuing the karma command with the term "me" returns the
146      * the karma rating for the initiating user.
147      *
148      * @return void
149      */
150     public function testKarmaCommandOnUser()
151     {
152         $term = 'me';
153         $this->createMockDatabase();
154         $event = $this->initiateKarmaEvent($term);
155         $this->checkForKarmaResponse($event, $term, 'You have neutral karma.');
156     }
157
158     /**
159      * Tests that issuing the karma command with a term that has a fixed
160      * karma rating results in that rating being returned.
161      *
162      * @return void
163      */
164     public function testKarmaCommandWithFixedKarmaTerm()
165     {
166         $term = 'phergie';
167         $this->createMockDatabase();
168         $event = $this->initiateKarmaEvent($term);
169         $this->checkForKarmaResponse($event, $term, 'phergie has karma of awesome.');
170     }
171
172     /**
173      * Supporting method that tests the result of a karma term rating change.
174      *
175      * @param string $term      Karma term for which the rating is being
176      *                          changed
177      * @param string $operation ++ or --
178      * @param int    $karma     Expected karma rating after the change is
179      *                          applied
180      */
181     private function checkForKarmaRatingChange($term, $operation, $karma)
182     {
183         $args = array(
184             'receiver' => $this->source,
185             'text' => $term . $operation
186         );
187         $event = $this->getMockEvent('privmsg', $args);
188         $this->plugin->setEvent($event);
189         $this->plugin->onPrivmsg();
190         $event = $this->initiateKarmaEvent($term);
191         $this->checkForKarmaResponse($event, $term, $term . ' has karma of ' . $karma . '.');
192     }
193
194     /**
195      * Tests incrementing the karma rating of a new term.
196      *
197      * @return void
198      */
199     public function testIncrementingKarmaRating()
200     {
201         $this->createMockDatabase();
202         $this->checkForKarmaRatingChange('foo', '++', 1);
203     }
204
205     /**
206      * Tests decrementing the karma rating of a new term.
207      *
208      * @return void
209      */
210     public function testDecrementingKarmaRating()
211     {
212         $this->createMockDatabase();
213         $this->checkForKarmaRatingChange('foo', '--', -1);
214     }
215
216     /**
217      * Tests modifying the karma rating of an existing term.
218      *
219      * @return void
220      */
221     public function testChangingExistingKarmaRating()
222     {
223         $term = 'foo';
224         $this->createMockDatabase();
225         $this->checkForKarmaRatingChange($term, '++', 1);
226         $this->checkForKarmaRatingChange($term, '++', 2);
227     }
228
229     /**
230      * Tests resetting the karma rating of an existing term to 0.
231      *
232      * @return void
233      */
234     public function testResettingExistingKarmaRating()
235     {
236         $term = 'foo';
237         $this->createMockDatabase();
238         $this->checkForKarmaRatingChange($term, '++', 1);
239         $this->plugin->onCommandReincarnate($term);
240         $event = $this->initiateKarmaEvent($term);
241         $this->checkForKarmaResponse($event, $term, $term . ' has neutral karma.');
242     }
243
244     /**
245      * Data provider for testKarmaComparisons().
246      *
247      * @return array Enumerated array of enumerated arrays each containing a
248      *               set of parameter values for a single call to
249      *               testKarmaComparisons()
250      */
251     public function dataProviderTestKarmaComparisons()
252     {
253         $term1 = 'foo';
254         $term2 = 'bar';
255
256         $positive = 'True that.';
257         $negative = 'No sir, not at all.';
258
259         return array(
260             array($term1, $term2, 1, 0, '>', $positive),
261             array($term1, $term2, 0, 1, '>', $negative),
262             array($term1, $term2, 1, 1, '>', $negative),
263             array($term1, $term2, 1, 0, '<', $negative),
264             array($term1, $term2, 0, 1, '<', $positive),
265             array($term1, $term2, 1, 1, '<', $negative),
266             array($term1, 'phergie', 1, 0, '>', $positive),
267             array('phergie', $term2, 0, 1, '<', $positive),
268             array($term1, 'everything', 0, 0, '>', $positive),
269             array('everything', $term2, 0, 0, '>', $positive),
270         );
271     }
272
273     /**
274      * Tests comparing the karma ratings of two terms.
275      *
276      * @param string $term1    First term
277      * @param string $term2    Second term
278      * @param int    $karma1   Karma rating of the first time, 0 or 1
279      * @param int    $karma2   Karma rating of the second term, 0 or 1
280      * @param string $operator Comparison operator, > or <
281      * @param string $response Response to check for
282      *
283      * @return void
284      * @dataProvider dataProviderTestKarmaComparisons
285      */
286     public function testKarmaComparisons($term1, $term2, $karma1, $karma2,
287         $operator, $response
288     ) {
289         $db = $this->createMockDatabase();
290
291         // Reduce answer tables to expected response
292         $stmt = $db->prepare('DELETE FROM positive_answers WHERE answer != ?');
293         $stmt->execute(array($response));
294         $stmt = $db->prepare('DELETE FROM negative_answers WHERE answer != ?');
295         $stmt->execute(array($response));
296
297         if ($karma1) {
298             $this->checkForKarmaRatingChange($term1, '++', 1);
299         }
300
301         if ($karma2) {
302             $this->checkForKarmaRatingChange($term2, '++', 1);
303         }
304
305         $args = array(
306             'receiver' => $this->source,
307             'text' => $term1 . ' ' . $operator . ' ' . $term2
308         );
309         $event = $this->getMockEvent('privmsg', $args);
310         $this->plugin->setEvent($event);
311
312         // Test lack of a response for terms with fixed karma ratings
313         if ($term1 == 'phergie' || $term2 == 'phergie') {
314             $callback = 'assertDoesNotEmitEvent';
315         } else {
316             $callback = 'assertEmitsEvent';
317         }
318
319         $this->$callback('privmsg', array($event->getSource(), $response));
320         $this->plugin->onPrivmsg();
321
322         // Test for karma changes when one term is "everything"
323         if ($term1 == 'everything' || $term2 == 'everything') {
324             if ($term1 == 'everything') {
325                 $term = $term2;
326                 $karma = ($operator == '>') ? -1 : 1;
327             } else {
328                 $term = $term1;
329                 $karma = ($operator == '>') ? 1 : -1;
330             }
331             $event = $this->initiateKarmaEvent($term);
332             $this->checkForKarmaResponse($event, $term, $term . ' has karma of ' . $karma . '.');
333         }
334     }
335 }