-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[9.x] Fix inconsistent results of firstOrNew() when using withCasts() (…
…#41257) * inconsistent results of firstOrNew test * fix inconsistent results of firstOrNew * styleci
- Loading branch information
1 parent
1a1c140
commit ee00c09
Showing
2 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
} |