BaseUrlTest.php 6.1 KB

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