PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Feed/PubSubHubbub/SubscriberHttpTest.php

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