SubscriberHttpTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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-2007 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 'PHPUnit/Framework/TestCase.php';
  22. require_once 'Zend/Feed/Pubsubhubbub/Subscriber.php';
  23. require_once 'Zend/Http/Client.php';
  24. require_once 'Zend/Http/Client/Adapter/Socket.php';
  25. require_once 'Zend/Uri/Http.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. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Feed_Pubsubhubbub_SubscriberHttpTest extends PHPUnit_Framework_TestCase
  42. {
  43. protected $_subscriber = null;
  44. protected $_baseuri;
  45. protected $_client = null;
  46. protected $_adapter = null;
  47. protected $_config = array(
  48. 'adapter' => 'Zend_Http_Client_Adapter_Socket'
  49. );
  50. public function setUp()
  51. {
  52. if (defined('TESTS_Zend_Feed_Pubsubhubbub_BASEURI') &&
  53. Zend_Uri_Http::check(TESTS_Zend_Feed_Pubsubhubbub_BASEURI)) {
  54. $this->_baseuri = TESTS_Zend_Feed_Pubsubhubbub_BASEURI;
  55. if (substr($this->_baseuri, -1) != '/') $this->_baseuri .= '/';
  56. $name = $this->getName();
  57. if (($pos = strpos($name, ' ')) !== false) {
  58. $name = substr($name, 0, $pos);
  59. }
  60. $uri = $this->_baseuri . $name . '.php';
  61. $this->_adapter = new $this->_config['adapter'];
  62. $this->_client = new Zend_Http_Client($uri, $this->_config);
  63. $this->_client->setAdapter($this->_adapter);
  64. Zend_Feed_Pubsubhubbub::setHttpClient($this->_client);
  65. $this->_subscriber = new Zend_Feed_Pubsubhubbub_Subscriber;
  66. $this->_storage = $this->_getCleanMock('Zend_Feed_Pubsubhubbub_Entity_TopicSubscription');
  67. $this->_subscriber->setStorage($this->_storage);
  68. } else {
  69. // Skip tests
  70. $this->markTestSkipped("Zend_Feed_Pubsubhubbub_Subscriber dynamic tests'
  71. . ' are not enabled in TestConfiguration.php");
  72. }
  73. }
  74. public function testSubscriptionRequestSendsExpectedPostData()
  75. {
  76. $this->_subscriber->setTopicUrl('http://www.example.com/topic');
  77. $this->_subscriber->addHubUrl($this->_baseuri . '/testRawPostData.php');
  78. $this->_subscriber->setCallbackUrl('http://www.example.com/callback');
  79. $this->_subscriber->setTestStaticToken('abc'); // override for testing
  80. $this->_subscriber->subscribeAll();
  81. $this->assertEquals(
  82. 'hub.callback=http%3A%2F%2Fwww.example.com%2Fcallback%3Fxhub.subscription%3D5536df06b5d'
  83. .'cb966edab3a4c4d56213c16a8184b&hub.lease_seconds=2592000&hub.mode='
  84. .'subscribe&hub.topic=http%3A%2F%2Fwww.example.com%2Ftopic&hub.veri'
  85. .'fy=sync&hub.verify=async&hub.verify_token=abc',
  86. $this->_client->getLastResponse()->getBody());
  87. }
  88. public function testUnsubscriptionRequestSendsExpectedPostData()
  89. {
  90. $this->_subscriber->setTopicUrl('http://www.example.com/topic');
  91. $this->_subscriber->addHubUrl($this->_baseuri . '/testRawPostData.php');
  92. $this->_subscriber->setCallbackUrl('http://www.example.com/callback');
  93. $this->_subscriber->setTestStaticToken('abc'); //override for testing
  94. $this->_subscriber->unsubscribeAll();
  95. $this->assertEquals(
  96. 'hub.callback=http%3A%2F%2Fwww.example.com%2Fcallback%3Fxhub.subscription%3D5536df06b5d'
  97. .'cb966edab3a4c4d56213c16a8184b&hub.mode=unsubscribe&hub.topic=http'
  98. .'%3A%2F%2Fwww.example.com%2Ftopic&hub.verify=sync&hub.verify=async'
  99. .'&hub.verify_token=abc',
  100. $this->_client->getLastResponse()->getBody());
  101. }
  102. protected function _getCleanMock($className) {
  103. $class = new ReflectionClass($className);
  104. $methods = $class->getMethods();
  105. $stubMethods = array();
  106. foreach ($methods as $method) {
  107. if ($method->isPublic() || ($method->isProtected()
  108. && $method->isAbstract())) {
  109. $stubMethods[] = $method->getName();
  110. }
  111. }
  112. $mocked = $this->getMock(
  113. $className,
  114. $stubMethods,
  115. array(),
  116. $className . '_SubscriberHttpTestMock_' . uniqid(),
  117. false
  118. );
  119. return $mocked;
  120. }
  121. }