remove fixme; it is fixed now
[services_libravatar:stelians-services_libravatar.git] / tests / Services / LibravatarTest.php
1 <?php
2 require_once 'Services/Libravatar.php';
3
4 class Services_LibravatarTest extends PHPUnit_Framework_TestCase
5 {
6     public function setUp()
7     {
8         $this->sl = new Services_Libravatar();
9     }
10
11     public function testUrl()
12     {
13         $this->loadSLMock();
14         $this->assertEquals(
15             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99',
16             $this->sl->url('cweiske@cweiske.de')
17         );
18     }
19
20     public function testGetUrl()
21     {
22         $this->loadSLMock();
23         $this->assertEquals(
24             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99',
25             $this->sl->getUrl('cweiske@cweiske.de')
26         );
27     }
28
29     public function testGetUrlHttpsDefault()
30     {
31         $this->loadSLMock();
32         $this->sl->setHttps(true);
33         $this->assertEquals(
34             'https://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99',
35             $this->sl->getUrl('cweiske@cweiske.de')
36         );
37     }
38
39     public function testGetUrlHttpsOptionOff()
40     {
41         $this->loadSLMock();
42         $this->sl->setHttps(true);
43         $this->assertEquals(
44             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99',
45             $this->sl->getUrl('cweiske@cweiske.de', array('https' => false))
46         );
47     }
48
49     public function testGetUrlAlgorithmDefault()
50     {
51         $this->loadSLMock();
52         $this->sl->setAlgorithm('sha256');
53         $this->assertEquals(
54             'http://example.org/avatar/baa4e986ac6bb3f3715de5b08727be61d33afb4b03e792eb6db2f184a61626d6',
55             $this->sl->getUrl('cweiske@cweiske.de')
56         );
57     }
58
59     public function testGetUrlAlgorithmOption()
60     {
61         $this->loadSLMock();
62         $this->assertEquals(
63             'http://example.org/avatar/baa4e986ac6bb3f3715de5b08727be61d33afb4b03e792eb6db2f184a61626d6',
64             $this->sl->getUrl('cweiske@cweiske.de', array('algorithm' => 'sha256'))
65         );
66     }
67
68     public function testGetUrlSizeDefault()
69     {
70         $this->loadSLMock();
71         $this->sl->setSize(128);
72         $this->assertEquals(
73             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?size=128',
74             $this->sl->getUrl('cweiske@cweiske.de')
75         );
76     }
77
78     public function testGetUrlSizeOption()
79     {
80         $this->loadSLMock();
81         $this->sl->setSize(128);
82         $this->assertEquals(
83             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?size=256',
84             $this->sl->getUrl('cweiske@cweiske.de', array('size' => 256))
85         );
86     }
87
88     public function testGetUrlDefaultDefault()
89     {
90         $this->loadSLMock();
91         $this->sl->setDefault('identicon');
92         $this->assertEquals(
93             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?default=identicon',
94             $this->sl->getUrl('cweiske@cweiske.de')
95         );
96     }
97
98     public function testGetUrlDefaultOption()
99     {
100         $this->loadSLMock();
101         $this->assertEquals(
102             'http://example.org/avatar/9e263681488308e5e5d5e548b2f9bc99?default=404',
103             $this->sl->getUrl('cweiske@cweiske.de', array('default' => 404))
104         );
105     }
106
107     public function testGetUrlNoIdentifier()
108     {
109         $this->loadSLMock();
110         $this->assertEquals(
111             'http://example.org/avatar/d41d8cd98f00b204e9800998ecf8427e',
112             $this->sl->getUrl(false)
113         );
114     }
115
116
117
118     public function testSetAlgorithmValid()
119     {
120         $this->sl->setAlgorithm('md5');
121         $this->assertEquals('md5', $this->getProtected('algorithm', $this->sl));
122
123         $this->sl->setAlgorithm('sha256');
124         $this->assertEquals('sha256', $this->getProtected('algorithm', $this->sl));
125     }
126
127     /**
128      * @expectedException InvalidArgumentException
129      * @expectedExceptionMessage Only md5 and sha256 hashing supported
130      */
131     public function testSetAlgorithmInvalid()
132     {
133         $this->sl->setAlgorithm('foo');
134     }
135
136     public function testSetDefaultIdenticon()
137     {
138         $this->sl->setDefault('identicon');
139         $this->assertEquals('identicon', $this->getProtected('default', $this->sl));
140     }
141
142     public function testSetDefaultUrl()
143     {
144         $this->sl->setDefault('http://example.org/default.png');
145         $this->assertEquals(
146             'http://example.org/default.png',
147             $this->getProtected('default', $this->sl)
148         );
149     }
150
151     public function testSetDefaultNull()
152     {
153         $this->sl->setDefault(null);
154         $this->assertNull(
155             $this->getProtected('default', $this->sl)
156         );
157     }
158
159     /**
160      * @expectedException InvalidArgumentException
161      * @expectedExceptionMessage Invalid default avatar URL
162      */
163     public function testSetDefaultInvalidShortcut()
164     {
165         $this->sl->setDefault('foo');
166     }
167
168     /**
169      * @expectedException InvalidArgumentException
170      * @expectedExceptionMessage Invalid default avatar URL
171      */
172     public function testSetDefaultInvalidUrl()
173     {
174         //missing protocol
175         $this->sl->setDefault('example.org/default.png');
176     }
177
178     public function testSetHttps()
179     {
180         $this->sl->setHttps(true);
181         $this->assertEquals(true, $this->getProtected('https', $this->sl));
182     }
183
184     public function testSetSize()
185     {
186         $this->sl->setSize(32);
187         $this->assertEquals(32, $this->getProtected('size', $this->sl));
188     }
189
190     public function testSetSizeNull()
191     {
192         $this->sl->setSize(null);
193         $this->assertNull($this->getProtected('size', $this->sl));
194     }
195
196     /**
197      * @expectedException InvalidArgumentException
198      * @expectedExceptionMessage Size has to be larger than 0
199      */
200     public function testSetSizeInvalid()
201     {
202         $this->sl->setSize(-21);
203     }
204
205     public function testDetectHttpsOn()
206     {
207         $_SERVER['HTTPS'] = 'on';
208         $this->sl->detectHttps();
209         $this->assertEquals(true, $this->getProtected('https', $this->sl));
210     }
211
212     public function testDetectHttpsOff()
213     {
214         unset($_SERVER['HTTPS']);
215         $this->sl->detectHttps();
216         $this->assertEquals(false, $this->getProtected('https', $this->sl));
217     }
218
219
220     /**
221      * Get the value of a protected/private class property
222      */
223     protected function getProtected($variable, $object)
224     {
225         $rc = new ReflectionClass($object);
226         $prop = $rc->getProperty($variable);
227         $prop->setAccessible(true);
228         return $prop->getValue($object);
229     }
230
231     protected function loadSLMock()
232     {
233         $this->sl = $this->getMock('Services_Libravatar', array('srvGet'));
234         $this->sl->expects($this->once())
235             ->method('srvGet')
236             ->will($this->returnValue('example.org'));
237     }
238 }
239
240 ?>