UserListModelTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Model自定义表名测试
  4. * User: Tioncico
  5. * Date: 2019/10/22 0022
  6. * Time: 15:08
  7. */
  8. namespace EasySwoole\ORM\Tests;
  9. use EasySwoole\Mysqli\QueryBuilder;
  10. use EasySwoole\ORM\Db\Config;
  11. use EasySwoole\ORM\Db\Connection;
  12. use EasySwoole\ORM\DbManager;
  13. use EasySwoole\ORM\Utility\Schema\Table;
  14. use PHPUnit\Framework\TestCase;
  15. use EasySwoole\ORM\Tests\models\TestUserListModel;
  16. use EasySwoole\ORM\Tests\models\TestUserModel;
  17. use EasySwoole\ORM\Tests\models\TestTimeStampModel;
  18. use EasySwoole\ORM\Tests\models\TestFunctionFieldNameModel;
  19. use EasySwoole\ORM\Tests\models\TestRelationModel;
  20. use EasySwoole\ORM\Tests\models\TestUserEventModel;
  21. use EasySwoole\ORM\Tests\models\TestUserListGetterModel;
  22. /**
  23. * Model自定义表名测试
  24. * Class UserListModelTest
  25. * @package EasySwoole\ORM\Tests
  26. */
  27. class UserListModelTest extends TestCase
  28. {
  29. /**
  30. * @var $connection Connection
  31. */
  32. protected $connection;
  33. protected $tableName = 'user_test_list';
  34. protected function setUp(): void
  35. {
  36. parent::setUp(); // TODO: Change the autogenerated stub
  37. $config = new Config(MYSQL_CONFIG);
  38. $this->connection = new Connection($config);
  39. DbManager::getInstance()->addConnection($this->connection);
  40. $connection = DbManager::getInstance()->getConnection();
  41. $this->assertTrue($connection === $this->connection);
  42. $this->createTestTable();
  43. }
  44. function createTestTable()
  45. {
  46. $query = new QueryBuilder();
  47. $tableDDL = new Table($this->tableName);
  48. $tableDDL->colInt('id', 11)->setIsPrimaryKey()->setIsAutoIncrement();
  49. $tableDDL->colVarChar('name', 255);
  50. $tableDDL->colTinyInt('age', 1);
  51. $tableDDL->colDateTime('addTime');
  52. $tableDDL->colTinyInt('state', 1);
  53. $tableDDL->setIfNotExists();
  54. $sql = $tableDDL->__createDDL();
  55. $query->raw($sql);
  56. $data = $this->connection->defer()->query($query);
  57. $this->assertTrue($data->getResult());
  58. }
  59. function testGetSchemaInfo()
  60. {
  61. $testUserModel = new TestUserListModel();
  62. $schemaInfo = $testUserModel->schemaInfo();
  63. $this->assertTrue($schemaInfo instanceof Table);
  64. }
  65. function testAdd()
  66. {
  67. $testUserModel = new TestUserListModel();
  68. $testUserModel->state = 1;
  69. $testUserModel->name = '仙士可';
  70. $testUserModel->age = 100;
  71. $testUserModel->addTime = date('Y-m-d H:i:s');
  72. $data = $testUserModel->save();
  73. $this->assertIsInt($data);
  74. }
  75. /**
  76. * @depends testAdd
  77. * testUpdate
  78. * @author Tioncico
  79. * Time: 15:41
  80. */
  81. function testUpdate()
  82. {
  83. $testUserModel = new TestUserListModel();
  84. /**
  85. * @var $user TestUserListModel
  86. */
  87. $user = $testUserModel->get();
  88. $user->name = '仙士可2号';
  89. $result = $user->update();
  90. $this->assertTrue($result);
  91. $user = $testUserModel->get(['id' => $user->id]);
  92. $this->assertEquals('仙士可2号', $user->name);
  93. }
  94. function testGetAll()
  95. {
  96. $testUserModel = new TestUserListModel();
  97. $data = $testUserModel->all();
  98. $this->assertIsArray($data);
  99. }
  100. function testDelete()
  101. {
  102. $testUserModel = new TestUserListModel();
  103. /**
  104. * @var $user TestUserListModel
  105. */
  106. $user = $testUserModel->get();
  107. $result = $user->destroy();
  108. $this->assertEquals(1, $result);
  109. $user = $user->get(['id' => $user->id]);
  110. $this->assertNull($user);
  111. }
  112. }