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.6 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__) . '/../Load.php';
  12. require_once 'vfsStream/vfsStream.php';
  13. class LoadTest extends PHPUnit_Framework_TestCase
  14. {
  15. private static $inc_dirs = array();
  16. private static $file;
  17. private $root;
  18. public static $file_contents;
  19. public static $autoload_array;
  20. public function run(PHPUnit_Framework_TestResult $result = NULL)
  21. {
  22. $this->setPreserveGlobalState(false);
  23. return parent::run($result);
  24. }
  25. /**
  26. * @TODO: Load->buildAutoload() should recieve AutoloadBuilder as a parameter
  27. * @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can cause error.
  28. */
  29. public function setUp()
  30. {
  31. self::$file_contents = '<?php return array("Db" => "/lib/core/model/Db.php", "DbDriver" => "/lib/core/model/DbDriver.php"); ?>';
  32. self::$autoload_array = array('Db' => '/lib/core/model/Db.php', 'DbDriver' => '/lib/core/model/DbDriver.php');
  33. vfsStreamWrapper::register();
  34. vfsStream::setup();
  35. $this->root = vfsStream::create(
  36. array(
  37. 'lib' => array(
  38. 'core' =>
  39. array(
  40. 'util' => array(
  41. 'AutoloadBuilder.php' => ''
  42. ),
  43. 'model' => array(
  44. 'Db.php' => '',
  45. 'DbDriver.php' => ''
  46. ),
  47. 'Registry.php' => '',
  48. 'Load.php' => '',
  49. 'devel.config' => ' development config file'
  50. )
  51. ),
  52. 'autoload.php' => self::$file_contents
  53. )
  54. );
  55. if (!class_exists('AutoloadBuilder')) {
  56. $this->getMock('AutoloadBuilder');
  57. }
  58. vfsStreamWrapper::setRoot($this->root);
  59. self::$file = vfsStream::url('root/autoload.php');
  60. set_new_overload(array($this, 'newCallback'));
  61. }
  62. /**
  63. * @runInSeparateProcess
  64. */
  65. public function testSetAutoLoadFromExistingFile()
  66. {
  67. $this->setConstants();
  68. $this->assertFileExists(self::$file);
  69. Load::setAutoloadFrom(self::$file);
  70. $autoload = require(self::$file);
  71. $this->assertSame(self::$autoload_array, $autoload);
  72. Load::autoload('Db');
  73. }
  74. /**
  75. * @runInSeparateProcess
  76. */
  77. public function testAutoloadFromNonExistingFile()
  78. {
  79. $this->setConstants();
  80. $this->assertTrue($this->root->removeChild('autoload.php'));
  81. $this->assertFileNotExists(self::$file);
  82. Load::setAutoloadFrom(self::$file);
  83. $autoload = require(self::$file);
  84. $this->assertSame(self::$autoload_array, $autoload);
  85. }
  86. public function testAutoloadArrayExists()
  87. {
  88. $this->assertFileExists(self::$file);
  89. }
  90. /**
  91. * @runInSeparateProcess
  92. * @TODO: Load - check if input file returns array
  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. * @TODO: Load::getFilePath - check for wrong index
  119. * @runInSeparateProcess
  120. */
  121. public function testAutoloadGetFilePathNullIndex()
  122. {
  123. $this->setConstants();
  124. Load::setAutoloadFrom(self::$file);
  125. $autoload = require(self::$file);
  126. $this->setExpectedException('PHPUnit_Framework_Error');
  127. $this->assertNotEmpty(Load::getFilePath('anton'));
  128. }
  129. /**
  130. * @TODO: Load::autoload() needs self::$autoload = require(self::$file); after self::buildAutoload();
  131. * @runInSeparateProcess
  132. */
  133. public function testDebugAutoload()
  134. {
  135. $this->setConstants();
  136. Load::setAutoloadFrom(self::$file);
  137. $autoload = require(self::$file);
  138. $this->assertNotEmpty($autoload);
  139. if (!defined('DEBUG')) {
  140. define('DEBUG', true);
  141. }
  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. }