1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bex\Behat\BehatRSTSpecificationLocatorExtension\RST; |
4
|
|
|
|
5
|
|
|
use Behat\Gherkin\Node\FeatureNode; |
6
|
|
|
use Behat\Gherkin\Parser as GherkinParser; |
7
|
|
|
use Doctrine\RST\Nodes\CodeNode; |
8
|
|
|
use Doctrine\RST\Nodes\QuoteNode; |
9
|
|
|
use Doctrine\RST\Parser as DoctrineRSTParser; |
10
|
|
|
|
11
|
|
|
class Parser |
12
|
|
|
{ |
13
|
|
|
/** @var DoctrineRSTParser */ |
14
|
|
|
private $parser; |
15
|
|
|
|
16
|
|
|
/** @var GherkinParser */ |
17
|
|
|
private $gherkinParser; |
18
|
|
|
|
19
|
|
|
public function __construct(DoctrineRSTParser $parser, GherkinParser $gherkinParser) |
20
|
|
|
{ |
21
|
|
|
$this->parser = $parser; |
22
|
|
|
$this->gherkinParser = $gherkinParser; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function parse(string $input, string $file = null): ?FeatureNode |
26
|
|
|
{ |
27
|
|
|
$mainDocument = $this->parser->parse($input); |
28
|
|
|
|
29
|
|
|
// collect documents (content of quote notes counts as an embedded document) |
30
|
|
|
$documents = [$mainDocument]; |
31
|
|
|
foreach ($mainDocument->getNodes() as $node) { |
32
|
|
|
if ($node instanceof QuoteNode) { |
33
|
|
|
$documents[] = $node->getValue(); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// collect all code node |
38
|
|
|
$gherkinNodes = []; |
39
|
|
|
foreach ($documents as $document) { |
40
|
|
|
$documentGherkinNodes = $document->getNodes(function ($node) { |
41
|
|
|
return ($node instanceof CodeNode) && ($node->getLanguage() === 'gherkin'); |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
$gherkinNodes = array_merge($gherkinNodes, $documentGherkinNodes); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$feature = $mainDocument->getTitle(); |
48
|
|
|
|
49
|
|
|
$text = ''; |
50
|
|
|
foreach ($gherkinNodes as $node) { |
51
|
|
|
$text .= $node->getValue() . PHP_EOL; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->gherkinParser->parse('Feature: ' . $feature . PHP_EOL . $text, $file); |
55
|
|
|
} |
56
|
|
|
} |