PublisherTest.php 11 KB

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