UrlTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // Call Zend_Controller_Action_Helper_UrlTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_UrlTest::main");
  25. }
  26. require_once 'Zend/Controller/Action/Helper/Url.php';
  27. require_once 'Zend/Controller/Front.php';
  28. require_once 'Zend/Controller/Request/Http.php';
  29. /**
  30. * Test class for Zend_Controller_Action_Helper_Url.
  31. *
  32. * @category Zend
  33. * @package Zend_Controller
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2015 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_Action
  39. * @group Zend_Controller_Action_Helper
  40. */
  41. class Zend_Controller_Action_Helper_UrlTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Runs the test methods of this class.
  45. *
  46. * @return void
  47. */
  48. public static function main()
  49. {
  50. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_UrlTest");
  51. $result = PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. /**
  54. * Sets up the fixture, for example, open a network connection.
  55. * This method is called before a test is executed.
  56. *
  57. * @return void
  58. */
  59. public function setUp()
  60. {
  61. $this->front = Zend_Controller_Front::getInstance();
  62. $this->front->resetInstance();
  63. $this->front->setRequest(new Zend_Controller_Request_Http());
  64. $this->helper = new Zend_Controller_Action_Helper_Url();
  65. }
  66. /**
  67. * Tears down the fixture, for example, close a network connection.
  68. * This method is called after a test is executed.
  69. *
  70. * @return void
  71. */
  72. public function tearDown()
  73. {
  74. unset($this->helper);
  75. }
  76. public function testSimpleWithAllParamsProducesAppropriateUrl()
  77. {
  78. $url = $this->helper->simple('baz', 'bar', 'foo', array('bat' => 'foo', 'ho' => 'hum'));
  79. $this->assertEquals('/foo/bar/baz', substr($url, 0, 12));
  80. $this->assertContains('/bat/foo', $url);
  81. $this->assertContains('/ho/hum', $url);
  82. }
  83. public function testSimpleWithMissingControllerAndModuleProducesAppropriateUrl()
  84. {
  85. $request = $this->front->getRequest();
  86. $request->setModuleName('foo')
  87. ->setControllerName('bar');
  88. $url = $this->helper->simple('baz', null, null, array('bat' => 'foo', 'ho' => 'hum'));
  89. $this->assertEquals('/foo/bar/baz', substr($url, 0, 12));
  90. $this->assertContains('/bat/foo', $url);
  91. $this->assertContains('/ho/hum', $url);
  92. }
  93. public function testSimpleWithDefaultModuleProducesUrlWithoutModuleSegment()
  94. {
  95. $url = $this->helper->simple('baz', 'bar', 'default', array('bat' => 'foo', 'ho' => 'hum'));
  96. $this->assertEquals('/bar/baz', substr($url, 0, 8));
  97. }
  98. public function testUrlMethodCreatesUrlBasedOnNamedRouteAndPassedParameters()
  99. {
  100. $router = $this->front->getRouter();
  101. $route = new Zend_Controller_Router_Route(
  102. 'foo/:action/:page',
  103. array(
  104. 'module' => 'default',
  105. 'controller' => 'foobar',
  106. 'action' => 'bazbat',
  107. 'page' => 1
  108. )
  109. );
  110. $router->addRoute('foo', $route);
  111. $url = $this->helper->url(array('action' => 'bar', 'page' => 3), 'foo');
  112. $this->assertEquals('/foo/bar/3', $url);
  113. }
  114. public function testUrlMethodCreatesUrlBasedOnNamedRouteAndDefaultParameters()
  115. {
  116. $router = $this->front->getRouter();
  117. $route = new Zend_Controller_Router_Route(
  118. 'foo/:action/:page',
  119. array(
  120. 'module' => 'default',
  121. 'controller' => 'foobar',
  122. 'action' => 'bazbat',
  123. 'page' => 1
  124. )
  125. );
  126. $router->addRoute('foo', $route);
  127. $url = $this->helper->url(array(), 'foo');
  128. $this->assertEquals('/foo', $url);
  129. }
  130. public function testUrlMethodCreatesUrlBasedOnPassedParametersUsingDefaultRouteWhenNoNamedRoutePassed()
  131. {
  132. $this->front->getRouter()->addDefaultRoutes();
  133. $this->front->addModuleDirectory(dirname(__FILE__) . '/../../_files/modules');
  134. $url = $this->helper->url(array(
  135. 'module' => 'foo',
  136. 'controller' => 'bar',
  137. 'action' => 'baz',
  138. 'bat' => 'foo',
  139. 'ho' => 'hum'
  140. ));
  141. $this->assertEquals('/foo/bar/baz', substr($url, 0, 12));
  142. $this->assertContains('/bat/foo', $url);
  143. $this->assertContains('/ho/hum', $url);
  144. }
  145. public function testDirectProxiesToSimple()
  146. {
  147. $url = $this->helper->direct('baz', 'bar', 'foo', array('bat' => 'foo', 'ho' => 'hum'));
  148. $this->assertEquals('/foo/bar/baz', substr($url, 0, 12));
  149. $this->assertContains('/bat/foo', $url);
  150. $this->assertContains('/ho/hum', $url);
  151. }
  152. /**
  153. * @group ZF-2822
  154. */
  155. public function testBaseUrlIsAssembledIntoUrl()
  156. {
  157. $this->front->setBaseUrl('baseurl');
  158. $request = $this->front->getRequest();
  159. $request->setModuleName('module')
  160. ->setControllerName('controller');
  161. $url = $this->helper->simple('action', null, null, array('foo' => 'bar'));
  162. $this->assertEquals('/baseurl/module/controller/action/foo/bar', $url);
  163. }
  164. }
  165. // Call Zend_Controller_Action_Helper_UrlTest::main() if this source file is executed directly.
  166. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_UrlTest::main") {
  167. Zend_Controller_Action_Helper_UrlTest::main();
  168. }