Skip to content

Commit

Permalink
Merge pull request #23 from koriym/nostate
Browse files Browse the repository at this point in the history
Fix #21: Non application state is not appeared in diagram
  • Loading branch information
koriym authored Nov 16, 2020
2 parents 2555716 + 235d237 commit cd41ed8
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 16 deletions.
12 changes: 12 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
codecov:
notify:
require_ci_to_pass: yes

coverage:
status:
project:
default:
target: 90%
patch:
default:
target: 90%
32 changes: 30 additions & 2 deletions src/DrawDiagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use stdClass;

use function assert;
use function in_array;
use function sprintf;
use function strpos;
use function substr;
Expand All @@ -20,9 +21,10 @@ final class DrawDiagram

public function __invoke(AlpsProfile $profile): string
{
$transNodes = $this->getTransNodes($profile);
$appSate = new AppState($profile->links, $profile->descriptors);
$this->descriptors = $profile->descriptors;
$nodes = $this->getNodes($appSate);
$nodes = $this->getNodes($appSate, $transNodes);
$edge = new Edge($profile);
$graph = (string) $edge;

Expand All @@ -46,16 +48,42 @@ public function __invoke(AlpsProfile $profile): string
return sprintf($template, $profile->title, $nodes, $graph, $appSateWithNoLink);
}

public function getNodes(AppState $appSate): string
/**
* @param list<string> $transNodes
*/
public function getNodes(AppState $appSate, array $transNodes): string
{
$dot = '';
foreach ($this->descriptors as $descriptor) {
if (! in_array($descriptor->id, $transNodes)) {
continue;
}

$dot .= $this->getNode($descriptor, $appSate);
}

return $dot;
}

/**
* @return list<string>
*/
private function getTransNodes(AlpsProfile $profile): array
{
$transNodes = [];
foreach ($profile->links as $link) {
if (! in_array($link->from, $transNodes)) {
$transNodes[] = $link->from;
}

if (! in_array($link->to, $transNodes)) {
$transNodes[] = $link->to;
}
}

return $transNodes;
}

private function getNode(AbstractDescriptor $descriptor, AppState $appSate): string
{
$hasDescriptor = $descriptor instanceof SemanticDescriptor && isset($descriptor->descriptor);
Expand Down
20 changes: 6 additions & 14 deletions tests/DrawDiagramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,10 @@ public function testMultipleLink(): void
$this->assertSame(1, $numberOfArrow);
}

// public function testInvalidHref(): void
// {
// $this->expectException(InvalidHrefException::class);
// $descriptor = new SemanticDescriptor(json_decode((string) file_get_contents(__DIR__ . '/Fake/invalid_href.json')));
// ($this->drawDiagram)([], [$descriptor]);
// }
//
// public function testBox(): void
// {
// $descriptor = new SemanticDescriptor(json_decode((string) file_get_contents(__DIR__ . '/Fake/BlogPosting.json')));
// $dot = ($this->drawDiagram)([], [$descriptor]);
// $this->assertStringContainsString('(articleBody)', $dot);
// $this->assertStringContainsString('(dateCreated)', $dot);
// }
public function testNoState(): void
{
$alpsFile = __DIR__ . '/Fake/no_state.json';
$dot = ($this->drawDiagram)(new AlpsProfile($alpsFile));
$this->assertStringNotContainsString('name [', $dot);
}
}
48 changes: 48 additions & 0 deletions tests/Fake/no_state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"$schema": "schema/alps.json",
"alps": {
"descriptor": [
{
"id": "Index",
"type": "semantic",
"descriptor": [
{
"id": "user",
"rt": "#User",
"type": "safe"
}
]
},
{
"id": "User",
"type": "semantic",
"descriptor": [
{
"id": "index",
"rt": "#Index",
"type": "safe"
},
{
"href": "#name"
}
]
},
{
"id": "name",
"type": "semantic",
"descriptor": [
{
"id": "giveName",
"type": "semantic",
"def": "https://schema.org/givenName"
},
{
"id": "familyName",
"type": "semantic",
"def": "https://schema.org/familyName"
}
]
}
]
}
}

0 comments on commit cd41ed8

Please sign in to comment.