|
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\HasOneThrough; |
|
9
|
|
|
use Illuminate\Database\Schema\Blueprint; |
|
10
|
|
|
|
|
11
|
|
|
class EloquentHasOneTroughTest extends EloquentTestCase |
|
12
|
|
|
{ |
|
13
|
|
|
protected function createSchema(): void |
|
14
|
|
|
{ |
|
15
|
|
|
$this->schema()->create('suppliers', function (Blueprint $table) { |
|
16
|
|
|
$table->increments('id'); |
|
17
|
|
|
}); |
|
18
|
|
|
|
|
19
|
|
|
$this->schema()->create('users', function (Blueprint $table) { |
|
20
|
|
|
$table->increments('id'); |
|
21
|
|
|
$table->unsignedBigInteger('supplier_id'); |
|
22
|
|
|
|
|
23
|
|
|
$table->foreign('supplier_id')->references('id')->on('suppliers')->onDelete('cascade'); |
|
24
|
|
|
}); |
|
25
|
|
|
|
|
26
|
|
|
$this->schema()->create('history', 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
|
|
|
EloquentHasOneTroughSupplier::query()->create(['id' => 1]); |
|
39
|
|
|
EloquentHasOneTroughUser::query()->create(['id' => 1, 'supplier_id' => 1]); |
|
40
|
|
|
EloquentHasOneTroughHistory::query()->create(['id' => 1, 'user_id' => 1, 'date' => Chronos::now()]); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function tearDown(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$this->schema()->drop('history'); |
|
46
|
|
|
$this->schema()->drop('suppliers'); |
|
47
|
|
|
$this->schema()->drop('users'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testDateFieldsReturnChronos(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$this->seedData(); |
|
53
|
|
|
|
|
54
|
|
|
$supplier = EloquentHasOneTroughSupplier::query()->first(); |
|
55
|
|
|
$userHistory = $supplier->userHistory; |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $userHistory->created_at); |
|
58
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $userHistory->date); |
|
59
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $userHistory->updated_at); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
class EloquentHasOneTroughSupplier extends Model |
|
64
|
|
|
{ |
|
65
|
|
|
protected $fillable = ['id']; |
|
66
|
|
|
|
|
67
|
|
|
protected $table = 'suppliers'; |
|
68
|
|
|
|
|
69
|
|
|
public $timestamps = false; |
|
70
|
|
|
|
|
71
|
|
|
public function userHistory(): HasOneThrough |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->hasOneThrough( |
|
74
|
|
|
EloquentHasOneTroughHistory::class, |
|
75
|
|
|
EloquentHasOneTroughUser::class, |
|
76
|
|
|
'supplier_id', |
|
77
|
|
|
'user_id' |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
class EloquentHasOneTroughHistory extends Model |
|
83
|
|
|
{ |
|
84
|
|
|
protected $dates = ['date']; |
|
85
|
|
|
|
|
86
|
|
|
protected $fillable = ['date', 'id', 'user_id']; |
|
87
|
|
|
|
|
88
|
|
|
protected $table = 'history'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
class EloquentHasOneTroughUser extends Model |
|
92
|
|
|
{ |
|
93
|
|
|
protected $fillable = ['id', 'supplier_id']; |
|
94
|
|
|
|
|
95
|
|
|
protected $table = 'users'; |
|
96
|
|
|
|
|
97
|
|
|
public $timestamps = false; |
|
98
|
|
|
} |
|
99
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.