|
7 | 7 | use Carbon\Carbon;
|
8 | 8 | use DateTime;
|
9 | 9 | use DateTimeImmutable;
|
| 10 | +use Illuminate\Support\Collection as LaravelCollection; |
10 | 11 | use Illuminate\Support\Facades\Date;
|
11 | 12 | use Illuminate\Support\Facades\DB;
|
12 | 13 | use Illuminate\Support\LazyCollection;
|
@@ -39,6 +40,7 @@ class QueryBuilderTest extends TestCase
|
39 | 40 | {
|
40 | 41 | public function tearDown(): void
|
41 | 42 | {
|
| 43 | + DB::table('books')->truncate(); |
42 | 44 | DB::table('users')->truncate();
|
43 | 45 | DB::table('items')->truncate();
|
44 | 46 | }
|
@@ -573,6 +575,12 @@ public function testAggregate()
|
573 | 575 | $this->assertEquals(3, DB::table('items')->min('amount'));
|
574 | 576 | $this->assertEquals(34, DB::table('items')->max('amount'));
|
575 | 577 | $this->assertEquals(17.75, DB::table('items')->avg('amount'));
|
| 578 | + $this->assertTrue(DB::table('items')->exists()); |
| 579 | + $this->assertTrue(DB::table('items')->where('name', 'knife')->exists()); |
| 580 | + $this->assertFalse(DB::table('items')->where('name', 'ladle')->exists()); |
| 581 | + $this->assertFalse(DB::table('items')->doesntExist()); |
| 582 | + $this->assertFalse(DB::table('items')->where('name', 'knife')->doesntExist()); |
| 583 | + $this->assertTrue(DB::table('items')->where('name', 'ladle')->doesntExist()); |
576 | 584 |
|
577 | 585 | $this->assertEquals(2, DB::table('items')->where('name', 'spoon')->count('amount'));
|
578 | 586 | $this->assertEquals(14, DB::table('items')->where('name', 'spoon')->max('amount'));
|
@@ -1100,4 +1108,37 @@ public function testIdAlias($insertId, $queryId): void
|
1100 | 1108 | $result = DB::table('items')->where($queryId, '=', 'abc')->delete();
|
1101 | 1109 | $this->assertSame(1, $result);
|
1102 | 1110 | }
|
| 1111 | + |
| 1112 | + public function testAggregateFunctionsWithGroupBy() |
| 1113 | + { |
| 1114 | + DB::table('users')->insert([ |
| 1115 | + ['name' => 'John Doe', 'role' => 'admin', 'score' => 1], |
| 1116 | + ['name' => 'Jane Doe', 'role' => 'admin', 'score' => 2], |
| 1117 | + ['name' => 'Robert Roe', 'role' => 'user', 'score' => 4], |
| 1118 | + ]); |
| 1119 | + |
| 1120 | + $results = DB::table('users')->groupBy('role')->orderBy('role')->count(); |
| 1121 | + $this->assertInstanceOf(LaravelCollection::class, $results); |
| 1122 | + $this->assertEquals([(object) ['role' => 'admin', 'aggregate' => 2], (object) ['role' => 'user', 'aggregate' => 1]], $results->toArray()); |
| 1123 | + |
| 1124 | + $results = DB::table('users')->groupBy('role')->orderBy('role')->max('score'); |
| 1125 | + $this->assertInstanceOf(LaravelCollection::class, $results); |
| 1126 | + $this->assertEquals([(object) ['role' => 'admin', 'aggregate' => 2], (object) ['role' => 'user', 'aggregate' => 4]], $results->toArray()); |
| 1127 | + |
| 1128 | + $results = DB::table('users')->groupBy('role')->orderBy('role')->min('score'); |
| 1129 | + $this->assertInstanceOf(LaravelCollection::class, $results); |
| 1130 | + $this->assertEquals([(object) ['role' => 'admin', 'aggregate' => 1], (object) ['role' => 'user', 'aggregate' => 4]], $results->toArray()); |
| 1131 | + |
| 1132 | + $results = DB::table('users')->groupBy('role')->orderBy('role')->sum('score'); |
| 1133 | + $this->assertInstanceOf(LaravelCollection::class, $results); |
| 1134 | + $this->assertEquals([(object) ['role' => 'admin', 'aggregate' => 3], (object) ['role' => 'user', 'aggregate' => 4]], $results->toArray()); |
| 1135 | + |
| 1136 | + $results = DB::table('users')->groupBy('role')->orderBy('role')->avg('score'); |
| 1137 | + $this->assertInstanceOf(LaravelCollection::class, $results); |
| 1138 | + $this->assertEquals([(object) ['role' => 'admin', 'aggregate' => 1.5], (object) ['role' => 'user', 'aggregate' => 4]], $results->toArray()); |
| 1139 | + |
| 1140 | + $results = DB::table('users')->groupBy('role')->orderBy('role')->average('score'); |
| 1141 | + $this->assertInstanceOf(LaravelCollection::class, $results); |
| 1142 | + $this->assertEquals([(object) ['role' => 'admin', 'aggregate' => 1.5], (object) ['role' => 'user', 'aggregate' => 4]], $results->toArray()); |
| 1143 | + } |
1103 | 1144 | }
|
0 commit comments