-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathEloquentWithCountTest.php
176 lines (143 loc) · 4.58 KB
/
EloquentWithCountTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace MongoDB\Laravel\Tests\Eloquent;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Tests\TestCase;
use function count;
/** Copied from {@see \Illuminate\Tests\Integration\Database\EloquentWithCountTest\EloquentWithCountTest} */
class EloquentWithCountTest extends TestCase
{
protected function tearDown(): void
{
EloquentWithCountModel1::truncate();
EloquentWithCountModel2::truncate();
EloquentWithCountModel3::truncate();
EloquentWithCountModel4::truncate();
parent::tearDown();
}
public function testItBasic()
{
$one = EloquentWithCountModel1::create(['id' => 123]);
$two = $one->twos()->create(['value' => 456]);
$two->threes()->create();
$results = EloquentWithCountModel1::withCount([
'twos' => function ($query) {
$query->where('value', '>=', 456);
},
]);
$this->assertEquals([
['id' => 123, 'twos_count' => 1],
], $results->get()->toArray());
}
public function testWithMultipleResults()
{
$connection = DB::connection('mongodb');
$ones = [
EloquentWithCountModel1::create(['id' => 1]),
EloquentWithCountModel1::create(['id' => 2]),
EloquentWithCountModel1::create(['id' => 3]),
];
$ones[0]->twos()->create(['value' => 1]);
$ones[0]->twos()->create(['value' => 2]);
$ones[0]->twos()->create(['value' => 3]);
$ones[0]->twos()->create(['value' => 1]);
$ones[2]->twos()->create(['value' => 1]);
$ones[2]->twos()->create(['value' => 2]);
$connection->enableQueryLog();
$results = EloquentWithCountModel1::withCount([
'twos' => function ($query) {
$query->where('value', '>=', 2);
},
]);
$this->assertEquals([
['id' => 1, 'twos_count' => 2],
['id' => 2, 'twos_count' => 0],
['id' => 3, 'twos_count' => 1],
], $results->get()->toArray());
$connection->disableQueryLog();
$this->assertEquals(2, count($connection->getQueryLog()));
}
public function testGlobalScopes()
{
$one = EloquentWithCountModel1::create();
$one->fours()->create();
$result = EloquentWithCountModel1::withCount('fours')->first();
$this->assertEquals(0, $result->fours_count);
$result = EloquentWithCountModel1::withCount('allFours')->first();
$this->assertEquals(1, $result->all_fours_count);
}
public function testSortingScopes()
{
$one = EloquentWithCountModel1::create();
$one->twos()->create();
$query = EloquentWithCountModel1::withCount('twos')->getQuery();
$this->assertNull($query->orders);
$this->assertSame([], $query->getRawBindings()['order']);
}
}
class EloquentWithCountModel1 extends Model
{
protected $connection = 'mongodb';
public $table = 'one';
public $timestamps = false;
protected $guarded = [];
public function twos()
{
return $this->hasMany(EloquentWithCountModel2::class, 'one_id');
}
public function fours()
{
return $this->hasMany(EloquentWithCountModel4::class, 'one_id');
}
public function allFours()
{
return $this->fours()->withoutGlobalScopes();
}
}
class EloquentWithCountModel2 extends Model
{
protected $connection = 'mongodb';
public $table = 'two';
public $timestamps = false;
protected $guarded = [];
protected $withCount = ['threes'];
protected static function boot()
{
parent::boot();
static::addGlobalScope('app', function ($builder) {
$builder->latest();
});
}
public function threes()
{
return $this->hasMany(EloquentWithCountModel3::class, 'two_id');
}
}
class EloquentWithCountModel3 extends Model
{
protected $connection = 'mongodb';
public $table = 'three';
public $timestamps = false;
protected $guarded = [];
protected static function boot()
{
parent::boot();
static::addGlobalScope('app', function ($builder) {
$builder->where('id', '>', 0);
});
}
}
class EloquentWithCountModel4 extends Model
{
protected $connection = 'mongodb';
public $table = 'four';
public $timestamps = false;
protected $guarded = [];
protected static function boot()
{
parent::boot();
static::addGlobalScope('app', function ($builder) {
$builder->where('id', '>', 1);
});
}
}