SubscriberTest.php 12 KB

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