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.

185 lines
5.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
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 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. if (!defined('PATH')) {
  58. //define('PATH', realpath(dirname(__FILE__) . '/../../../'));
  59. define('PATH', vfsStream::url('root'));
  60. }
  61. if (!defined('APP')) {
  62. define('APP', 'lib/core/tests/face');
  63. }
  64. vfsStreamWrapper::setRoot($this->root);
  65. self::$file = vfsStream::url('root/autoload.php');
  66. set_new_overload(array($this, 'newCallback'));
  67. }
  68. public function testSetAutoLoadFromExistingFile()
  69. {
  70. Load::setAutoloadFrom(self::$file);
  71. $autoload = require(self::$file);
  72. $this->assertEquals(self::$autoload_array, $autoload);
  73. Load::autoload('Db');
  74. }
  75. public function testAutoloadFromNonExistingFile()
  76. {
  77. $this->assertTrue($this->root->removeChild('autoload.php'));
  78. $this->assertFileNotExists(self::$file);
  79. Load::setAutoloadFrom(self::$file);
  80. $autoload = require(self::$file);
  81. $this->assertEquals(self::$autoload_array, $autoload);
  82. }
  83. public function testAutoloadArrayExists()
  84. {
  85. $this->assertFileExists(self::$file);
  86. }
  87. /**
  88. * @TODO: Load - check if input file returns array
  89. */
  90. public function testFileForArray()
  91. {
  92. $autoload = require(self::$file);
  93. $this->assertInternalType('array', $autoload);
  94. }
  95. public function testAutoloadArrayNotEmpty()
  96. {
  97. $autoload = require(self::$file);
  98. $this->assertNotEmpty($autoload);
  99. $this->assertArrayHasKey('Db', $autoload);
  100. }
  101. public function testAutoloadGetFilePath()
  102. {
  103. $autoload = require(self::$file);
  104. $this->assertNotEmpty(Load::getFilePath('DbDriver'));
  105. }
  106. /**
  107. * @TODO: Load::autoload() needs self::$autoload = require(self::$file); after self::buildAutoload();
  108. * @runInSeparateProcess
  109. */
  110. public function testDebugAutoload()
  111. {
  112. $this->setUp();
  113. Load::setAutoloadFrom(self::$file);
  114. $autoload = require(self::$file);
  115. $this->assertNotEmpty($autoload);
  116. if (!defined('DEBUG')) {
  117. define('DEBUG', true);
  118. }
  119. Load::autoload('Some');
  120. Load::autoload('DbDriver');
  121. }
  122. /**
  123. * @TODO: Load::getFilePath - check for wrong index
  124. * @expectedException PHPUnit_Framework_Error
  125. */
  126. public function testAutoloadGetFilePathNullIndex()
  127. {
  128. $autoload = require(self::$file);
  129. $this->assertNotEmpty(Load::getFilePath('anton'));
  130. }
  131. protected function newCallback($className)
  132. {
  133. switch ($className) {
  134. case 'AutoloadBuilder':
  135. return 'AutoloadBuilderMock';
  136. default:
  137. return $className;
  138. }
  139. }
  140. public function tearDown()
  141. {
  142. unset_new_overload();
  143. }
  144. }
  145. class AutoloadBuilderMock
  146. {
  147. public function build()
  148. {
  149. $file = new vfsStreamFile('autoload.php');
  150. $file->setContent(LoadTest::$file_contents);
  151. vfsStreamWrapper::getRoot()->addChild($file);
  152. }
  153. }