packages/ztd-query-core/tests/Unit/Shadow/Mutation/Upsert/UpsertComparisonTest.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\Exception\UnsupportedSqlException;
11use ZtdQuery\Shadow\Mutation\Upsert\UpsertComparison;
12use ZtdQuery\Shadow\Mutation\Upsert\UpsertNumber;
13
14#[CoversClass(UpsertComparison::class)]
15#[UsesClass(UpsertNumber::class)]
16#[UsesClass(UnsupportedSqlException::class)]
17final class UpsertComparisonTest extends TestCase
18{
19 public function testOfOrdersTwoNumbersAsNumbers(): void
20 {
21 self::assertSame(-1, (new UpsertComparison())->of(9, 10));
22 }
23
24 public function testOfOrdersNumbersWrittenAsTextAsNumbersToo(): void
25 {
26 self::assertSame(-1, (new UpsertComparison())->of('9', '10'));
27 }
28
29 public function testOfOrdersTwoPiecesOfTextAsText(): void
30 {
31 self::assertSame(1, (new UpsertComparison())->of('b', 'a'));
32 }
33
34 public function testOfIsUnknownWhenEitherSideIs(): void
35 {
36 self::assertNull((new UpsertComparison())->of(null, 1));
37 self::assertNull((new UpsertComparison())->of(1, null));
38 }
39
40 public function testOfRefusesANumberAgainstText(): void
41 {
42 $this->expectException(UnsupportedSqlException::class);
43
44 (new UpsertComparison())->of(1, 'paid');
45 }
46
47 public function testOfRefusesTextAgainstANumber(): void
48 {
49 $this->expectException(UnsupportedSqlException::class);
50
51 (new UpsertComparison())->of('paid', 1);
52 }
53
54 public function testTextKeepsTextAsItIs(): void
55 {
56 self::assertSame('paid', (new UpsertComparison())->text('paid'));
57 }
58
59 public function testTextWritesABooleanTheWayADatabaseComparesIt(): void
60 {
61 self::assertSame('1', (new UpsertComparison())->text(true));
62 self::assertSame('', (new UpsertComparison())->text(false));
63 }
64
65 public function testTextRefusesAValueWithNoTextForm(): void
66 {
67 $this->expectException(UnsupportedSqlException::class);
68
69 (new UpsertComparison())->text(null);
70 }
71
72 public function testEqualAnswersWhetherTheTwoAreTheSame(): void
73 {
74 self::assertTrue((new UpsertComparison())->equal(1, '1'));
75 self::assertFalse((new UpsertComparison())->equal(1, 2));
76 self::assertNull((new UpsertComparison())->equal(null, 1));
77 }
78
79 public function testNotEqualAnswersWhetherTheTwoDiffer(): void
80 {
81 self::assertTrue((new UpsertComparison())->notEqual(1, 2));
82 self::assertFalse((new UpsertComparison())->notEqual(1, 1));
83 self::assertNull((new UpsertComparison())->notEqual(null, 1));
84 }
85
86 public function testLessAnswersWhetherTheLeftOrdersFirst(): void
87 {
88 self::assertTrue((new UpsertComparison())->less(1, 2));
89 self::assertFalse((new UpsertComparison())->less(2, 2));
90 self::assertNull((new UpsertComparison())->less(null, 1));
91 }
92
93 public function testLessOrEqualAlsoHoldsWhereTheTwoOrderTogether(): void
94 {
95 self::assertTrue((new UpsertComparison())->lessOrEqual(2, 2));
96 self::assertFalse((new UpsertComparison())->lessOrEqual(3, 2));
97 self::assertNull((new UpsertComparison())->lessOrEqual(null, 1));
98 }
99
100 public function testGreaterAnswersWhetherTheLeftOrdersLast(): void
101 {
102 self::assertTrue((new UpsertComparison())->greater(3, 2));
103 self::assertFalse((new UpsertComparison())->greater(2, 2));
104 self::assertNull((new UpsertComparison())->greater(null, 1));
105 }
106
107 public function testGreaterOrEqualAlsoHoldsWhereTheTwoOrderTogether(): void
108 {
109 self::assertTrue((new UpsertComparison())->greaterOrEqual(2, 2));
110 self::assertFalse((new UpsertComparison())->greaterOrEqual(1, 2));
111 self::assertNull((new UpsertComparison())->greaterOrEqual(null, 1));
112 }
113}
114