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.

208 lines
5.5 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
  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. * @TODO: Load - check if input file returns array
  91. */
  92. public function testFileForArray()
  93. {
  94. $autoload = require(self::$file);
  95. $this->assertInternalType('array', $autoload);
  96. }
  97. public function testAutoloadArrayNotEmpty()
  98. {
  99. $autoload = require(self::$file);
  100. $this->assertNotEmpty($autoload);
  101. $this->assertArrayHasKey('Db', $autoload);
  102. }
  103. /**
  104. * @runInSeparateProcess
  105. */
  106. public function testAutoloadGetFilePath()
  107. {
  108. $this->setConstants();
  109. Load::setAutoloadFrom(self::$file);
  110. $this->assertNotEmpty(Load::getFilePath('DbDriver'));
  111. }
  112. /**
  113. * @TODO: Load::getFilePath - check for wrong index
  114. * @expectedException PHPUnit_Framework_Error
  115. * @runInSeparateProcess
  116. */
  117. public function testAutoloadGetFilePathNullIndex()
  118. {
  119. $this->setConstants();
  120. Load::setAutoloadFrom(self::$file);
  121. $autoload = require(self::$file);
  122. $this->assertNotEmpty(Load::getFilePath('anton'));
  123. }
  124. /**
  125. * @TODO: Load::autoload() needs self::$autoload = require(self::$file); after self::buildAutoload();
  126. * @runInSeparateProcess
  127. */
  128. public function testDebugAutoload()
  129. {
  130. $this->setConstants();
  131. Load::setAutoloadFrom(self::$file);
  132. $autoload = require(self::$file);
  133. $this->assertNotEmpty($autoload);
  134. if (!defined('DEBUG')) {
  135. define('DEBUG', true);
  136. }
  137. Load::autoload('Some');
  138. Load::autoload('DbDriver');
  139. }
  140. protected function newCallback($className)
  141. {
  142. switch ($className) {
  143. case 'AutoloadBuilder':
  144. return 'AutoloadBuilderMock';
  145. default:
  146. return $className;
  147. }
  148. }
  149. private function setConstants()
  150. {
  151. if (!defined('PATH')) {
  152. define('PATH', vfsStream::url('root'));
  153. }
  154. if (!defined('APP')) {
  155. define('APP', 'lib/core/tests/face');
  156. }
  157. }
  158. public function tearDown()
  159. {
  160. // if (defined('PATH')) {
  161. // echo PHP_EOL . __CLASS__ . ' ' . PATH . PHP_EOL;
  162. // } else {
  163. // echo PHP_EOL . __CLASS__ . ' ' . 'PATH NOT DEFINED' . PHP_EOL;
  164. // }
  165. unset_new_overload();
  166. }
  167. }
  168. class AutoloadBuilderMock
  169. {
  170. public function build()
  171. {
  172. $file = new vfsStreamFile('autoload.php');
  173. $file->setContent(LoadTest::$file_contents);
  174. vfsStreamWrapper::getRoot()->addChild($file);
  175. }
  176. }