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

Commit

Permalink
Graph algorithm is now a configuration setting
Browse files Browse the repository at this point in the history
  • Loading branch information
edno committed Apr 22, 2019
1 parent ff09b66 commit 06bc985
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 16 deletions.
11 changes: 8 additions & 3 deletions README.md
Expand Up @@ -23,14 +23,19 @@ The extension can be installed using [Composer](https://getcomposer.org)
$ composer require edno/codeception-graphwalker
```

And add the **GraphWalker** format to the list of supported format into your suite configuration file (`.suite.yml`):
## Configuration
Add the **GraphWalker** format to the list of supported format into your suite configuration file (`.suite.yml`):
```yaml
formats:
- \edno\codeception-graphwalker\GraphWalker
```

## Usage
> To be documented
In the configuration file, declare the graph algorithm class to be used:
```yaml
graphwalker:
algorithm: Graphp\Algorithms\ShortestPath\Dijkstra
```
*Refer to [graphp/algorithms](https://github.com/graphp/algorithms) for supported algorithms.*

## License
[![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)
45 changes: 33 additions & 12 deletions src/GraphWalker.php
Expand Up @@ -2,40 +2,61 @@
namespace edno\Codeception;

use edno\GraphYEd\Loader as GraphLoader;
use Graphp\Algorithms\ShortestPath\Dijkstra;
use Codeception\Test\Loader\LoaderInterface;
use Codeception\Test\Loader as TestLoader;
use Codeception\Lib\Di;
use Codeception\Exception\ModuleConfigException;
use Codeception\Exception\TestParseException;
use Codeception\Configuration;

class GraphWalker implements LoaderInterface
{

protected static $defaultSettings = [
'graphwalker' => [
'algorithm' => ''
],
'path' => ''
];

protected $settings = [];

protected $graph;

protected $parser;

protected $tests = [];

protected $settings = [];

protected $path;

protected $di;

protected $algorithmClass;

public function __construct($settings = [])
{
if(isset($settings['path'])) {
$this->path = $settings['path'];
} else {
$this->path = 'tests/';
$this->settings = Configuration::mergeConfigs(self::$defaultSettings, $settings);
$this->algorithmClass = $this->settings['graphwalker']['algorithm'];
if($this->algorithmClass == '' ) {
throw new ModuleConfigException(__CLASS__, 'Configuration setting "algorithm" is missing');
}
$this->settings = $settings;


$this->di = new Di();

$this->parser = new GraphLoader();
$this->loader = new TestLoader($this->settings);
}

public function loadTests($filename)
{
$this->graph = $this->parser->loadContents(file_get_contents($filename));
$shortestPath = new Dijkstra($this->graph->getVertices()->getVertexFirst());
$path = $shortestPath->getWalkTo($this->graph->getVertices()->getVertexLast())->getGraph();
try {
$this->graph = $this->parser->loadContents(file_get_contents($filename));
$start = $this->graph->getVertices()->getVertexFirst();
$algorithm = $this->di->instantiate($this->algorithmClass, [$start]);
} catch (\Exception $e) {
throw new TestParseException(__CLASS__, $e->getMessage());
}
$path = $algorithm->getWalkTo($this->graph->getVertices()->getVertexLast())->getGraph();
$steps = $path->getVertices();
foreach($steps as $step) {
$file = $this->path . $step->getAttribute('labels')[0];
Expand Down
1 change: 1 addition & 0 deletions tests/_data/notagrapfile,graphml
@@ -0,0 +1 @@
This is for testing purpose only
66 changes: 65 additions & 1 deletion tests/unit/BasicGraphTest.php
Expand Up @@ -13,7 +13,12 @@ protected function _before()
public function testGetTestsFromModel()
{
$model = codecept_data_dir().'basicgraph.graphml';
$graphwalker = new GraphWalker(['path' => 'tests/_data/']);
$graphwalker = new GraphWalker([
'path' => 'tests/_data/',
'graphwalker' => [
'algorithm' => 'Graphp\Algorithms\ShortestPath\Dijkstra'
]
]);
$graphwalker->loadTests($model);
$this->assertInstanceOf('\Fhaculty\Graph\Graph',$graphwalker->getGraph());
$tests = $graphwalker->getTests();
Expand All @@ -26,4 +31,63 @@ public function testGetTestsFromModel()
}


/**
* @expectedException Codeception\Exception\ModuleConfigException
*/
public function testExceptionAlgorithmSettingMissing()
{
$graphwalker = new GraphWalker();
}

/**
* @expectedException Codeception\Exception\TestParseException
*/
public function testExceptionAlgorithmSettingInvalid()
{
$model = codecept_data_dir().'basicgraph.graphml';
$graphwalker = new GraphWalker([
'path' => 'tests/_data/',
'graphwalker' => [
'algorithm' => 'Graphp\Algorithms\DoesNotExist'
]
]);
$graphwalker->loadTests($model);
$this->assertInstanceOf('\Fhaculty\Graph\Graph',$graphwalker->getGraph());
$tests = $graphwalker->getTests();
}

/**
* @expectedException Codeception\Exception\TestParseException
*/
public function testExceptionTestFileNotExist()
{
$model = codecept_data_dir().'doesnotexist.graphml';
$graphwalker = new GraphWalker([
'path' => 'tests/_data/',
'graphwalker' => [
'algorithm' => 'Graphp\Algorithms\ShortestPath\Dijkstra'
]
]);
$graphwalker->loadTests($model);
$this->assertInstanceOf('\Fhaculty\Graph\Graph',$graphwalker->getGraph());
$tests = $graphwalker->getTests();
}

/**
* @expectedException Codeception\Exception\TestParseException
*/
public function testExceptionTestFileInvalid()
{
$model = codecept_data_dir().'notagrapfile.graphml';
$graphwalker = new GraphWalker([
'path' => 'tests/_data/',
'graphwalker' => [
'algorithm' => 'Graphp\Algorithms\ShortestPath\Dijkstra'
]
]);
$graphwalker->loadTests($model);
$this->assertInstanceOf('\Fhaculty\Graph\Graph',$graphwalker->getGraph());
$tests = $graphwalker->getTests();
}

}

0 comments on commit 06bc985

Please sign in to comment.