PageRenderTime 62ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/Zend/Feed/PubSubHubbub/SubscriberTest.php

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