-
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.
[5.6] Allow to disable CREATED_AT in Models (#23667)
* [5.6] Allow to disable `CREATED_AT`. * [5.6] add unit tests for CREATED_AT and UPDATED_AT disabling. * [5.6] code reformat.
- Loading branch information
1 parent
c99911f
commit 981bb18
Showing
2 changed files
with
154 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,152 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Database; | ||
|
||
use Illuminate\Database\Capsule\Manager as DB; | ||
use Illuminate\Database\Connection; | ||
use Illuminate\Database\Eloquent\Model as Eloquent; | ||
use Illuminate\Support\Carbon; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DatabaseEloquentTimestamps extends TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
$db = new DB; | ||
|
||
$db->addConnection([ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
]); | ||
|
||
$db->bootEloquent(); | ||
$db->setAsGlobal(); | ||
|
||
$this->createSchema(); | ||
} | ||
|
||
/** | ||
* Setup the database schema. | ||
* | ||
* @return void | ||
*/ | ||
public function createSchema() | ||
{ | ||
$this->schema()->create('users', function ($table) { | ||
$table->increments('id'); | ||
$table->string('email')->unique(); | ||
$table->timestamps(); | ||
}); | ||
|
||
$this->schema()->create('users_created_at', function ($table) { | ||
$table->increments('id'); | ||
$table->string('email')->unique(); | ||
$table->string('created_at'); | ||
}); | ||
|
||
$this->schema()->create('users_updated_at', function ($table) { | ||
$table->increments('id'); | ||
$table->string('email')->unique(); | ||
$table->string('updated_at'); | ||
}); | ||
} | ||
|
||
/** | ||
* Tear down the database schema. | ||
* | ||
* @return void | ||
*/ | ||
public function tearDown() | ||
{ | ||
$this->schema()->drop('users'); | ||
$this->schema()->drop('users_created_at'); | ||
$this->schema()->drop('users_updated_at'); | ||
} | ||
|
||
/** | ||
* Tests... | ||
*/ | ||
public function testUserWithCreatedAtAndUpdatedAt() | ||
{ | ||
$now = Carbon::now(); | ||
$user = UserWithCreatedAndUpdated::create([ | ||
'email' => 'test@test.com', | ||
]); | ||
|
||
$this->assertEquals($now->toDateTimeString(), $user->created_at->toDateTimeString()); | ||
$this->assertEquals($now->toDateTimeString(), $user->updated_at->toDateTimeString()); | ||
} | ||
|
||
|
||
public function testUserWithCreatedAt() | ||
{ | ||
$now = Carbon::now(); | ||
$user = UserWithCreated::create([ | ||
'email' => 'test@test.com', | ||
]); | ||
|
||
$this->assertEquals($now->toDateTimeString(), $user->created_at->toDateTimeString()); | ||
} | ||
|
||
public function testUserWithUpdatedAt() | ||
{ | ||
$now = Carbon::now(); | ||
$user = UserWithUpdated::create([ | ||
'email' => 'test@test.com', | ||
]); | ||
|
||
$this->assertEquals($now->toDateTimeString(), $user->updated_at->toDateTimeString()); | ||
} | ||
|
||
/** | ||
* Get a database connection instance. | ||
* | ||
* @return Connection | ||
*/ | ||
protected function connection() | ||
{ | ||
return Eloquent::getConnectionResolver()->connection(); | ||
} | ||
|
||
/** | ||
* Get a schema builder instance. | ||
* | ||
* @return Schema\Builder | ||
*/ | ||
protected function schema() | ||
{ | ||
return $this->connection()->getSchemaBuilder(); | ||
} | ||
} | ||
|
||
/** | ||
* Eloquent Models... | ||
*/ | ||
class UserWithCreatedAndUpdated extends Eloquent | ||
{ | ||
protected $table = 'users'; | ||
|
||
protected $guarded = []; | ||
} | ||
|
||
class UserWithCreated extends Eloquent | ||
{ | ||
public const UPDATED_AT = null; | ||
|
||
protected $table = 'users_created_at'; | ||
|
||
protected $guarded = []; | ||
|
||
protected $dateFormat = 'U'; | ||
} | ||
|
||
class UserWithUpdated extends Eloquent | ||
{ | ||
public const CREATED_AT = null; | ||
|
||
protected $table = 'users_updated_at'; | ||
|
||
protected $guarded = []; | ||
|
||
protected $dateFormat = 'U'; | ||
} |