RandTest.php 4.0 KB

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