DbManagerTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * DbManager 添加链接、onQuery回调等基础操作
  4. * User: Tioncico
  5. * Date: 2019/10/22 0022
  6. * Time: 10:51
  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\Db\Result;
  13. use EasySwoole\ORM\DbManager;
  14. use PHPUnit\Framework\TestCase;
  15. class DbManagerTest extends TestCase
  16. {
  17. /**
  18. * @var $connection Connection
  19. */
  20. protected $connection;
  21. function setUp(): void
  22. {
  23. parent::setUp(); // TODO: Change the autogenerated stub
  24. $config = new Config(MYSQL_CONFIG);
  25. $this->connection = new Connection($config);
  26. DbManager::getInstance()->addConnection($this->connection);
  27. $connection = DbManager::getInstance()->getConnection();
  28. $this->assertTrue($connection === $this->connection);
  29. }
  30. function testAddConnection()
  31. {
  32. $config = new Config(MYSQL_CONFIG);
  33. DbManager::getInstance()->addConnection(new Connection($config), 'test');
  34. $connection = DbManager::getInstance()->getConnection('test');
  35. $this->assertFalse($connection === $this->connection);
  36. }
  37. function testOnQuery()
  38. {
  39. DbManager::getInstance()->onQuery(function ($ret, $temp, $start) {
  40. $this->assertTrue($ret instanceof Result);
  41. $this->assertTrue($temp instanceof QueryBuilder);
  42. $this->assertIsFloat($start);
  43. });
  44. $queryBuild = new QueryBuilder();
  45. $queryBuild->raw("show tables");
  46. $data = DbManager::getInstance()->query($queryBuild);
  47. $this->assertTrue($data instanceof Result);
  48. }
  49. function testGetConnection()
  50. {
  51. $connection = DbManager::getInstance()->getConnection();
  52. $queryBuild = new QueryBuilder();
  53. $queryBuild->raw("show tables");
  54. $data = $connection->defer()->query($queryBuild);
  55. $this->assertTrue($data instanceof Result);
  56. }
  57. function testQuery()
  58. {
  59. $queryBuild = new QueryBuilder();
  60. $queryBuild->raw("show tables");
  61. $data = DbManager::getInstance()->query($queryBuild);
  62. $this->assertTrue($data instanceof Result);
  63. }
  64. }