HostnameTest.php 8.4 KB

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