2
0

UrlTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. // Call Zend_Controller_Action_Helper_UrlTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_UrlTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  7. require_once "PHPUnit/Framework/TestCase.php";
  8. require_once "PHPUnit/Framework/TestSuite.php";
  9. require_once 'Zend/Controller/Action/Helper/Url.php';
  10. require_once 'Zend/Controller/Front.php';
  11. require_once 'Zend/Controller/Request/Http.php';
  12. /**
  13. * Test class for Zend_Controller_Action_Helper_Url.
  14. */
  15. class Zend_Controller_Action_Helper_UrlTest extends PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * Runs the test methods of this class.
  19. *
  20. * @return void
  21. */
  22. public static function main()
  23. {
  24. require_once "PHPUnit/TextUI/TestRunner.php";
  25. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_UrlTest");
  26. $result = PHPUnit_TextUI_TestRunner::run($suite);
  27. }
  28. /**
  29. * Sets up the fixture, for example, open a network connection.
  30. * This method is called before a test is executed.
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. $this->front = Zend_Controller_Front::getInstance();
  37. $this->front->resetInstance();
  38. $this->front->setRequest(new Zend_Controller_Request_Http());
  39. $this->helper = new Zend_Controller_Action_Helper_Url();
  40. }
  41. /**
  42. * Tears down the fixture, for example, close a network connection.
  43. * This method is called after a test is executed.
  44. *
  45. * @return void
  46. */
  47. public function tearDown()
  48. {
  49. unset($this->helper);
  50. }
  51. public function testSimpleWithAllParamsProducesAppropriateUrl()
  52. {
  53. $url = $this->helper->simple('baz', 'bar', 'foo', array('bat' => 'foo', 'ho' => 'hum'));
  54. $this->assertEquals('/foo/bar/baz', substr($url, 0, 12));
  55. $this->assertContains('/bat/foo', $url);
  56. $this->assertContains('/ho/hum', $url);
  57. }
  58. public function testSimpleWithMissingControllerAndModuleProducesAppropriateUrl()
  59. {
  60. $request = $this->front->getRequest();
  61. $request->setModuleName('foo')
  62. ->setControllerName('bar');
  63. $url = $this->helper->simple('baz', null, null, array('bat' => 'foo', 'ho' => 'hum'));
  64. $this->assertEquals('/foo/bar/baz', substr($url, 0, 12));
  65. $this->assertContains('/bat/foo', $url);
  66. $this->assertContains('/ho/hum', $url);
  67. }
  68. public function testSimpleWithDefaultModuleProducesUrlWithoutModuleSegment()
  69. {
  70. $url = $this->helper->simple('baz', 'bar', 'default', array('bat' => 'foo', 'ho' => 'hum'));
  71. $this->assertEquals('/bar/baz', substr($url, 0, 8));
  72. }
  73. public function testUrlMethodCreatesUrlBasedOnNamedRouteAndPassedParameters()
  74. {
  75. $router = $this->front->getRouter();
  76. $route = new Zend_Controller_Router_Route(
  77. 'foo/:action/:page',
  78. array(
  79. 'module' => 'default',
  80. 'controller' => 'foobar',
  81. 'action' => 'bazbat',
  82. 'page' => 1
  83. )
  84. );
  85. $router->addRoute('foo', $route);
  86. $url = $this->helper->url(array('action' => 'bar', 'page' => 3), 'foo');
  87. $this->assertEquals('/foo/bar/3', $url);
  88. }
  89. public function testUrlMethodCreatesUrlBasedOnNamedRouteAndDefaultParameters()
  90. {
  91. $router = $this->front->getRouter();
  92. $route = new Zend_Controller_Router_Route(
  93. 'foo/:action/:page',
  94. array(
  95. 'module' => 'default',
  96. 'controller' => 'foobar',
  97. 'action' => 'bazbat',
  98. 'page' => 1
  99. )
  100. );
  101. $router->addRoute('foo', $route);
  102. $url = $this->helper->url(array(), 'foo');
  103. $this->assertEquals('/foo', $url);
  104. }
  105. public function testUrlMethodCreatesUrlBasedOnPassedParametersUsingDefaultRouteWhenNoNamedRoutePassed()
  106. {
  107. $this->front->getRouter()->addDefaultRoutes();
  108. $this->front->addModuleDirectory(dirname(__FILE__) . '/../../_files/modules');
  109. $url = $this->helper->url(array(
  110. 'module' => 'foo',
  111. 'controller' => 'bar',
  112. 'action' => 'baz',
  113. 'bat' => 'foo',
  114. 'ho' => 'hum'
  115. ));
  116. $this->assertEquals('/foo/bar/baz', substr($url, 0, 12));
  117. $this->assertContains('/bat/foo', $url);
  118. $this->assertContains('/ho/hum', $url);
  119. }
  120. public function testDirectProxiesToSimple()
  121. {
  122. $url = $this->helper->direct('baz', 'bar', 'foo', array('bat' => 'foo', 'ho' => 'hum'));
  123. $this->assertEquals('/foo/bar/baz', substr($url, 0, 12));
  124. $this->assertContains('/bat/foo', $url);
  125. $this->assertContains('/ho/hum', $url);
  126. }
  127. /**
  128. * @group ZF-2822
  129. */
  130. public function testBaseUrlIsAssembledIntoUrl()
  131. {
  132. $this->front->setBaseUrl('baseurl');
  133. $request = $this->front->getRequest();
  134. $request->setModuleName('module')
  135. ->setControllerName('controller');
  136. $url = $this->helper->simple('action', null, null, array('foo' => 'bar'));
  137. $this->assertEquals('/baseurl/module/controller/action/foo/bar', $url);
  138. }
  139. }
  140. // Call Zend_Controller_Action_Helper_UrlTest::main() if this source file is executed directly.
  141. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_UrlTest::main") {
  142. Zend_Controller_Action_Helper_UrlTest::main();
  143. }