|
| 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