packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/Operator/BinaryOperatorTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Operator;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Rules\Expression\Precedence;
11use SqlSemantics\Platform\Sqlite\Statement\Expression\Operator\BinaryOperator;
12
13#[CoversClass(BinaryOperator::class)]
14#[Small]
15final class BinaryOperatorTest extends TestCase
16{
17 public function testLevelOrdersTheGroupsFromWeakestToTightest(): void
18 {
19 self::assertSame(Precedence::DISJUNCTION, BinaryOperator::Or->level());
20 self::assertSame(Precedence::CONJUNCTION, BinaryOperator::And->level());
21 self::assertSame(Precedence::EQUALITY, BinaryOperator::Equal->level());
22 self::assertSame(Precedence::COMPARISON, BinaryOperator::Less->level());
23 self::assertSame(Precedence::BITWISE, BinaryOperator::BitAnd->level());
24 self::assertSame(Precedence::ADDITIVE, BinaryOperator::Add->level());
25 self::assertSame(Precedence::MULTIPLICATIVE, BinaryOperator::Multiply->level());
26 self::assertSame(Precedence::CONCATENATION, BinaryOperator::Concat->level());
27 self::assertLessThan(BinaryOperator::And->level(), BinaryOperator::Or->level());
28 self::assertLessThan(BinaryOperator::Concat->level(), BinaryOperator::Multiply->level());
29 }
30
31 public function testLevelGivesEveryMemberOfAGroupTheSameLevel(): void
32 {
33 $levels = array_map(static fn (BinaryOperator $operator): int => $operator->level(), BinaryOperator::cases());
34
35 self::assertSame([1, 2, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 7, 7, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10], $levels);
36 }
37
38 public function testWordedHoldsForTheKeywordOperatorsOnly(): void
39 {
40 $worded = array_filter(BinaryOperator::cases(), static fn (BinaryOperator $operator): bool => $operator->worded());
41
42 self::assertSame([BinaryOperator::Or, BinaryOperator::And, BinaryOperator::Is, BinaryOperator::IsNot, BinaryOperator::IsDistinctFrom, BinaryOperator::IsNotDistinctFrom], array_values($worded));
43 self::assertFalse(BinaryOperator::Equal->worded());
44 self::assertFalse(BinaryOperator::ExtractValue->worded());
45 }
46
47 public function testWordedOperatorsSpellTheirKeywordsSeparatedBySpaces(): void
48 {
49 self::assertSame('IS NOT DISTINCT FROM', BinaryOperator::IsNotDistinctFrom->value);
50 self::assertSame('IS NOT', BinaryOperator::IsNot->value);
51 self::assertSame('->>', BinaryOperator::ExtractValue->value);
52 self::assertSame('||', BinaryOperator::Concat->value);
53 }
54}
55