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.

115 lines
3.2 KiB

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-..
  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. /**
  20. * @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can couse error.
  21. * @expectedException UnexpectedValueException
  22. * @expectedException PHPUnit_Framework_Error
  23. */
  24. public static function setUpBeforeClass()
  25. {
  26. if (!defined('PATH')) {
  27. define('PATH', realpath(dirname(__FILE__) . '/../../../..'));
  28. }
  29. // default application name
  30. if (!defined('APP')) {
  31. define('APP', 'lib/core/tests/face');
  32. }
  33. self::$file = PATH . '/' . APP . '/cache/autoload.php';
  34. // value - if dir exists (to delete it if it was created), default true
  35. self::$inc_dirs[PATH . '/' . APP . '/src'] = true;
  36. self::$inc_dirs[PATH . '/' . APP . '/cache'] = true;
  37. self::$inc_dirs[PATH . '/lib'] = true;
  38. foreach (self::$inc_dirs as $dir => &$is_exist) {
  39. if (!file_exists($dir)) {
  40. $is_exist = false;
  41. mkdir($dir, 0777, true);
  42. }
  43. }
  44. }
  45. /**
  46. * @TODO: AutoloadBuilder - check input params: string for filename, array for dirs
  47. * @expectedException PHPUnit_Framework_Error
  48. */
  49. public function testBuildParams()
  50. {
  51. $autoload = self::$file;
  52. $dirs = 'string';
  53. $builder = new AutoloadBuilder($autoload, $dirs);
  54. $builder->build();
  55. }
  56. public function testBuild()
  57. {
  58. $builder = new AutoloadBuilder(self::$file, array(PATH . '/' . APP . '/src', PATH . '/' . APP . '/cache', PATH . '/lib'));
  59. $builder->build();
  60. $this->assertFileExists(self::$file);
  61. }
  62. public function testAutoloadFileExists()
  63. {
  64. $this->assertFileExists(self::$file);
  65. }
  66. public function testAutoloadArray()
  67. {
  68. $this->assertFileExists(self::$file);
  69. $array = require self::$file;
  70. $this->assertInternalType('array', $array);
  71. $this->assertNotEmpty($array);
  72. }
  73. /**
  74. * @expectedException PHPUnit_Framework_Error
  75. */
  76. public function testAccessDenied()
  77. {
  78. chmod(self::$file, 0400);
  79. $builder = new AutoloadBuilder(self::$file, array(PATH . '/' . APP . '/src', PATH . '/' . APP . '/cache', PATH . '/lib'));
  80. $builder->build();
  81. chmod(self::$file, 0777);
  82. }
  83. public static function tearDownAfterClass()
  84. {
  85. if (file_exists(self::$file)) {
  86. unlink(self::$file);
  87. }
  88. foreach (self::$inc_dirs as $dir => $is_exist) {
  89. if (!$is_exist) {
  90. rmdir($dir);
  91. }
  92. }
  93. rmdir(PATH . '/' . APP);
  94. }
  95. }