|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | + |
| 4 | +namespace Properties\Formal\AccessLayer\Connection; |
| 5 | + |
| 6 | +use Formal\AccessLayer\{ |
| 7 | + Query\MultipleInsert, |
| 8 | + Query\Select, |
| 9 | + Query\Select\Direction, |
| 10 | + Table\Name, |
| 11 | + Table\Column, |
| 12 | + Row, |
| 13 | + Connection, |
| 14 | +}; |
| 15 | +use Innmind\Specification\{ |
| 16 | + Comparator, |
| 17 | + Sign, |
| 18 | +}; |
| 19 | +use Innmind\Immutable\Sequence; |
| 20 | +use Innmind\BlackBox\{ |
| 21 | + Property, |
| 22 | + Set, |
| 23 | + Runner\Assert, |
| 24 | +}; |
| 25 | + |
| 26 | +/** |
| 27 | + * @implements Property<Connection> |
| 28 | + */ |
| 29 | +final class SelectMultiOrder implements Property |
| 30 | +{ |
| 31 | + private string $uuid1; |
| 32 | + private string $uuid2; |
| 33 | + private string $uuid3; |
| 34 | + private string $username; |
| 35 | + private int $number; |
| 36 | + |
| 37 | + public function __construct( |
| 38 | + string $uuid1, |
| 39 | + string $uuid2, |
| 40 | + string $uuid3, |
| 41 | + string $username, |
| 42 | + int $number, |
| 43 | + ) { |
| 44 | + $this->uuid1 = $uuid1; |
| 45 | + $this->uuid2 = $uuid2; |
| 46 | + $this->uuid3 = $uuid3; |
| 47 | + $this->username = $username; |
| 48 | + $this->number = $number; |
| 49 | + } |
| 50 | + |
| 51 | + public static function any(): Set |
| 52 | + { |
| 53 | + return Set::compose( |
| 54 | + static fn(...$args) => new self(...$args), |
| 55 | + Set::uuid(), |
| 56 | + Set::uuid(), |
| 57 | + Set::uuid(), |
| 58 | + Set::strings() |
| 59 | + ->madeOf(Set::strings()->chars()->ascii()) |
| 60 | + ->between(0, 254), |
| 61 | + Set::integers(), |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public function applicableTo(object $connection): bool |
| 66 | + { |
| 67 | + return true; |
| 68 | + } |
| 69 | + |
| 70 | + public function ensureHeldBy(Assert $assert, object $connection): object |
| 71 | + { |
| 72 | + $insert = MultipleInsert::into( |
| 73 | + Name::of('test'), |
| 74 | + Column\Name::of('id'), |
| 75 | + Column\Name::of('username'), |
| 76 | + Column\Name::of('registerNumber'), |
| 77 | + ); |
| 78 | + $_ = $connection($insert(Sequence::of( |
| 79 | + Row::of([ |
| 80 | + 'id' => $this->uuid1, |
| 81 | + 'username' => 'a'.$this->username, |
| 82 | + 'registerNumber' => $this->number, |
| 83 | + ]), |
| 84 | + Row::of([ |
| 85 | + 'id' => $this->uuid2, |
| 86 | + 'username' => 'b'.$this->username, |
| 87 | + 'registerNumber' => $this->number, |
| 88 | + ]), |
| 89 | + Row::of([ |
| 90 | + 'id' => $this->uuid3, |
| 91 | + 'username' => 'b'.$this->username, |
| 92 | + 'registerNumber' => $this->number + 1, |
| 93 | + ]), |
| 94 | + ))); |
| 95 | + |
| 96 | + $table = Name::of('test'); |
| 97 | + $select = Select::from($table) |
| 98 | + ->orderBy( |
| 99 | + Column\Name::of('username')->in($table), |
| 100 | + Direction::asc, |
| 101 | + ) |
| 102 | + ->andOrderBy( |
| 103 | + Column\Name::of('registerNumber')->in($table), |
| 104 | + Direction::desc, |
| 105 | + ) |
| 106 | + ->where(Comparator\Property::of( |
| 107 | + 'id', |
| 108 | + Sign::in, |
| 109 | + [$this->uuid1, $this->uuid2, $this->uuid3], |
| 110 | + )); |
| 111 | + $rows = $connection($select); |
| 112 | + |
| 113 | + $assert |
| 114 | + ->expected([$this->uuid1, $this->uuid3, $this->uuid2]) |
| 115 | + ->same( |
| 116 | + $rows |
| 117 | + ->flatMap(static fn($row) => $row->column('id')->toSequence()) |
| 118 | + ->toList(), |
| 119 | + ); |
| 120 | + |
| 121 | + $rows = $connection( |
| 122 | + $select |
| 123 | + ->orderBy( |
| 124 | + Column\Name::of('username')->in($table), |
| 125 | + Direction::desc, |
| 126 | + ) |
| 127 | + ->andOrderBy( |
| 128 | + Column\Name::of('registerNumber')->in($table), |
| 129 | + Direction::asc, |
| 130 | + ), |
| 131 | + ); |
| 132 | + |
| 133 | + $assert |
| 134 | + ->expected([$this->uuid2, $this->uuid3, $this->uuid1]) |
| 135 | + ->same( |
| 136 | + $rows |
| 137 | + ->flatMap(static fn($row) => $row->column('id')->toSequence()) |
| 138 | + ->toList(), |
| 139 | + ); |
| 140 | + |
| 141 | + return $connection; |
| 142 | + } |
| 143 | +} |
0 commit comments