QueryTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * @dataProvider typesProvider
  54. */
  55. public function testPreparedStatementQuery($type)
  56. {
  57. $this->setType($type);
  58. $this->database->query(
  59. "SELECT * FROM <account> WHERE <user_name> = :user_name AND <age> = :age",
  60. [
  61. ":user_name" => "John Smite",
  62. ":age" => 20
  63. ]
  64. );
  65. $this->assertQuery(
  66. <<<EOD
  67. SELECT *
  68. FROM "account"
  69. WHERE "user_name" = 'John Smite' AND "age" = 20
  70. EOD,
  71. $this->database->queryString
  72. );
  73. }
  74. /**
  75. * @covers ::query()
  76. * @covers ::isRaw()
  77. * @covers ::buildRaw()
  78. */
  79. public function testQueryEscape()
  80. {
  81. $database = new Medoo([
  82. 'testMode' => true,
  83. 'prefix' => 'PREFIX_'
  84. ]);
  85. $database->type = "sqlite";
  86. $database->query("SELECT * FROM <account> WHERE <name> = '<John>'");
  87. $this->assertQuery(
  88. <<<EOD
  89. SELECT *
  90. FROM "PREFIX_account"
  91. WHERE "name" = '<John>'
  92. EOD,
  93. $database->queryString
  94. );
  95. }
  96. }