PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Feed/PubSubHubbub/PublisherTest.php

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