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.

116 lines
3.2 KiB

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-..
  8. *
  9. * Unit tests for AutoloadBuilder class
  10. */
  11. require_once 'vfsStream/vfsStream.php';
  12. require_once dirname(__FILE__) . '/../../util/AutoloadBuilder.php';
  13. /**
  14. * @TODO: AutoloadBuilder - fix writing to file: construct array first, write to file next - atomic operation
  15. */
  16. class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
  17. {
  18. private static $inc_dirs = array();
  19. private static $file;
  20. /**
  21. * @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can couse error.
  22. * @expectedException UnexpectedValueException
  23. * @expectedException PHPUnit_Framework_Error
  24. */
  25. public static function setUpBeforeClass()
  26. {
  27. if (!defined('PATH')) {
  28. define('PATH', realpath(dirname(__FILE__) . '/../../../..'));
  29. }
  30. // default application name
  31. if (!defined('APP')) {
  32. define('APP', 'lib/core/tests/face');
  33. }
  34. self::$file = PATH . '/' . APP . '/cache/autoload.php';
  35. // value - if dir exists (to delete it if it was created), default true
  36. self::$inc_dirs[PATH . '/' . APP . '/src'] = true;
  37. self::$inc_dirs[PATH . '/' . APP . '/cache'] = true;
  38. self::$inc_dirs[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. */
  50. public function testBuildParams()
  51. {
  52. $autoload = self::$file;
  53. $dirs = 'string';
  54. $builder = new AutoloadBuilder($autoload, $dirs);
  55. $builder->build();
  56. }
  57. public function testBuild()
  58. {
  59. $builder = new AutoloadBuilder(self::$file, array(PATH . '/' . APP . '/src', PATH . '/' . APP . '/cache', PATH . '/lib'));
  60. $builder->build();
  61. $this->assertFileExists(self::$file);
  62. }
  63. public function testAutoloadFileExists()
  64. {
  65. $this->assertFileExists(self::$file);
  66. }
  67. public function testAutoloadArray()
  68. {
  69. $this->assertFileExists(self::$file);
  70. $array = require self::$file;
  71. $this->assertInternalType('array', $array);
  72. $this->assertNotEmpty($array);
  73. }
  74. /**
  75. * @expectedException PHPUnit_Framework_Error
  76. */
  77. public function testAccessDenied()
  78. {
  79. chmod(self::$file, 0400);
  80. $builder = new AutoloadBuilder(self::$file, array(PATH . '/' . APP . '/src', PATH . '/' . APP . '/cache', PATH . '/lib'));
  81. $builder->build();
  82. chmod(self::$file, 0777);
  83. }
  84. public static function tearDownAfterClass()
  85. {
  86. if (file_exists(self::$file)) {
  87. unlink(self::$file);
  88. }
  89. foreach (self::$inc_dirs as $dir => $is_exist) {
  90. if (!$is_exist) {
  91. rmdir($dir);
  92. }
  93. }
  94. rmdir(PATH . '/' . APP);
  95. }
  96. }