2
0

BaseUrlTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id:$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. // Call Zend_View_Helper_BaseUrlTest::main() if this source file is executed directly.
  23. if (!defined('PHPUnit_MAIN_METHOD')) {
  24. define('PHPUnit_MAIN_METHOD', 'Zend_View_Helper_BaseUrlTest::main');
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. /**
  28. * @see Zend_View_Helper_BaseUrl
  29. */
  30. require_once 'Zend/View/Helper/BaseUrl.php';
  31. /**
  32. * @see Zend_Controller_Front
  33. */
  34. require_once 'Zend/Controller/Front.php';
  35. /**
  36. * @category Zend
  37. * @package Zend_View
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_View_Helper_BaseUrlTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Previous baseUrl before changing
  46. *
  47. * @var string
  48. */
  49. protected $_previousBaseUrl;
  50. /**
  51. * Server backup
  52. *
  53. * @var array
  54. */
  55. protected $_server;
  56. /**
  57. * Main
  58. */
  59. public static function main()
  60. {
  61. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_BaseUrlTest");
  62. $result = PHPUnit_TextUI_TestRunner::run($suite);
  63. }
  64. /**
  65. * Prepares the environment before running a test.
  66. */
  67. protected function setUp()
  68. {
  69. $this->_previousBaseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
  70. $this->_server = $_SERVER;
  71. }
  72. /**
  73. * Cleans up the environment after running a test.
  74. */
  75. protected function tearDown()
  76. {
  77. Zend_Controller_Front::getInstance()->setBaseUrl($this->_previousBaseUrl);
  78. Zend_Controller_Front::getInstance()->resetInstance();
  79. $_SERVER = $this->_server;
  80. }
  81. /**
  82. * Test and make sure base url returned is consistent with the FC
  83. *
  84. */
  85. public function testBaseUrlIsSameAsFrontController()
  86. {
  87. $baseUrls = array('', '/subdir', '/subdir/', '/sub/sub/dir');
  88. foreach ($baseUrls as $baseUrl) {
  89. Zend_Controller_Front::getInstance()->setBaseUrl($baseUrl);
  90. $helper = new Zend_View_Helper_BaseUrl();
  91. $this->assertEquals(rtrim($baseUrl, '/\\'), $helper->baseUrl());
  92. }
  93. }
  94. /**
  95. * Test and make sure if paths given without / prefix are fixed
  96. *
  97. */
  98. public function testBaseUrlIsCorrectingFilePath()
  99. {
  100. $baseUrls = array(
  101. '' => '/file.js',
  102. '/subdir' => '/subdir/file.js',
  103. '/sub/sub/dir' => '/sub/sub/dir/file.js',
  104. );
  105. foreach ($baseUrls as $baseUrl => $val) {
  106. Zend_Controller_Front::getInstance()->setBaseUrl($baseUrl);
  107. $helper = new Zend_View_Helper_BaseUrl();
  108. $this->assertEquals($val, $helper->baseUrl('file.js'));
  109. }
  110. }
  111. /**
  112. * Test and make sure baseUrl appended with file works
  113. *
  114. */
  115. public function testBaseUrlIsAppendedWithFile()
  116. {
  117. $baseUrls = array(
  118. '' => '/file.js',
  119. '/subdir' => '/subdir/file.js',
  120. '/sub/sub/dir' => '/sub/sub/dir/file.js',
  121. );
  122. foreach ($baseUrls as $baseUrl => $val) {
  123. Zend_Controller_Front::getInstance()->setBaseUrl($baseUrl);
  124. $helper = new Zend_View_Helper_BaseUrl();
  125. $this->assertEquals($val, $helper->baseUrl('/file.js'));
  126. }
  127. }
  128. /**
  129. * Test and makes sure that baseUrl appended with path works
  130. *
  131. */
  132. public function testBaseUrlIsAppendedWithPath()
  133. {
  134. $baseUrls = array(
  135. '' => '/path/bar',
  136. '/subdir' => '/subdir/path/bar',
  137. '/sub/sub/dir' => '/sub/sub/dir/path/bar',
  138. );
  139. foreach ($baseUrls as $baseUrl => $val) {
  140. Zend_Controller_Front::getInstance()->setBaseUrl($baseUrl);
  141. $helper = new Zend_View_Helper_BaseUrl();
  142. $this->assertEquals($val, $helper->baseUrl('/path/bar'));
  143. }
  144. }
  145. /**
  146. * Test and makes sure that baseUrl appended with root path
  147. *
  148. */
  149. public function testBaseUrlIsAppendedWithRootPath()
  150. {
  151. $baseUrls = array(
  152. '' => '/',
  153. '/foo' => '/foo/'
  154. );
  155. foreach ($baseUrls as $baseUrl => $val) {
  156. Zend_Controller_Front::getInstance()->setBaseUrl($baseUrl);
  157. $helper = new Zend_View_Helper_BaseUrl();
  158. $this->assertEquals($val, $helper->baseUrl('/'));
  159. }
  160. }
  161. public function testSetBaseUrlModifiesBaseUrl()
  162. {
  163. $helper = new Zend_View_Helper_BaseUrl();
  164. $helper->setBaseUrl('/myfoo');
  165. $this->assertEquals('/myfoo', $helper->getBaseUrl());
  166. }
  167. public function testGetBaseUrlReturnsBaseUrl()
  168. {
  169. Zend_Controller_Front::getInstance()->setBaseUrl('/mybar');
  170. $helper = new Zend_View_Helper_BaseUrl();
  171. $this->assertEquals('/mybar', $helper->getBaseUrl());
  172. }
  173. public function testGetBaseUrlReturnsBaseUrlWithoutScriptName()
  174. {
  175. $_SERVER['SCRIPT_NAME'] = '/foo/bar/bat/mybar/index.php';
  176. Zend_Controller_Front::getInstance()->setBaseUrl('/mybar/index.php');
  177. $helper = new Zend_View_Helper_BaseUrl();
  178. $this->assertEquals('/mybar', $helper->getBaseUrl());
  179. }
  180. }
  181. // Call Zend_View_Helper_BaseUrlTest::main() if this source file is executed directly.
  182. if (PHPUnit_MAIN_METHOD == 'Zend_View_Helper_BaseUrlTest::main') {
  183. Zend_View_Helper_BaseUrlTest::main();
  184. }