QueryTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Medoo\Tests;
  3. use Medoo\Medoo;
  4. /**
  5. * @coversDefaultClass \Medoo\Medoo
  6. */
  7. class QueryTest extends MedooTestCase
  8. {
  9. /**
  10. * @covers ::query()
  11. * @covers ::isRaw()
  12. * @covers ::buildRaw()
  13. * @dataProvider typesProvider
  14. */
  15. public function testQuery($type)
  16. {
  17. $this->setType($type);
  18. $this->database->query("SELECT <account.email>,<account.nickname> FROM <account> WHERE <id> != 100");
  19. $this->assertQuery(
  20. <<<EOD
  21. SELECT "account"."email","account"."nickname"
  22. FROM "account"
  23. WHERE "id" != 100
  24. EOD,
  25. $this->database->queryString
  26. );
  27. }
  28. /**
  29. * @covers ::query()
  30. * @covers ::isRaw()
  31. * @covers ::buildRaw()
  32. */
  33. public function testQueryWithPrefix()
  34. {
  35. $database = new Medoo([
  36. 'testMode' => true,
  37. 'prefix' => 'PREFIX_'
  38. ]);
  39. $database->type = "sqlite";
  40. $database->query("SELECT <account.name> FROM <account>");
  41. $this->assertQuery(
  42. <<<EOD
  43. SELECT "PREFIX_account"."name"
  44. FROM "PREFIX_account"
  45. EOD,
  46. $database->queryString
  47. );
  48. }
  49. /**
  50. * @covers ::query()
  51. * @covers ::isRaw()
  52. * @covers ::buildRaw()
  53. */
  54. public function testQueryTableWithPrefix()
  55. {
  56. $database = new Medoo([
  57. 'testMode' => true,
  58. 'prefix' => 'PREFIX_'
  59. ]);
  60. $database->type = "sqlite";
  61. $database->query("DROP TABLE IF EXISTS <account>");
  62. $this->assertQuery(
  63. <<<EOD
  64. DROP TABLE IF EXISTS "PREFIX_account"
  65. EOD,
  66. $database->queryString
  67. );
  68. }
  69. /**
  70. * @covers ::query()
  71. * @covers ::isRaw()
  72. * @covers ::buildRaw()
  73. * @dataProvider typesProvider
  74. */
  75. public function testPreparedStatementQuery($type)
  76. {
  77. $this->setType($type);
  78. $this->database->query(
  79. "SELECT * FROM <account> WHERE <user_name> = :user_name AND <age> = :age",
  80. [
  81. ":user_name" => "John Smite",
  82. ":age" => 20
  83. ]
  84. );
  85. $this->assertQuery(
  86. <<<EOD
  87. SELECT *
  88. FROM "account"
  89. WHERE "user_name" = 'John Smite' AND "age" = 20
  90. EOD,
  91. $this->database->queryString
  92. );
  93. }
  94. /**
  95. * @covers ::query()
  96. * @covers ::isRaw()
  97. * @covers ::buildRaw()
  98. */
  99. public function testQueryEscape()
  100. {
  101. $database = new Medoo([
  102. 'testMode' => true,
  103. 'prefix' => 'PREFIX_'
  104. ]);
  105. $database->type = "sqlite";
  106. $database->query("SELECT * FROM <account> WHERE <name> = '<John>'");
  107. $this->assertQuery(
  108. <<<EOD
  109. SELECT *
  110. FROM "PREFIX_account"
  111. WHERE "name" = '<John>'
  112. EOD,
  113. $database->queryString
  114. );
  115. }
  116. }