PageRenderTime 29ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Feed/Pubsubhubbub/SubscriberTest.php

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