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.

213 lines
5.7 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. * @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can cause error.
  30. */
  31. public function setUp()
  32. {
  33. self::$file_contents = '<?php return array("Db" => "/lib/core/model/Db.php", "DbDriver" => "/lib/core/model/DbDriver.php"); ?>';
  34. self::$autoload_array = array('Db' => '/lib/core/model/Db.php', 'DbDriver' => '/lib/core/model/DbDriver.php');
  35. vfsStreamWrapper::register();
  36. vfsStream::setup();
  37. $this->root = vfsStream::create(
  38. array(
  39. 'lib' => array(
  40. 'core' =>
  41. array(
  42. 'util' => array(
  43. 'AutoloadBuilder.php' => ''
  44. ),
  45. 'model' => array(
  46. 'Db.php' => '',
  47. 'DbDriver.php' => ''
  48. ),
  49. 'Registry.php' => '',
  50. 'Load.php' => '',
  51. 'devel.config' => ' development config file'
  52. )
  53. ),
  54. 'autoload.php' => self::$file_contents
  55. )
  56. );
  57. if (!class_exists('AutoloadBuilder')) {
  58. $this->getMock('AutoloadBuilder');
  59. }
  60. vfsStreamWrapper::setRoot($this->root);
  61. self::$file = vfsStream::url('root/autoload.php');
  62. set_new_overload(array($this, 'newCallback'));
  63. }
  64. /**
  65. * @runInSeparateProcess
  66. */
  67. public function testSetAutoLoadFromExistingFile()
  68. {
  69. $this->setConstants();
  70. $this->assertFileExists(self::$file);
  71. Load::setAutoloadFrom(self::$file);
  72. $autoload = require(self::$file);
  73. $this->assertSame(self::$autoload_array, $autoload);
  74. Load::autoload('Db');
  75. }
  76. /**
  77. * @runInSeparateProcess
  78. */
  79. public function testAutoloadFromNonExistingFile()
  80. {
  81. $this->setConstants();
  82. $this->assertTrue($this->root->removeChild('autoload.php'));
  83. $this->assertFileNotExists(self::$file);
  84. Load::setAutoloadFrom(self::$file);
  85. $autoload = require(self::$file);
  86. $this->assertSame(self::$autoload_array, $autoload);
  87. }
  88. public function testAutoloadArrayExists()
  89. {
  90. $this->assertFileExists(self::$file);
  91. }
  92. /**
  93. * @runInSeparateProcess
  94. * @TODO: Load - check if input file returns array
  95. */
  96. public function testFileForArray()
  97. {
  98. $autoload = require(self::$file);
  99. $this->assertInternalType('array', $autoload);
  100. }
  101. /**
  102. * @runInSeparateProcess
  103. */
  104. public function testAutoloadArrayNotEmpty()
  105. {
  106. $autoload = require(self::$file);
  107. $this->assertNotEmpty($autoload);
  108. $this->assertArrayHasKey('Db', $autoload);
  109. }
  110. /**
  111. * @runInSeparateProcess
  112. */
  113. public function testAutoloadGetFilePath()
  114. {
  115. $this->setConstants();
  116. Load::setAutoloadFrom(self::$file);
  117. $this->assertNotEmpty(Load::getFilePath('DbDriver'));
  118. }
  119. /**
  120. * @TODO: Load::getFilePath - check for wrong index
  121. * @runInSeparateProcess
  122. */
  123. public function testAutoloadGetFilePathNullIndex()
  124. {
  125. $this->setConstants();
  126. Load::setAutoloadFrom(self::$file);
  127. $autoload = require(self::$file);
  128. $this->setExpectedException('PHPUnit_Framework_Error');
  129. $this->assertNotEmpty(Load::getFilePath('anton'));
  130. }
  131. /**
  132. * @TODO: Load::autoload() needs self::$autoload = require(self::$file); after self::buildAutoload();
  133. * @runInSeparateProcess
  134. */
  135. public function testDebugAutoload()
  136. {
  137. $this->setConstants();
  138. Load::setAutoloadFrom(self::$file);
  139. $autoload = require(self::$file);
  140. $this->assertNotEmpty($autoload);
  141. Config::set('DEBUG', 1);
  142. Load::autoload('Some');
  143. Load::autoload('DbDriver');
  144. }
  145. protected function newCallback($className)
  146. {
  147. switch ($className) {
  148. case 'AutoloadBuilder':
  149. return 'AutoloadBuilderMock';
  150. default:
  151. return $className;
  152. }
  153. }
  154. private function setConstants()
  155. {
  156. if (!defined('PATH')) {
  157. define('PATH', vfsStream::url('root'));
  158. }
  159. if (!defined('APP')) {
  160. define('APP', 'lib/core/tests/face');
  161. }
  162. }
  163. public function tearDown()
  164. {
  165. // if (defined('PATH')) {
  166. // echo PHP_EOL . __CLASS__ . ' ' . PATH . PHP_EOL;
  167. // } else {
  168. // echo PHP_EOL . __CLASS__ . ' ' . 'PATH NOT DEFINED' . PHP_EOL;
  169. // }
  170. unset_new_overload();
  171. }
  172. }
  173. class AutoloadBuilderMock
  174. {
  175. public function build()
  176. {
  177. $file = new vfsStreamFile('autoload.php');
  178. $file->setContent(LoadTest::$file_contents);
  179. vfsStreamWrapper::getRoot()->addChild($file);
  180. }
  181. }