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.

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