/ZendFramework/tests/Zend/Loader/AutoloaderMultiVersionTest.php

https://bitbucket.org/Dal-Papa/is-340-publish-base · PHP · 224 lines · 148 code · 29 blank · 47 comment · 6 complexity · e710cbfd08afee79e04fe453628ba9be MD5 · raw file

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