VertexTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. use Fhaculty\Graph\Graph;
  3. use Fhaculty\Graph\Vertex;
  4. class VertexTest extends AbstractAttributeAwareTest
  5. {
  6. public function setUp()
  7. {
  8. $this->graph = new Graph();
  9. $this->vertex = $this->graph->createVertex(1);
  10. }
  11. public function testPrecondition()
  12. {
  13. $this->assertCount(1, $this->graph->getVertices());
  14. $this->assertTrue($this->graph->hasVertex(1));
  15. $this->assertFalse($this->graph->hasVertex(2));
  16. $this->assertSame($this->vertex, $this->graph->getVertex(1));
  17. }
  18. public function testConstructor()
  19. {
  20. $v2 = new Vertex($this->graph, 2);
  21. $this->assertCount(2, $this->graph->getVertices());
  22. $this->assertTrue($this->graph->hasVertex(2));
  23. $this->assertSame($v2, $this->graph->getVertex(2));
  24. }
  25. /**
  26. * @expectedException OverflowException
  27. */
  28. public function testCanNotConstructDuplicateVertex()
  29. {
  30. $v2 = new Vertex($this->graph, 1);
  31. }
  32. public function testEdges()
  33. {
  34. // v1 -> v2, v1 -- v3, v1 <- v4
  35. $v2 = $this->graph->createVertex(2);
  36. $v3 = $this->graph->createVertex(3);
  37. $v4 = $this->graph->createVertex(4);
  38. $e1 = $this->vertex->createEdgeTo($v2);
  39. $e2 = $this->vertex->createEdge($v3);
  40. $e3 = $v4->createEdgeTo($this->vertex);
  41. $this->assertEquals(array($e1, $e2, $e3), $this->vertex->getEdges()->getVector());
  42. $this->assertEquals(array($e2, $e3), $this->vertex->getEdgesIn()->getVector());
  43. $this->assertEquals(array($e1, $e2), $this->vertex->getEdgesOut()->getVector());
  44. $this->assertTrue($this->vertex->hasEdgeTo($v2));
  45. $this->assertTrue($this->vertex->hasEdgeTo($v3));
  46. $this->assertFalse($this->vertex->hasEdgeTo($v4));
  47. $this->assertFalse($this->vertex->hasEdgeFrom($v2));
  48. $this->assertTrue($this->vertex->hasEdgeFrom($v3));
  49. $this->assertTrue($this->vertex->hasEdgeFrom($v4));
  50. $this->assertEquals(array($e1), $this->vertex->getEdgesTo($v2)->getVector());
  51. $this->assertEquals(array($e2), $this->vertex->getEdgesTo($v3)->getVector());
  52. $this->assertEquals(array(), $this->vertex->getEdgesTo($v4)->getVector());
  53. $this->assertEquals(array(), $this->vertex->getEdgesFrom($v2)->getVector());
  54. $this->assertEquals(array($e2), $this->vertex->getEdgesTo($v3)->getVector());
  55. $this->assertEquals(array($e3), $this->vertex->getEdgesFrom($v4)->getVector());
  56. $this->assertEquals(array($v2, $v3, $v4), $this->vertex->getVerticesEdge()->getVector());
  57. $this->assertEquals(array($v2, $v3), $this->vertex->getVerticesEdgeTo()->getVector());
  58. $this->assertEquals(array($v3, $v4), $this->vertex->getVerticesEdgeFrom()->getVector());
  59. }
  60. public function testBalance()
  61. {
  62. $this->vertex->setBalance(10);
  63. $this->assertEquals(10, $this->vertex->getBalance());
  64. }
  65. /**
  66. * @expectedException InvalidArgumentException
  67. */
  68. public function testBalanceInvalid()
  69. {
  70. $this->vertex->setBalance("10");
  71. }
  72. public function testGroup()
  73. {
  74. $this->vertex->setGroup(2);
  75. $this->assertEquals(2, $this->vertex->getGroup());
  76. }
  77. /**
  78. * @expectedException InvalidArgumentException
  79. */
  80. public function testGroupInvalid()
  81. {
  82. $this->vertex->setGroup("3");
  83. }
  84. /**
  85. * @expectedException InvalidArgumentException
  86. */
  87. public function testCreateEdgeOtherGraphFails()
  88. {
  89. $graphOther = new Graph();
  90. $this->vertex->createEdge($graphOther->createVertex(2));
  91. }
  92. /**
  93. * @expectedException InvalidArgumentException
  94. */
  95. public function testCreateEdgeToOtherGraphFails()
  96. {
  97. $graphOther = new Graph();
  98. $this->vertex->createEdgeTo($graphOther->createVertex(2));
  99. }
  100. /**
  101. * @expectedException InvalidArgumentException
  102. */
  103. public function testRemoveInvalidEdge()
  104. {
  105. // 2 -- 3
  106. $v2 = $this->graph->createVertex(2);
  107. $v3 = $this->graph->createVertex(3);
  108. $edge = $v2->createEdge($v3);
  109. $this->vertex->removeEdge($edge);
  110. }
  111. protected function createAttributeAware()
  112. {
  113. return new Vertex(new Graph(), 1);
  114. }
  115. }