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 Illuminate\Database\Eloquent\Relations\HasManyThrough; |
9
|
|
|
use Illuminate\Database\Schema\Blueprint; |
10
|
|
|
|
11
|
|
|
class EloquentHasManyThroughTest extends EloquentTestCase |
12
|
|
|
{ |
13
|
|
|
protected function createSchema(): void |
14
|
|
|
{ |
15
|
|
|
$this->schema()->create('countries', function (Blueprint $table) { |
16
|
|
|
$table->increments('id'); |
17
|
|
|
}); |
18
|
|
|
|
19
|
|
|
$this->schema()->create('users', function (Blueprint $table) { |
20
|
|
|
$table->increments('id'); |
21
|
|
|
$table->unsignedBigInteger('country_id'); |
22
|
|
|
|
23
|
|
|
$table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade'); |
24
|
|
|
}); |
25
|
|
|
|
26
|
|
|
$this->schema()->create('posts', function (Blueprint $table) { |
27
|
|
|
$table->increments('id'); |
28
|
|
|
$table->date('date'); |
29
|
|
|
$table->unsignedBigInteger('user_id'); |
30
|
|
|
$table->timestamps(); |
31
|
|
|
|
32
|
|
|
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function seedData(): void |
37
|
|
|
{ |
38
|
|
|
EloquentHasManyTroughCountry::query()->create(['id' => 1]); |
39
|
|
|
EloquentHasManyTroughUser::query()->create(['id' => 1, 'country_id' => 1]); |
40
|
|
|
EloquentHasManyTroughPost::query()->create(['id' => 1, 'user_id' => 1, 'date' => Chronos::now()]); |
41
|
|
|
EloquentHasManyTroughPost::query()->create(['id' => 2, 'user_id' => 1, 'date' => Chronos::now()]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function tearDown(): void |
45
|
|
|
{ |
46
|
|
|
$this->schema()->drop('posts'); |
47
|
|
|
$this->schema()->drop('users'); |
48
|
|
|
$this->schema()->drop('countries'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testDateFieldsReturnChronos(): void |
52
|
|
|
{ |
53
|
|
|
$this->seedData(); |
54
|
|
|
|
55
|
|
|
$country = EloquentHasManyTroughCountry::query()->first(); |
56
|
|
|
$posts = $country->posts; |
57
|
|
|
|
58
|
|
|
foreach ($posts as $post) { |
59
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $post->created_at); |
60
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $post->date); |
61
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $post->updated_at); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
class EloquentHasManyTroughCountry extends Model |
67
|
|
|
{ |
68
|
|
|
protected $fillable = ['id']; |
69
|
|
|
|
70
|
|
|
protected $table = 'countries'; |
71
|
|
|
|
72
|
|
|
public $timestamps = false; |
73
|
|
|
|
74
|
|
|
public function posts(): HasManyThrough |
75
|
|
|
{ |
76
|
|
|
return $this->hasManyThrough( |
77
|
|
|
EloquentHasManyTroughPost::class, |
78
|
|
|
EloquentHasManyTroughUser::class, |
79
|
|
|
'country_id', |
80
|
|
|
'user_id' |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
class EloquentHasManyTroughUser extends Model |
86
|
|
|
{ |
87
|
|
|
protected $fillable = ['id', 'country_id']; |
88
|
|
|
|
89
|
|
|
protected $table = 'users'; |
90
|
|
|
|
91
|
|
|
public $timestamps = false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
class EloquentHasManyTroughPost extends Model |
95
|
|
|
{ |
96
|
|
|
protected $dates = ['date']; |
97
|
|
|
|
98
|
|
|
protected $fillable = ['date', 'id', 'user_id']; |
99
|
|
|
|
100
|
|
|
protected $table = 'posts'; |
101
|
|
|
} |
102
|
|
|
|