Skip to content

Commit

Permalink
[9.x] Fix inconsistent results of firstOrNew() when using withCasts() (
Browse files Browse the repository at this point in the history
…#41257)

* inconsistent results of firstOrNew test

* fix inconsistent results of firstOrNew

* styleci
  • Loading branch information
Attia-Ahmed authored Feb 28, 2022
1 parent 1a1c140 commit ee00c09
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public function newInstance($attributes = [], $exists = false)
// This method just provides a convenient way for us to generate fresh model
// instances of this current model. It is particularly useful during the
// hydration of new objects via the Eloquent query builder instances.
$model = new static((array) $attributes);
$model = new static;

$model->exists = $exists;

Expand All @@ -505,6 +505,8 @@ public function newInstance($attributes = [], $exists = false)

$model->mergeCasts($this->casts);

$model->fill((array) $attributes);

return $model;
}

Expand Down
90 changes: 90 additions & 0 deletions tests/Database/DatabaseEloquentWithCastsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Illuminate\Tests\Database;

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
use PHPUnit\Framework\TestCase;

class DatabaseEloquentWithCastsTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

$db = new DB;

$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);

$db->bootEloquent();
$db->setAsGlobal();

$this->createSchema();
}

protected function createSchema()
{
$this->schema()->create('times', function ($table) {
$table->increments('id');
$table->time('time');
$table->timestamps();
});
}

public function testWithFirstOrNew()
{
$time1 = Time::query()->withCasts(['time' => 'string'])
->firstOrNew(['time' => '07:30']);

Time::query()->insert(['time' => '07:30']);

$time2 = Time::query()->withCasts(['time' => 'string'])
->firstOrNew(['time' => '07:30']);

$this->assertSame('07:30', $time1->time);
$this->assertSame($time1->time, $time2->time);
}

public function testWithFirstOrCreate()
{
$time1 = Time::query()->withCasts(['time' => 'string'])
->firstOrCreate(['time' => '07:30']);

$time2 = Time::query()->withCasts(['time' => 'string'])
->firstOrCreate(['time' => '07:30']);

$this->assertSame($time1->id, $time2->id);
}

/**
* Get a database connection instance.
*
* @return \Illuminate\Database\Connection
*/
protected function connection()
{
return Eloquent::getConnectionResolver()->connection();
}

/**
* Get a schema builder instance.
*
* @return \Illuminate\Database\Schema\Builder
*/
protected function schema()
{
return $this->connection()->getSchemaBuilder();
}
}

class Time extends Eloquent
{
protected $guarded = [];

protected $casts = [
'time' => 'datetime',
];
}

0 comments on commit ee00c09

Please sign in to comment.