UrlTest.php 6.6 KB

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