Skip to content
This repository was archived by the owner on Sep 27, 2021. It is now read-only.

Commit 06bc985

Browse files
committed
Graph algorithm is now a configuration setting
1 parent ff09b66 commit 06bc985

File tree

4 files changed

+107
-16
lines changed

4 files changed

+107
-16
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@ The extension can be installed using [Composer](https://getcomposer.org)
2323
$ composer require edno/codeception-graphwalker
2424
```
2525

26-
And add the **GraphWalker** format to the list of supported format into your suite configuration file (`.suite.yml`):
26+
## Configuration
27+
Add the **GraphWalker** format to the list of supported format into your suite configuration file (`.suite.yml`):
2728
```yaml
2829
formats:
2930
- \edno\codeception-graphwalker\GraphWalker
3031
```
3132
32-
## Usage
33-
> To be documented
33+
In the configuration file, declare the graph algorithm class to be used:
34+
```yaml
35+
graphwalker:
36+
algorithm: Graphp\Algorithms\ShortestPath\Dijkstra
37+
```
38+
*Refer to [graphp/algorithms](https://github.com/graphp/algorithms) for supported algorithms.*
3439
3540
## License
3641
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fedno%2Fcodeception-graphwalker.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fedno%2Fcodeception-graphwalker?ref=badge_large)

src/GraphWalker.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,61 @@
22
namespace edno\Codeception;
33

44
use edno\GraphYEd\Loader as GraphLoader;
5-
use Graphp\Algorithms\ShortestPath\Dijkstra;
65
use Codeception\Test\Loader\LoaderInterface;
76
use Codeception\Test\Loader as TestLoader;
7+
use Codeception\Lib\Di;
8+
use Codeception\Exception\ModuleConfigException;
9+
use Codeception\Exception\TestParseException;
10+
use Codeception\Configuration;
811

912
class GraphWalker implements LoaderInterface
1013
{
14+
15+
protected static $defaultSettings = [
16+
'graphwalker' => [
17+
'algorithm' => ''
18+
],
19+
'path' => ''
20+
];
21+
22+
protected $settings = [];
23+
1124
protected $graph;
1225

1326
protected $parser;
1427

1528
protected $tests = [];
1629

17-
protected $settings = [];
18-
1930
protected $path;
2031

32+
protected $di;
33+
34+
protected $algorithmClass;
35+
2136
public function __construct($settings = [])
2237
{
23-
if(isset($settings['path'])) {
24-
$this->path = $settings['path'];
25-
} else {
26-
$this->path = 'tests/';
38+
$this->settings = Configuration::mergeConfigs(self::$defaultSettings, $settings);
39+
$this->algorithmClass = $this->settings['graphwalker']['algorithm'];
40+
if($this->algorithmClass == '' ) {
41+
throw new ModuleConfigException(__CLASS__, 'Configuration setting "algorithm" is missing');
2742
}
28-
$this->settings = $settings;
29-
43+
44+
$this->di = new Di();
45+
3046
$this->parser = new GraphLoader();
3147
$this->loader = new TestLoader($this->settings);
3248
}
3349

3450
public function loadTests($filename)
3551
{
36-
$this->graph = $this->parser->loadContents(file_get_contents($filename));
37-
$shortestPath = new Dijkstra($this->graph->getVertices()->getVertexFirst());
38-
$path = $shortestPath->getWalkTo($this->graph->getVertices()->getVertexLast())->getGraph();
52+
try {
53+
$this->graph = $this->parser->loadContents(file_get_contents($filename));
54+
$start = $this->graph->getVertices()->getVertexFirst();
55+
$algorithm = $this->di->instantiate($this->algorithmClass, [$start]);
56+
} catch (\Exception $e) {
57+
throw new TestParseException(__CLASS__, $e->getMessage());
58+
}
59+
$path = $algorithm->getWalkTo($this->graph->getVertices()->getVertexLast())->getGraph();
3960
$steps = $path->getVertices();
4061
foreach($steps as $step) {
4162
$file = $this->path . $step->getAttribute('labels')[0];

tests/_data/notagrapfile,graphml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is for testing purpose only

tests/unit/BasicGraphTest.php

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ protected function _before()
1313
public function testGetTestsFromModel()
1414
{
1515
$model = codecept_data_dir().'basicgraph.graphml';
16-
$graphwalker = new GraphWalker(['path' => 'tests/_data/']);
16+
$graphwalker = new GraphWalker([
17+
'path' => 'tests/_data/',
18+
'graphwalker' => [
19+
'algorithm' => 'Graphp\Algorithms\ShortestPath\Dijkstra'
20+
]
21+
]);
1722
$graphwalker->loadTests($model);
1823
$this->assertInstanceOf('\Fhaculty\Graph\Graph',$graphwalker->getGraph());
1924
$tests = $graphwalker->getTests();
@@ -26,4 +31,63 @@ public function testGetTestsFromModel()
2631
}
2732

2833

34+
/**
35+
* @expectedException Codeception\Exception\ModuleConfigException
36+
*/
37+
public function testExceptionAlgorithmSettingMissing()
38+
{
39+
$graphwalker = new GraphWalker();
40+
}
41+
42+
/**
43+
* @expectedException Codeception\Exception\TestParseException
44+
*/
45+
public function testExceptionAlgorithmSettingInvalid()
46+
{
47+
$model = codecept_data_dir().'basicgraph.graphml';
48+
$graphwalker = new GraphWalker([
49+
'path' => 'tests/_data/',
50+
'graphwalker' => [
51+
'algorithm' => 'Graphp\Algorithms\DoesNotExist'
52+
]
53+
]);
54+
$graphwalker->loadTests($model);
55+
$this->assertInstanceOf('\Fhaculty\Graph\Graph',$graphwalker->getGraph());
56+
$tests = $graphwalker->getTests();
57+
}
58+
59+
/**
60+
* @expectedException Codeception\Exception\TestParseException
61+
*/
62+
public function testExceptionTestFileNotExist()
63+
{
64+
$model = codecept_data_dir().'doesnotexist.graphml';
65+
$graphwalker = new GraphWalker([
66+
'path' => 'tests/_data/',
67+
'graphwalker' => [
68+
'algorithm' => 'Graphp\Algorithms\ShortestPath\Dijkstra'
69+
]
70+
]);
71+
$graphwalker->loadTests($model);
72+
$this->assertInstanceOf('\Fhaculty\Graph\Graph',$graphwalker->getGraph());
73+
$tests = $graphwalker->getTests();
74+
}
75+
76+
/**
77+
* @expectedException Codeception\Exception\TestParseException
78+
*/
79+
public function testExceptionTestFileInvalid()
80+
{
81+
$model = codecept_data_dir().'notagrapfile.graphml';
82+
$graphwalker = new GraphWalker([
83+
'path' => 'tests/_data/',
84+
'graphwalker' => [
85+
'algorithm' => 'Graphp\Algorithms\ShortestPath\Dijkstra'
86+
]
87+
]);
88+
$graphwalker->loadTests($model);
89+
$this->assertInstanceOf('\Fhaculty\Graph\Graph',$graphwalker->getGraph());
90+
$tests = $graphwalker->getTests();
91+
}
92+
2993
}

0 commit comments

Comments
 (0)