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.
16 * @package Zend_Loader
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
23 if (!defined('PHPUnit_MAIN_METHOD')) {
24 define('PHPUnit_MAIN_METHOD', 'Zend_Loader_AutoloaderMultiVersionTest::main');
30 require_once dirname(__FILE__
) . '/../../TestHelper.php';
33 * @see Zend_Loader_Autoloader
35 require_once 'Zend/Loader/Autoloader.php';
39 * @package Zend_Loader
40 * @subpackage UnitTests
41 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
42 * @license http://framework.zend.com/license/new-bsd New BSD License
45 class Zend_Loader_AutoloaderMultiVersionTest
extends PHPUnit_Framework_TestCase
47 public static function main()
49 $suite = new PHPUnit_Framework_TestSuite(__CLASS__
);
50 $result = PHPUnit_TextUI_TestRunner
::run($suite);
53 public function setUp()
55 if (!constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_ENABLED')) {
56 $this->markTestSkipped();
59 // Store original autoloaders
60 $this->loaders
= spl_autoload_functions();
61 if (!is_array($this->loaders
)) {
62 // spl_autoload_functions does not return empty array when no
63 // autoloaders registered...
64 $this->loaders
= array();
67 // Store original include_path
68 $this->includePath
= get_include_path();
70 Zend_Loader_Autoloader
::resetInstance();
71 $this->path
= constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_PATH');
72 $this->latest
= constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST');
73 $this->latestMajor
= constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST_MAJOR');
74 $this->latestMinor
= constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_LATEST_MINOR');
75 $this->specific
= constant('TESTS_ZEND_LOADER_AUTOLOADER_MULTIVERSION_SPECIFIC');
76 $this->autoloader
= Zend_Loader_Autoloader
::getInstance();
79 public function tearDown()
81 // Restore original autoloaders
82 $loaders = spl_autoload_functions();
83 foreach ($loaders as $loader) {
84 spl_autoload_unregister($loader);
87 foreach ($this->loaders
as $loader) {
88 spl_autoload_register($loader);
91 // Retore original include_path
92 set_include_path($this->includePath
);
94 // Reset autoloader instance so it doesn't affect other tests
95 Zend_Loader_Autoloader
::resetInstance();
98 public function testZfPathIsNullByDefault()
100 $this->assertNull($this->autoloader
->getZfPath());
104 * @expectedException Zend_Loader_Exception
106 public function testSettingZfPathFailsOnInvalidVersionString()
108 $this->autoloader
->setZfPath($this->path
, 'foo.bar.baz.bat');
112 * @expectedException Zend_Loader_Exception
114 public function testSettingZfPathFailsWhenBasePathDoesNotExist()
116 $this->autoloader
->setZfPath('foo.bar.baz.bat', 'latest');
120 * @expectedException Zend_Loader_Exception
122 public function testSettingZfVersionFailsWhenNoValidInstallsDiscovered()
124 $this->autoloader
->setZfPath(dirname(__FILE__
), 'latest');
127 public function testAutoloadLatestUsesLatestVersion()
129 $this->autoloader
->setZfPath($this->path
, 'latest');
130 $actual = $this->autoloader
->getZfPath();
131 $this->assertContains($this->latest
, $actual);
134 public function testAutoloadLatestIncludesLibraryInPath()
136 $this->autoloader
->setZfPath($this->path
, 'latest');
137 $actual = $this->autoloader
->getZfPath();
138 $this->assertRegexp('#' . preg_quote($this->latest
) . '[^/\\\]*/library#', $actual);
141 public function testAutoloadLatestAddsPathToIncludePath()
143 $this->autoloader
->setZfPath($this->path
, 'latest');
144 $incPath = get_include_path();
145 $this->assertRegexp('#' . preg_quote($this->latest
) . '[^/\\\]*/library#', $incPath);
148 public function testAutoloadMajorRevisionShouldUseLatestFromMajorRevision()
150 $this->autoloader
->setZfPath($this->path
, $this->_getVersion($this->latestMajor
, 'major'));
151 $actual = $this->autoloader
->getZfPath();
152 $this->assertContains($this->latestMajor
, $actual);
155 public function testAutoloadMajorRevisionIncludesLibraryInPath()
157 $this->autoloader
->setZfPath($this->path
, $this->_getVersion($this->latestMajor
, 'major'));
158 $actual = $this->autoloader
->getZfPath();
159 $this->assertRegexp('#' . preg_quote($this->latestMajor
) . '[^/\\\]*/library#', $actual);
162 public function testAutoloadMajorRevisionAddsPathToIncludePath()
164 $this->autoloader
->setZfPath($this->path
, $this->_getVersion($this->latestMajor
, 'major'));
165 $incPath = get_include_path();
166 $this->assertRegexp('#' . preg_quote($this->latestMajor
) . '[^/\\\]*/library#', $incPath);
169 public function testAutoloadMinorRevisionShouldUseLatestFromMinorRevision()
171 $this->autoloader
->setZfPath($this->path
, $this->_getVersion($this->latestMinor
, 'minor'));
172 $actual = $this->autoloader
->getZfPath();
173 $this->assertContains($this->latestMinor
, $actual);
176 public function testAutoloadMinorRevisionIncludesLibraryInPath()
178 $this->autoloader
->setZfPath($this->path
, $this->_getVersion($this->latestMinor
, 'minor'));
179 $actual = $this->autoloader
->getZfPath();
180 $this->assertRegexp('#' . preg_quote($this->latestMinor
) . '[^/\\\]*/library#', $actual);
183 public function testAutoloadMinorRevisionAddsPathToIncludePath()
185 $this->autoloader
->setZfPath($this->path
, $this->_getVersion($this->latestMinor
, 'minor'));
186 $incPath = get_include_path();
187 $this->assertRegexp('#' . preg_quote($this->latestMinor
) . '[^/\\\]*/library#', $incPath);
190 public function testAutoloadSpecificRevisionShouldUseThatVersion()
192 $this->autoloader
->setZfPath($this->path
, $this->specific
);
193 $actual = $this->autoloader
->getZfPath();
194 $this->assertContains($this->specific
, $actual);
197 public function testAutoloadSpecificRevisionIncludesLibraryInPath()
199 $this->autoloader
->setZfPath($this->path
, $this->specific
);
200 $actual = $this->autoloader
->getZfPath();
201 $this->assertRegexp('#' . preg_quote($this->specific
) . '[^/\\\]*/library#', $actual);
204 public function testAutoloadSpecificRevisionAddsPathToIncludePath()
206 $this->autoloader
->setZfPath($this->path
, $this->specific
);
207 $incPath = get_include_path();
208 $this->assertRegexp('#' . preg_quote($this->specific
) . '[^/\\\]*/library#', $incPath);
211 protected function _getVersion($version, $type)
213 $parts = explode('.', $version);
216 $value = array_shift($parts);
219 $value = array_shift($parts);
220 $value .= '.' . array_shift($parts);
227 if (PHPUnit_MAIN_METHOD
== 'Zend_Loader_AutoloaderMultiVersionTest::main') {
228 Zend_Loader_AutoloaderMultiVersionTest
::main();