BaseUrlTest.php 6.0 KB

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