查询语言.sql

Testing Is Documentation

tests/Database/Query/SqlTest.php查询语言.sql - 图1

Uses

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

基本用法

  1. public function testBaseUse(): void
  2. {
  3. $connect = $this->createDatabaseConnectMock();
  4. $sql = <<<'eot'
  5. [
  6. "SELECT `test_query`.* FROM `test_query` ORDER BY `test_query`.`create_at` DESC LIMIT 1",
  7. [],
  8. false
  9. ]
  10. eot;
  11. $this->assertSame(
  12. $sql,
  13. $this->varJson(
  14. $connect
  15. ->table('test_query')
  16. ->sql(true)
  17. ->latest()
  18. ->findOne()
  19. )
  20. );
  21. $sql = <<<'eot'
  22. [
  23. "delete from test where id = ?",
  24. [
  25. 22
  26. ],
  27. false
  28. ]
  29. eot;
  30. $this->assertSame(
  31. $sql,
  32. $this->varJson(
  33. $connect
  34. ->sql(true)
  35. ->delete('delete from test where id = ?', [22]),
  36. 1
  37. )
  38. );
  39. }

例外 findOne 等也支持快捷

绝大多数都支持这个功能,例如 findAll,insertAll 等。

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