ResultTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * ResultColumn ResultIndexBy ResultScalar
  4. */
  5. namespace EasySwoole\ORM\Tests;
  6. use EasySwoole\ORM\Db\Config;
  7. use EasySwoole\ORM\Db\Connection;
  8. use EasySwoole\ORM\DbManager;
  9. use EasySwoole\ORM\Tests\models\TestRelationModel;
  10. use PHPUnit\Framework\TestCase;
  11. use EasySwoole\Mysqli\QueryBuilder;
  12. class ResultTest extends TestCase
  13. {
  14. /**
  15. * @var $connection Connection
  16. */
  17. protected $connection;
  18. protected $tableName = 'user_test_list';
  19. protected function setUp(): void
  20. {
  21. parent::setUp(); // TODO: Change the autogenerated stub
  22. $config = new Config(MYSQL_CONFIG);
  23. $this->connection = new Connection($config);
  24. DbManager::getInstance()->addConnection($this->connection);
  25. $connection = DbManager::getInstance()->getConnection();
  26. $this->assertTrue($connection === $this->connection);
  27. $this->connection->defer()->query((new QueryBuilder())->raw('TRUNCATE TABLE ' . $this->tableName));
  28. $this->connection->defer()->query(
  29. (new QueryBuilder())
  30. ->raw("INSERT INTO {$this->tableName} (name,age,addTime,state) VALUES
  31. ('一',10,'2019-12-13 01:02:03.0',1),('二',11,'2019-12-13 01:02:03.0',2);"));
  32. }
  33. public function testGetResult()
  34. {
  35. $result = DbManager::getInstance()->query(
  36. (new QueryBuilder())->get($this->tableName)
  37. )->getResult();
  38. $this->assertIsArray($result);
  39. }
  40. public function testGetResultOne()
  41. {
  42. $result = DbManager::getInstance()->query(
  43. (new QueryBuilder())->get($this->tableName)
  44. )->getResultOne();
  45. $this->assertIsArray($result);
  46. $this->assertTrue(isset($result['name']));
  47. $this->assertTrue($result['name'] === '一');
  48. }
  49. public function testGetResultColumn()
  50. {
  51. $result = DbManager::getInstance()->query(
  52. (new QueryBuilder())->fields('age')->orderBy('age')->get($this->tableName)
  53. )->getResultColumn();
  54. $this->assertIsArray($result);
  55. $this->assertTrue($result[0] === 11);
  56. $this->assertTrue($result[1] === 10);
  57. $result = DbManager::getInstance()->query(
  58. (new QueryBuilder())->fields('name, age')->orderBy('age')->get($this->tableName)
  59. )->getResultColumn('age');
  60. $this->assertIsArray($result);
  61. $this->assertTrue($result[0] === 11);
  62. $this->assertTrue($result[1] === 10);
  63. }
  64. public function testGetResultScalar()
  65. {
  66. $result = DbManager::getInstance()->query(
  67. (new QueryBuilder())->fields('age')->orderBy('age')->get($this->tableName)
  68. )->getResultScalar();
  69. $this->assertTrue($result === 11);
  70. $result = DbManager::getInstance()->query(
  71. (new QueryBuilder())->fields('name, age')->orderBy('age')->get($this->tableName)
  72. )->getResultScalar('age');
  73. $this->assertTrue($result === 11);
  74. }
  75. public function testGetResultIndexBy()
  76. {
  77. $result = DbManager::getInstance()->query(
  78. (new QueryBuilder())->fields('name, age')->orderBy('age')->get($this->tableName)
  79. )->getResultIndexBy('name');
  80. $this->assertTrue(isset($result['一']));
  81. $this->assertTrue(isset($result['二']));
  82. $this->assertTrue($result['二']['name'] === '二');
  83. $this->assertTrue($result['二']['age'] === 11);
  84. }
  85. public function testTotalCount()
  86. {
  87. $model = TestRelationModel::create();
  88. $model->withTotalCount()->all();
  89. $count = $model->lastQueryResult()->getTotalCount();
  90. $sql = DbManager::getInstance()->getLastQuery()->getLastQuery();
  91. $this->assertEquals(0, $count);
  92. $this->assertEquals("SELECT SQL_CALC_FOUND_ROWS * FROM `test_user_model`", $sql);
  93. }
  94. }