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.

127 lines
5.5 KiB

  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. * @TODO: vfsStream doesn't work with SPLFIleObject->getRealPath - $file->getPath .'/'. $file->getFIleName instead
  16. */
  17. class AutoloadBuilderTestVFS extends PHPUnit_Framework_TestCase
  18. {
  19. private static $inc_dirs = array();
  20. private static $file;
  21. private $root;
  22. private $array = array();
  23. /**
  24. * @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can couse error.
  25. */
  26. public function setUp()
  27. {
  28. if (!defined('PATH')) {
  29. define('PATH', realpath(dirname(__FILE__) . '/../../../..'));
  30. }
  31. if (!defined('APP')) {
  32. define('APP', 'lib/core/tests/face');
  33. }
  34. vfsStreamWrapper::register();
  35. $this->root = vfsStream::create(
  36. array(
  37. 'cache' => array(
  38. 'face' => array()
  39. ),
  40. 'face' => array(
  41. 'src' => array(
  42. 'Registry.php' => ' class Registry'
  43. )
  44. ),
  45. 'lib' => array(
  46. 'Registry.php' => ' class Registry',
  47. 'Load.php' => 'class Load extends Registry',
  48. 'devel.config' => ' development config file'
  49. )
  50. )
  51. );
  52. vfsStreamWrapper::setRoot($this->root);
  53. }
  54. public function testVFS()
  55. {
  56. $builder = new AutoloadBuilder(vfsStream::url('root/cache/face/autoload.php'), array(vfsStream::url('root/lib'), vfsStream::url('root/face/src')));
  57. $this->assertTrue($this->root->hasChild('lib'));
  58. $this->assertFalse($this->root->hasChild('nochild'));
  59. $this->assertFalse($this->root->hasChild('cache/face/autoload.php'));
  60. $handle = fopen(vfsStream::url('root/cache/face/autoload.php'), 'w');
  61. fwrite($handle, 'new string');
  62. fclose($handle);
  63. $this->assertTrue($this->root->hasChild('cache/face/autoload.php'));
  64. $this->assertSame('new string', $this->root->getChild('cache/face/autoload.php')->getContent());
  65. }
  66. public function testBuild()
  67. {
  68. $this->markTestSkipped(
  69. 'AutoloadBuilder uses $file->getRealPath method, that doesn`t work properly with vfsStream.'
  70. );
  71. $builder = new AutoloadBuilder(vfsStream::url('root/cache/face/autoload.php'), array(vfsStream::url('root/lib'), vfsStream::url('root/face/src')));
  72. $builder->build();
  73. $this->assertTrue($this->root->hasChild('cache/face/autoload.php'));
  74. $this->assertFileExists(vfsStream::url('root/cache/face/autoload.php'));
  75. }
  76. public function testAutoloadArray()
  77. {
  78. $this->markTestSkipped(
  79. 'AutoloadBuilder uses $file->getRealPath method, that doesn`t work properly with vfsStream.'
  80. );
  81. $this->assertFileNotExists(vfsStream::url('root/cache/face/autoload.php'));
  82. $builder = new AutoloadBuilder(vfsStream::url('root/cache/face/autoload.php'), array(vfsStream::url('root/lib'), vfsStream::url('root/face/src')));
  83. $builder->build();
  84. $this->assertFileExists(vfsStream::url('root/cache/face/autoload.php'));
  85. $this->assertEmpty($this->array);
  86. $this->array = require vfsStream::url('root/cache/face/autoload.php');
  87. $this->assertInternalType('array', $this->array);
  88. $this->assertArrayHasKey('Load', $this->array);
  89. $this->assertArrayNotHasKey('Key', $this->array);
  90. $this->assertSame(2, count($this->array));
  91. }
  92. public function testAutoloadHasNoAccess()
  93. {
  94. $this->markTestSkipped(
  95. 'AutoloadBuilder uses $file->getRealPath method, that doesn`t work properly with vfsStream.'
  96. );
  97. $this->assertFileNotExists(vfsStream::url('root/cache/face/autoload.php'));
  98. $autoloadFIle = new vfsStreamFile('cache/face/autoload.php', 0400);
  99. $this->root->addChild($autoloadFIle);
  100. $builder = new AutoloadBuilder(vfsStream::url('root/cache/face/autoload.php'), array(vfsStream::url('root/lib'), vfsStream::url('root/face/src')));
  101. $builder->build();
  102. $this->assertFileExists(vfsStream::url('root/cache/face/autoload.php'));
  103. $this->assertEmpty($this->root->getChild('cache/face/autoload.php')->getContent());
  104. $this->array = require vfsStream::url('root/cache/face/autoload.php');
  105. $this->assertInternalType('integer', $this->array);
  106. $autoloadFIle->chmod(0777);
  107. $builder = new AutoloadBuilder(vfsStream::url('root/cache/face/autoload.php'), array(vfsStream::url('root/lib'), vfsStream::url('root/face/src')));
  108. $builder->build();
  109. $this->assertFileExists(vfsStream::url('root/cache/face/autoload.php'));
  110. $this->assertNotEmpty($this->root->getChild('cache/face/autoload.php')->getContent());
  111. $this->array = require vfsStream::url('root/cache/face/autoload.php');
  112. $this->assertInternalType('array', $this->array);
  113. $this->assertNotEmpty($this->array);
  114. }
  115. }