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.

175 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
  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-28
  8. *
  9. * Unit tests for AutoloadBuilder class
  10. */
  11. require_once dirname(__FILE__) . '/../../util/AutoloadBuilder.php';
  12. /**
  13. * @TODO: AutoloadBuilder - fix writing to file: construct array first, write to file next - atomic operation
  14. */
  15. class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
  16. {
  17. private static $inc_dirs = array();
  18. private static $file;
  19. private static $path;
  20. private static $lib_path;
  21. private static $app;
  22. public function run(PHPUnit_Framework_TestResult $result = NULL)
  23. {
  24. $this->setPreserveGlobalState(false);
  25. return parent::run($result);
  26. }
  27. /**
  28. * @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can couse error.
  29. */
  30. public static function setUpBeforeClass()
  31. {
  32. self::$path = realpath(dirname(__FILE__) . '/../../../..');
  33. self::$lib_path = realpath(dirname(__FILE__) . '/../..');
  34. self::$app = 'lib/core/tests/face';
  35. self::$file = self::$path . '/' . self::$app . '/cache/autoload.php';
  36. self::$inc_dirs[self::$path . '/' . self::$app . '/src'] = true;
  37. self::$inc_dirs[self::$path . '/' . self::$app . '/cache'] = true;
  38. self::$inc_dirs[self::$lib_path . '/'] = true;
  39. foreach (self::$inc_dirs as $dir => &$is_exist) {
  40. if (!file_exists($dir)) {
  41. $is_exist = false;
  42. mkdir($dir, 0777, true);
  43. }
  44. }
  45. }
  46. /**
  47. * @TODO: AutoloadBuilder - check input params: string for filename, array for dirs
  48. * @runInSeparateProcess
  49. */
  50. public function testBuildParams()
  51. {
  52. $this->setConstants();
  53. $autoload = self::$file;
  54. $dirs = 'string';
  55. $builder = new AutoloadBuilder($autoload, $dirs);
  56. $this->setExpectedException('PHPUnit_Framework_Error');
  57. $builder->build();
  58. }
  59. /**
  60. * @runInSeparateProcess
  61. */
  62. public function testBuild()
  63. {
  64. $this->setConstants();
  65. $builder = new AutoloadBuilder(self::$file, array_keys(self::$inc_dirs));
  66. $this->assertFileNotExists(self::$file);
  67. $builder->build();
  68. $this->assertFileExists(self::$file);
  69. $array = require self::$file;
  70. $this->assertFileExists(self::$file);
  71. $this->assertInternalType('array', $array);
  72. $this->assertNotEmpty($array);
  73. $this->assertArrayHasKey('AutoloadBuilder', $array);
  74. $this->assertArrayHasKey('Load', $array);
  75. }
  76. /**
  77. * @runInSeparateProcess
  78. */
  79. public function testBuildWithExcluded()
  80. {
  81. $this->setConstants();
  82. $builder = new AutoloadBuilder(self::$file, array_keys(self::$inc_dirs), array(self::$lib_path . '/app/'));
  83. $this->assertFileNotExists(self::$file);
  84. $builder->build();
  85. $this->assertFileExists(self::$file);
  86. $array = require self::$file;
  87. $this->assertInternalType('array', $array);
  88. $this->assertNotEmpty($array);
  89. $this->assertArrayHasKey('AutoloadBuilder', $array);
  90. $this->assertArrayNotHasKey('FrontController', $array);
  91. $this->assertArrayNotHasKey('AjaxAction', $array);
  92. }
  93. /**
  94. * @runInSeparateProcess
  95. */
  96. public function testAccessDenied()
  97. {
  98. $this->setConstants();
  99. $this->assertFileNotExists(self::$file);
  100. $path = dirname(self::$file);
  101. chmod($path, 0400);
  102. $builder = new AutoloadBuilder(self::$file, array(self::$path . '/' . self::$app . '/src', self::$path . '/' . self::$app . '/cache', self::$path . '/lib'));
  103. $this->setExpectedException('PHPUnit_Framework_Error', 'Permission denied');
  104. $builder->build();
  105. chmod(self::$file, 0777);
  106. }
  107. public static function tearDownAfterClass()
  108. {
  109. if (file_exists(self::$file)) {
  110. unlink(self::$file);
  111. }
  112. foreach (self::$inc_dirs as $dir => $is_exist) {
  113. if (!$is_exist) {
  114. self::rrmdir($dir);
  115. }
  116. }
  117. self::rrmdir(self::$path . '/' . self::$app);
  118. }
  119. private function setConstants()
  120. {
  121. if (!defined('PATH')) {
  122. define('PATH', realpath(dirname(__FILE__) . '/../../../..'));
  123. }
  124. if (!defined('APP')) {
  125. define('APP', 'lib/core/tests/face');
  126. }
  127. }
  128. public static function rrmdir($dir)
  129. {
  130. if (is_dir($dir)) {
  131. $objects = scandir($dir);
  132. foreach ($objects as $object) {
  133. if ($object != "." && $object != "..") {
  134. if (filetype($dir . "/" . $object) == "dir") {
  135. self::rrmdir($dir . "/" . $object);
  136. } else {
  137. unlink($dir . "/" . $object);
  138. }
  139. }
  140. }
  141. reset($objects);
  142. rmdir($dir);
  143. }
  144. }
  145. }