InsertTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace Medoo\Tests;
  3. use Medoo\Medoo;
  4. /**
  5. * @coversDefaultClass \Medoo\Medoo
  6. */
  7. class InsertTest extends MedooTestCase
  8. {
  9. /**
  10. * @covers ::insert()
  11. * @covers ::typeMap()
  12. * @dataProvider typesProvider
  13. */
  14. public function testInsert($type)
  15. {
  16. $this->setType($type);
  17. $this->database->insert("account", [
  18. "user_name" => "foo",
  19. "email" => "foo@bar.com"
  20. ]);
  21. $this->assertQuery(
  22. <<<EOD
  23. INSERT INTO "account" ("user_name", "email")
  24. VALUES ('foo', 'foo@bar.com')
  25. EOD,
  26. $this->database->queryString
  27. );
  28. }
  29. /**
  30. * @covers ::insert()
  31. * @covers ::typeMap()
  32. * @dataProvider typesProvider
  33. */
  34. public function testInsertWithArray($type)
  35. {
  36. $this->setType($type);
  37. $this->database->insert("account", [
  38. "user_name" => "foo",
  39. "lang" => ["en", "fr"]
  40. ]);
  41. $this->assertQuery([
  42. 'default' => <<<EOD
  43. INSERT INTO "account" ("user_name", "lang")
  44. VALUES ('foo', 'a:2:{i:0;s:2:"en";i:1;s:2:"fr";}')
  45. EOD,
  46. 'mysql' => <<<EOD
  47. INSERT INTO "account" ("user_name", "lang")
  48. VALUES ('foo', 'a:2:{i:0;s:2:\"en\";i:1;s:2:\"fr\";}')
  49. EOD
  50. ], $this->database->queryString);
  51. }
  52. /**
  53. * @covers ::insert()
  54. * @covers ::typeMap()
  55. * @dataProvider typesProvider
  56. */
  57. public function testInsertWithJSON($type)
  58. {
  59. $this->setType($type);
  60. $this->database->insert("account", [
  61. "user_name" => "foo",
  62. "lang [JSON]" => ["en", "fr"]
  63. ]);
  64. $this->assertQuery([
  65. 'default' => <<<EOD
  66. INSERT INTO "account" ("user_name", "lang")
  67. VALUES ('foo', '["en","fr"]')
  68. EOD,
  69. 'mysql' => <<<EOD
  70. INSERT INTO `account` (`user_name`, `lang`)
  71. VALUES ('foo', '[\"en\",\"fr\"]')
  72. EOD
  73. ], $this->database->queryString);
  74. }
  75. /**
  76. * @covers ::insert()
  77. * @dataProvider typesProvider
  78. */
  79. public function testInsertWithRaw($type)
  80. {
  81. $this->setType($type);
  82. $this->database->insert("account", [
  83. "user_name" => Medoo::raw("UUID()")
  84. ]);
  85. $this->assertQuery(
  86. <<<EOD
  87. INSERT INTO "account" ("user_name")
  88. VALUES (UUID())
  89. EOD,
  90. $this->database->queryString
  91. );
  92. }
  93. /**
  94. * @covers ::insert()
  95. * @covers ::typeMap()
  96. * @dataProvider typesProvider
  97. */
  98. public function testInsertWithNull($type)
  99. {
  100. $this->setType($type);
  101. $this->database->insert("account", [
  102. "location" => null
  103. ]);
  104. $this->assertQuery(
  105. <<<EOD
  106. INSERT INTO "account" ("location")
  107. VALUES (NULL)
  108. EOD,
  109. $this->database->queryString
  110. );
  111. }
  112. /**
  113. * @covers ::insert()
  114. * @covers ::typeMap()
  115. * @dataProvider typesProvider
  116. */
  117. public function testInsertWithObject($type)
  118. {
  119. $this->setType($type);
  120. $objectData = new Foo();
  121. $this->database->insert("account", [
  122. "object" => $objectData
  123. ]);
  124. $this->assertQuery(
  125. <<<EOD
  126. INSERT INTO "account" ("object")
  127. VALUES (:MeD0_mK)
  128. EOD,
  129. $this->database->queryString
  130. );
  131. }
  132. /**
  133. * @covers ::insert()
  134. * @dataProvider typesProvider
  135. */
  136. public function testMultiInsert($type)
  137. {
  138. $this->setType($type);
  139. $this->database->insert("account", [
  140. [
  141. "user_name" => "foo",
  142. "email" => "foo@bar.com"
  143. ],
  144. [
  145. "user_name" => "bar",
  146. "email" => "bar@foo.com"
  147. ]
  148. ]);
  149. $this->assertQuery(
  150. <<<EOD
  151. INSERT INTO "account" ("user_name", "email")
  152. VALUES ('foo', 'foo@bar.com'), ('bar', 'bar@foo.com')
  153. EOD,
  154. $this->database->queryString
  155. );
  156. }
  157. public function testOracleWithPrimaryKeyInsert()
  158. {
  159. $this->setType("oracle");
  160. $this->database->insert("ACCOUNT", [
  161. "NAME" => "foo",
  162. "EMAIL" => "foo@bar.com"
  163. ], "ID");
  164. $this->assertQuery(
  165. <<<EOD
  166. INSERT INTO "ACCOUNT" ("NAME", "EMAIL")
  167. VALUES ('foo', 'foo@bar.com')
  168. RETURNING "ID" INTO :RETURNID
  169. EOD,
  170. $this->database->queryString
  171. );
  172. }
  173. public function testOracleWithLOBsInsert()
  174. {
  175. $this->setType("oracle");
  176. $fp = fopen('README.md', 'r');
  177. $this->database->insert("ACCOUNT", [
  178. "NAME" => "foo",
  179. "DATA" => $fp
  180. ]);
  181. $this->assertQuery(
  182. <<<EOD
  183. INSERT INTO "ACCOUNT" ("NAME", "DATA")
  184. VALUES ('foo', EMPTY_BLOB())
  185. RETURNING "DATA" INTO :MeD1_mK
  186. EOD,
  187. $this->database->queryString
  188. );
  189. }
  190. public function testOracleWithLOBsAndIdInsert()
  191. {
  192. $this->setType("oracle");
  193. $fp = fopen('README.md', 'r');
  194. $this->database->insert("ACCOUNT", [
  195. "NAME" => "foo",
  196. "DATA" => $fp
  197. ], "ID");
  198. $this->assertQuery(
  199. <<<EOD
  200. INSERT INTO "ACCOUNT" ("NAME", "DATA")
  201. VALUES ('foo', EMPTY_BLOB())
  202. RETURNING "DATA", "ID" INTO :MeD1_mK, :RETURNID
  203. EOD,
  204. $this->database->queryString
  205. );
  206. }
  207. }