|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Codemonster\Database\Tests\Fakes; |
| 4 | + |
| 5 | +use Codemonster\Database\Query\QueryBuilder; |
| 6 | + |
| 7 | +class FakeQueryBuilder extends QueryBuilder |
| 8 | +{ |
| 9 | + public FakeConnection $fake; |
| 10 | + protected array $wheres = []; |
| 11 | + protected array $joins = []; |
| 12 | + protected ?int $limit = null; |
| 13 | + protected ?int $offset = null; |
| 14 | + |
| 15 | + public function __construct(FakeConnection $fake, string $table) |
| 16 | + { |
| 17 | + $this->fake = $fake; |
| 18 | + parent::__construct($fake, $table); |
| 19 | + } |
| 20 | + |
| 21 | + public function select(string|array ...$columns): static |
| 22 | + { |
| 23 | + // selection is ignored in fake builder (we always return full rows) |
| 24 | + return $this; |
| 25 | + } |
| 26 | + |
| 27 | + public function where(string|callable $column, mixed $operator = null, mixed $value = null, string $boolean = 'AND'): static |
| 28 | + { |
| 29 | + if (is_callable($column)) { |
| 30 | + // not needed for fake tests |
| 31 | + return $this; |
| 32 | + } |
| 33 | + |
| 34 | + if (func_num_args() === 2) { |
| 35 | + $value = $operator; |
| 36 | + $operator = '='; |
| 37 | + } |
| 38 | + |
| 39 | + $this->wheres[] = [ |
| 40 | + 'column' => $column, |
| 41 | + 'operator'=> $operator, |
| 42 | + 'value' => $value, |
| 43 | + 'boolean' => strtoupper($boolean), |
| 44 | + 'type' => 'basic', |
| 45 | + ]; |
| 46 | + |
| 47 | + return $this; |
| 48 | + } |
| 49 | + |
| 50 | + public function whereNull(string $column, string $boolean = 'AND'): static |
| 51 | + { |
| 52 | + $this->wheres[] = [ |
| 53 | + 'column' => $column, |
| 54 | + 'operator' => 'NULL', |
| 55 | + 'boolean' => strtoupper($boolean), |
| 56 | + 'type' => 'null', |
| 57 | + ]; |
| 58 | + |
| 59 | + return $this; |
| 60 | + } |
| 61 | + |
| 62 | + public function whereNotNull(string $column, string $boolean = 'AND'): static |
| 63 | + { |
| 64 | + $this->wheres[] = [ |
| 65 | + 'column' => $column, |
| 66 | + 'operator' => 'NOT_NULL', |
| 67 | + 'boolean' => strtoupper($boolean), |
| 68 | + 'type' => 'not_null', |
| 69 | + ]; |
| 70 | + |
| 71 | + return $this; |
| 72 | + } |
| 73 | + |
| 74 | + public function join(string $table, string|callable $first, ?string $operator = null, ?string $second = null, string $type = 'INNER'): static |
| 75 | + { |
| 76 | + if (is_callable($first)) { |
| 77 | + return $this; |
| 78 | + } |
| 79 | + |
| 80 | + $this->joins[] = [ |
| 81 | + 'table' => $table, |
| 82 | + 'first' => $first, |
| 83 | + 'operator' => $operator, |
| 84 | + 'second' => $second, |
| 85 | + 'type' => strtoupper($type), |
| 86 | + ]; |
| 87 | + |
| 88 | + return $this; |
| 89 | + } |
| 90 | + |
| 91 | + public function get(): array |
| 92 | + { |
| 93 | + if (!empty($this->joins)) { |
| 94 | + return $this->runJoinQuery(); |
| 95 | + } |
| 96 | + |
| 97 | + $rows = $this->fake->tables[$this->table] ?? []; |
| 98 | + |
| 99 | + $rows = array_values(array_filter($rows, fn($row) => $this->matchesWhere($row))); |
| 100 | + |
| 101 | + if ($this->offset !== null) { |
| 102 | + $rows = array_slice($rows, $this->offset); |
| 103 | + } |
| 104 | + |
| 105 | + if ($this->limit !== null) { |
| 106 | + $rows = array_slice($rows, 0, $this->limit); |
| 107 | + } |
| 108 | + |
| 109 | + return array_map(fn($row) => (array) $row, $rows); |
| 110 | + } |
| 111 | + |
| 112 | + public function first(): ?array |
| 113 | + { |
| 114 | + $rows = $this->get(); |
| 115 | + |
| 116 | + return $rows[0] ?? null; |
| 117 | + } |
| 118 | + |
| 119 | + public function insert(array $values): bool |
| 120 | + { |
| 121 | + $this->fake->tables[$this->table][] = $values; |
| 122 | + |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + public function insertGetId(array $values, $sequence = null): int |
| 127 | + { |
| 128 | + $current = $this->fake->tables[$this->table] ?? []; |
| 129 | + $last = empty($current) ? 0 : ((int) ($current[array_key_last($current)]['id'] ?? count($current))); |
| 130 | + |
| 131 | + $values['id'] = $last + 1; |
| 132 | + $this->fake->tables[$this->table][] = $values; |
| 133 | + |
| 134 | + return $values['id']; |
| 135 | + } |
| 136 | + |
| 137 | + public function update(array $values): int |
| 138 | + { |
| 139 | + $updated = 0; |
| 140 | + |
| 141 | + foreach ($this->fake->tables[$this->table] as &$row) { |
| 142 | + if ($this->matchesWhere($row)) { |
| 143 | + $row = array_merge($row, $values); |
| 144 | + $updated++; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return $updated; |
| 149 | + } |
| 150 | + |
| 151 | + public function delete(): int |
| 152 | + { |
| 153 | + $deleted = 0; |
| 154 | + |
| 155 | + foreach ($this->fake->tables[$this->table] as $idx => $row) { |
| 156 | + if ($this->matchesWhere($row)) { |
| 157 | + unset($this->fake->tables[$this->table][$idx]); |
| 158 | + $deleted++; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + $this->fake->tables[$this->table] = array_values($this->fake->tables[$this->table] ?? []); |
| 163 | + |
| 164 | + return $deleted; |
| 165 | + } |
| 166 | + |
| 167 | + public function count(string $column = '*'): int |
| 168 | + { |
| 169 | + return count($this->get()); |
| 170 | + } |
| 171 | + |
| 172 | + public function exists(): bool |
| 173 | + { |
| 174 | + return !empty($this->get()); |
| 175 | + } |
| 176 | + |
| 177 | + public function limit(int $value): self |
| 178 | + { |
| 179 | + $this->limit = $value; |
| 180 | + |
| 181 | + return $this; |
| 182 | + } |
| 183 | + |
| 184 | + public function offset(int $value): self |
| 185 | + { |
| 186 | + $this->offset = $value; |
| 187 | + |
| 188 | + return $this; |
| 189 | + } |
| 190 | + |
| 191 | + protected function matchesWhere(array $row): bool |
| 192 | + { |
| 193 | + $result = null; |
| 194 | + |
| 195 | + foreach ($this->wheres as $where) { |
| 196 | + $column = str_contains($where['column'], '.') |
| 197 | + ? explode('.', $where['column'])[1] |
| 198 | + : $where['column']; |
| 199 | + |
| 200 | + $current = match ($where['type']) { |
| 201 | + 'null' => !array_key_exists($column, $row) || $row[$column] === null, |
| 202 | + 'not_null' => array_key_exists($column, $row) && $row[$column] !== null, |
| 203 | + default => ($row[$column] ?? null) == $where['value'], |
| 204 | + }; |
| 205 | + |
| 206 | + if ($result === null) { |
| 207 | + $result = $current; |
| 208 | + continue; |
| 209 | + } |
| 210 | + |
| 211 | + if ($where['boolean'] === 'OR') { |
| 212 | + $result = $result || $current; |
| 213 | + } else { |
| 214 | + $result = $result && $current; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + return $result ?? true; |
| 219 | + } |
| 220 | + |
| 221 | + protected function runJoinQuery(): array |
| 222 | + { |
| 223 | + // Simplified join handling for belongsToMany (single pivot join). |
| 224 | + $join = $this->joins[0]; |
| 225 | + |
| 226 | + $pivotTable = $join['table']; |
| 227 | + $pivotRows = $this->fake->tables[$pivotTable] ?? []; |
| 228 | + |
| 229 | + $filter = array_values(array_filter( |
| 230 | + $this->wheres, |
| 231 | + fn($w) => str_starts_with($w['column'], $pivotTable . '.') |
| 232 | + )); |
| 233 | + |
| 234 | + $relatedIds = []; |
| 235 | + foreach ($pivotRows as $pivot) { |
| 236 | + if ($filter) { |
| 237 | + $cond = $filter[0]; |
| 238 | + $pivotColumn = str_replace($pivotTable . '.', '', $cond['column']); |
| 239 | + |
| 240 | + if (($pivot[$pivotColumn] ?? null) != $cond['value']) { |
| 241 | + continue; |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + $pivotRelatedKey = str_replace($pivotTable . '.', '', $join['first']); |
| 246 | + $relatedIds[] = $pivot[$pivotRelatedKey] ?? null; |
| 247 | + } |
| 248 | + |
| 249 | + $relatedTable = $this->table; |
| 250 | + $relatedRows = $this->fake->tables[$relatedTable] ?? []; |
| 251 | + |
| 252 | + $relatedKey = str_contains($join['second'], '.') |
| 253 | + ? explode('.', $join['second'])[1] |
| 254 | + : $join['second']; |
| 255 | + |
| 256 | + $filtered = array_filter( |
| 257 | + $relatedRows, |
| 258 | + fn($row) => in_array($row[$relatedKey] ?? null, $relatedIds, true) |
| 259 | + ); |
| 260 | + |
| 261 | + return array_map(fn($row) => (array) $row, array_values($filtered)); |
| 262 | + } |
| 263 | +} |
0 commit comments