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\BelongsTo; |
9
|
|
|
use Illuminate\Database\Schema\Blueprint; |
10
|
|
|
|
11
|
|
|
class EloquentBelongsToOneTest extends EloquentTestCase |
12
|
|
|
{ |
13
|
|
|
protected function createSchema(): void |
14
|
|
|
{ |
15
|
|
|
$this->schema()->create('users', function (Blueprint $table) { |
16
|
|
|
$table->increments('id'); |
17
|
|
|
$table->date('date'); |
18
|
|
|
$table->timestamps(); |
19
|
|
|
}); |
20
|
|
|
|
21
|
|
|
$this->schema()->create('phones', function (Blueprint $table) { |
22
|
|
|
$table->increments('id'); |
23
|
|
|
$table->unsignedBigInteger('user_id'); |
24
|
|
|
|
25
|
|
|
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); |
26
|
|
|
}); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function seedData(): void |
30
|
|
|
{ |
31
|
|
|
EloquentBelongsToUser::query()->create(['date' => Chronos::now()]); |
32
|
|
|
EloquentBelongsToPhone::query()->create(['user_id' => 1]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function tearDown(): void |
36
|
|
|
{ |
37
|
|
|
$this->schema()->drop('users'); |
38
|
|
|
$this->schema()->drop('phones'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testDateFieldsReturnChronos(): void |
42
|
|
|
{ |
43
|
|
|
$this->seedData(); |
44
|
|
|
|
45
|
|
|
$phone = EloquentBelongsToPhone::query()->first(); |
46
|
|
|
$user = $phone->first()->user; |
47
|
|
|
|
48
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $user->created_at); |
|
|
|
|
49
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $user->date); |
|
|
|
|
50
|
|
|
$this->assertInstanceOf(ChronosInterface::class, $user->updated_at); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
class EloquentBelongsToUser extends Model |
55
|
|
|
{ |
56
|
|
|
protected $dates = ['date']; |
57
|
|
|
|
58
|
|
|
protected $fillable = ['date', 'user_id']; |
59
|
|
|
|
60
|
|
|
protected $table = 'users'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
class EloquentBelongsToPhone extends Model |
64
|
|
|
{ |
65
|
|
|
protected $fillable = ['user_id']; |
66
|
|
|
|
67
|
|
|
protected $table = 'phones'; |
68
|
|
|
|
69
|
|
|
public $timestamps = false; |
70
|
|
|
|
71
|
|
|
public function user(): BelongsTo |
72
|
|
|
{ |
73
|
|
|
return $this->BelongsTo(EloquentBelongsToUser::class, 'user_id', 'id'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|