-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathDatabaseEloquentBelongsToManyWithDefaultAttributesTest.php
64 lines (51 loc) · 2.74 KB
/
DatabaseEloquentBelongsToManyWithDefaultAttributesTest.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
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Query\Grammars\Grammar;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use stdClass;
class DatabaseEloquentBelongsToManyWithDefaultAttributesTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
public function testWithPivotValueMethodSetsWhereConditionsForFetching()
{
$relation = $this->getMockBuilder(BelongsToMany::class)->onlyMethods(['touchIfTouching'])->setConstructorArgs($this->getRelationArguments())->getMock();
$relation->withPivotValue(['is_admin' => 1]);
}
public function testWithPivotValueMethodSetsDefaultArgumentsForInsertion()
{
$relation = $this->getMockBuilder(BelongsToMany::class)->onlyMethods(['touchIfTouching'])->setConstructorArgs($this->getRelationArguments())->getMock();
$relation->withPivotValue(['is_admin' => 1]);
$query = m::mock(stdClass::class);
$query->shouldReceive('from')->once()->with('club_user')->andReturn($query);
$query->shouldReceive('insert')->once()->with([['club_id' => 1, 'user_id' => 1, 'is_admin' => 1]])->andReturn(true);
$relation->getQuery()->getQuery()->shouldReceive('newQuery')->once()->andReturn($query);
$relation->attach(1);
}
public function getRelationArguments()
{
$parent = m::mock(Model::class);
$parent->shouldReceive('getKey')->andReturn(1);
$parent->shouldReceive('getCreatedAtColumn')->andReturn('created_at');
$parent->shouldReceive('getUpdatedAtColumn')->andReturn('updated_at');
$parent->shouldReceive('getAttribute')->with('id')->andReturn(1);
$builder = m::mock(Builder::class);
$related = m::mock(Model::class);
$builder->shouldReceive('getModel')->andReturn($related);
$related->shouldReceive('getTable')->andReturn('users');
$related->shouldReceive('getKeyName')->andReturn('id');
$related->shouldReceive('qualifyColumn')->with('id')->andReturn('users.id');
$builder->shouldReceive('join')->once()->with('club_user', 'users.id', '=', 'club_user.user_id');
$builder->shouldReceive('where')->once()->with('club_user.club_id', '=', 1);
$builder->shouldReceive('where')->once()->with('club_user.is_admin', '=', 1, 'and');
$builder->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock(stdClass::class));
$mockQueryBuilder->shouldReceive('getGrammar')->andReturn(m::mock(Grammar::class, ['isExpression' => false]));
return [$builder, $parent, 'club_user', 'club_id', 'user_id', 'id', 'id', null, false];
}
}