UrlTest.php 6.4 KB

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