EloquentModelDateCastingTest.php 4.7 KB

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