SubscriberTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package UnitTests
  17. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. require_once 'PHPUnit/Framework/TestCase.php';
  22. require_once 'Zend/Feed/Pubsubhubbub/Subscriber.php';
  23. require_once 'Zend/Feed/Pubsubhubbub/Model/Subscription.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Feed
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Feed_Pubsubhubbub_SubscriberTest extends PHPUnit_Framework_TestCase
  32. {
  33. protected $_subscriber = null;
  34. protected $_adapter = null;
  35. protected $_tableGateway = null;
  36. public function setUp()
  37. {
  38. $client = new Zend_Http_Client;
  39. Zend_Feed_Pubsubhubbub::setHttpClient($client);
  40. $this->_subscriber = new Zend_Feed_Pubsubhubbub_Subscriber;
  41. $this->_adapter = $this->_getCleanMock(
  42. 'Zend_Db_Adapter_Abstract'
  43. );
  44. $this->_tableGateway = $this->_getCleanMock(
  45. 'Zend_Db_Table_Abstract'
  46. );
  47. $this->_tableGateway->expects($this->any())->method('getAdapter')
  48. ->will($this->returnValue($this->_adapter));
  49. }
  50. public function testAddsHubServerUrl()
  51. {
  52. $this->_subscriber->addHubUrl('http://www.example.com/hub');
  53. $this->assertEquals(array('http://www.example.com/hub'), $this->_subscriber->getHubUrls());
  54. }
  55. public function testAddsHubServerUrlsFromArray()
  56. {
  57. $this->_subscriber->addHubUrls(array(
  58. 'http://www.example.com/hub', 'http://www.example.com/hub2'
  59. ));
  60. $this->assertEquals(array(
  61. 'http://www.example.com/hub', 'http://www.example.com/hub2'
  62. ), $this->_subscriber->getHubUrls());
  63. }
  64. public function testAddsHubServerUrlsFromArrayUsingSetConfig()
  65. {
  66. $this->_subscriber->setConfig(array('hubUrls' => array(
  67. 'http://www.example.com/hub', 'http://www.example.com/hub2'
  68. )));
  69. $this->assertEquals(array(
  70. 'http://www.example.com/hub', 'http://www.example.com/hub2'
  71. ), $this->_subscriber->getHubUrls());
  72. }
  73. public function testRemovesHubServerUrl()
  74. {
  75. $this->_subscriber->addHubUrls(array(
  76. 'http://www.example.com/hub', 'http://www.example.com/hub2'
  77. ));
  78. $this->_subscriber->removeHubUrl('http://www.example.com/hub');
  79. $this->assertEquals(array(
  80. 1 => 'http://www.example.com/hub2'
  81. ), $this->_subscriber->getHubUrls());
  82. }
  83. public function testRetrievesUniqueHubServerUrlsOnly()
  84. {
  85. $this->_subscriber->addHubUrls(array(
  86. 'http://www.example.com/hub', 'http://www.example.com/hub2',
  87. 'http://www.example.com/hub'
  88. ));
  89. $this->assertEquals(array(
  90. 'http://www.example.com/hub', 'http://www.example.com/hub2'
  91. ), $this->_subscriber->getHubUrls());
  92. }
  93. public function testThrowsExceptionOnSettingEmptyHubServerUrl()
  94. {
  95. try {
  96. $this->_subscriber->addHubUrl('');
  97. $this->fail('Should not fail as an Exception would be raised and caught');
  98. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  99. }
  100. public function testThrowsExceptionOnSettingNonStringHubServerUrl()
  101. {
  102. try {
  103. $this->_subscriber->addHubUrl(123);
  104. $this->fail('Should not fail as an Exception would be raised and caught');
  105. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  106. }
  107. public function testThrowsExceptionOnSettingInvalidHubServerUrl()
  108. {
  109. try {
  110. $this->_subscriber->addHubUrl('http://');
  111. $this->fail('Should not fail as an Exception would be raised and caught');
  112. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  113. }
  114. public function testAddsParameter()
  115. {
  116. $this->_subscriber->setParameter('foo', 'bar');
  117. $this->assertEquals(array('foo'=>'bar'), $this->_subscriber->getParameters());
  118. }
  119. public function testAddsParametersFromArray()
  120. {
  121. $this->_subscriber->setParameters(array(
  122. 'foo' => 'bar', 'boo' => 'baz'
  123. ));
  124. $this->assertEquals(array(
  125. 'foo' => 'bar', 'boo' => 'baz'
  126. ), $this->_subscriber->getParameters());
  127. }
  128. public function testAddsParametersFromArrayInSingleMethod()
  129. {
  130. $this->_subscriber->setParameter(array(
  131. 'foo' => 'bar', 'boo' => 'baz'
  132. ));
  133. $this->assertEquals(array(
  134. 'foo' => 'bar', 'boo' => 'baz'
  135. ), $this->_subscriber->getParameters());
  136. }
  137. public function testAddsParametersFromArrayUsingSetConfig()
  138. {
  139. $this->_subscriber->setConfig(array('parameters' => array(
  140. 'foo' => 'bar', 'boo' => 'baz'
  141. )));
  142. $this->assertEquals(array(
  143. 'foo' => 'bar', 'boo' => 'baz'
  144. ), $this->_subscriber->getParameters());
  145. }
  146. public function testRemovesParameter()
  147. {
  148. $this->_subscriber->setParameters(array(
  149. 'foo' => 'bar', 'boo' => 'baz'
  150. ));
  151. $this->_subscriber->removeParameter('boo');
  152. $this->assertEquals(array(
  153. 'foo' => 'bar'
  154. ), $this->_subscriber->getParameters());
  155. }
  156. public function testRemovesParameterIfSetToNull()
  157. {
  158. $this->_subscriber->setParameters(array(
  159. 'foo' => 'bar', 'boo' => 'baz'
  160. ));
  161. $this->_subscriber->setParameter('boo', null);
  162. $this->assertEquals(array(
  163. 'foo' => 'bar'
  164. ), $this->_subscriber->getParameters());
  165. }
  166. public function testCanSetTopicUrl()
  167. {
  168. $this->_subscriber->setTopicUrl('http://www.example.com/topic');
  169. $this->assertEquals('http://www.example.com/topic', $this->_subscriber->getTopicUrl());
  170. }
  171. public function testThrowsExceptionOnSettingEmptyTopicUrl()
  172. {
  173. try {
  174. $this->_subscriber->setTopicUrl('');
  175. $this->fail('Should not fail as an Exception would be raised and caught');
  176. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  177. }
  178. public function testThrowsExceptionOnSettingNonStringTopicUrl()
  179. {
  180. try {
  181. $this->_subscriber->setTopicUrl(123);
  182. $this->fail('Should not fail as an Exception would be raised and caught');
  183. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  184. }
  185. public function testThrowsExceptionOnSettingInvalidTopicUrl()
  186. {
  187. try {
  188. $this->_subscriber->setTopicUrl('http://');
  189. $this->fail('Should not fail as an Exception would be raised and caught');
  190. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  191. }
  192. public function testThrowsExceptionOnMissingTopicUrl()
  193. {
  194. try {
  195. $this->_subscriber->getTopicUrl();
  196. $this->fail('Should not fail as an Exception would be raised and caught');
  197. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  198. }
  199. public function testCanSetCallbackUrl()
  200. {
  201. $this->_subscriber->setCallbackUrl('http://www.example.com/callback');
  202. $this->assertEquals('http://www.example.com/callback', $this->_subscriber->getCallbackUrl());
  203. }
  204. public function testThrowsExceptionOnSettingEmptyCallbackUrl()
  205. {
  206. try {
  207. $this->_subscriber->setCallbackUrl('');
  208. $this->fail('Should not fail as an Exception would be raised and caught');
  209. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  210. }
  211. public function testThrowsExceptionOnSettingNonStringCallbackUrl()
  212. {
  213. try {
  214. $this->_subscriber->setCallbackUrl(123);
  215. $this->fail('Should not fail as an Exception would be raised and caught');
  216. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  217. }
  218. public function testThrowsExceptionOnSettingInvalidCallbackUrl()
  219. {
  220. try {
  221. $this->_subscriber->setCallbackUrl('http://');
  222. $this->fail('Should not fail as an Exception would be raised and caught');
  223. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  224. }
  225. public function testThrowsExceptionOnMissingCallbackUrl()
  226. {
  227. try {
  228. $this->_subscriber->getCallbackUrl();
  229. $this->fail('Should not fail as an Exception would be raised and caught');
  230. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  231. }
  232. public function testCanSetLeaseSeconds()
  233. {
  234. $this->_subscriber->setLeaseSeconds('10000');
  235. $this->assertEquals(10000, $this->_subscriber->getLeaseSeconds());
  236. }
  237. public function testThrowsExceptionOnSettingZeroAsLeaseSeconds()
  238. {
  239. try {
  240. $this->_subscriber->setLeaseSeconds(0);
  241. $this->fail('Should not fail as an Exception would be raised and caught');
  242. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  243. }
  244. public function testThrowsExceptionOnSettingLessThanZeroAsLeaseSeconds()
  245. {
  246. try {
  247. $this->_subscriber->setLeaseSeconds(-1);
  248. $this->fail('Should not fail as an Exception would be raised and caught');
  249. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  250. }
  251. public function testThrowsExceptionOnSettingAnyScalarTypeCastToAZeroOrLessIntegerAsLeaseSeconds()
  252. {
  253. try {
  254. $this->_subscriber->setLeaseSeconds('0aa');
  255. $this->fail('Should not fail as an Exception would be raised and caught');
  256. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  257. }
  258. public function testCanSetPreferredVerificationMode()
  259. {
  260. $this->_subscriber->setPreferredVerificationMode(Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_ASYNC);
  261. $this->assertEquals(Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_ASYNC, $this->_subscriber->getPreferredVerificationMode());
  262. }
  263. public function testSetsPreferredVerificationModeThrowsExceptionOnSettingBadMode()
  264. {
  265. try {
  266. $this->_subscriber->setPreferredVerificationMode('abc');
  267. $this->fail('Should not fail as an Exception would be raised and caught');
  268. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  269. }
  270. public function testPreferredVerificationModeDefaultsToSync()
  271. {
  272. $this->assertEquals(Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC, $this->_subscriber->getPreferredVerificationMode());
  273. }
  274. public function testCanSetStorageImplementation()
  275. {
  276. $storage = new Zend_Feed_Pubsubhubbub_Model_Subscription($this->_tableGateway);
  277. $this->_subscriber->setStorage($storage);
  278. $this->assertThat($this->_subscriber->getStorage(), $this->identicalTo($storage));
  279. }
  280. /**
  281. * @expectedException Zend_Feed_Pubsubhubbub_Exception
  282. */
  283. public function testGetStorageThrowsExceptionIfNoneSet()
  284. {
  285. $this->_subscriber->getStorage();
  286. }
  287. protected function _getCleanMock($className) {
  288. $class = new ReflectionClass($className);
  289. $methods = $class->getMethods();
  290. $stubMethods = array();
  291. foreach ($methods as $method) {
  292. if ($method->isPublic() || ($method->isProtected()
  293. && $method->isAbstract())) {
  294. $stubMethods[] = $method->getName();
  295. }
  296. }
  297. $mocked = $this->getMock(
  298. $className,
  299. $stubMethods,
  300. array(),
  301. $className . '_PubsubSubscriberMock_' . uniqid(),
  302. false
  303. );
  304. return $mocked;
  305. }
  306. }