ReuniteTableTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace EasySwoole\ORM\Tests;
  3. use EasySwoole\ORM\Db\Config;
  4. use EasySwoole\ORM\Db\Connection;
  5. use EasySwoole\ORM\DbManager;
  6. use EasySwoole\ORM\Tests\models\ReuniteTableModel;
  7. use PHPUnit\Framework\TestCase;
  8. use EasySwoole\Mysqli\QueryBuilder;
  9. class ReuniteTableTest extends TestCase
  10. {
  11. /**
  12. * @var $connection Connection
  13. */
  14. protected $connection;
  15. protected $tableName = 'reunite_table';
  16. protected function setUp(): void
  17. {
  18. parent::setUp(); // TODO: Change the autogenerated stub
  19. $config = new Config(MYSQL_CONFIG);
  20. $this->connection = new Connection($config);
  21. DbManager::getInstance()->addConnection($this->connection);
  22. $connection = DbManager::getInstance()->getConnection();
  23. $this->assertTrue($connection === $this->connection);
  24. $this->createTestTable();
  25. }
  26. function createTestTable()
  27. {
  28. $query = new QueryBuilder();
  29. $sql = <<<sql
  30. CREATE TABLE If Not Exists `reunite_table` (
  31. `id` int(11) NOT NULL AUTO_INCREMENT,
  32. `pk_1` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  33. `pk_2` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  34. PRIMARY KEY (`pk_1`, `pk_2`) USING BTREE,
  35. INDEX `id`(`id`) USING BTREE
  36. ) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
  37. sql;
  38. $query->raw($sql);
  39. $data = $this->connection->defer()->query($query);
  40. $this->assertTrue($data->getResult());
  41. }
  42. /**
  43. * @throws \EasySwoole\ORM\Exception\Exception
  44. * @throws \Throwable
  45. */
  46. function testAdd()
  47. {
  48. $model = ReuniteTableModel::create([
  49. 'pk_1' => 1,
  50. 'pk_2' => 2
  51. ])->save();
  52. $this->assertIsInt($model);
  53. $model = ReuniteTableModel::create([
  54. 'pk_1' => 1,
  55. 'pk_2' => 1
  56. ])->save();
  57. $this->assertIsInt($model);
  58. }
  59. /**
  60. * @throws \EasySwoole\Mysqli\Exception\Exception
  61. * @throws \EasySwoole\ORM\Exception\Exception
  62. * @throws \Throwable
  63. */
  64. function testGet()
  65. {
  66. $model = ReuniteTableModel::create()->get();
  67. $this->assertInstanceOf(ReuniteTableModel::class, $model);
  68. $model = ReuniteTableModel::create()->get([
  69. 'pk_1' => 1,
  70. 'pk_2' => 1
  71. ]);
  72. $this->assertInstanceOf(ReuniteTableModel::class, $model);
  73. $deleteOne = $model->destroy();
  74. $this->assertEquals(1, $deleteOne);
  75. }
  76. function testUpdate()
  77. {
  78. $model = ReuniteTableModel::create()->get([
  79. 'pk_1' => 1,
  80. 'pk_2' => 2
  81. ]);
  82. $this->assertInstanceOf(ReuniteTableModel::class, $model);
  83. $model->pk_2 = 'new';
  84. $updateRes = $model->update();
  85. $this->assertEquals(true, $updateRes);
  86. $this->assertEquals(1, $model->lastQueryResult()->getAffectedRows());
  87. }
  88. /**
  89. * @throws \EasySwoole\ORM\Exception\Exception
  90. * @throws \Throwable
  91. */
  92. function testDeleteAll()
  93. {
  94. $int = ReuniteTableModel::create()->destroy(null, true);
  95. $this->assertIsInt($int);
  96. }
  97. }