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.

154 lines
4.2 KiB

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