Skip to content

Commit a7c3867

Browse files
committed
Add Range node generator and tests
1 parent 285543d commit a7c3867

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace QueryTranslator\Languages\Galach\Generators\Native;
4+
5+
use LogicException;
6+
use QueryTranslator\Languages\Galach\Generators\Common\Visitor;
7+
use QueryTranslator\Languages\Galach\Values\Node\Term;
8+
use QueryTranslator\Languages\Galach\Values\Token\Range as RangeToken;
9+
use QueryTranslator\Values\Node;
10+
11+
/**
12+
* Range Node Visitor implementation.
13+
*/
14+
final class Range extends Visitor
15+
{
16+
public function accept(Node $node)
17+
{
18+
return $node instanceof Term && $node->token instanceof RangeToken;
19+
}
20+
21+
public function visit(Node $node, Visitor $subVisitor = null, $options = null)
22+
{
23+
if (!$node instanceof Term) {
24+
throw new LogicException(
25+
'Implementation accepts instance of Term Node'
26+
);
27+
}
28+
29+
$token = $node->token;
30+
31+
if (!$token instanceof RangeToken) {
32+
throw new LogicException(
33+
'Implementation accepts instance of Range Token'
34+
);
35+
}
36+
37+
$domainPrefix = '' === $token->domain ? '' : "{$token->domain}:";
38+
39+
switch ($token->type) {
40+
case RangeToken::TYPE_INCLUSIVE:
41+
return $domainPrefix . '[' . $token->rangeFrom . ' TO ' . $token->rangeTo . ']';
42+
43+
case RangeToken::TYPE_EXCLUSIVE:
44+
return $domainPrefix . '{' . $token->rangeFrom . ' TO ' . $token->rangeTo . '}';
45+
46+
default:
47+
throw new LogicException(sprintf('Range type %s is not supported', $token->type));
48+
}
49+
}
50+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace QueryTranslator\Tests\Galach\Generators\Native;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use QueryTranslator\Languages\Galach\Generators\Common\Visitor;
7+
use QueryTranslator\Languages\Galach\Generators\Native\Range;
8+
use QueryTranslator\Languages\Galach\Values\Node\Mandatory;
9+
use QueryTranslator\Languages\Galach\Values\Node\Term;
10+
use QueryTranslator\Languages\Galach\Values\Token\Range as RangeToken;
11+
use QueryTranslator\Languages\Galach\Values\Token\Word;
12+
use QueryTranslator\Values\Node;
13+
14+
class RangeTest extends TestCase
15+
{
16+
/**
17+
* @var Visitor
18+
*/
19+
public $visitor;
20+
21+
protected function setUp()
22+
{
23+
$this->visitor = new Range();
24+
}
25+
26+
public function acceptDataprovider()
27+
{
28+
return [
29+
[true, new Term(new RangeToken('[a TO b]', 0, '', 'a', 'b', 'inclusive'))],
30+
[false, new Term(new Word('word', 0, '', 'a'))],
31+
];
32+
}
33+
34+
/**
35+
* @param bool $expected
36+
* @param Node $token
37+
*
38+
* @dataProvider acceptDataprovider
39+
*/
40+
public function testAccepts($expected, $node)
41+
{
42+
$this->assertSame($expected, $this->visitor->accept($node));
43+
}
44+
45+
public function visitDataprovider()
46+
{
47+
return [
48+
['[a TO b]', new Term(new RangeToken('[a TO b]', 0, '', 'a', 'b', 'inclusive'))],
49+
['{a TO b}', new Term(new RangeToken('{a TO b}', 0, '', 'a', 'b', 'exclusive'))],
50+
];
51+
}
52+
53+
/**
54+
* @param string $expected
55+
* @param Node $token
56+
*
57+
* @dataProvider visitDataprovider
58+
*/
59+
public function testVisit($expected, $node)
60+
{
61+
$this->assertSame($expected, $this->visitor->visit($node));
62+
}
63+
64+
public function visitWrongNodeDataprovider()
65+
{
66+
return [
67+
[new Mandatory()],
68+
[new Term(new Word('word', 0, '', 'a'))],
69+
];
70+
}
71+
72+
/**
73+
* @param string $expected
74+
* @param Node $token
75+
*
76+
* @dataProvider visitWrongNodeDataprovider
77+
*/
78+
public function testVisitWrongNodeFails($node)
79+
{
80+
$this->expectException(\LogicException::class);
81+
$this->visitor->visit($node);
82+
}
83+
84+
public function testVisitUnknownTypeFails()
85+
{
86+
$this->expectException(\LogicException::class);
87+
$node = new Term(new RangeToken('{a TO b}', 0, '', 'a', 'b', 'unknown'));
88+
$this->visitor->visit($node);
89+
}
90+
}

0 commit comments

Comments
 (0)