SubscriberHttpTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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-2014 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/Subscriber.php';
  22. require_once 'Zend/Http/Client.php';
  23. require_once 'Zend/Http/Client/Adapter/Socket.php';
  24. require_once 'Zend/Uri/Http.php';
  25. require_once 'Zend/Feed/Pubsubhubbub/Entity/TopicSubscription.php';
  26. /**
  27. * Note that $this->_baseuri must point to a directory on a web server
  28. * containing all the files under the _files directory. You should symlink
  29. * or copy these files and set '_baseuri' properly using the constant in
  30. * TestConfiguration.php (based on TestConfiguration.php.dist)
  31. *
  32. * You can also set the proper constant in your test configuration file to
  33. * point to the right place.
  34. *
  35. * @category Zend
  36. * @package Zend_Feed
  37. * @subpackage UnitTests
  38. * @group Zend_Feed
  39. * @group Zend_Feed_Subsubhubbub
  40. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_Feed_Pubsubhubbub_SubscriberHttpTest extends PHPUnit_Framework_TestCase
  44. {
  45. protected $_subscriber = null;
  46. protected $_baseuri;
  47. protected $_client = null;
  48. protected $_adapter = null;
  49. protected $_config = array(
  50. 'adapter' => 'Zend_Http_Client_Adapter_Socket'
  51. );
  52. public function setUp()
  53. {
  54. if (defined('TESTS_Zend_Feed_Pubsubhubbub_BASEURI') &&
  55. Zend_Uri_Http::check(TESTS_Zend_Feed_Pubsubhubbub_BASEURI)) {
  56. $this->_baseuri = TESTS_Zend_Feed_Pubsubhubbub_BASEURI;
  57. if (substr($this->_baseuri, -1) != '/') $this->_baseuri .= '/';
  58. $name = $this->getName();
  59. if (($pos = strpos($name, ' ')) !== false) {
  60. $name = substr($name, 0, $pos);
  61. }
  62. $uri = $this->_baseuri . $name . '.php';
  63. $this->_adapter = new $this->_config['adapter'];
  64. $this->_client = new Zend_Http_Client($uri, $this->_config);
  65. $this->_client->setAdapter($this->_adapter);
  66. Zend_Feed_Pubsubhubbub::setHttpClient($this->_client);
  67. $this->_subscriber = new Zend_Feed_Pubsubhubbub_Subscriber;
  68. $this->_storage = $this->_getCleanMock('Zend_Feed_Pubsubhubbub_Entity_TopicSubscription');
  69. $this->_subscriber->setStorage($this->_storage);
  70. } else {
  71. // Skip tests
  72. $this->markTestSkipped("Zend_Feed_Pubsubhubbub_Subscriber dynamic tests'
  73. . ' are not enabled in TestConfiguration.php");
  74. }
  75. }
  76. public function testSubscriptionRequestSendsExpectedPostData()
  77. {
  78. $this->_subscriber->setTopicUrl('http://www.example.com/topic');
  79. $this->_subscriber->addHubUrl($this->_baseuri . '/testRawPostData.php');
  80. $this->_subscriber->setCallbackUrl('http://www.example.com/callback');
  81. $this->_subscriber->setTestStaticToken('abc'); // override for testing
  82. $this->_subscriber->subscribeAll();
  83. $this->assertEquals(
  84. 'hub.callback=http%3A%2F%2Fwww.example.com%2Fcallback%3Fxhub.subscription%3D5536df06b5d'
  85. .'cb966edab3a4c4d56213c16a8184b&hub.lease_seconds=2592000&hub.mode='
  86. .'subscribe&hub.topic=http%3A%2F%2Fwww.example.com%2Ftopic&hub.veri'
  87. .'fy=sync&hub.verify=async&hub.verify_token=abc',
  88. $this->_client->getLastResponse()->getBody());
  89. }
  90. public function testUnsubscriptionRequestSendsExpectedPostData()
  91. {
  92. $this->_subscriber->setTopicUrl('http://www.example.com/topic');
  93. $this->_subscriber->addHubUrl($this->_baseuri . '/testRawPostData.php');
  94. $this->_subscriber->setCallbackUrl('http://www.example.com/callback');
  95. $this->_subscriber->setTestStaticToken('abc'); //override for testing
  96. $this->_subscriber->unsubscribeAll();
  97. $this->assertEquals(
  98. 'hub.callback=http%3A%2F%2Fwww.example.com%2Fcallback%3Fxhub.subscription%3D5536df06b5d'
  99. .'cb966edab3a4c4d56213c16a8184b&hub.mode=unsubscribe&hub.topic=http'
  100. .'%3A%2F%2Fwww.example.com%2Ftopic&hub.verify=sync&hub.verify=async'
  101. .'&hub.verify_token=abc',
  102. $this->_client->getLastResponse()->getBody());
  103. }
  104. protected function _getCleanMock($className) {
  105. $class = new ReflectionClass($className);
  106. $methods = $class->getMethods();
  107. $stubMethods = array();
  108. foreach ($methods as $method) {
  109. if ($method->isPublic() || ($method->isProtected()
  110. && $method->isAbstract())) {
  111. $stubMethods[] = $method->getName();
  112. }
  113. }
  114. $mocked = $this->getMock(
  115. $className,
  116. $stubMethods,
  117. array(),
  118. $className . '_SubscriberHttpTestMock_' . uniqid(),
  119. false
  120. );
  121. return $mocked;
  122. }
  123. }