Skip to content

Commit 303ceb7

Browse files
committed
Fix CS
1 parent 29ff22a commit 303ceb7

7 files changed

+123
-95
lines changed

src/Eloquent/Builder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,12 @@ public function withAggregate($relations, $column, $function = null)
326326
if ($relation instanceof EmbedsOneOrMany) {
327327
switch ($function) {
328328
case 'count':
329-
$this->project([$alias => ['$size' => ['$ifNull' => ['$' . $name, []]]]]);
329+
$this->getQuery()->project([$alias => ['$size' => ['$ifNull' => ['$' . $name, []]]]]);
330330
break;
331331
case 'min':
332332
case 'max':
333333
case 'avg':
334-
$this->project([$alias => ['$' . $function => '$' . $name . '.' . $column]]);
334+
$this->getQuery()->project([$alias => ['$' . $function => '$' . $name . '.' . $column]]);
335335
break;
336336
default:
337337
throw new InvalidArgumentException(sprintf('Invalid aggregate function "%s"', $function));

tests/Eloquent/EloquentWithAggregateTest.php

+4-93
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Support\Collection;
77
use Illuminate\Support\Facades\DB;
8-
use MongoDB\Laravel\Eloquent\Model;
8+
use MongoDB\Laravel\Tests\Eloquent\Models\EloquentWithAggregateModel1;
9+
use MongoDB\Laravel\Tests\Eloquent\Models\EloquentWithAggregateModel2;
10+
use MongoDB\Laravel\Tests\Eloquent\Models\EloquentWithAggregateModel3;
11+
use MongoDB\Laravel\Tests\Eloquent\Models\EloquentWithAggregateModel4;
912
use MongoDB\Laravel\Tests\TestCase;
1013

1114
use function count;
@@ -260,95 +263,3 @@ private static function assertSameResults(array $expected, Collection $collectio
260263
self::assertSame($expected, $actual);
261264
}
262265
}
263-
264-
class EloquentWithAggregateModel1 extends Model
265-
{
266-
protected $connection = 'mongodb';
267-
public $table = 'one';
268-
public $timestamps = false;
269-
protected $guarded = [];
270-
271-
public function twos()
272-
{
273-
return $this->hasMany(EloquentWithAggregateModel2::class, 'one_id');
274-
}
275-
276-
public function fours()
277-
{
278-
return $this->hasMany(EloquentWithAggregateModel4::class, 'one_id');
279-
}
280-
281-
public function allFours()
282-
{
283-
return $this->fours()->withoutGlobalScopes();
284-
}
285-
286-
public function embeddeds()
287-
{
288-
return $this->embedsMany(EloquentWithAggregateEmbeddedModel::class);
289-
}
290-
}
291-
292-
class EloquentWithAggregateModel2 extends Model
293-
{
294-
protected $connection = 'mongodb';
295-
public $table = 'two';
296-
public $timestamps = false;
297-
protected $guarded = [];
298-
protected $withCount = ['threes'];
299-
300-
protected static function boot()
301-
{
302-
parent::boot();
303-
304-
static::addGlobalScope('app', function ($builder) {
305-
$builder->latest();
306-
});
307-
}
308-
309-
public function threes()
310-
{
311-
return $this->hasMany(EloquentWithAggregateModel3::class, 'two_id');
312-
}
313-
}
314-
315-
class EloquentWithAggregateModel3 extends Model
316-
{
317-
protected $connection = 'mongodb';
318-
public $table = 'three';
319-
public $timestamps = false;
320-
protected $guarded = [];
321-
322-
protected static function boot()
323-
{
324-
parent::boot();
325-
326-
static::addGlobalScope('app', function ($builder) {
327-
$builder->where('id', '>', 0);
328-
});
329-
}
330-
}
331-
332-
class EloquentWithAggregateModel4 extends Model
333-
{
334-
protected $connection = 'mongodb';
335-
public $table = 'four';
336-
public $timestamps = false;
337-
protected $guarded = [];
338-
339-
protected static function boot()
340-
{
341-
parent::boot();
342-
343-
static::addGlobalScope('app', function ($builder) {
344-
$builder->where('id', '>', 1);
345-
});
346-
}
347-
}
348-
349-
class EloquentWithAggregateEmbeddedModel extends Model
350-
{
351-
protected $connection = 'mongodb';
352-
public $timestamps = false;
353-
protected $guarded = [];
354-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Tests\Eloquent\Models;
4+
5+
use MongoDB\Laravel\Eloquent\Model;
6+
7+
class EloquentWithAggregateEmbeddedModel extends Model
8+
{
9+
protected $connection = 'mongodb';
10+
public $timestamps = false;
11+
protected $guarded = [];
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Tests\Eloquent\Models;
4+
5+
use MongoDB\Laravel\Eloquent\Model;
6+
7+
class EloquentWithAggregateModel1 extends Model
8+
{
9+
protected $connection = 'mongodb';
10+
public $table = 'one';
11+
public $timestamps = false;
12+
protected $guarded = [];
13+
14+
public function twos()
15+
{
16+
return $this->hasMany(EloquentWithAggregateModel2::class, 'one_id');
17+
}
18+
19+
public function fours()
20+
{
21+
return $this->hasMany(EloquentWithAggregateModel4::class, 'one_id');
22+
}
23+
24+
public function allFours()
25+
{
26+
return $this->fours()->withoutGlobalScopes();
27+
}
28+
29+
public function embeddeds()
30+
{
31+
return $this->embedsMany(EloquentWithAggregateEmbeddedModel::class);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Tests\Eloquent\Models;
4+
5+
use MongoDB\Laravel\Eloquent\Model;
6+
7+
class EloquentWithAggregateModel2 extends Model
8+
{
9+
protected $connection = 'mongodb';
10+
public $table = 'two';
11+
public $timestamps = false;
12+
protected $guarded = [];
13+
protected $withCount = ['threes'];
14+
15+
protected static function boot()
16+
{
17+
parent::boot();
18+
19+
static::addGlobalScope('app', function ($builder) {
20+
$builder->latest();
21+
});
22+
}
23+
24+
public function threes()
25+
{
26+
return $this->hasMany(EloquentWithAggregateModel3::class, 'two_id');
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Tests\Eloquent\Models;
4+
5+
use MongoDB\Laravel\Eloquent\Model;
6+
7+
class EloquentWithAggregateModel3 extends Model
8+
{
9+
protected $connection = 'mongodb';
10+
public $table = 'three';
11+
public $timestamps = false;
12+
protected $guarded = [];
13+
14+
protected static function boot()
15+
{
16+
parent::boot();
17+
18+
static::addGlobalScope('app', function ($builder) {
19+
$builder->where('id', '>', 0);
20+
});
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Tests\Eloquent\Models;
4+
5+
use MongoDB\Laravel\Eloquent\Model;
6+
7+
class EloquentWithAggregateModel4 extends Model
8+
{
9+
protected $connection = 'mongodb';
10+
public $table = 'four';
11+
public $timestamps = false;
12+
protected $guarded = [];
13+
14+
protected static function boot()
15+
{
16+
parent::boot();
17+
18+
static::addGlobalScope('app', function ($builder) {
19+
$builder->where('id', '>', 1);
20+
});
21+
}
22+
}

0 commit comments

Comments
 (0)