Skip to content

Commit

Permalink
#277 - added the TreeGraph::get() method, and a unit test for the Tre…
Browse files Browse the repository at this point in the history
…eGraph class
  • Loading branch information
alphadevx committed Apr 17, 2016
1 parent e53e590 commit 44156a4
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Alpha/Util/Graph/TreeGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function __construct($rowSpace = 40, $colSpace = 40, $branchSpace = 80)
* @param string $message
* @param int $w
* @param int $h
* @param string $nodeColour
* @param array $nodeColour
* @param string $URL
*
* @since 1.0
Expand All @@ -196,6 +196,24 @@ public function add($id, $pid, $message = '', $w = 0, $h = 0, $nodeColour, $URL)
$this->nodes[$id] = $node;
}

/**
* Get the specified node from the graph.
*
* @param int $id
*
* @return Alpha\Util\Graph\GraphNode
*
* @since 2.0.1
*/
public function get($id)
{
if (isset($this->nodes[$id])) {
return $this->nodes[$id];
} else {
return null;
}
}

/**
* The first pass of the graph.
*
Expand Down
71 changes: 71 additions & 0 deletions test/Alpha/Test/Util/Graph/TreeGraphTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Alpha\Test\Util\Graph;

use Alpha\Util\Graph\TreeGraph;

/**
* Test cases for the TreeGraph class.
*
* @since 2.0.1
*
* @author John Collins <dev@alphaframework.org>
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
* All rights reserved.
*
* <pre>
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
* * Neither the name of the Alpha Framework nor the names
* of its contributors may be used to endorse or promote
* products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* </pre>
*/
class TreeGraphTest extends \PHPUnit_Framework_TestCase
{
public function testAdd()
{
$graph = new TreeGraph();
$graph->add(1, 0, 'First child', 10, 10, array(0, 0, 0), 'http://www.alphaframework.org/');

$this->assertEquals('First child', $graph->get(1)->getMessage(), 'Testing the add method');
}

public function testNext()
{
$graph = new TreeGraph();
$graph->add(1, 0, 'First child', 10, 10, array(0, 0, 0), 'http://www.alphaframework.org/');
$graph->add(2, 0, 'Second child', 10, 10, array(0, 0, 0), 'http://www.alphaframework.org/');

$this->assertTrue($graph->hasNext(), 'Testing the hasNext method');
$this->assertEquals('First child', $graph->next()->getMessage(), 'Testing the next method');
$this->assertTrue($graph->hasNext(), 'Testing the hasNext method');
$this->assertEquals('Second child', $graph->next()->getMessage(), 'Testing the next method');
$this->assertFalse($graph->hasNext(), 'Testing the hasNext method');
}
}

0 comments on commit 44156a4

Please sign in to comment.