Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
macbookandrew committed Sep 27, 2021
1 parent 60b02d5 commit a432e44
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/Integration/Database/EloquentModelDateCastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Integration\Database\EloquentModelDateCastingTest;

use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Carbon;
Expand All @@ -21,6 +22,8 @@ protected function setUp(): void
$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();
});
}

Expand All @@ -36,6 +39,67 @@ public function testDatesAreCustomCastable()
$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;
});

$user = TestModel1::create([
'date_field' => '2019-10',
'datetime_field' => new CarbonImmutable('2019-10-01 10:15:20'),
'immutable_date_field' => new CarbonImmutable('2019-10-01'),
'immutable_datetime_field' => '2019-10-01 10:15',
]);

$this->assertSame(['2019-10', '2019-10-01 10:15:20', '2019-10-01 00:00:00', '2019-10-01 10:15'], $bindings);
}


public function testCustomDateCastsAreComparedAsDatesForCarbonInstances()
{
/** @var TestModel1 */
$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()
{
/** @var TestModel1 */
$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
Expand All @@ -47,5 +111,7 @@ class TestModel1 extends Model
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',
];
}

0 comments on commit a432e44

Please sign in to comment.