PageRenderTime 84ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Sabre/DAV/TreeTest.php

https://github.com/KOLANICH/SabreDAV
PHP | 175 lines | 101 code | 71 blank | 3 comment | 4 complexity | 40f7fee51f8e7b5eeb80c98172d3b97d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\DAV;
  3. /**
  4. * @covers \Sabre\DAV\Tree
  5. */
  6. class TreeTest extends \PHPUnit_Framework_TestCase {
  7. function testNodeExists() {
  8. $tree = new TreeMock();
  9. $this->assertTrue($tree->nodeExists('hi'));
  10. $this->assertFalse($tree->nodeExists('hello'));
  11. }
  12. function testCopy() {
  13. $tree = new TreeMock();
  14. $tree->copy('hi','hi2');
  15. $this->assertArrayHasKey('hi2', $tree->getNodeForPath('')->newDirectories);
  16. $this->assertEquals('foobar', $tree->getNodeForPath('hi/file')->get());
  17. $this->assertEquals(array('test1'=>'value'), $tree->getNodeForPath('hi/file')->getProperties(array()));
  18. }
  19. function testMove() {
  20. $tree = new TreeMock();
  21. $tree->move('hi','hi2');
  22. $this->assertEquals('hi2', $tree->getNodeForPath('hi')->getName());
  23. $this->assertTrue($tree->getNodeForPath('hi')->isRenamed);
  24. }
  25. function testDeepMove() {
  26. $tree = new TreeMock();
  27. $tree->move('hi/sub','hi2');
  28. $this->assertArrayHasKey('hi2', $tree->getNodeForPath('')->newDirectories);
  29. $this->assertTrue($tree->getNodeForPath('hi/sub')->isDeleted);
  30. }
  31. function testDelete() {
  32. $tree = new TreeMock();
  33. $tree->delete('hi');
  34. $this->assertTrue($tree->getNodeForPath('hi')->isDeleted);
  35. }
  36. function testGetChildren() {
  37. $tree = new TreeMock();
  38. $children = $tree->getChildren('');
  39. $this->assertEquals(1,count($children));
  40. $this->assertEquals('hi', $children[0]->getName());
  41. }
  42. }
  43. class TreeMock extends Tree {
  44. private $nodes = array();
  45. function __construct() {
  46. $this->nodes['hi/sub'] = new TreeDirectoryTester('sub');
  47. $this->nodes['hi/file'] = new TreeFileTester('file');
  48. $this->nodes['hi/file']->properties = array('test1' => 'value');
  49. $this->nodes['hi/file']->data = 'foobar';
  50. $this->nodes['hi'] = new TreeDirectoryTester('hi',array($this->nodes['hi/sub'], $this->nodes['hi/file']));
  51. $this->nodes[''] = new TreeDirectoryTester('hi', array($this->nodes['hi']));
  52. }
  53. function getNodeForPath($path) {
  54. if (isset($this->nodes[$path])) return $this->nodes[$path];
  55. throw new Exception\NotFound('item not found');
  56. }
  57. }
  58. class TreeDirectoryTester extends SimpleCollection {
  59. public $newDirectories = array();
  60. public $newFiles = array();
  61. public $isDeleted = false;
  62. public $isRenamed = false;
  63. function createDirectory($name) {
  64. $this->newDirectories[$name] = true;
  65. }
  66. function createFile($name,$data = null) {
  67. $this->newFiles[$name] = $data;
  68. }
  69. function getChild($name) {
  70. if (isset($this->newDirectories[$name])) return new TreeDirectoryTester($name);
  71. if (isset($this->newFiles[$name])) return new TreeFileTester($name, $this->newFiles[$name]);
  72. return parent::getChild($name);
  73. }
  74. function delete() {
  75. $this->isDeleted = true;
  76. }
  77. function setName($name) {
  78. $this->isRenamed = true;
  79. $this->name = $name;
  80. }
  81. }
  82. class TreeFileTester extends File implements IProperties {
  83. public $name;
  84. public $data;
  85. public $properties;
  86. function __construct($name, $data = null) {
  87. $this->name = $name;
  88. if (is_null($data)) $data = 'bla';
  89. $this->data = $data;
  90. }
  91. function getName() {
  92. return $this->name;
  93. }
  94. function get() {
  95. return $this->data;
  96. }
  97. function getProperties($properties) {
  98. return $this->properties;
  99. }
  100. function updateProperties($properties) {
  101. $this->properties = $properties;
  102. return true;
  103. }
  104. }