123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- /**
- * 自动时间戳
- * User: Administrator
- * Date: 2019/11/3 0003
- * Time: 0:05
- */
- namespace EasySwoole\ORM\Tests;
- use EasySwoole\ORM\Db\Config;
- use EasySwoole\ORM\Db\Connection;
- use EasySwoole\ORM\DbManager;
- use PHPUnit\Framework\TestCase;
- use EasySwoole\ORM\Tests\models\TestTimeStampModel;
- /**
- * Class TestUserModel
- * @package EasySwoole\ORM\Tests
- * @property $id
- * @property $name
- * @property $age
- * @property $addTime
- * @property $state
- */
- class TimeStampTest extends TestCase
- {
- /**
- * @var $connection Connection
- */
- protected $connection;
- protected $tableName = 'user_test_list';
- protected function setUp(): void
- {
- parent::setUp(); // TODO: Change the autogenerated stub
- $config = new Config(MYSQL_CONFIG);
- $this->connection = new Connection($config);
- DbManager::getInstance()->addConnection($this->connection);
- $connection = DbManager::getInstance()->getConnection();
- $this->assertTrue($connection === $this->connection);
- }
- /**
- * @throws \EasySwoole\ORM\Exception\Exception
- * @throws \Throwable
- */
- public function testInsertInt()
- {
- $user = TestTimeStampModel::create();
- $user->name = 'siam';
- $user->age = 21;
- $res = $user->save();
- $this->assertIsInt($user->id);
- $this->assertIsInt($user->create_at);
- $this->assertIsInt($user->update_at);
- }
- /**
- * @throws \EasySwoole\Mysqli\Exception\Exception
- * @throws \EasySwoole\ORM\Exception\Exception
- * @throws \Throwable
- */
- public function testUpdateInt()
- {
- $user = TestTimeStampModel::create()->get([
- 'name' => 'siam'
- ]);
- $user->age = 22;
- $time = $user->update_at;
- sleep(1);
- $user->update();
- $this->assertTrue(($user->update_at - $time) > 0);
- }
- /**
- * @throws \EasySwoole\ORM\Exception\Exception
- * @throws \Throwable
- */
- public function testInsertDate()
- {
- $user = TestTimeStampModel::create();
- $user->setAutoTime('datetime');
- $user->setCreateTime('create_time');
- $user->setUpdateTime('update_time');
- $user->name = 'siam_date';
- $user->age = 21;
- $res = $user->save();
- $this->assertIsInt($user->id);
- $this->assertIsInt(strtotime($user->create_time));
- $this->assertIsInt(strtotime($user->update_time));
- }
- /**
- * @throws \EasySwoole\Mysqli\Exception\Exception
- * @throws \EasySwoole\ORM\Exception\Exception
- * @throws \Throwable
- */
- public function testUpdateDate()
- {
- $user = TestTimeStampModel::create();
- $user = $user->get([
- 'name' => 'siam_date'
- ]);
- // get返回的是一个新模型 需要在这里设置
- $user->setAutoTime('datetime');
- $user->setCreateTime('create_time');
- $user->setUpdateTime('update_time');
- $user->age = 22;
- sleep(1);
- $time = date('Y-m-d H:i:s');
- $user->update();
- $this->assertEquals($time, $user->update_time);
- }
- /**
- * @throws \EasySwoole\ORM\Exception\Exception
- * @throws \Throwable
- */
- public function testDeleteAll()
- {
- $res = TestTimeStampModel::create()->destroy(null, true);
- $this->assertIsInt($res);
- }
- }
|