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\Schema\Blueprint; |
||
9 | |||
10 | class EloquentMorphToTest extends EloquentTestCase |
||
11 | { |
||
12 | protected function createSchema(): void |
||
13 | { |
||
14 | $this->schema()->create('posts', function (Blueprint $table) { |
||
15 | $table->increments('id'); |
||
16 | }); |
||
17 | |||
18 | $this->schema()->create('images', function (Blueprint $table) { |
||
19 | $table->increments('id'); |
||
20 | $table->unsignedBigInteger('imageable_id'); |
||
21 | $table->string('imageable_type'); |
||
22 | $table->date('date'); |
||
23 | $table->timestamps(); |
||
24 | }); |
||
25 | } |
||
26 | |||
27 | protected function seedData(): void |
||
28 | { |
||
29 | EloquentMorphToPost::query()->create(['id' => 1]); |
||
30 | EloquentMorphToImage::query()->create([ |
||
31 | 'imageable_id' => 1, |
||
32 | 'imageable_type' => EloquentMorphToPost::class, |
||
33 | 'date' => Chronos::now(), |
||
34 | ]); |
||
35 | } |
||
36 | |||
37 | protected function tearDown(): void |
||
38 | { |
||
39 | $this->schema()->drop('images'); |
||
40 | $this->schema()->drop('posts'); |
||
41 | } |
||
42 | |||
43 | public function testDateFieldsReturnChronos(): void |
||
44 | { |
||
45 | $this->seedData(); |
||
46 | |||
47 | $post = EloquentMorphToPost::query()->first(); |
||
48 | $image = $post->image; |
||
49 | |||
50 | $this->assertInstanceOf(ChronosInterface::class, $image->created_at); |
||
51 | $this->assertInstanceOf(ChronosInterface::class, $image->date); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
52 | $this->assertInstanceOf(ChronosInterface::class, $image->updated_at); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | class EloquentMorphToImage extends Model |
||
57 | { |
||
58 | protected $dates = ['date']; |
||
59 | |||
60 | protected $fillable = ['imageable_id', 'imageable_type', 'date']; |
||
61 | |||
62 | protected $table = 'images'; |
||
63 | |||
64 | public function imageable() |
||
65 | { |
||
66 | return $this->morphTo(); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | class EloquentMorphToPost extends Model |
||
71 | { |
||
72 | protected $fillable = ['id']; |
||
73 | |||
74 | protected $table = 'posts'; |
||
75 | |||
76 | public $timestamps = false; |
||
77 | |||
78 | public function image() |
||
79 | { |
||
80 | return $this->morphOne(EloquentMorphToImage::class, 'imageable'); |
||
81 | } |
||
82 | } |
||
83 |