TimeStampTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * 自动时间戳
  4. * User: Administrator
  5. * Date: 2019/11/3 0003
  6. * Time: 0:05
  7. */
  8. namespace EasySwoole\ORM\Tests;
  9. use EasySwoole\ORM\Db\Config;
  10. use EasySwoole\ORM\Db\Connection;
  11. use EasySwoole\ORM\DbManager;
  12. use PHPUnit\Framework\TestCase;
  13. use EasySwoole\ORM\Tests\models\TestTimeStampModel;
  14. /**
  15. * Class TestUserModel
  16. * @package EasySwoole\ORM\Tests
  17. * @property $id
  18. * @property $name
  19. * @property $age
  20. * @property $addTime
  21. * @property $state
  22. */
  23. class TimeStampTest extends TestCase
  24. {
  25. /**
  26. * @var $connection Connection
  27. */
  28. protected $connection;
  29. protected $tableName = 'user_test_list';
  30. protected function setUp(): void
  31. {
  32. parent::setUp(); // TODO: Change the autogenerated stub
  33. $config = new Config(MYSQL_CONFIG);
  34. $this->connection = new Connection($config);
  35. DbManager::getInstance()->addConnection($this->connection);
  36. $connection = DbManager::getInstance()->getConnection();
  37. $this->assertTrue($connection === $this->connection);
  38. }
  39. /**
  40. * @throws \EasySwoole\ORM\Exception\Exception
  41. * @throws \Throwable
  42. */
  43. public function testInsertInt()
  44. {
  45. $user = TestTimeStampModel::create();
  46. $user->name = 'siam';
  47. $user->age = 21;
  48. $res = $user->save();
  49. $this->assertIsInt($user->id);
  50. $this->assertIsInt($user->create_at);
  51. $this->assertIsInt($user->update_at);
  52. }
  53. /**
  54. * @throws \EasySwoole\Mysqli\Exception\Exception
  55. * @throws \EasySwoole\ORM\Exception\Exception
  56. * @throws \Throwable
  57. */
  58. public function testUpdateInt()
  59. {
  60. $user = TestTimeStampModel::create()->get([
  61. 'name' => 'siam'
  62. ]);
  63. $user->age = 22;
  64. $time = $user->update_at;
  65. sleep(1);
  66. $user->update();
  67. $this->assertTrue(($user->update_at - $time) > 0);
  68. }
  69. /**
  70. * @throws \EasySwoole\ORM\Exception\Exception
  71. * @throws \Throwable
  72. */
  73. public function testInsertDate()
  74. {
  75. $user = TestTimeStampModel::create();
  76. $user->setAutoTime('datetime');
  77. $user->setCreateTime('create_time');
  78. $user->setUpdateTime('update_time');
  79. $user->name = 'siam_date';
  80. $user->age = 21;
  81. $res = $user->save();
  82. $this->assertIsInt($user->id);
  83. $this->assertIsInt(strtotime($user->create_time));
  84. $this->assertIsInt(strtotime($user->update_time));
  85. }
  86. /**
  87. * @throws \EasySwoole\Mysqli\Exception\Exception
  88. * @throws \EasySwoole\ORM\Exception\Exception
  89. * @throws \Throwable
  90. */
  91. public function testUpdateDate()
  92. {
  93. $user = TestTimeStampModel::create();
  94. $user = $user->get([
  95. 'name' => 'siam_date'
  96. ]);
  97. // get返回的是一个新模型 需要在这里设置
  98. $user->setAutoTime('datetime');
  99. $user->setCreateTime('create_time');
  100. $user->setUpdateTime('update_time');
  101. $user->age = 22;
  102. sleep(1);
  103. $time = date('Y-m-d H:i:s');
  104. $user->update();
  105. $this->assertEquals($time, $user->update_time);
  106. }
  107. /**
  108. * @throws \EasySwoole\ORM\Exception\Exception
  109. * @throws \Throwable
  110. */
  111. public function testDeleteAll()
  112. {
  113. $res = TestTimeStampModel::create()->destroy(null, true);
  114. $this->assertIsInt($res);
  115. }
  116. }