packages/ztd-query-core/tests/Unit/Shadow/Mutation/Upsert/UpsertOperatorTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Shadow\Mutation\Upsert;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use ZtdQuery\Shadow\Mutation\Upsert\UpsertComparison;
11use ZtdQuery\Shadow\Mutation\Upsert\UpsertNumber;
12use ZtdQuery\Shadow\Mutation\Upsert\UpsertOperator;
13use ZtdQuery\Shadow\Mutation\Upsert\UpsertTruth;
14use ZtdQuery\Shadow\Mutation\UpsertExpressionKind;
15
16#[CoversClass(UpsertOperator::class)]
17#[UsesClass(UpsertNumber::class)]
18#[UsesClass(UpsertComparison::class)]
19#[UsesClass(UpsertTruth::class)]
20final class UpsertOperatorTest extends TestCase
21{
22    public function testApplyDoesTheArithmeticEachOperatorStandsFor(): void
23    {
24        $operator = new UpsertOperator();
25
26        self::assertSame(
27            [2, -2, 5, 1, 6, 3, 1],
28            [
29                $operator->apply(UpsertExpressionKind::UnaryPlus, 2, null),
30                $operator->apply(UpsertExpressionKind::UnaryMinus, 2, null),
31                $operator->apply(UpsertExpressionKind::Add, 2, 3),
32                $operator->apply(UpsertExpressionKind::Subtract, 3, 2),
33                $operator->apply(UpsertExpressionKind::Multiply, 2, 3),
34                $operator->apply(UpsertExpressionKind::Divide, 6, 2),
35                $operator->apply(UpsertExpressionKind::Modulo, 7, 3),
36            ],
37        );
38    }
39
40    public function testApplyDoesTheComparingEachOperatorStandsFor(): void
41    {
42        $operator = new UpsertOperator();
43
44        self::assertSame(
45            [true, true, true, true, true, true],
46            [
47                $operator->apply(UpsertExpressionKind::Equal, 1, 1),
48                $operator->apply(UpsertExpressionKind::NotEqual, 1, 2),
49                $operator->apply(UpsertExpressionKind::Less, 1, 2),
50                $operator->apply(UpsertExpressionKind::LessOrEqual, 2, 2),
51                $operator->apply(UpsertExpressionKind::Greater, 2, 1),
52                $operator->apply(UpsertExpressionKind::GreaterOrEqual, 2, 2),
53            ],
54        );
55    }
56
57    public function testApplyAnswersWhatIsTrueOfEachLogicalOperator(): void
58    {
59        $operator = new UpsertOperator();
60
61        self::assertSame(
62            [false, true, true],
63            [
64                $operator->apply(UpsertExpressionKind::Not, 1, null),
65                $operator->apply(UpsertExpressionKind::And, 1, 1),
66                $operator->apply(UpsertExpressionKind::Or, 0, 1),
67            ],
68        );
69    }
70}
71