GetTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Medoo\Tests;
  3. /**
  4. * @coversDefaultClass \Medoo\Medoo
  5. */
  6. class GetTest extends MedooTestCase
  7. {
  8. /**
  9. * @covers ::get()
  10. * @dataProvider typesProvider
  11. */
  12. public function testGet($type)
  13. {
  14. $this->setType($type);
  15. $this->database->get("account", "email", [
  16. "user_id" => 1234
  17. ]);
  18. $this->assertQuery([
  19. 'default' => <<<EOD
  20. SELECT "email"
  21. FROM "account"
  22. WHERE "user_id" = 1234
  23. LIMIT 1
  24. EOD,
  25. 'mssql' => <<<EOD
  26. SELECT [email]
  27. FROM [account]
  28. WHERE [user_id] = 1234
  29. ORDER BY (SELECT 0)
  30. OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
  31. EOD,
  32. 'oracle' => <<<EOD
  33. SELECT "email"
  34. FROM "account"
  35. WHERE "user_id" = 1234
  36. OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
  37. EOD,
  38. ], $this->database->queryString);
  39. }
  40. /**
  41. * @covers ::get()
  42. * @dataProvider typesProvider
  43. */
  44. public function testGetWithColumns($type)
  45. {
  46. $this->setType($type);
  47. $this->database->get("account", [
  48. "email",
  49. "location"
  50. ], [
  51. "user_id" => 1234
  52. ]);
  53. $this->assertQuery([
  54. 'default' => <<<EOD
  55. SELECT "email","location"
  56. FROM "account"
  57. WHERE "user_id" = 1234
  58. LIMIT 1
  59. EOD,
  60. 'mssql' => <<<EOD
  61. SELECT [email],[location]
  62. FROM [account]
  63. WHERE [user_id] = 1234
  64. ORDER BY (SELECT 0)
  65. OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
  66. EOD,
  67. 'oracle' => <<<EOD
  68. SELECT "email","location"
  69. FROM "account"
  70. WHERE "user_id" = 1234
  71. OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
  72. EOD,
  73. ], $this->database->queryString);
  74. }
  75. /**
  76. * @covers ::get()
  77. * @dataProvider typesProvider
  78. */
  79. public function testGetWithJoin($type)
  80. {
  81. $this->setType($type);
  82. $this->database->get("post", [
  83. "[>]account" => "user_id"
  84. ], [
  85. "post.content",
  86. "account.user_name"
  87. ]);
  88. $this->assertQuery([
  89. 'default' => <<<EOD
  90. SELECT "post"."content","account"."user_name"
  91. FROM "post"
  92. LEFT JOIN "account" USING ("user_id")
  93. LIMIT 1
  94. EOD,
  95. 'mssql' => <<<EOD
  96. SELECT [post].[content],[account].[user_name]
  97. FROM [post]
  98. LEFT JOIN [account] USING ([user_id])
  99. ORDER BY (SELECT 0)
  100. OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
  101. EOD,
  102. 'oracle' => <<<EOD
  103. SELECT "post"."content","account"."user_name"
  104. FROM "post"
  105. LEFT JOIN "account" USING ("user_id")
  106. OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
  107. EOD,
  108. ], $this->database->queryString);
  109. }
  110. /**
  111. * @covers ::get()
  112. * @dataProvider typesProvider
  113. */
  114. public function testGetWithJoinAndWhere($type)
  115. {
  116. $this->setType($type);
  117. $this->database->get("post", [
  118. "[>]account" => "user_id"
  119. ], [
  120. "post.content",
  121. "account.user_name"
  122. ], [
  123. 'account.age[>]' => 18
  124. ]);
  125. $this->assertQuery([
  126. 'default' => <<<EOD
  127. SELECT "post"."content","account"."user_name"
  128. FROM "post"
  129. LEFT JOIN "account" USING ("user_id")
  130. WHERE "account"."age" > 18
  131. LIMIT 1
  132. EOD,
  133. 'mssql' => <<<EOD
  134. SELECT [post].[content],[account].[user_name]
  135. FROM [post]
  136. LEFT JOIN [account] USING ([user_id])
  137. WHERE [account].[age] > 18
  138. ORDER BY (SELECT 0)
  139. OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
  140. EOD,
  141. 'oracle' => <<<EOD
  142. SELECT "post"."content","account"."user_name"
  143. FROM "post"
  144. LEFT JOIN "account" USING ("user_id")
  145. WHERE "account"."age" > 18
  146. OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
  147. EOD,
  148. ], $this->database->queryString);
  149. }
  150. }