DatabaseEloquentTimestampsTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Capsule\Manager as DB;
  4. use Illuminate\Database\Eloquent\Model as Eloquent;
  5. use Illuminate\Support\Carbon;
  6. use PHPUnit\Framework\TestCase;
  7. class DatabaseEloquentTimestampsTest extends TestCase
  8. {
  9. protected function setUp(): void
  10. {
  11. $db = new DB;
  12. $db->addConnection([
  13. 'driver' => 'sqlite',
  14. 'database' => ':memory:',
  15. ]);
  16. $db->bootEloquent();
  17. $db->setAsGlobal();
  18. $this->createSchema();
  19. Carbon::setTestNow(Carbon::now());
  20. }
  21. /**
  22. * Setup the database schema.
  23. *
  24. * @return void
  25. */
  26. public function createSchema()
  27. {
  28. $this->schema()->create('users', function ($table) {
  29. $table->increments('id');
  30. $table->string('email')->unique();
  31. $table->timestamps();
  32. });
  33. $this->schema()->create('users_created_at', function ($table) {
  34. $table->increments('id');
  35. $table->string('email')->unique();
  36. $table->string('created_at');
  37. });
  38. $this->schema()->create('users_updated_at', function ($table) {
  39. $table->increments('id');
  40. $table->string('email')->unique();
  41. $table->string('updated_at');
  42. });
  43. }
  44. /**
  45. * Tear down the database schema.
  46. *
  47. * @return void
  48. */
  49. protected function tearDown(): void
  50. {
  51. $this->schema()->drop('users');
  52. $this->schema()->drop('users_created_at');
  53. $this->schema()->drop('users_updated_at');
  54. }
  55. /**
  56. * Tests...
  57. */
  58. public function testUserWithCreatedAtAndUpdatedAt()
  59. {
  60. $now = Carbon::now();
  61. $user = UserWithCreatedAndUpdated::create([
  62. 'email' => 'test@test.com',
  63. ]);
  64. $this->assertEquals($now->toDateTimeString(), $user->created_at->toDateTimeString());
  65. $this->assertEquals($now->toDateTimeString(), $user->updated_at->toDateTimeString());
  66. }
  67. public function testUserWithCreatedAt()
  68. {
  69. $now = Carbon::now();
  70. $user = UserWithCreated::create([
  71. 'email' => 'test@test.com',
  72. ]);
  73. $this->assertEquals($now->toDateTimeString(), $user->created_at->toDateTimeString());
  74. }
  75. public function testUserWithUpdatedAt()
  76. {
  77. $now = Carbon::now();
  78. $user = UserWithUpdated::create([
  79. 'email' => 'test@test.com',
  80. ]);
  81. $this->assertEquals($now->toDateTimeString(), $user->updated_at->toDateTimeString());
  82. }
  83. /**
  84. * Get a database connection instance.
  85. *
  86. * @return \Illuminate\Database\Connection
  87. */
  88. protected function connection()
  89. {
  90. return Eloquent::getConnectionResolver()->connection();
  91. }
  92. /**
  93. * Get a schema builder instance.
  94. *
  95. * @return \Illuminate\Database\Schema\Builder
  96. */
  97. protected function schema()
  98. {
  99. return $this->connection()->getSchemaBuilder();
  100. }
  101. }
  102. /**
  103. * Eloquent Models...
  104. */
  105. class UserWithCreatedAndUpdated extends Eloquent
  106. {
  107. protected $table = 'users';
  108. protected $guarded = [];
  109. }
  110. class UserWithCreated extends Eloquent
  111. {
  112. public const UPDATED_AT = null;
  113. protected $table = 'users_created_at';
  114. protected $guarded = [];
  115. protected $dateFormat = 'U';
  116. }
  117. class UserWithUpdated extends Eloquent
  118. {
  119. public const CREATED_AT = null;
  120. protected $table = 'users_updated_at';
  121. protected $guarded = [];
  122. protected $dateFormat = 'U';
  123. }