123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- /**
- * ResultColumn ResultIndexBy ResultScalar
- */
- namespace EasySwoole\ORM\Tests;
- use EasySwoole\ORM\Db\Config;
- use EasySwoole\ORM\Db\Connection;
- use EasySwoole\ORM\DbManager;
- use EasySwoole\ORM\Tests\models\TestRelationModel;
- use PHPUnit\Framework\TestCase;
- use EasySwoole\Mysqli\QueryBuilder;
- class ResultTest extends TestCase
- {
- /**
- * @var $connection Connection
- */
- protected $connection;
- protected $tableName = 'user_test_list';
- protected function setUp(): void
- {
- parent::setUp(); // TODO: Change the autogenerated stub
- $config = new Config(MYSQL_CONFIG);
- $this->connection = new Connection($config);
- DbManager::getInstance()->addConnection($this->connection);
- $connection = DbManager::getInstance()->getConnection();
- $this->assertTrue($connection === $this->connection);
- $this->connection->defer()->query((new QueryBuilder())->raw('TRUNCATE TABLE ' . $this->tableName));
- $this->connection->defer()->query(
- (new QueryBuilder())
- ->raw("INSERT INTO {$this->tableName} (name,age,addTime,state) VALUES
- ('一',10,'2019-12-13 01:02:03.0',1),('二',11,'2019-12-13 01:02:03.0',2);"));
- }
- public function testGetResult()
- {
- $result = DbManager::getInstance()->query(
- (new QueryBuilder())->get($this->tableName)
- )->getResult();
- $this->assertIsArray($result);
- }
- public function testGetResultOne()
- {
- $result = DbManager::getInstance()->query(
- (new QueryBuilder())->get($this->tableName)
- )->getResultOne();
- $this->assertIsArray($result);
- $this->assertTrue(isset($result['name']));
- $this->assertTrue($result['name'] === '一');
- }
- public function testGetResultColumn()
- {
- $result = DbManager::getInstance()->query(
- (new QueryBuilder())->fields('age')->orderBy('age')->get($this->tableName)
- )->getResultColumn();
- $this->assertIsArray($result);
- $this->assertTrue($result[0] === 11);
- $this->assertTrue($result[1] === 10);
- $result = DbManager::getInstance()->query(
- (new QueryBuilder())->fields('name, age')->orderBy('age')->get($this->tableName)
- )->getResultColumn('age');
- $this->assertIsArray($result);
- $this->assertTrue($result[0] === 11);
- $this->assertTrue($result[1] === 10);
- }
- public function testGetResultScalar()
- {
- $result = DbManager::getInstance()->query(
- (new QueryBuilder())->fields('age')->orderBy('age')->get($this->tableName)
- )->getResultScalar();
- $this->assertTrue($result === 11);
- $result = DbManager::getInstance()->query(
- (new QueryBuilder())->fields('name, age')->orderBy('age')->get($this->tableName)
- )->getResultScalar('age');
- $this->assertTrue($result === 11);
- }
- public function testGetResultIndexBy()
- {
- $result = DbManager::getInstance()->query(
- (new QueryBuilder())->fields('name, age')->orderBy('age')->get($this->tableName)
- )->getResultIndexBy('name');
- $this->assertTrue(isset($result['一']));
- $this->assertTrue(isset($result['二']));
- $this->assertTrue($result['二']['name'] === '二');
- $this->assertTrue($result['二']['age'] === 11);
- }
- public function testTotalCount()
- {
- $model = TestRelationModel::create();
- $model->withTotalCount()->all();
- $count = $model->lastQueryResult()->getTotalCount();
- $sql = DbManager::getInstance()->getLastQuery()->getLastQuery();
- $this->assertEquals(0, $count);
- $this->assertEquals("SELECT SQL_CALC_FOUND_ROWS * FROM `test_user_model`", $sql);
- }
- }
|