packages/sql-fixture/tests/Unit/Plan/Printing/RelationGroupsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Plan\Printing;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9use SqlFixture\Plan\ColumnRef;
10use SqlFixture\Plan\Printing\RelationGroups as Subject;
11use SqlFixture\Plan\Relation;
12use SqlFixture\Plan\RelationKind;
13
14#[CoversClass(Subject::class)]
15#[\PHPUnit\Framework\Attributes\UsesClass(ColumnRef::class)]
16#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\FixturePlan::class)]
17#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\PlanParser::class)]
18#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\PlanPrinter::class)]
19#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\PlanSyntaxException::class)]
20#[\PHPUnit\Framework\Attributes\UsesClass(Relation::class)]
21#[\PHPUnit\Framework\Attributes\UsesClass(RelationKind::class)]
22#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\RelationSide::class)]
23#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Parsing\PlanStatements::class)]
24#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Parsing\RelationCursor::class)]
25#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Parsing\RelationReader::class)]
26#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\PlanStructureException::class)]
27#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Printing\PlanTables::class)]
28#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Printing\StatementPrinter::class)]
29#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Validation\PlanValidation::class)]
30#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Validation\TableName::class)]
31#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Exception\EmptyPlanException::class)]
32#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Exception\EmptyTableNameException::class)]
33#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Exception\MissingEndpointColumnsException::class)]
34#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Exception\InvalidTableNameException::class)]
35#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Exception\UnbalancedBracketsException::class)]
36#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Exception\UnexpectedPlanTokenException::class)]
37#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Exception\UnsupportedManyToManyException::class)]
38#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Exception\CompositeArityMismatchException::class)]
39#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Exception\DuplicateColumnBindingException::class)]
40#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Exception\CyclicDependencyException::class)]
41#[\PHPUnit\Framework\Attributes\UsesClass(\SqlFixture\Plan\Exception\UnboundedSelfReferenceException::class)]
42final class RelationGroupsTest extends TestCase
43{
44 public function testGroupCombinesOnlyMatchingLeftEndpoints(): void
45 {
46 $relations = [Relation::oneToMany('a.id', 'b.a_id'), Relation::oneToMany('a.id', 'c.a_id'), Relation::manyToOne('d.b_id', 'b.id')];
47 $groups = (new Subject())->group($relations);
48 self::assertSame([2, 1], array_map(count(...), array_values($groups)));
49 }
50
51 public function testGroupKeyIncludesOptionality(): void
52 {
53 self::assertSame('a.id <?', (new Subject())->groupKey(Relation::oneToMany('a.id', 'b.a_id', true)));
54 }
55
56 public function testOperatorPreservesBothMarkers(): void
57 {
58 $relation = new Relation(new ColumnRef('a', ['id']), RelationKind::OneToOne, new ColumnRef('b', ['a_id']), true, true);
59 self::assertSame('?-?', (new Subject())->operator($relation));
60 }
61}
62