|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cino\LaravelChronos\Tests\Database; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\Chronos\Chronos; |
|
6
|
|
|
use Cake\Chronos\ChronosInterface; |
|
7
|
|
|
use Cino\LaravelChronos\Eloquent\Model; |
|
8
|
|
|
use Cino\LaravelChronos\Eloquent\Relations\MorphPivot; |
|
9
|
|
|
use Cino\LaravelChronos\Eloquent\Relations\MorphToMany; |
|
10
|
|
|
use Cino\LaravelChronos\Eloquent\Relations\Pivot; |
|
11
|
|
|
use Illuminate\Database\Schema\Blueprint; |
|
12
|
|
|
|
|
13
|
|
|
class EloquentMorphToManyTest extends EloquentTestCase |
|
14
|
|
|
{ |
|
15
|
|
|
protected function createSchema(): void |
|
16
|
|
|
{ |
|
17
|
|
|
$this->schema()->create('posts', function (Blueprint $table) { |
|
18
|
|
|
$table->increments('id'); |
|
19
|
|
|
$table->timestamps(); |
|
20
|
|
|
}); |
|
21
|
|
|
|
|
22
|
|
|
$this->schema()->create('tags', function (Blueprint $table) { |
|
23
|
|
|
$table->increments('id'); |
|
24
|
|
|
$table->date('date'); |
|
25
|
|
|
$table->timestamps(); |
|
26
|
|
|
}); |
|
27
|
|
|
|
|
28
|
|
|
$this->schema()->create('taggables', function (Blueprint $table) { |
|
29
|
|
|
$table->unsignedBigInteger('tag_id'); |
|
30
|
|
|
$table->unsignedBigInteger('taggable_id'); |
|
31
|
|
|
$table->string('taggable_type'); |
|
32
|
|
|
$table->timestamps(); |
|
33
|
|
|
}); |
|
34
|
|
|
|
|
35
|
|
|
$this->schema()->create('taggables_using', function (Blueprint $table) { |
|
36
|
|
|
$table->unsignedBigInteger('tag_id'); |
|
37
|
|
|
$table->unsignedBigInteger('taggable_id'); |
|
38
|
|
|
$table->string('taggable_type'); |
|
39
|
|
|
$table->timestamps(); |
|
40
|
|
|
}); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function seedData(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$post = EloquentMorphToManyPost::query()->create(['id' => 1]); |
|
46
|
|
|
$tag = EloquentMorphToManyTag::query()->create(['id' => 1, 'date' => Chronos::now()]); |
|
47
|
|
|
|
|
48
|
|
|
$tag->posts()->save($post); |
|
49
|
|
|
$tag->postsUsing()->save($post); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function tearDown(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->schema()->drop('taggables'); |
|
55
|
|
|
$this->schema()->drop('tags'); |
|
56
|
|
|
$this->schema()->drop('posts'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testDateFieldsReturnChronos(): void |
|
60
|
|
|
{ |
|
61
|
|
|
$this->seedData(); |
|
62
|
|
|
|
|
63
|
|
|
$tag = EloquentMorphToManyTag::query()->first(); |
|
64
|
|
|
foreach ($tag->posts as $post) { |
|
|
|
|
|
|
65
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $post->created_at); |
|
66
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $post->updated_at); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
foreach ($tag->postsUsing as $post) { |
|
|
|
|
|
|
70
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $post->created_at); |
|
71
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $post->updated_at); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$post = EloquentMorphToManyPost::query()->first(); |
|
75
|
|
|
foreach ($post->tags as $tag) { |
|
|
|
|
|
|
76
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $tag->created_at); |
|
77
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $tag->date); |
|
78
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $tag->updated_at); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->assertSame(Pivot::class, $tag->posts()->getPivotClass()); |
|
82
|
|
|
$this->assertSame(EloquentMorphToManyTaggablesUsing::class, $tag->postsUsing()->getPivotClass()); |
|
83
|
|
|
$this->assertSame(Pivot::class, $post->tags()->getPivotClass()); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
class EloquentMorphToManyPost extends Model |
|
88
|
|
|
{ |
|
89
|
|
|
protected $fillable = ['id']; |
|
90
|
|
|
|
|
91
|
|
|
protected $table = 'posts'; |
|
92
|
|
|
|
|
93
|
|
|
public function tags(): MorphToMany |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->morphToMany( |
|
96
|
|
|
EloquentMorphToManyTag::class, |
|
97
|
|
|
'taggable', |
|
98
|
|
|
null, |
|
99
|
|
|
null, |
|
100
|
|
|
'tag_id' |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
class EloquentMorphToManyTag extends Model |
|
106
|
|
|
{ |
|
107
|
|
|
protected $dates = ['date']; |
|
108
|
|
|
|
|
109
|
|
|
protected $fillable = ['id', 'date']; |
|
110
|
|
|
|
|
111
|
|
|
protected $table = 'tags'; |
|
112
|
|
|
|
|
113
|
|
|
public function posts(): MorphToMany |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->morphedByMany( |
|
116
|
|
|
EloquentMorphToManyPost::class, |
|
117
|
|
|
'taggable', |
|
118
|
|
|
null, |
|
119
|
|
|
'tag_id' |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function postsUsing(): MorphToMany |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->morphedByMany( |
|
126
|
|
|
EloquentMorphToManyPost::class, |
|
127
|
|
|
'taggable', |
|
128
|
|
|
null, |
|
129
|
|
|
'tag_id' |
|
130
|
|
|
)->using(EloquentMorphToManyTaggablesUsing::class); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
class EloquentMorphToManyTaggablesUsing extends MorphPivot |
|
135
|
|
|
{ |
|
136
|
|
|
protected $table = 'taggables_using'; |
|
137
|
|
|
} |
|
138
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.