You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
6.0 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-..
  8. *
  9. * Unit tests for Load class
  10. */
  11. require_once dirname(__FILE__) . '/../Registry.php';
  12. require_once dirname(__FILE__) . '/../Config.php';
  13. require_once dirname(__FILE__) . '/../Load.php';
  14. require_once 'vfsStream/vfsStream.php';
  15. class LoadTest extends PHPUnit_Framework_TestCase
  16. {
  17. private static $inc_dirs = array();
  18. private static $file;
  19. private $root;
  20. public static $file_contents;
  21. public static $autoload_array;
  22. public function run(PHPUnit_Framework_TestResult $result = NULL)
  23. {
  24. $this->setPreserveGlobalState(false);
  25. return parent::run($result);
  26. }
  27. /**
  28. * @TODO: Load->buildAutoload() should recieve AutoloadBuilder as a parameter
  29. */
  30. public function setUp()
  31. {
  32. self::$file_contents = '<?php return array("Db" => "/lib/core/model/Db.php", "DbDriver" => "/lib/core/model/DbDriver.php"); ?>';
  33. self::$autoload_array = array('Db' => '/lib/core/model/Db.php', 'DbDriver' => '/lib/core/model/DbDriver.php');
  34. vfsStreamWrapper::register();
  35. vfsStream::setup();
  36. $this->root = vfsStream::create(
  37. array(
  38. 'lib' => array(
  39. 'core' =>
  40. array(
  41. 'util' => array(
  42. 'AutoloadBuilder.php' => ''
  43. ),
  44. 'model' => array(
  45. 'Db.php' => '',
  46. 'DbDriver.php' => ''
  47. ),
  48. 'Registry.php' => '',
  49. 'Load.php' => '',
  50. 'devel.config' => ' development config file'
  51. )
  52. ),
  53. 'autoload.php' => self::$file_contents
  54. )
  55. );
  56. if (!class_exists('AutoloadBuilder')) {
  57. $this->getMock('AutoloadBuilder');
  58. }
  59. vfsStreamWrapper::setRoot($this->root);
  60. self::$file = vfsStream::url('root/autoload.php');
  61. set_new_overload(array($this, 'newCallback'));
  62. }
  63. /**
  64. * @runInSeparateProcess
  65. */
  66. public function testSetAutoLoadFromExistingFile()
  67. {
  68. $this->setConstants();
  69. $this->assertFileExists(self::$file);
  70. Load::setAutoloadFrom(self::$file);
  71. $autoload = require(self::$file);
  72. $this->assertSame(self::$autoload_array, $autoload);
  73. Load::autoload('Db');
  74. }
  75. /**
  76. * @runInSeparateProcess
  77. */
  78. public function testAutoloadFromNonExistingFile()
  79. {
  80. $this->setConstants();
  81. $this->assertTrue($this->root->removeChild('autoload.php'));
  82. $this->assertFileNotExists(self::$file);
  83. Load::setAutoloadFrom(self::$file);
  84. $autoload = require(self::$file);
  85. $this->assertSame(self::$autoload_array, $autoload);
  86. }
  87. public function testAutoloadArrayExists()
  88. {
  89. $this->assertFileExists(self::$file);
  90. }
  91. /**
  92. * @runInSeparateProcess
  93. */
  94. public function testFileForArray()
  95. {
  96. $autoload = require(self::$file);
  97. $this->assertInternalType('array', $autoload);
  98. }
  99. /**
  100. * @runInSeparateProcess
  101. */
  102. public function testAutoloadArrayNotEmpty()
  103. {
  104. $autoload = require(self::$file);
  105. $this->assertNotEmpty($autoload);
  106. $this->assertArrayHasKey('Db', $autoload);
  107. }
  108. /**
  109. * @runInSeparateProcess
  110. */
  111. public function testAutoloadGetFilePath()
  112. {
  113. $this->setConstants();
  114. Load::setAutoloadFrom(self::$file);
  115. $this->assertNotEmpty(Load::getFilePath('DbDriver'));
  116. }
  117. /**
  118. * @runInSeparateProcess
  119. */
  120. public function testAutoloadGetFilePathNullIndex()
  121. {
  122. $this->setConstants();
  123. Load::setAutoloadFrom(self::$file);
  124. $autoload = require(self::$file);
  125. $this->setExpectedException('PHPUnit_Framework_Error', 'Undefined index');
  126. $this->assertNotEmpty(Load::getFilePath('ClassDontExist'));
  127. }
  128. /**
  129. * @runInSeparateProcess
  130. */
  131. public function testDebugAutoload()
  132. {
  133. $this->setConstants();
  134. unlink(self::$file);
  135. Load::setAutoloadFrom(self::$file);
  136. $autoload = require(self::$file);
  137. $this->assertNotEmpty($autoload);
  138. Config::set('DEBUG', true);
  139. Load::autoload('Some');
  140. Load::autoload('DbDriver');
  141. }
  142. /**
  143. * @runInSeparateProcess
  144. */
  145. public function testSetExclude()
  146. {
  147. $reflection_prop = new ReflectionProperty('Load', 'exclude');
  148. $reflection_prop->setAccessible(true);
  149. $this->assertCount(0, $reflection_prop->getValue('Load'));
  150. Load::setExclude('dir1');
  151. $this->assertCount(1, $reflection_prop->getValue('Load'));
  152. Load::setExclude(array('dir2'));
  153. $this->assertCount(2, $reflection_prop->getValue('Load'));
  154. $this->assertSame(array('dir1', 'dir2'), $reflection_prop->getValue('Load'));
  155. }
  156. protected function newCallback($className)
  157. {
  158. switch ($className) {
  159. case 'AutoloadBuilder':
  160. return 'AutoloadBuilderMock';
  161. default:
  162. return $className;
  163. }
  164. }
  165. private function setConstants()
  166. {
  167. if (!defined('PATH')) {
  168. define('PATH', vfsStream::url('root'));
  169. }
  170. if (!defined('APP')) {
  171. define('APP', 'lib/core/tests/face');
  172. }
  173. }
  174. public function tearDown()
  175. {
  176. // if (defined('PATH')) {
  177. // echo PHP_EOL . __CLASS__ . ' ' . PATH . PHP_EOL;
  178. // } else {
  179. // echo PHP_EOL . __CLASS__ . ' ' . 'PATH NOT DEFINED' . PHP_EOL;
  180. // }
  181. unset_new_overload();
  182. }
  183. }
  184. class AutoloadBuilderMock
  185. {
  186. public function build()
  187. {
  188. $file = new vfsStreamFile('autoload.php');
  189. $file->setContent(LoadTest::$file_contents);
  190. vfsStreamWrapper::getRoot()->addChild($file);
  191. }
  192. }