Skip to content

Commit 0887542

Browse files
committed
fix tests
1 parent aa43efe commit 0887542

13 files changed

+73
-19
lines changed

lib/Model.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,16 @@ public static function select(): Relation
15991599
return static::Relation()->select(...func_get_args());
16001600
}
16011601

1602+
/**
1603+
* @return Relation<static>
1604+
*
1605+
*@see Relation::distinct()
1606+
*/
1607+
public static function distinct(bool $distinct = true): Relation
1608+
{
1609+
return static::Relation()->distinct($distinct);
1610+
}
1611+
16021612
/**
16031613
* @return Relation<static>
16041614
*

lib/Relation.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ public function pluck(): array
146146
$options = array_merge($this->options, ['select' => [static::toSingleArg(...$args)]]);
147147
$table = $this->table();
148148
$sql = $table->options_to_sql($options);
149-
$retValue = iterator_to_array($table->conn->query_and_fetch($sql->to_s(), $sql->get_where_values(), \PDO::FETCH_NUM));
149+
$retValue = iterator_to_array(
150+
$table->conn->query_and_fetch($sql->to_s(), $sql->get_where_values(), \PDO::FETCH_NUM)
151+
);
150152

151153
return array_map(static function ($row) {
152154
return 1 == count($row) ? $row[0] : $row;
@@ -539,6 +541,24 @@ public function readonly(bool $readonly): Relation
539541
return $this;
540542
}
541543

544+
/**
545+
* Specifies whether the records should be unique or not. For example:
546+
*
547+
* User::select('name') // Might return two records with the same name
548+
*
549+
* User::select('name')->distinct() // Returns 1 record per distinct name
550+
*
551+
* User::select('name')->distinct()->distinct(false) // You can also remove the uniqueness
552+
*
553+
* @return Relation<TModel>
554+
*/
555+
public function distinct(bool $distinct=true): Relation
556+
{
557+
$this->options['distinct'] = $distinct;
558+
559+
return $this;
560+
}
561+
542562
/**
543563
* Find by id - This can either be a specific id (1),
544564
* a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]).

lib/SQLBuilder.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class SQLBuilder
1818
{
1919
private Connection $connection;
2020
private string $operation = 'SELECT';
21+
22+
private bool $distinct = false;
2123
private string $table;
2224
private string $select = '*';
2325

@@ -175,8 +177,9 @@ public function offset(int $offset): static
175177
return $this;
176178
}
177179

178-
public function select(string $select): static
180+
public function select(string $select, bool $distinct = false): static
179181
{
182+
$this->distinct = $distinct;
180183
$this->operation = 'SELECT';
181184
$this->select = $select;
182185

@@ -344,7 +347,7 @@ private function build_insert(): string
344347

345348
private function build_select(): string
346349
{
347-
$sql = "SELECT $this->select FROM $this->table";
350+
$sql = 'SELECT ' . ($this->distinct ? 'DISTINCT ' : '') . "$this->select FROM $this->table";
348351

349352
if (!empty($this->joins)) {
350353
$sql .= ' ' . $this->joins;

lib/Table.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ public function options_to_sql(array $options): SQLBuilder
223223
$tokens = array_merge($tokens, array_map('trim', explode(',', $select)));
224224
}
225225

226-
$sql->select(array_search('*', $tokens) ? '*' : implode(', ', array_unique($tokens)));
226+
$sql->select(
227+
array_search('*', $tokens) ? '*' : implode(', ', array_unique($tokens)),
228+
!empty($options['distinct'])
229+
);
227230
}
228231

229232
$sql->where($options['conditions'] ?? [], $options['mapped_names'] ?? []);

lib/Types.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* offset?: int,
4343
* order?: string,
4444
* readonly?: bool,
45+
* distinct?: bool,
4546
* select?: string|array<string>,
4647
* }
4748
*/

