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.

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