EloquentHasManyTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 20
c 1
b 0
f 0
dl 0
loc 40
rs 10

4 Methods

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