RangeTest::testVisitUnknownRangeEndTypeFails()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php declare(strict_types = 1);
2
3
namespace Apicart\FQL\Tests\Generator\Native;
4
5
use Apicart\FQL\Generator\Common\AbstractVisitor;
6
use Apicart\FQL\Generator\Native\Range;
7
use Apicart\FQL\Token\Node\Mandatory;
8
use Apicart\FQL\Token\Node\Term;
9
use Apicart\FQL\Token\Token\Range as RangeToken;
10
use Apicart\FQL\Token\Token\Word;
11
use Apicart\FQL\Tokenizer\Tokenizer;
12
use Apicart\FQL\Value\AbstractNode;
13
use Apicart\FQL\Value\Token;
14
use LogicException;
15
use PHPUnit\Framework\TestCase;
16
17
final class RangeTest extends TestCase
18
{
19
    /**
20
     * @var AbstractVisitor
21
     */
22
    public $visitor;
23
24
25
    protected function setUp(): void
26
    {
27
        $this->visitor = new Range();
28
    }
29
30
31
    public function acceptDataprovider(): array
32
    {
33
        return [
34
            [true, new Term(new RangeToken('[a TO b]', 0, '', 'a', 'b', 'inclusive', 'inclusive'))],
35
            [false, new Term(new Word('word', 0, '', 'a'))],
36
        ];
37
    }
38
39
40
    /**
41
     * @dataProvider acceptDataprovider
42
     */
43
    public function testAccepts(bool $expected, AbstractNode $node): void
44
    {
45
        self::assertSame($expected, $this->visitor->accept($node));
46
    }
47
48
49
    public function visitDataprovider(): array
50
    {
51
        return [
52
            ['[a TO b]', new Term(new RangeToken('[a TO b]', 0, '', 'a', 'b', 'inclusive', 'inclusive'))],
53
            ['[a TO b}', new Term(new RangeToken('[a TO b}', 0, '', 'a', 'b', 'inclusive', 'exclusive'))],
54
            ['{a TO b}', new Term(new RangeToken('{a TO b}', 0, '', 'a', 'b', 'exclusive', 'exclusive'))],
55
            ['{a TO b]', new Term(new RangeToken('{a TO b]', 0, '', 'a', 'b', 'exclusive', 'inclusive'))],
56
        ];
57
    }
58
59
60
    /**
61
     * @dataProvider visitDataprovider
62
     */
63
    public function testVisit(string $expected, AbstractNode $node): void
64
    {
65
        self::assertSame($expected, $this->visitor->visit($node));
66
    }
67
68
69
    public function visitWrongNodeDataprovider(): array
70
    {
71
        $operand = $this->getMockForAbstractClass(AbstractNode::class);
72
        $token = new Token(Tokenizer::TOKEN_TERM, '', 0);
73
74
        return [[new Mandatory($operand, $token)], [new Term(new Word('word', 0, '', 'a'))]];
75
    }
76
77
78
    /**
79
     * @dataProvider visitWrongNodeDataprovider
80
     */
81
    public function testVisitWrongNodeFails(AbstractNode $node): void
82
    {
83
        $this->expectException(LogicException::class);
84
        $this->visitor->visit($node);
85
    }
86
87
88
    public function testVisitUnknownRangeStartTypeFails(): void
89
    {
90
        $token = new RangeToken('{a TO b}', 0, '', 'a', 'b', 'inclusive', 'inclusive');
91
        $token->setStartType('unknown');
92
        $node = new Term($token);
93
        $this->expectException(LogicException::class);
94
        $this->expectExceptionMessage('Range start type unknown is not supported');
95
        $this->visitor->visit($node);
96
    }
97
98
99
    public function testVisitUnknownRangeEndTypeFails(): void
100
    {
101
        $token = new RangeToken('{a TO b}', 0, '', 'a', 'b', 'inclusive', 'inclusive');
102
        $token->setEndType('unknown');
103
        $node = new Term($token);
104
        $this->expectException(LogicException::class);
105
        $this->expectExceptionMessage('Range end type unknown is not supported');
106
        $this->visitor->visit($node);
107
    }
108
109
}
110