HostnameTest.php 8.5 KB

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