-
Notifications
You must be signed in to change notification settings - Fork 11.3k
/
Copy pathDatabaseEloquentBelongsToManyWithoutTouchingTest.php
70 lines (56 loc) · 2.19 KB
/
DatabaseEloquentBelongsToManyWithoutTouchingTest.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
<?php
declare(strict_types=1);
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 DatabaseEloquentBelongsToManyWithoutTouchingTest extends TestCase
{
public function testItWillNotTouchRelatedModelsWhenUpdatingChild(): void
{
/** @var Article $related */
$related = m::mock(Article::class)->makePartial();
$related->shouldReceive('getUpdatedAtColumn')->never();
$related->shouldReceive('freshTimestampString')->never();
$this->assertFalse($related::isIgnoringTouch());
Model::withoutTouching(function () use ($related) {
$this->assertTrue($related::isIgnoringTouch());
$builder = m::mock(Builder::class);
$builder->shouldReceive('join');
$parent = m::mock(User::class);
$parent->shouldReceive('getAttribute')->with('id')->andReturn(1);
$builder->shouldReceive('getModel')->andReturn($related);
$builder->shouldReceive('where');
$builder->shouldReceive('getQuery')->andReturn(
m::mock(stdClass::class, ['getGrammar' => m::mock(Grammar::class, ['isExpression' => false])])
);
$relation = new BelongsToMany($builder, $parent, 'article_users', 'user_id', 'article_id', 'id', 'id');
$builder->shouldReceive('update')->never();
$relation->touch();
});
$this->assertFalse($related::isIgnoringTouch());
}
}
class User extends Model
{
protected $table = 'users';
protected $fillable = ['id', 'email'];
public function articles(): BelongsToMany
{
return $this->belongsToMany(Article::class, 'article_user', 'user_id', 'article_id');
}
}
class Article extends Model
{
protected $table = 'articles';
protected $fillable = ['id', 'title'];
protected $touches = ['user'];
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'article_user', 'article_id', 'user_id');
}
}