test/ActiveRecordCountTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ActiveRecordCountTest extends \DatabaseTestCase
99
{
1010
public function testNoArguments()
1111
{
12-
$this->assertEquals(4, Author::count());
12+
$this->assertEquals(5, Author::count());
1313
}
1414

1515
public function testColumnNameAsArgument()

test/ActiveRecordFindTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ public function testFindBySqltakesValuesArray()
234234
public function testFindLast()
235235
{
236236
$author = Author::last();
237-
$this->assertEquals(4, $author->author_id);
238-
$this->assertEquals('Uncle Bob', $author->name);
237+
$this->assertEquals(5, $author->author_id);
238+
$this->assertEquals('Tito', $author->name);
239239
}
240240

241241
public function testFindLastUsingStringCondition()
@@ -316,7 +316,7 @@ public function testFindWithHash()
316316
{
317317
$this->assertNotNull(Author::where(['name' => 'Tito'])->first());
318318
$this->assertNull(Author::where(['name' => 'Mortimer'])->first());
319-
$this->assertEquals(1, count(Author::where(['name' => 'Tito'])->to_a()));
319+
$this->assertEquals(2, count(Author::where(['name' => 'Tito'])->to_a()));
320320
}
321321

322322
public function testFindOrCreateByOnExistingRecord()

test/ActiveRecordPluckTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,29 @@ public function testNoArguments()
1717
public function testSingleArgument()
1818
{
1919
$authors = Author::pluck('name');
20+
$this->assertEquals(5, count($authors));
21+
$this->assertEquals('Tito', $authors[0]);
22+
$this->assertEquals('George W. Bush', $authors[1]);
23+
}
24+
25+
public function testSingleArgumentWithDistinct()
26+
{
27+
$authors = Author::distinct()->pluck('name');
2028
$this->assertEquals(4, count($authors));
2129
$this->assertEquals('Tito', $authors[0]);
2230
$this->assertEquals('George W. Bush', $authors[1]);
2331
}
2432

33+
public function testSingleArgumentWithRemoveDistinct()
34+
{
35+
$authors = Author::distinct()->distinct(false)->pluck('name');
36+
$this->assertEquals(5, count($authors));
37+
}
38+
2539
public function testMultipleArguments()
2640
{
2741
$authors = Author::pluck('name', 'author_id');
28-
$this->assertEquals(4, count($authors));
42+
$this->assertEquals(5, count($authors));
2943
$this->assertEquals('Tito', $authors[0][0]);
3044
$this->assertEquals(1, $authors[0][1]);
3145
$this->assertEquals('George W. Bush', $authors[1][0]);
@@ -73,6 +87,6 @@ public function testIds()
7387
public function testIdsAll()
7488
{
7589
$authors = Author::ids();
76-
$this->assertEquals(4, count($authors));
90+
$this->assertEquals(5, count($authors));
7791
}
7892
}

test/ActiveRecordTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,6 @@ public function testQuery()
478478
$this->assertTrue($row['n'] > 1);
479479

480480
$row = Author::query('SELECT COUNT(*) AS n FROM authors WHERE name=?', ['Tito'])->fetch();
481-
$this->assertEquals(['n' => 1], $row);
481+
$this->assertEquals(['n' => 2], $row);
482482
}
483483
}

test/ActiveRecordWriteTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,13 @@ public function testDeleteAllWithLimitAndOrder()
388388
public function testUpdateAllWithSetAsString()
389389
{
390390
$num_affected = Author::update_all(['set' => 'parent_author_id = 2']);
391-
$this->assertEquals(2, $num_affected);
392-
// $this->assertEquals(4, Author::count_by_parent_author_id(2));
391+
$this->assertEquals(3, $num_affected);
393392
}
394393

395394
public function testUpdateAllWithSetAsHash()
396395
{
397396
$num_affected = Author::update_all(['set' => ['parent_author_id' => 2]]);
398-
$this->assertEquals(2, $num_affected);
397+
$this->assertEquals(3, $num_affected);
399398
}
400399

401400
/**
@@ -404,19 +403,19 @@ public function testUpdateAllWithSetAsHash()
404403
public function testUpdateAllWithConditionsAsString()
405404
{
406405
$num_affected = Author::update_all(['set' => 'parent_author_id = 2', 'conditions' => 'name = "Tito"']);
407-
$this->assertEquals(1, $num_affected);
406+
$this->assertEquals(2, $num_affected);
408407
}
409408

410409
public function testUpdateAllWithConditionsAsHash()
411410
{
412411
$num_affected = Author::update_all(['set' => 'parent_author_id = 2', 'conditions' => ['name' => 'Tito']]);
413-
$this->assertEquals(1, $num_affected);
412+
$this->assertEquals(2, $num_affected);
414413
}
415414

416415
public function testUpdateAllWithConditionsAsArray()
417416
{
418417
$num_affected = Author::update_all(['set' => 'parent_author_id = 2', 'conditions' => ['name = ?', 'Tito']]);
419-
$this->assertEquals(1, $num_affected);
418+
$this->assertEquals(2, $num_affected);
420419
}
421420

422421
public function testUpdateAllWithLimitAndOrder()

0 commit comments

Comments
 (0)