packages/sql-semantics-mysql/tests/Unit/Rules/Expression/NumericResultTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\MySql\Model\Statement\Fact\ScalarFact;
12use SqlSemantics\Platform\MySql\Model\Statement\Type\Choice;
13use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\MySql\Rules\Expression\NumericClass;
16use SqlSemantics\Platform\MySql\Rules\Expression\NumericResult;
17use SqlSemantics\Platform\MySql\Statement\Expression\Operator\ArithmeticOperator;
18use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
19use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
20use SqlSemantics\Platform\MySql\Statement\Type\Binary;
21use SqlSemantics\Platform\MySql\Statement\Type\Decimal;
22use SqlSemantics\Platform\MySql\Statement\Type\Floating;
23use SqlSemantics\Platform\MySql\Statement\Type\Integral;
24use SqlSemantics\Platform\MySql\Statement\Type\Kind\BinaryKind;
25use SqlSemantics\Platform\MySql\Statement\Type\Kind\FloatingKind;
26use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
27use SqlSemantics\Platform\MySql\Statement\Type\Kind\NumericModifier;
28
29#[CoversClass(NumericResult::class)]
30#[Small]
31final class NumericResultTest extends TestCase
32{
33 public function testBinaryTypesASumOfAnIntegerAndAString(): void
34 {
35 $integer = new ScalarFact(new Known(new Integral(IntegralKind::Int)), Nullability::NotNull);
36 $text = new ScalarFact(new Known(new Binary(BinaryKind::VarBinary)), Nullability::NotNull);
37
38 self::assertEquals(new Known(new Floating(FloatingKind::Double)), (new NumericResult())->binary(ArithmeticOperator::Plus, new NumberLiteral('1'), $integer, new StringLiteral(['2']), $text, GrammarRelease::MySql847));
39 }
40
41 public function testBinaryMakesAnUnsignedSubtractionTheChoiceTheSessionModeDecides(): void
42 {
43 $unsigned = new ScalarFact(new Known(new Integral(IntegralKind::Int, null, [NumericModifier::Unsigned])), Nullability::NotNull);
44
45 self::assertEquals(new Choice([new Integral(IntegralKind::BigInt, null, [NumericModifier::Unsigned]), new Integral(IntegralKind::BigInt)]), (new NumericResult())->binary(ArithmeticOperator::Minus, new NumberLiteral('1'), $unsigned, new NumberLiteral('2'), $unsigned, GrammarRelease::MySql847));
46 }
47
48 public function testBinaryMakesABitOperationOfBinaryStringsABinaryStringFromMySql80(): void
49 {
50 $bytes = new ScalarFact(new Known(new Binary(BinaryKind::VarBinary)), Nullability::NotNull);
51 $results = new NumericResult();
52
53 self::assertEquals(new Known(new Binary(BinaryKind::VarBinary)), $results->binary(ArithmeticOperator::BitAnd, new StringLiteral(['a']), $bytes, new StringLiteral(['b']), $bytes, GrammarRelease::MySql847));
54 self::assertEquals(new Known(new Integral(IntegralKind::BigInt, null, [NumericModifier::Unsigned])), $results->binary(ArithmeticOperator::BitAnd, new StringLiteral(['a']), $bytes, new StringLiteral(['b']), $bytes, GrammarRelease::MySql5744));
55 }
56
57 public function testArithmeticFollowsTheOperandClasses(): void
58 {
59 $results = new NumericResult();
60
61 self::assertEquals(
62 [new Decimal(), new Integral(IntegralKind::BigInt, null, [NumericModifier::Unsigned]), new Integral(IntegralKind::BigInt), new Floating(FloatingKind::Double), null],
63 [
64 $results->arithmetic(ArithmeticOperator::Divide, NumericClass::Signed, NumericClass::Signed),
65 $results->arithmetic(ArithmeticOperator::Modulo, NumericClass::Unsigned, NumericClass::Signed),
66 $results->arithmetic(ArithmeticOperator::Modulo, NumericClass::Signed, NumericClass::Unsigned),
67 $results->arithmetic(ArithmeticOperator::Multiply, NumericClass::Decimal, NumericClass::Double),
68 $results->arithmetic(ArithmeticOperator::Minus, NumericClass::Signed, NumericClass::Unsigned),
69 ],
70 );
71 }
72
73 public function testBitsIgnoreAHexadecimalLiteral(): void
74 {
75 $bytes = new ScalarFact(new Known(new Binary(BinaryKind::VarBinary)), Nullability::NotNull);
76
77 self::assertEquals(new Known(new Integral(IntegralKind::BigInt, null, [NumericModifier::Unsigned])), (new NumericResult())->bits([[new \SqlSemantics\Platform\MySql\Statement\Literal\RadixLiteral(\SqlSemantics\Platform\MySql\Statement\Literal\Radix::Hexadecimal, '01'), $bytes]], GrammarRelease::MySql847));
78 }
79
80 public function testNegationMakesALiteralBeyondTheRangeADecimal(): void
81 {
82 $fact = new ScalarFact(new Known(new Integral(IntegralKind::BigInt, null, [NumericModifier::Unsigned])), Nullability::NotNull);
83 $results = new NumericResult();
84
85 self::assertEquals(new Known(new Decimal()), $results->negation(new NumberLiteral('9223372036854775809'), $fact));
86 self::assertEquals(new Known(new Integral(IntegralKind::BigInt)), $results->negation(new NumberLiteral('9223372036854775808'), $fact));
87 }
88
89 public function testBeyondComparesWithTheNegatedRange(): void
90 {
91 self::assertSame([false, true, false], [(new NumericResult())->beyond(new NumberLiteral('9223372036854775808')), (new NumericResult())->beyond(new NumberLiteral('18446744073709551615')), (new NumericResult())->beyond(new StringLiteral(['1']))]);
92 }
93
94 public function testIntegerIsABigint(): void
95 {
96 self::assertTrue((new NumericResult())->integer(true)->unsigned());
97 self::assertSame('BIGINT', (new NumericResult())->integer(false)->name());
98 }
99}
100