2
0

HostnameTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 Zend_Controller
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Test helper */
  23. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  24. /** Zend_Controller_Router_Route_Hostname */
  25. require_once 'Zend/Controller/Router/Route/Hostname.php';
  26. /** Zend_Controller_Request_Http */
  27. require_once 'Zend/Controller/Request/Http.php';
  28. /** PHPUnit test case */
  29. require_once 'PHPUnit/Framework/TestCase.php';
  30. if (!defined('PHPUnit_MAIN_METHOD')) {
  31. define('PHPUnit_MAIN_METHOD', 'Zend_Controller_Router_Route_HostnameTest::main');
  32. }
  33. /**
  34. * @category Zend
  35. * @package Zend_Controller
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_Controller
  40. * @group Zend_Controller_Router
  41. */
  42. class Zend_Controller_Router_Route_HostnameTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Runs the test methods of this class.
  46. *
  47. * @access public
  48. * @static
  49. */
  50. public static function main()
  51. {
  52. require_once "PHPUnit/TextUI/TestRunner.php";
  53. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Router_Route_HostnameTest");
  54. $result = PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. public function testCorrectStaticHostMatch()
  57. {
  58. $route = $this->_getStaticHostRoute();
  59. $values = $route->match($this->_getRequest('www.zend.com'));
  60. $this->assertEquals('ctrl', $values['controller']);
  61. }
  62. public function testHostMatchWithPort()
  63. {
  64. $route = $this->_getStaticHostRoute();
  65. $values = $route->match($this->_getRequest('www.zend.com:666'));
  66. $this->assertEquals('ctrl', $values['controller']);
  67. }
  68. public function testWrongStaticHostMatch()
  69. {
  70. $route = $this->_getStaticHostRoute();
  71. $values = $route->match($this->_getRequest('foo.zend.com'));
  72. $this->assertFalse($values);
  73. }
  74. public function testCorrectHostMatch()
  75. {
  76. $route = $this->_getHostRoute();
  77. $values = $route->match($this->_getRequest('foo.zend.com'));
  78. $this->assertEquals('ctrl', $values['controller']);
  79. }
  80. public function testWrongHostMatch()
  81. {
  82. $route = $this->_getHostRoute();
  83. $values = $route->match($this->_getRequest('www.zend.com'));
  84. $this->assertFalse($values);
  85. }
  86. public function testAssembleStaticHost()
  87. {
  88. $route = $this->_getStaticHostRoute();
  89. $this->assertRegexp('/[^a-z0-9]?www\.zend\.com$/i', $route->assemble());
  90. }
  91. public function testAssembleHost()
  92. {
  93. $route = $this->_getHostRoute();
  94. $this->assertRegexp('/[^a-z0-9]?foo\.zend\.com$/i', $route->assemble(array('subdomain' => 'foo')));
  95. }
  96. public function testAssembleHostWithMissingParam()
  97. {
  98. $route = $this->_getHostRoute();
  99. try {
  100. $route->assemble();
  101. $this->fail('An expected Zend_Controller_Router_Exception has not been raised');
  102. } catch (Zend_Controller_Router_Exception $expected) {
  103. $this->assertContains('subdomain is not specified', $expected->getMessage());
  104. }
  105. }
  106. public function testAssembleHostWithDefaultParam()
  107. {
  108. $route = $this->_getHostRouteWithDefault();
  109. $this->assertRegexp('/[^a-z0-9]?bar\.zend\.com$/i', $route->assemble());
  110. }
  111. public function testHostGetDefault()
  112. {
  113. $route = $this->_getHostRouteWithDefault();
  114. $this->assertEquals('bar', $route->getDefault('subdomain'));
  115. }
  116. public function testHostGetNonExistentDefault()
  117. {
  118. $route = $this->_getHostRouteWithDefault();
  119. $this->assertEquals(null, $route->getDefault('blah'));
  120. }
  121. public function testHostGetDefaults()
  122. {
  123. $route = $this->_getHostRouteWithDefault();
  124. $defaults = $route->getDefaults();
  125. $this->assertEquals('bar', $defaults['subdomain']);
  126. }
  127. public function testRouteWithHostname()
  128. {
  129. $request = new Zend_Controller_Router_RewriteTest_Request_Stub('www.zend.com');
  130. $route = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('controller' => 'host-foo', 'action' => 'host-bar'));
  131. $values = $route->match($request);
  132. $this->assertEquals('host-foo', $values['controller']);
  133. $this->assertEquals('host-bar', $values['action']);
  134. }
  135. public function testSchemeMatch()
  136. {
  137. $request = new Zend_Controller_Router_RewriteTest_Request_Stub('www.zend.com', 'https');
  138. $route = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('controller' => 'host-foo', 'action' => 'host-bar'), array(), 'https');
  139. $values = $route->match($request);
  140. $this->assertEquals('host-foo', $values['controller']);
  141. $this->assertEquals('host-bar', $values['action']);
  142. }
  143. public function testSchemeNoMatch()
  144. {
  145. $request = new Zend_Controller_Router_RewriteTest_Request_Stub('www.zend.com', 'http');
  146. $route = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('controller' => 'host-foo', 'action' => 'host-bar'), array(), 'https');
  147. $values = $route->match($request);
  148. $this->assertFalse($values);
  149. }
  150. public function testAutomaticSchemeAssembling()
  151. {
  152. $route = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('controller' => 'host-foo', 'action' => 'host-bar'), array());
  153. $url = $route->assemble();
  154. $this->assertEquals('http://www.zend.com', $url);
  155. }
  156. public function testForcedSchemeAssembling()
  157. {
  158. $request = new Zend_Controller_Router_RewriteTest_Request_Stub('www.zend.com');
  159. $route = new Zend_Controller_Router_Route_Hostname('www.zend.com', array('controller' => 'host-foo', 'action' => 'host-bar'), array(), 'https');
  160. $route->setRequest($request);
  161. $url = $route->assemble();
  162. $this->assertEquals('https://www.zend.com', $url);
  163. }
  164. protected function _getStaticHostRoute()
  165. {
  166. $route = new Zend_Controller_Router_Route_Hostname('www.zend.com',
  167. array('controller' => 'ctrl',
  168. 'action' => 'act'));
  169. return $route;
  170. }
  171. protected function _getHostRoute()
  172. {
  173. $route = new Zend_Controller_Router_Route_Hostname(':subdomain.zend.com',
  174. array('controller' => 'ctrl',
  175. 'action' => 'act'),
  176. array('subdomain' => '(foo|bar)'));
  177. return $route;
  178. }
  179. protected function _getHostRouteWithDefault()
  180. {
  181. $route = new Zend_Controller_Router_Route_Hostname(':subdomain.zend.com',
  182. array('controller' => 'ctrl',
  183. 'action' => 'act',
  184. 'subdomain' => 'bar'),
  185. array('subdomain' => '(foo|bar)'));
  186. return $route;
  187. }
  188. protected function _getRequest($host) {
  189. return new Zend_Controller_Router_RewriteTest_Request_Stub($host);
  190. }
  191. }
  192. /**
  193. * Zend_Controller_RouterTest_Request_Stub - request object for route testing
  194. */
  195. class Zend_Controller_Router_RewriteTest_Request_Stub extends Zend_Controller_Request_Abstract
  196. {
  197. protected $_host;
  198. protected $_scheme;
  199. public function __construct($host, $scheme = 'http') {
  200. $this->_host = $host;
  201. $this->_scheme = $scheme;
  202. }
  203. public function getHttpHost() {
  204. return $this->_host;
  205. }
  206. public function getScheme() {
  207. return $this->_scheme;
  208. }
  209. }
  210. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Router_Route_HostnameTest::main") {
  211. Zend_Controller_Router_Route_HostnameTest::main();
  212. }