test_node.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. require_once 'node.php';
  3. use \WebBasics\Node;
  4. class NodeTest extends PHPUnit_Framework_TestCase {
  5. var $autoloader;
  6. function setUp() {
  7. $this->root = new Node('test node');
  8. }
  9. function test_get_id() {
  10. $this->assertEquals($this->root->get_id(), 1);
  11. $this->assertEquals(Node::create('')->get_id(), 2);
  12. }
  13. function test_get_name() {
  14. $this->assertEquals($this->root->get_name(), 'test node');
  15. $this->assertEquals(Node::create('second node')->get_name(), 'second node');
  16. }
  17. function test_get_parent() {
  18. $this->assertNull($this->root->get_parent());
  19. $this->assertSame(Node::create('', $this->root)->get_parent(), $this->root);
  20. }
  21. function test_is() {
  22. $mirror = $this->root;
  23. $this->assertTrue($mirror->is($this->root));
  24. $this->assertFalse(Node::create('')->is($this->root));
  25. }
  26. function test_is_root() {
  27. $this->assertTrue($this->root->is_root());
  28. $this->assertFalse(Node::create('', $this->root)->is_root());
  29. }
  30. function test_add_child() {
  31. $node = new Node('');
  32. $this->root->add_child($node);
  33. $this->assertAttributeEquals(array($node), 'children', $this->root);
  34. $this->assertSame($node->get_parent(), $this->root);
  35. }
  36. /**
  37. * @depends test_add_child
  38. */
  39. function test_get_children() {
  40. $this->assertEquals($this->root->get_children(), array());
  41. $node = new Node('');
  42. $this->root->add_child($node);
  43. $this->assertSame($this->root->get_children(), array($node));
  44. }
  45. function test_add_child_no_set_parent() {
  46. $node = new Node('');
  47. $this->root->add_child($node, false);
  48. $this->assertAttributeEquals(array($node), 'children', $this->root);
  49. $this->assertNull($node->get_parent());
  50. }
  51. /**
  52. * @depends test_add_child
  53. */
  54. function test_is_leaf() {
  55. $node = new Node('');
  56. $this->root->add_child($node);
  57. $this->assertTrue($node->is_leaf());
  58. $this->assertFalse($this->root->is_leaf());
  59. }
  60. /**
  61. * @depends test_add_child
  62. */
  63. function test_add() {
  64. $node = $this->root->add('name', array('foo' => 'bar'));
  65. $this->assertEquals($node->get_name(), 'name');
  66. $this->assertEquals($node->get('foo'), 'bar');
  67. $this->assertSame($node->get_parent(), $this->root);
  68. }
  69. /**
  70. * @depends test_add
  71. */
  72. function test_remove_child() {
  73. $node1 = $this->root->add('name', array('foo' => 'bar'));
  74. $node2 = $this->root->add('name', array('foo' => 'bar'));
  75. $this->root->remove_child($node2);
  76. $this->assertAttributeSame(array($node1), 'children', $this->root);
  77. }
  78. /**
  79. * @depends test_remove_child
  80. */
  81. function test_remove_leaf() {
  82. $node1 = $this->root->add('name', array('foo' => 'bar'));
  83. $node2 = $this->root->add('name', array('foo' => 'bar'));
  84. $node1->remove();
  85. $this->assertAttributeSame(array($node2), 'children', $this->root);
  86. }
  87. /**
  88. * @depends test_remove_leaf
  89. */
  90. function test_remove_node() {
  91. $node = $this->root->add('node');
  92. $leaf = $node->add('leaf');
  93. $node->remove();
  94. $this->assertAttributeEquals(array(), 'children', $this->root);
  95. $this->assertNull($leaf->get_parent());
  96. }
  97. /**
  98. * @depends test_remove_child
  99. * @expectedException \RuntimeException
  100. */
  101. function test_remove_root() {
  102. $node1 = $this->root->add('name', array('foo' => 'bar'));
  103. $node2 = $this->root->add('name', array('foo' => 'bar'));
  104. $this->root->remove();
  105. $this->assertAttributeSame(array($node2), 'children', $this->root);
  106. }
  107. function test_set_single() {
  108. $this->root->set('foo', 'bar');
  109. $this->assertAttributeEquals(array('foo' => 'bar'), 'variables', $this->root);
  110. $this->root->set('bar', 'baz');
  111. $this->assertAttributeEquals(array('foo' => 'bar', 'bar' => 'baz'), 'variables', $this->root);
  112. }
  113. function test_set_return() {
  114. $this->assertSame($this->root->set('foo', 'bar'), $this->root);
  115. }
  116. function test_set_multiple() {
  117. $this->root->set(array('foo' => 'bar'));
  118. $this->assertAttributeEquals(array('foo' => 'bar'), 'variables', $this->root);
  119. $this->root->set(array('bar' => 'baz'));
  120. $this->assertAttributeEquals(array('foo' => 'bar', 'bar' => 'baz'), 'variables', $this->root);
  121. }
  122. /**
  123. * @depends test_set_single
  124. */
  125. function test___set() {
  126. $this->root->foo = 'bar';
  127. $this->assertAttributeEquals(array('foo' => 'bar'), 'variables', $this->root);
  128. $this->root->bar = 'baz';
  129. $this->assertAttributeEquals(array('foo' => 'bar', 'bar' => 'baz'), 'variables', $this->root);
  130. }
  131. /**
  132. * @depends test_set_multiple
  133. */
  134. function test_get_direct() {
  135. $this->root->set(array('foo' => 'bar', 'bar' => 'baz'));
  136. $this->assertEquals($this->root->get('foo'), 'bar');
  137. $this->assertEquals($this->root->get('bar'), 'baz');
  138. }
  139. /**
  140. * @depends test_get_direct
  141. */
  142. function test___get() {
  143. $this->root->set(array('foo' => 'bar', 'bar' => 'baz'));
  144. $this->assertEquals($this->root->foo, 'bar');
  145. $this->assertEquals($this->root->bar, 'baz');
  146. }
  147. /**
  148. * @depends test_set_single
  149. */
  150. function test_get_ancestor() {
  151. $this->root->set('foo', 'bar');
  152. $node = $this->root->add('');
  153. $this->assertEquals($node->get('foo'), 'bar');
  154. }
  155. function test_get_failure() {
  156. $this->assertNull($this->root->get('foo'));
  157. }
  158. /**
  159. * @depends test_get_name
  160. */
  161. function test_find() {
  162. $node1 = $this->root->add('foo');
  163. $node2 = $this->root->add('bar');
  164. $node3 = $this->root->add('foo');
  165. $this->assertSame($this->root->find('foo'), array($node1, $node3));
  166. }
  167. /**
  168. * @depends test_set_multiple
  169. */
  170. function test_copy_simple() {
  171. $copy = $this->root->copy();
  172. $this->assertEquals($this->root, $copy);
  173. $this->assertNotSame($this->root, $copy);
  174. }
  175. /**
  176. * @depends test_copy_simple
  177. */
  178. function test_copy_shallow() {
  179. $child = $this->root->add('');
  180. $copy = $this->root->copy();
  181. $this->assertAttributeSame(array($child), 'children', $copy);
  182. }
  183. /**
  184. * @depends test_get_children
  185. * @depends test_copy_simple
  186. */
  187. function test_copy_deep() {
  188. $child = $this->root->add('foo');
  189. $copy = $this->root->copy(true);
  190. $copy_children = $copy->get_children();
  191. $child_copy = reset($copy_children);
  192. $this->assertNotSame($copy_children, $this->root->get_children());
  193. $this->assertSame($child_copy->get_parent(), $copy);
  194. }
  195. }
  196. ?>