SubscriberTest.php 12 KB

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