WhereTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace EasySwoole\ORM\Tests;
  3. use EasySwoole\Mysqli\QueryBuilder;
  4. use EasySwoole\ORM\AbstractModel;
  5. use EasySwoole\ORM\Db\Config;
  6. use EasySwoole\ORM\Db\Connection;
  7. use EasySwoole\ORM\DbManager;
  8. use EasySwoole\ORM\Tests\models\TestWhereModel;
  9. use EasySwoole\ORM\Utility\Schema\Table;
  10. use PHPUnit\Framework\TestCase;
  11. class WhereTest extends TestCase
  12. {
  13. /**
  14. * @var $connection Connection
  15. */
  16. protected $connection;
  17. protected function setUp(): void
  18. {
  19. parent::setUp(); // TODO: Change the autogenerated stub
  20. $config = new Config(MYSQL_CONFIG);
  21. $config->setReturnCollection(true);
  22. $this->connection = new Connection($config);
  23. DbManager::getInstance()->addConnection($this->connection);
  24. $connection = DbManager::getInstance()->getConnection();
  25. $this->assertTrue($connection === $this->connection);
  26. }
  27. public function testCreateTable()
  28. {
  29. $sql = "SHOW TABLES LIKE 'test_where';";
  30. $query = new QueryBuilder();
  31. $query->raw($sql);
  32. $data = $this->connection->defer()->query($query);
  33. if (empty($data->getResult())) {
  34. $tableDDL = new Table('test_where');
  35. $tableDDL->colInt('id', 11)->setIsPrimaryKey()->setIsAutoIncrement();
  36. $tableDDL->colVarChar('content', 255);
  37. $tableDDL->setIfNotExists();
  38. $sql = $tableDDL->__createDDL();
  39. $query->raw($sql);
  40. $data = $this->connection->defer()->query($query);
  41. $this->assertTrue($data->getResult());
  42. }
  43. }
  44. public function testWhereIn()
  45. {
  46. $ids[] = TestWhereModel::create(['content' => 'a,b,c'])->save();
  47. $ids[] = TestWhereModel::create(['content' => 'a,b,c,d'])->save();
  48. $ids[] = TestWhereModel::create(['content' => 'a,b,c,d,e'])->save();
  49. $ret = TestWhereModel::create()->where('content', 'a,b,c')->get();
  50. $this->assertInstanceOf(AbstractModel::class, $ret);
  51. $this->assertEquals("SELECT * FROM `test_where` WHERE `content` = 'a,b,c' LIMIT 1", DbManager::getInstance()->getLastQuery()->getLastQuery());
  52. $this->assertEquals($ids[0], $ret->id);
  53. $ret = TestWhereModel::create()->where('id', $ids, 'IN')->all();
  54. $idStrings = implode(', ', $ids);
  55. $this->assertEquals("SELECT * FROM `test_where` WHERE `id` IN ( {$idStrings} ) ", DbManager::getInstance()->getLastQuery()->getLastQuery());
  56. $this->assertEquals($ids, array_column($ret->toArray(), 'id'));
  57. try {
  58. $idStrings = implode(', ', $ids);
  59. TestWhereModel::create()->where('id', $idStrings, 'IN')->all();
  60. } catch (\Throwable $e) {
  61. $this->assertEquals('Invalid argument supplied for foreach()', $e->getMessage());
  62. }
  63. }
  64. public function testDeleteAll()
  65. {
  66. $res = TestWhereModel::create()->destroy(null, true);
  67. $this->assertIsInt($res);
  68. }
  69. }