1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
<?php
namespace Illuminate\Tests\Integration\Database\EloquentModelDateCastingTest;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;
class EloquentModelDateCastingTest extends DatabaseTestCase
{
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
{
Schema::create('test_model1', function (Blueprint $table) {
$table->increments('id');
$table->date('date_field')->nullable();
$table->datetime('datetime_field')->nullable();
$table->date('immutable_date_field')->nullable();
$table->datetime('immutable_datetime_field')->nullable();
});
}
public function testDatesAreCustomCastable()
{
$user = TestModel1::create([
'date_field' => '2019-10-01',
'datetime_field' => '2019-10-01 10:15:20',
]);
$this->assertSame('2019-10', $user->toArray()['date_field']);
$this->assertSame('2019-10 10:15', $user->toArray()['datetime_field']);
$this->assertInstanceOf(Carbon::class, $user->date_field);
$this->assertInstanceOf(Carbon::class, $user->datetime_field);
}
public function testDatesFormattedAttributeBindings()
{
$bindings = [];
$this->app->make('db')->listen(static function ($query) use (&$bindings) {
$bindings = $query->bindings;
});
TestModel1::create([
'date_field' => '2019-10-01',
'datetime_field' => '2019-10-01 10:15:20',
'immutable_date_field' => '2019-10-01',
'immutable_datetime_field' => '2019-10-01 10:15',
]);
$this->assertSame(['2019-10-01', '2019-10-01 10:15:20', '2019-10-01', '2019-10-01 10:15'], $bindings);
}
public function testDatesFormattedArrayAndJson()
{
$user = TestModel1::create([
'date_field' => '2019-10-01',
'datetime_field' => '2019-10-01 10:15:20',
'immutable_date_field' => '2019-10-01',
'immutable_datetime_field' => '2019-10-01 10:15',
]);
$expected = [
'date_field' => '2019-10',
'datetime_field' => '2019-10 10:15',
'immutable_date_field' => '2019-10',
'immutable_datetime_field' => '2019-10 10:15',
'id' => 1,
];
$this->assertSame($expected, $user->toArray());
$this->assertSame(json_encode($expected), $user->toJson());
}
public function testCustomDateCastsAreComparedAsDatesForCarbonInstances()
{
$user = TestModel1::create([
'date_field' => '2019-10-01',
'datetime_field' => '2019-10-01 10:15:20',
'immutable_date_field' => '2019-10-01',
'immutable_datetime_field' => '2019-10-01 10:15:20',
]);
$user->date_field = new Carbon('2019-10-01');
$user->datetime_field = new Carbon('2019-10-01 10:15:20');
$user->immutable_date_field = new CarbonImmutable('2019-10-01');
$user->immutable_datetime_field = new CarbonImmutable('2019-10-01 10:15:20');
$this->assertArrayNotHasKey('date_field', $user->getDirty());
$this->assertArrayNotHasKey('datetime_field', $user->getDirty());
$this->assertArrayNotHasKey('immutable_date_field', $user->getDirty());
$this->assertArrayNotHasKey('immutable_datetime_field', $user->getDirty());
}
public function testCustomDateCastsAreComparedAsDatesForStringValues()
{
$user = TestModel1::create([
'date_field' => '2019-10-01',
'datetime_field' => '2019-10-01 10:15:20',
'immutable_date_field' => '2019-10-01',
'immutable_datetime_field' => '2019-10-01 10:15:20',
]);
$user->date_field = '2019-10-01';
$user->datetime_field = '2019-10-01 10:15:20';
$user->immutable_date_field = '2019-10-01';
$user->immutable_datetime_field = '2019-10-01 10:15:20';
$this->assertArrayNotHasKey('date_field', $user->getDirty());
$this->assertArrayNotHasKey('datetime_field', $user->getDirty());
$this->assertArrayNotHasKey('immutable_date_field', $user->getDirty());
$this->assertArrayNotHasKey('immutable_datetime_field', $user->getDirty());
}
}
class TestModel1 extends Model
{
public $table = 'test_model1';
public $timestamps = false;
protected $guarded = [];
public $casts = [
'date_field' => 'date:Y-m',
'datetime_field' => 'datetime:Y-m H:i',
'immutable_date_field' => 'date:Y-m',
'immutable_datetime_field' => 'datetime:Y-m H:i',
];
}
|