PublisherTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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/Publisher.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-2010 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_PublisherTest extends PHPUnit_Framework_TestCase
  33. {
  34. protected $_publisher = null;
  35. public function setUp()
  36. {
  37. $client = new Zend_Http_Client;
  38. Zend_Feed_Pubsubhubbub::setHttpClient($client);
  39. $this->_publisher = new Zend_Feed_Pubsubhubbub_Publisher;
  40. }
  41. public function testAddsHubServerUrl()
  42. {
  43. $this->_publisher->addHubUrl('http://www.example.com/hub');
  44. $this->assertEquals(array('http://www.example.com/hub'), $this->_publisher->getHubUrls());
  45. }
  46. public function testAddsHubServerUrlsFromArray()
  47. {
  48. $this->_publisher->addHubUrls(array(
  49. 'http://www.example.com/hub', 'http://www.example.com/hub2'
  50. ));
  51. $this->assertEquals(array(
  52. 'http://www.example.com/hub', 'http://www.example.com/hub2'
  53. ), $this->_publisher->getHubUrls());
  54. }
  55. public function testAddsHubServerUrlsFromArrayUsingSetConfig()
  56. {
  57. $this->_publisher->setConfig(array('hubUrls' => 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->_publisher->getHubUrls());
  63. }
  64. public function testRemovesHubServerUrl()
  65. {
  66. $this->_publisher->addHubUrls(array(
  67. 'http://www.example.com/hub', 'http://www.example.com/hub2'
  68. ));
  69. $this->_publisher->removeHubUrl('http://www.example.com/hub');
  70. $this->assertEquals(array(
  71. 1 => 'http://www.example.com/hub2'
  72. ), $this->_publisher->getHubUrls());
  73. }
  74. public function testRetrievesUniqueHubServerUrlsOnly()
  75. {
  76. $this->_publisher->addHubUrls(array(
  77. 'http://www.example.com/hub', 'http://www.example.com/hub2',
  78. 'http://www.example.com/hub'
  79. ));
  80. $this->assertEquals(array(
  81. 'http://www.example.com/hub', 'http://www.example.com/hub2'
  82. ), $this->_publisher->getHubUrls());
  83. }
  84. public function testThrowsExceptionOnSettingEmptyHubServerUrl()
  85. {
  86. try {
  87. $this->_publisher->addHubUrl('');
  88. $this->fail('Should not fail as an Exception would be raised and caught');
  89. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  90. }
  91. public function testThrowsExceptionOnSettingNonStringHubServerUrl()
  92. {
  93. try {
  94. $this->_publisher->addHubUrl(123);
  95. $this->fail('Should not fail as an Exception would be raised and caught');
  96. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  97. }
  98. public function testThrowsExceptionOnSettingInvalidHubServerUrl()
  99. {
  100. try {
  101. $this->_publisher->addHubUrl('http://');
  102. $this->fail('Should not fail as an Exception would be raised and caught');
  103. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  104. }
  105. public function testAddsUpdatedTopicUrl()
  106. {
  107. $this->_publisher->addUpdatedTopicUrl('http://www.example.com/topic');
  108. $this->assertEquals(array('http://www.example.com/topic'), $this->_publisher->getUpdatedTopicUrls());
  109. }
  110. public function testAddsUpdatedTopicUrlsFromArray()
  111. {
  112. $this->_publisher->addUpdatedTopicUrls(array(
  113. 'http://www.example.com/topic', 'http://www.example.com/topic2'
  114. ));
  115. $this->assertEquals(array(
  116. 'http://www.example.com/topic', 'http://www.example.com/topic2'
  117. ), $this->_publisher->getUpdatedTopicUrls());
  118. }
  119. public function testAddsUpdatedTopicUrlsFromArrayUsingSetConfig()
  120. {
  121. $this->_publisher->setConfig(array('updatedTopicUrls' => array(
  122. 'http://www.example.com/topic', 'http://www.example.com/topic2'
  123. )));
  124. $this->assertEquals(array(
  125. 'http://www.example.com/topic', 'http://www.example.com/topic2'
  126. ), $this->_publisher->getUpdatedTopicUrls());
  127. }
  128. public function testRemovesUpdatedTopicUrl()
  129. {
  130. $this->_publisher->addUpdatedTopicUrls(array(
  131. 'http://www.example.com/topic', 'http://www.example.com/topic2'
  132. ));
  133. $this->_publisher->removeUpdatedTopicUrl('http://www.example.com/topic');
  134. $this->assertEquals(array(
  135. 1 => 'http://www.example.com/topic2'
  136. ), $this->_publisher->getUpdatedTopicUrls());
  137. }
  138. public function testRetrievesUniqueUpdatedTopicUrlsOnly()
  139. {
  140. $this->_publisher->addUpdatedTopicUrls(array(
  141. 'http://www.example.com/topic', 'http://www.example.com/topic2',
  142. 'http://www.example.com/topic'
  143. ));
  144. $this->assertEquals(array(
  145. 'http://www.example.com/topic', 'http://www.example.com/topic2'
  146. ), $this->_publisher->getUpdatedTopicUrls());
  147. }
  148. public function testThrowsExceptionOnSettingEmptyUpdatedTopicUrl()
  149. {
  150. try {
  151. $this->_publisher->addUpdatedTopicUrl('');
  152. $this->fail('Should not fail as an Exception would be raised and caught');
  153. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  154. }
  155. public function testThrowsExceptionOnSettingNonStringUpdatedTopicUrl()
  156. {
  157. try {
  158. $this->_publisher->addUpdatedTopicUrl(123);
  159. $this->fail('Should not fail as an Exception would be raised and caught');
  160. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  161. }
  162. public function testThrowsExceptionOnSettingInvalidUpdatedTopicUrl()
  163. {
  164. try {
  165. $this->_publisher->addUpdatedTopicUrl('http://');
  166. $this->fail('Should not fail as an Exception would be raised and caught');
  167. } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
  168. }
  169. public function testAddsParameter()
  170. {
  171. $this->_publisher->setParameter('foo', 'bar');
  172. $this->assertEquals(array('foo'=>'bar'), $this->_publisher->getParameters());
  173. }
  174. public function testAddsParametersFromArray()
  175. {
  176. $this->_publisher->setParameters(array(
  177. 'foo' => 'bar', 'boo' => 'baz'
  178. ));
  179. $this->assertEquals(array(
  180. 'foo' => 'bar', 'boo' => 'baz'
  181. ), $this->_publisher->getParameters());
  182. }
  183. public function testAddsParametersFromArrayInSingleMethod()
  184. {
  185. $this->_publisher->setParameter(array(
  186. 'foo' => 'bar', 'boo' => 'baz'
  187. ));
  188. $this->assertEquals(array(
  189. 'foo' => 'bar', 'boo' => 'baz'
  190. ), $this->_publisher->getParameters());
  191. }
  192. public function testAddsParametersFromArrayUsingSetConfig()
  193. {
  194. $this->_publisher->setConfig(array('parameters' => array(
  195. 'foo' => 'bar', 'boo' => 'baz'
  196. )));
  197. $this->assertEquals(array(
  198. 'foo' => 'bar', 'boo' => 'baz'
  199. ), $this->_publisher->getParameters());
  200. }
  201. public function testRemovesParameter()
  202. {
  203. $this->_publisher->setParameters(array(
  204. 'foo' => 'bar', 'boo' => 'baz'
  205. ));
  206. $this->_publisher->removeParameter('boo');
  207. $this->assertEquals(array(
  208. 'foo' => 'bar'
  209. ), $this->_publisher->getParameters());
  210. }
  211. public function testRemovesParameterIfSetToNull()
  212. {
  213. $this->_publisher->setParameters(array(
  214. 'foo' => 'bar', 'boo' => 'baz'
  215. ));
  216. $this->_publisher->setParameter('boo', null);
  217. $this->assertEquals(array(
  218. 'foo' => 'bar'
  219. ), $this->_publisher->getParameters());
  220. }
  221. public function testNotifiesHubWithCorrectParameters()
  222. {
  223. Zend_Feed_Pubsubhubbub::setHttpClient(new Zend_Feed_Pubsubhubbub_PublisherTest_ClientSuccess);
  224. $client = Zend_Feed_Pubsubhubbub::getHttpClient();
  225. $this->_publisher->addHubUrl('http://www.example.com/hub');
  226. $this->_publisher->addUpdatedTopicUrl('http://www.example.com/topic');
  227. $this->_publisher->setParameter('foo', 'bar');
  228. $this->_publisher->notifyAll();
  229. $this->assertEquals('hub.mode=publish&hub.url=http%3A%2F%2Fwww.example.com%2Ftopic&foo=bar', $client->getBody());
  230. }
  231. public function testNotifiesHubWithCorrectParametersAndMultipleTopics()
  232. {
  233. Zend_Feed_Pubsubhubbub::setHttpClient(new Zend_Feed_Pubsubhubbub_PublisherTest_ClientSuccess);
  234. $client = Zend_Feed_Pubsubhubbub::getHttpClient();
  235. $this->_publisher->addHubUrl('http://www.example.com/hub');
  236. $this->_publisher->addUpdatedTopicUrl('http://www.example.com/topic');
  237. $this->_publisher->addUpdatedTopicUrl('http://www.example.com/topic2');
  238. $this->_publisher->notifyAll();
  239. $this->assertEquals('hub.mode=publish&hub.url=http%3A%2F%2Fwww.example.com%2Ftopic&hub.url=http%3A%2F%2Fwww.example.com%2Ftopic2', $client->getBody());
  240. }
  241. public function testNotifiesHubAndReportsSuccess()
  242. {
  243. Zend_Feed_Pubsubhubbub::setHttpClient(new Zend_Feed_Pubsubhubbub_PublisherTest_ClientSuccess);
  244. $client = Zend_Feed_Pubsubhubbub::getHttpClient();
  245. $this->_publisher->addHubUrl('http://www.example.com/hub');
  246. $this->_publisher->addUpdatedTopicUrl('http://www.example.com/topic');
  247. $this->_publisher->setParameter('foo', 'bar');
  248. $this->_publisher->notifyAll();
  249. $this->assertTrue($this->_publisher->isSuccess());
  250. }
  251. public function testNotifiesHubAndReportsFail()
  252. {
  253. Zend_Feed_Pubsubhubbub::setHttpClient(new Zend_Feed_Pubsubhubbub_PublisherTest_ClientFail);
  254. $client = Zend_Feed_Pubsubhubbub::getHttpClient();
  255. $this->_publisher->addHubUrl('http://www.example.com/hub');
  256. $this->_publisher->addUpdatedTopicUrl('http://www.example.com/topic');
  257. $this->_publisher->setParameter('foo', 'bar');
  258. $this->_publisher->notifyAll();
  259. $this->assertFalse($this->_publisher->isSuccess());
  260. }
  261. }
  262. // Some stubs for what Http_Client would be doing
  263. class Zend_Feed_Pubsubhubbub_PublisherTest_ClientSuccess extends Zend_Http_Client
  264. {
  265. public function request($method = null) {
  266. $response = new Zend_Feed_Pubsubhubbub_PublisherTest_ResponseSuccess;
  267. return $response;
  268. }
  269. public function getBody(){return $this->_prepareBody();}
  270. }
  271. class Zend_Feed_Pubsubhubbub_PublisherTest_ClientFail extends Zend_Http_Client
  272. {
  273. public function request($method = null) {
  274. $response = new Zend_Feed_Pubsubhubbub_PublisherTest_ResponseFail;
  275. return $response;
  276. }
  277. public function getBody(){return $this->_prepareBody();}
  278. }
  279. class Zend_Feed_Pubsubhubbub_PublisherTest_ResponseSuccess
  280. {
  281. public function getStatus(){return 204;}
  282. }
  283. class Zend_Feed_Pubsubhubbub_PublisherTest_ResponseFail
  284. {
  285. public function getStatus(){return 404;}
  286. }