查询数据.select

Testing Is Documentation

tests/Database/Read/SelectTest.php查询数据.select - 图1

Uses

  1. <?php
  2. use Tests\Database\DatabaseTestCase as TestCase;

select 查询指定 SQL

  1. public function testBaseUse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "select *from test where id = ?",
  7. [
  8. 1
  9. ],
  10. false
  11. ]
  12. eot;
  13. $this->assertSame(
  14. $sql,
  15. $this->varJson(
  16. $connect
  17. ->sql()
  18. ->table('test')
  19. ->select('select *from test where id = ?', [1])
  20. )
  21. );
  22. }

select 直接查询

  1. public function testSelect(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test`.* FROM `test`",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->sql()
  16. ->table('test')
  17. ->select(),
  18. 1
  19. )
  20. );
  21. }

select 查询支持闭包

  1. public function testSelectClosure(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test`.* FROM `test` WHERE `test`.`id` = :test_id",
  7. {
  8. "test_id": [
  9. 1
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $sql,
  17. $this->varJson(
  18. $connect
  19. ->sql()
  20. ->table('test')
  21. ->select(function ($select) {
  22. $select->where('id', 1);
  23. }),
  24. 2
  25. )
  26. );
  27. }

select 查询支持 \Leevel\Database\Select 对象

  1. public function testSelectObject(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test`.* FROM `test` WHERE `test`.`id` = :test_id",
  7. {
  8. "test_id": [
  9. 5
  10. ]
  11. },
  12. false
  13. ]
  14. eot;
  15. $select = $connect
  16. ->table('test')
  17. ->where('id', 5);
  18. $this->assertSame(
  19. $sql,
  20. $this->varJson(
  21. $connect
  22. ->sql()
  23. ->select($select),
  24. 3
  25. )
  26. );
  27. }