171 lines
4.8 KiB

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