2
0

AutoloaderMultiVersionTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_Loader
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 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_Loader_AutoloaderMultiVersionTest::main');
  24. }
  25. /**
  26. * Test helper
  27. */
  28. require_once dirname(__FILE__) . '/../../TestHelper.php';
  29. /**
  30. * @see Zend_Loader_Autoloader
  31. */
  32. require_once 'Zend/Loader/Autoloader.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Loader
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_Loader
  40. */
  41. class Zend_Loader_AutoloaderMultiVersionTest extends PHPUnit_Framework_TestCase
  42. {
  43. public static function main()
  44. {
  45. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. public function setUp()
  49. {
  50. if (!constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_ENABLED')) {
  51. $this->markTestSkipped();
  52. }
  53. // Store original autoloaders
  54. $this->loaders = spl_autoload_functions();
  55. if (!is_array($this->loaders)) {
  56. // spl_autoload_functions does not return empty array when no
  57. // autoloaders registered...
  58. $this->loaders = array();
  59. }
  60. // Store original include_path
  61. $this->includePath = get_include_path();
  62. Zend_Loader_Autoloader::resetInstance();
  63. $this->path = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_PATH');
  64. $this->latest = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST');
  65. $this->latestMajor = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST_MAJOR');
  66. $this->latestMinor = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST_MINOR');
  67. $this->specific = constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_SPECIFIC');
  68. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  69. }
  70. public function tearDown()
  71. {
  72. // Restore original autoloaders
  73. $loaders = spl_autoload_functions();
  74. foreach ($loaders as $loader) {
  75. spl_autoload_unregister($loader);
  76. }
  77. foreach ($this->loaders as $loader) {
  78. spl_autoload_register($loader);
  79. }
  80. // Retore original include_path
  81. set_include_path($this->includePath);
  82. // Reset autoloader instance so it doesn't affect other tests
  83. Zend_Loader_Autoloader::resetInstance();
  84. }
  85. public function testZfPathIsNullByDefault()
  86. {
  87. $this->assertNull($this->autoloader->getZfPath());
  88. }
  89. /**
  90. * @expectedException Zend_Loader_Exception
  91. */
  92. public function testSettingZfPathFailsOnInvalidVersionString()
  93. {
  94. $this->autoloader->setZfPath($this->path, 'foo.bar.baz.bat');
  95. }
  96. /**
  97. * @expectedException Zend_Loader_Exception
  98. */
  99. public function testSettingZfPathFailsWhenBasePathDoesNotExist()
  100. {
  101. $this->autoloader->setZfPath('foo.bar.baz.bat', 'latest');
  102. }
  103. /**
  104. * @expectedException Zend_Loader_Exception
  105. */
  106. public function testSettingZfVersionFailsWhenNoValidInstallsDiscovered()
  107. {
  108. $this->autoloader->setZfPath(dirname(__FILE__), 'latest');
  109. }
  110. public function testAutoloadLatestUsesLatestVersion()
  111. {
  112. $this->autoloader->setZfPath($this->path, 'latest');
  113. $actual = $this->autoloader->getZfPath();
  114. $this->assertContains($this->latest, $actual);
  115. }
  116. public function testAutoloadLatestIncludesLibraryInPath()
  117. {
  118. $this->autoloader->setZfPath($this->path, 'latest');
  119. $actual = $this->autoloader->getZfPath();
  120. $this->assertRegexp('#' . preg_quote($this->latest) . '[^/\\\]*/library#', $actual);
  121. }
  122. public function testAutoloadLatestAddsPathToIncludePath()
  123. {
  124. $this->autoloader->setZfPath($this->path, 'latest');
  125. $incPath = get_include_path();
  126. $this->assertRegexp('#' . preg_quote($this->latest) . '[^/\\\]*/library#', $incPath);
  127. }
  128. public function testAutoloadMajorRevisionShouldUseLatestFromMajorRevision()
  129. {
  130. $this->autoloader->setZfPath($this->path, $this->_getVersion($this->latestMajor, 'major'));
  131. $actual = $this->autoloader->getZfPath();
  132. $this->assertContains($this->latestMajor, $actual);
  133. }
  134. public function testAutoloadMajorRevisionIncludesLibraryInPath()
  135. {
  136. $this->autoloader->setZfPath($this->path, $this->_getVersion($this->latestMajor, 'major'));
  137. $actual = $this->autoloader->getZfPath();
  138. $this->assertRegexp('#' . preg_quote($this->latestMajor) . '[^/\\\]*/library#', $actual);
  139. }
  140. public function testAutoloadMajorRevisionAddsPathToIncludePath()
  141. {
  142. $this->autoloader->setZfPath($this->path, $this->_getVersion($this->latestMajor, 'major'));
  143. $incPath = get_include_path();
  144. $this->assertRegexp('#' . preg_quote($this->latestMajor) . '[^/\\\]*/library#', $incPath);
  145. }
  146. public function testAutoloadMinorRevisionShouldUseLatestFromMinorRevision()
  147. {
  148. $this->autoloader->setZfPath($this->path, $this->_getVersion($this->latestMinor, 'minor'));
  149. $actual = $this->autoloader->getZfPath();
  150. $this->assertContains($this->latestMinor, $actual);
  151. }
  152. public function testAutoloadMinorRevisionIncludesLibraryInPath()
  153. {
  154. $this->autoloader->setZfPath($this->path, $this->_getVersion($this->latestMinor, 'minor'));
  155. $actual = $this->autoloader->getZfPath();
  156. $this->assertRegexp('#' . preg_quote($this->latestMinor) . '[^/\\\]*/library#', $actual);
  157. }
  158. public function testAutoloadMinorRevisionAddsPathToIncludePath()
  159. {
  160. $this->autoloader->setZfPath($this->path, $this->_getVersion($this->latestMinor, 'minor'));
  161. $incPath = get_include_path();
  162. $this->assertRegexp('#' . preg_quote($this->latestMinor) . '[^/\\\]*/library#', $incPath);
  163. }
  164. public function testAutoloadSpecificRevisionShouldUseThatVersion()
  165. {
  166. $this->autoloader->setZfPath($this->path, $this->specific);
  167. $actual = $this->autoloader->getZfPath();
  168. $this->assertContains($this->specific, $actual);
  169. }
  170. public function testAutoloadSpecificRevisionIncludesLibraryInPath()
  171. {
  172. $this->autoloader->setZfPath($this->path, $this->specific);
  173. $actual = $this->autoloader->getZfPath();
  174. $this->assertRegexp('#' . preg_quote($this->specific) . '[^/\\\]*/library#', $actual);
  175. }
  176. public function testAutoloadSpecificRevisionAddsPathToIncludePath()
  177. {
  178. $this->autoloader->setZfPath($this->path, $this->specific);
  179. $incPath = get_include_path();
  180. $this->assertRegexp('#' . preg_quote($this->specific) . '[^/\\\]*/library#', $incPath);
  181. }
  182. protected function _getVersion($version, $type)
  183. {
  184. $parts = explode('.', $version);
  185. switch ($type) {
  186. case 'major':
  187. $value = array_shift($parts);
  188. break;
  189. case 'minor':
  190. $value = array_shift($parts);
  191. $value .= '.' . array_shift($parts);
  192. break;
  193. }
  194. return $value;
  195. }
  196. }
  197. if (PHPUnit_MAIN_METHOD == 'Zend_Loader_AutoloaderMultiVersionTest::main') {
  198. Zend_Loader_AutoloaderMultiVersionTest::main();
  199. }