TableObjectGenerationTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * 生成表
  4. * User: Tioncico
  5. * Date: 2019/10/22 0022
  6. * Time: 14:10
  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 EasySwoole\ORM\Utility\TableObjectGeneration;
  15. use PHPUnit\Framework\TestCase;
  16. class TableObjectGenerationTest extends TestCase
  17. {
  18. /**
  19. * @var $generation TableObjectGeneration
  20. */
  21. protected $generation;
  22. /**
  23. * @var $connection Connection
  24. */
  25. protected $connection;
  26. protected $tableName = 'test';
  27. protected function setUp(): void
  28. {
  29. parent::setUp(); // TODO: Change the autogenerated stub
  30. $config = new Config(MYSQL_CONFIG);
  31. $connection = new Connection($config);
  32. $this->connection = $connection;
  33. $this->generation = new TableObjectGeneration($connection, $this->tableName);
  34. $this->createTestTable();
  35. }
  36. function createTestTable()
  37. {
  38. $sql = "DROP TABLE if exists {$this->tableName};";
  39. $query = new QueryBuilder();
  40. $query->raw($sql);
  41. $data = $this->connection->defer()->query($query);
  42. $this->assertTrue($data->getResult());
  43. $tableDDL = new Table($this->tableName);
  44. $tableDDL->colInt('id', 11)->setIsPrimaryKey()->setIsAutoIncrement();
  45. $tableDDL->colVarChar('name', 255);
  46. $tableDDL->colTinyInt('age', 1);
  47. $tableDDL->colDateTime('addTime');
  48. $tableDDL->colTinyInt('state', 1);
  49. $tableDDL->setIfNotExists();
  50. $sql = $tableDDL->__createDDL();
  51. $query->raw($sql);
  52. $data = $this->connection->defer()->query($query);
  53. $this->assertTrue($data->getResult());
  54. }
  55. function testGetTableInfo()
  56. {
  57. $data = $this->generation->getTableColumnsInfo();
  58. $this->assertIsArray($data);
  59. }
  60. }