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.

179 lines
4.9 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
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. /**
  21. * @TODO: Load->buildAutoload() should recieve AutoloadBuilder as a parameter
  22. * @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can cause error.
  23. */
  24. public function setUp()
  25. {
  26. self::$file_contents = '<?php return array("Db" => "/lib/core/model/Db.php", "RedisDebug" => "/lib/core/redis/RedisDebug.php"); ?>';
  27. self::$autoload_array = array('Db' => '/lib/core/model/Db.php', 'RedisDebug' => '/lib/core/redis/RedisDebug.php');
  28. vfsStreamWrapper::register();
  29. $this->root = vfsStream::create(
  30. array(
  31. 'lib' => array(
  32. 'core' =>
  33. array(
  34. 'util' => array(
  35. 'AutoloadBuilder.php' => ''
  36. ),
  37. 'model' => array(
  38. 'Db.php' => '',
  39. ),
  40. 'redis' => array(
  41. 'RedisDebug.php' => ''
  42. ),
  43. 'Registry.php' => '',
  44. 'Load.php' => '',
  45. 'devel.config' => ' development config file'
  46. )
  47. ),
  48. 'autoload.php' => self::$file_contents
  49. )
  50. );
  51. if (!class_exists('AutoloadBuilder')) {
  52. $this->getMock('AutoloadBuilder');
  53. }
  54. if (!defined('PATH')) {
  55. //define('PATH', realpath(dirname(__FILE__) . '/../../../'));
  56. define('PATH', vfsStream::url('root'));
  57. }
  58. if (!defined('APP')) {
  59. define('APP', 'lib/core/tests/face');
  60. }
  61. vfsStreamWrapper::setRoot($this->root);
  62. self::$file = vfsStream::url('root/autoload.php');
  63. set_new_overload(array($this, 'newCallback'));
  64. }
  65. public function testSetAutoLoadFromExistingFile()
  66. {
  67. Load::setAutoloadFrom(self::$file);
  68. $autoload = require(self::$file);
  69. $this->assertEquals(self::$autoload_array, $autoload);
  70. Load::autoload('Db');
  71. }
  72. public function testAutoloadFromNonExistingFile()
  73. {
  74. $this->assertTrue($this->root->removeChild('autoload.php'));
  75. $this->assertFileNotExists(self::$file);
  76. Load::setAutoloadFrom(self::$file);
  77. $autoload = require(self::$file);
  78. $this->assertEquals(self::$autoload_array, $autoload);
  79. }
  80. /**
  81. * @TODO: Load::autoload() needs self::$autoload = require(self::$file); after self::buildAutoload();
  82. */
  83. public function testDebugAutoload()
  84. {
  85. $this->setUp();
  86. Load::setAutoloadFrom(self::$file);
  87. $autoload = require(self::$file);
  88. $this->assertNotEmpty($autoload);
  89. if (!defined('DEBUG')) {
  90. define('DEBUG', true);
  91. }
  92. Load::autoload('Some');
  93. Load::autoload('RedisDebug');
  94. }
  95. public function testAutoloadArrayExists()
  96. {
  97. $this->assertFileExists(self::$file);
  98. }
  99. /**
  100. * @TODO: Load - check if input file returns array
  101. */
  102. public function testFileForArray()
  103. {
  104. $autoload = require(self::$file);
  105. $this->assertInternalType('array', $autoload);
  106. }
  107. public function testAutoloadArrayNotEmpty()
  108. {
  109. $autoload = require(self::$file);
  110. $this->assertNotEmpty($autoload);
  111. $this->assertArrayHasKey('Db', $autoload);
  112. }
  113. public function testAutoloadGetFilePath()
  114. {
  115. $autoload = require(self::$file);
  116. $this->assertNotEmpty(Load::getFilePath('RedisDebug'));
  117. }
  118. /**
  119. * @TODO: Load::getFilePath - check for wrong index
  120. * @expectedException PHPUnit_Framework_Error
  121. */
  122. public function testAutoloadGetFilePathNullIndex()
  123. {
  124. $autoload = require(self::$file);
  125. $this->assertNotEmpty(Load::getFilePath('anton'));
  126. }
  127. protected function newCallback($className)
  128. {
  129. switch ($className) {
  130. case 'AutoloadBuilder':
  131. return 'AutoloadBuilderMock';
  132. default:
  133. return $className;
  134. }
  135. }
  136. public function tearDown()
  137. {
  138. unset_new_overload();
  139. }
  140. }
  141. class AutoloadBuilderMock
  142. {
  143. public function build()
  144. {
  145. $file = new vfsStreamFile('autoload.php');
  146. $file->setContent(LoadTest::$file_contents);
  147. vfsStreamWrapper::getRoot()->addChild($file);
  148. }
  149. }