packages/ztd-query-core/tests/Unit/Shadow/Mutation/Upsert/UpsertTruthTest.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\UpsertNumber;
12use ZtdQuery\Shadow\Mutation\Upsert\UpsertTruth;
13
14#[CoversClass(UpsertTruth::class)]
15#[UsesClass(UpsertNumber::class)]
16#[UsesClass(UnsupportedSqlException::class)]
17final class UpsertTruthTest extends TestCase
18{
19 public function testOfKeepsABooleanAsItIs(): void
20 {
21 self::assertTrue((new UpsertTruth())->of(true));
22 self::assertFalse((new UpsertTruth())->of(false));
23 }
24
25 public function testOfCountsAnyNumberButZeroAsTrue(): void
26 {
27 self::assertTrue((new UpsertTruth())->of(1));
28 self::assertTrue((new UpsertTruth())->of(-1));
29 self::assertFalse((new UpsertTruth())->of(0));
30 self::assertFalse((new UpsertTruth())->of(0.0));
31 }
32
33 public function testOfIsUnknownForAnUnknownValue(): void
34 {
35 self::assertNull((new UpsertTruth())->of(null));
36 }
37
38 public function testOfRefusesAValueThatIsNeitherBooleanNorANumber(): void
39 {
40 $this->expectException(UnsupportedSqlException::class);
41
42 (new UpsertTruth())->of('paid');
43 }
44
45 public function testNotTurnsTheAnswerAround(): void
46 {
47 self::assertFalse((new UpsertTruth())->not(1));
48 self::assertTrue((new UpsertTruth())->not(0));
49 }
50
51 public function testNotLeavesTheUnknownUnknown(): void
52 {
53 self::assertNull((new UpsertTruth())->not(null));
54 }
55
56 public function testAndHoldsOnlyWhenBothSidesDo(): void
57 {
58 self::assertTrue((new UpsertTruth())->and(true, true));
59 self::assertFalse((new UpsertTruth())->and(true, false));
60 }
61
62 public function testAndIsFalseWhereOneSideIsFalseHoweverUnknownTheOtherIs(): void
63 {
64 self::assertFalse((new UpsertTruth())->and(null, false));
65 self::assertFalse((new UpsertTruth())->and(false, null));
66 }
67
68 public function testAndIsUnknownWhereOneSideIsUnknownAndTheOtherHolds(): void
69 {
70 self::assertNull((new UpsertTruth())->and(true, null));
71 self::assertNull((new UpsertTruth())->and(null, true));
72 }
73
74 public function testOrHoldsWhenEitherSideDoes(): void
75 {
76 self::assertTrue((new UpsertTruth())->or(false, true));
77 self::assertFalse((new UpsertTruth())->or(false, false));
78 }
79
80 public function testOrIsTrueWhereOneSideHoldsHoweverUnknownTheOtherIs(): void
81 {
82 self::assertTrue((new UpsertTruth())->or(null, true));
83 self::assertTrue((new UpsertTruth())->or(true, null));
84 }
85
86 public function testOrIsUnknownWhereOneSideIsUnknownAndTheOtherDoesNotHold(): void
87 {
88 self::assertNull((new UpsertTruth())->or(false, null));
89 self::assertNull((new UpsertTruth())->or(null, false));
90 }
91}
92