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