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.

151 lines
4.3 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
  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(self::$path . '/' . self::$app . '/src', self::$path . '/' . self::$app . '/cache', self::$path . '/lib'));
  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 testAccessDenied()
  77. {
  78. $this->setConstants();
  79. $this->setExpectedException('PHPUnit_Framework_Error');
  80. $this->assertFileNotExists(self::$file);
  81. touch(self::$file);
  82. $this->assertFileExists(self::$file);
  83. chmod(self::$file, 0400);
  84. $builder = new AutoloadBuilder(self::$file, array(self::$path . '/' . self::$app . '/src', self::$path . '/' . self::$app . '/cache', self::$path . '/lib'));
  85. $builder->build();
  86. chmod(self::$file, 0777);
  87. }
  88. public static function tearDownAfterClass()
  89. {
  90. if (file_exists(self::$file)) {
  91. unlink(self::$file);
  92. }
  93. foreach (self::$inc_dirs as $dir => $is_exist) {
  94. if (!$is_exist) {
  95. self::rrmdir($dir);
  96. }
  97. }
  98. self::rrmdir(self::$path . '/' . self::$app);
  99. }
  100. private function setConstants()
  101. {
  102. if (!defined('PATH')) {
  103. define('PATH', realpath(dirname(__FILE__) . '/../../../..'));
  104. }
  105. if (!defined('APP')) {
  106. define('APP', 'lib/core/tests/face');
  107. }
  108. }
  109. public static function rrmdir($dir)
  110. {
  111. if (is_dir($dir)) {
  112. $objects = scandir($dir);
  113. foreach ($objects as $object) {
  114. if ($object != "." && $object != "..") {
  115. if (filetype($dir . "/" . $object) == "dir") {
  116. self::rrmdir($dir . "/" . $object);
  117. } else {
  118. unlink($dir . "/" . $object);
  119. }
  120. }
  121. }
  122. reset($objects);
  123. rmdir($dir);
  124. }
  125. }
  126. }