UriTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_Uri
  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. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_UriTest::main');
  24. }
  25. /**
  26. * Zend_Uri
  27. */
  28. require_once 'Zend/Uri.php';
  29. /**
  30. * Zend_Config
  31. */
  32. require_once 'Zend/Config.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Uri
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_Uri
  40. */
  41. class Zend_UriTest extends PHPUnit_Framework_TestCase
  42. {
  43. public static function main()
  44. {
  45. $suite = new PHPUnit_Framework_TestSuite("Zend_UriTest");
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. public function setUp()
  49. {
  50. $this->notices = array();
  51. $this->errorReporting = error_reporting();
  52. $this->displayErrors = ini_get('display_errors');
  53. }
  54. public function tearDown()
  55. {
  56. error_reporting($this->errorReporting);
  57. ini_set('display_errors', $this->displayErrors);
  58. }
  59. public function testSchemeEmpty()
  60. {
  61. $this->_testInvalidUri('', '/empty/i');
  62. $this->_testInvalidUri('://www.zend.com', '/empty/i');
  63. }
  64. public function testSchemeUnsupported()
  65. {
  66. $this->_testInvalidUri('unsupported', '/unsupported/i');
  67. $this->_testInvalidUri('unsupported://zend.com', '/unsupported/i');
  68. }
  69. public function testSchemeIllegal()
  70. {
  71. $this->_testInvalidUri('!@#$%^&*()', '/illegal/i');
  72. }
  73. public function testSchemeHttp()
  74. {
  75. $this->_testValidUri('http');
  76. }
  77. public function testSchemeHttps()
  78. {
  79. $this->_testValidUri('https');
  80. }
  81. public function testSchemeMailto()
  82. {
  83. $this->markTestIncomplete('Zend_Uri_Mailto is not implemented yet');
  84. $this->_testValidUri('mailto');
  85. }
  86. /**
  87. * Tests that Zend_Uri::setConfig() allows Zend_Config
  88. *
  89. * @group ZF-5578
  90. */
  91. public function testSetConfigWithArray()
  92. {
  93. Zend_Uri::setConfig(array('allow_unwise' => true));
  94. }
  95. /**
  96. * Tests that Zend_Uri::setConfig() allows Array
  97. *
  98. * @group ZF-5578
  99. */
  100. public function testSetConfigWithZendConfig()
  101. {
  102. Zend_Uri::setConfig(new Zend_Config(array('allow_unwise' => true)));
  103. }
  104. /**
  105. * Tests that Zend_Uri::setConfig() throws Zend_Uri_Exception if no array
  106. * nor Zend_Config is given as first parameter
  107. *
  108. * @group ZF-5578
  109. * @expectedException Zend_Uri_Exception
  110. */
  111. public function testSetConfigInvalid()
  112. {
  113. Zend_Uri::setConfig('This should cause an exception');
  114. }
  115. /**
  116. * Tests that if an exception is thrown when calling the __toString()
  117. * method an empty string is returned and a Warning is triggered, instead
  118. * of a Fatal Error being triggered.
  119. *
  120. * @group ZF-10405
  121. */
  122. public function testToStringRaisesWarningWhenExceptionCaught()
  123. {
  124. $uri = Zend_Uri::factory('http://example.com', 'Zend_Uri_ExceptionCausing');
  125. set_error_handler(array($this, 'handleErrors'), E_USER_WARNING);
  126. $text = sprintf('%s', $uri);
  127. restore_error_handler();
  128. $this->assertTrue(empty($text));
  129. $this->assertTrue(isset($this->error));
  130. $this->assertContains('Exception in getUri()', $this->error);
  131. }
  132. /**
  133. * Error handler for testExceptionThrownInToString()
  134. *
  135. * @group ZF-10405
  136. */
  137. public function handleErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
  138. {
  139. $this->error = $errstr;
  140. }
  141. /**
  142. * Tests that an invalid $uri throws an exception and that the
  143. * message of that exception matches $regex.
  144. *
  145. * @param string $uri
  146. * @param string $regex
  147. */
  148. protected function _testInvalidUri($uri, $regex)
  149. {
  150. $e = null;
  151. try {
  152. $uri = Zend_Uri::factory($uri);
  153. } catch (Zend_Uri_Exception $e) {
  154. $this->assertRegExp($regex, $e->getMessage());
  155. return;
  156. }
  157. $this->fail('Zend_Uri_Exception was expected but not thrown');
  158. }
  159. /**
  160. * Tests that a valid $uri returns a Zend_Uri object.
  161. *
  162. * @param string $uri
  163. */
  164. protected function _testValidUri($uri, $className = null)
  165. {
  166. $uri = Zend_Uri::factory($uri, $className);
  167. $this->assertTrue($uri instanceof Zend_Uri, 'Zend_Uri object not returned.');
  168. return $uri;
  169. }
  170. public function testFactoryWithUnExistingClassThrowException()
  171. {
  172. $this->setExpectedException('Zend_Uri_Exception', '"This_Is_An_Unknown_Class" not found');
  173. Zend_Uri::factory('http://example.net', 'This_Is_An_Unknown_Class');
  174. }
  175. public function testFactoryWithExistingClassButNotImplementingZendUriThrowException()
  176. {
  177. $this->setExpectedException('Zend_Uri_Exception', '"Fake_Zend_Uri" is not an instance of Zend_Uri');
  178. Zend_Uri::factory('http://example.net', 'Fake_Zend_Uri');
  179. }
  180. public function testFactoryWithExistingClassReturnObject()
  181. {
  182. $uri = $this->_testValidUri('http://example.net', 'Zend_Uri_Mock');
  183. $this->assertTrue($uri instanceof Zend_Uri_Mock, 'Zend_Uri_Mock object not returned.');
  184. }
  185. }
  186. class Zend_Uri_Mock extends Zend_Uri
  187. {
  188. protected function __construct($scheme, $schemeSpecific = '') { }
  189. public function getUri() { }
  190. public function valid() { }
  191. }
  192. class Zend_Uri_ExceptionCausing extends Zend_Uri
  193. {
  194. protected function __construct($scheme, $schemeSpecific = '') { }
  195. public function valid() { }
  196. public function getUri()
  197. {
  198. throw new Exception('Exception in getUri()');
  199. }
  200. }
  201. class Fake_Zend_Uri
  202. {
  203. }
  204. // Call Zend_UriTest::main() if this source file is executed directly.
  205. if (PHPUnit_MAIN_METHOD == "Zend_UriTest::main") {
  206. Zend_UriTest::main();
  207. }