1<?php 2 3namespace Sabre\DAV; 4 5class TreeTest extends \PHPUnit_Framework_TestCase { 6 7 function testNodeExists() { 8 9 $tree = new TreeMock(); 10 11 $this->assertTrue($tree->nodeExists('hi')); 12 $this->assertFalse($tree->nodeExists('hello')); 13 14 } 15 16 function testCopy() { 17 18 $tree = new TreeMock(); 19 $tree->copy('hi','hi2'); 20 21 $this->assertArrayHasKey('hi2', $tree->getNodeForPath('')->newDirectories); 22 $this->assertEquals('foobar', $tree->getNodeForPath('hi/file')->get()); 23 $this->assertEquals(array('test1'=>'value'), $tree->getNodeForPath('hi/file')->getProperties(array())); 24 25 } 26 27 function testMove() { 28 29 $tree = new TreeMock(); 30 $tree->move('hi','hi2'); 31 32 $this->assertEquals('hi2', $tree->getNodeForPath('hi')->getName()); 33 $this->assertTrue($tree->getNodeForPath('hi')->isRenamed); 34 35 } 36 37 function testDeepMove() { 38 39 $tree = new TreeMock(); 40 $tree->move('hi/sub','hi2'); 41 42 $this->assertArrayHasKey('hi2', $tree->getNodeForPath('')->newDirectories); 43 $this->assertTrue($tree->getNodeForPath('hi/sub')->isDeleted); 44 45 } 46 47 function testDelete() { 48 49 $tree = new TreeMock(); 50 $tree->delete('hi'); 51 $this->assertTrue($tree->getNodeForPath('hi')->isDeleted); 52 53 } 54 55 function testGetChildren() { 56 57 $tree = new TreeMock(); 58 $children = $tree->getChildren(''); 59 $this->assertEquals(2,count($children)); 60 $this->assertEquals('hi', $children[0]->getName()); 61 62 } 63 64 function testGetMultipleNodes() { 65 66 $tree = new TreeMock(); 67 $result = $tree->getMultipleNodes(['hi/sub', 'hi/file']); 68 $this->assertArrayHasKey('hi/sub', $result); 69 $this->assertArrayHasKey('hi/file', $result); 70 71 $this->assertEquals('sub', $result['hi/sub']->getName()); 72 $this->assertEquals('file', $result['hi/file']->getName()); 73 74 } 75 function testGetMultipleNodes2() { 76 77 $tree = new TreeMock(); 78 $result = $tree->getMultipleNodes(['multi/1', 'multi/2']); 79 $this->assertArrayHasKey('multi/1', $result); 80 $this->assertArrayHasKey('multi/2', $result); 81 82 } 83 84} 85 86class TreeMock extends Tree { 87 88 private $nodes = array(); 89 90 function __construct() { 91 92 $file = new TreeFileTester('file'); 93 $file->properties = ['test1'=>'value']; 94 $file->data = 'foobar'; 95 96 parent::__construct( 97 new TreeDirectoryTester('root', [ 98 new TreeDirectoryTester('hi', [ 99 new TreeDirectoryTester('sub'), 100 $file, 101 ]), 102 new TreeMultiGetTester('multi', [ 103 new TreeFileTester('1'), 104 new TreeFileTester('2'), 105 new TreeFileTester('3'), 106 ]) 107 ]) 108 ); 109 110 } 111 112} 113 114class TreeDirectoryTester extends SimpleCollection { 115 116 public $newDirectories = array(); 117 public $newFiles = array(); 118 public $isDeleted = false; 119 public $isRenamed = false; 120 121 function createDirectory($name) { 122 123 $this->newDirectories[$name] = true; 124 125 } 126 127 function createFile($name,$data = null) { 128 129 $this->newFiles[$name] = $data; 130 131 } 132 133 function getChild($name) { 134 135 if (isset($this->newDirectories[$name])) return new TreeDirectoryTester($name); 136 if (isset($this->newFiles[$name])) return new TreeFileTester($name, $this->newFiles[$name]); 137 return parent::getChild($name); 138 139 } 140 141 function childExists($name) { 142 143 return !!$this->getChild($name); 144 145 } 146 147 function delete() { 148 149 $this->isDeleted = true; 150 151 } 152 153 function setName($name) { 154 155 $this->isRenamed = true; 156 $this->name = $name; 157 158 } 159 160} 161 162class TreeFileTester extends File implements IProperties { 163 164 public $name; 165 public $data; 166 public $properties; 167 168 function __construct($name, $data = null) { 169 170 $this->name = $name; 171 if (is_null($data)) $data = 'bla'; 172 $this->data = $data; 173 174 } 175 176 function getName() { 177 178 return $this->name; 179 180 } 181 182 function get() { 183 184 return $this->data; 185 186 } 187 188 function getProperties($properties) { 189 190 return $this->properties; 191 192 } 193 194 /** 195 * Updates properties on this node. 196 * 197 * This method received a PropPatch object, which contains all the 198 * information about the update. 199 * 200 * To update specific properties, call the 'handle' method on this object. 201 * Read the PropPatch documentation for more information. 202 * 203 * @param array $mutations 204 * @return bool|array 205 */ 206 function propPatch(PropPatch $propPatch) { 207 208 $this->properties = $propPatch->getMutations(); 209 $propPatch->setRemainingResultCode(200); 210 211 } 212 213} 214 215class TreeMultiGetTester extends TreeDirectoryTester implements IMultiGet { 216 217 /** 218 * This method receives a list of paths in it's first argument. 219 * It must return an array with Node objects. 220 * 221 * If any children are not found, you do not have to return them. 222 * 223 * @return array 224 */ 225 function getMultipleChildren(array $paths) { 226 227 $result = []; 228 foreach($paths as $path) { 229 try { 230 $child = $this->getChild($path); 231 $result[] = $child; 232 } catch (Exception\NotFound $e) { 233 // Do nothing 234 } 235 } 236 237 return $result; 238 239 } 240 241} 242