Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. use Carbon\Carbon;
  3. use Illuminate\Database\Query\Builder;
  4. use Luticate\Utils\DataAccess\LuDataAccess;
  5. use Luticate\Utils\DataAccess\LuStoredProcedure;
  6. use Luticate\Utils\Dbo\LuDbo;
  7. use Luticate\Utils\Dbo\LuDboDeserializeException;
  8. use Luticate\Utils\Dbo\LuIntDbo;
  9. use Luticate\Utils\Dbo\LuIntDboArray;
  10. /**
  11. * Created by PhpStorm.
  12. * User: robin
  13. * Date: 6/7/16
  14. * Time: 2:57 PM
  15. */
  16. class TestTableDbo extends LuDbo
  17. {
  18. /**
  19. * @var $_id int
  20. */
  21. private $_id;
  22. /**
  23. * @var $_someText string
  24. */
  25. private $_someText;
  26. /**
  27. * @var $_someIntegerArray int[]
  28. */
  29. private $_someIntegerArray;
  30. /**
  31. * @var $_createdAt Carbon
  32. */
  33. private $_createdAt;
  34. /**
  35. * @return int
  36. */
  37. public function getId()
  38. {
  39. return $this->_id;
  40. }
  41. /**
  42. * @param int $id
  43. */
  44. public function setId($id)
  45. {
  46. $this->_id = $id;
  47. }
  48. /**
  49. * @return string
  50. */
  51. public function getSomeText()
  52. {
  53. return $this->_someText;
  54. }
  55. /**
  56. * @param string $someText
  57. */
  58. public function setSomeText($someText)
  59. {
  60. $this->_someText = $someText;
  61. }
  62. /**
  63. * @return int[]
  64. */
  65. public function getSomeIntegerArray()
  66. {
  67. return $this->_someIntegerArray;
  68. }
  69. /**
  70. * @param int[] $someIntegerArray
  71. */
  72. public function setSomeIntegerArray($someIntegerArray)
  73. {
  74. $this->_someIntegerArray = $someIntegerArray;
  75. }
  76. /**
  77. * @return Carbon
  78. */
  79. public function getCreatedAt()
  80. {
  81. return $this->_createdAt;
  82. }
  83. /**
  84. * @param Carbon $createdAt
  85. */
  86. public function setCreatedAt($createdAt)
  87. {
  88. $this->_createdAt = $createdAt;
  89. }
  90. }
  91. class TestTableDboArray extends LuDbo
  92. {
  93. /**
  94. * @var TestTableDbo[]
  95. */
  96. protected $_array;
  97. public function getArray()
  98. {
  99. return $this->_array;
  100. }
  101. public function setArray($value)
  102. {
  103. $this->_array = $value;
  104. }
  105. public function jsonSerialize()
  106. {
  107. return $this->_array;
  108. }
  109. public static function jsonDeserialize($json)
  110. {
  111. if (!is_array($json)) {
  112. throw new LuDboDeserializeException("Invalid array value");
  113. }
  114. $dbo = new static();
  115. $array = [];
  116. foreach ($json as $data) {
  117. $array[] = TestTableDbo::jsonDeserialize($data);
  118. }
  119. $dbo->setArray($array);
  120. return $dbo;
  121. }
  122. public static function generateSample()
  123. {
  124. return [
  125. TestTableDbo::generateSample(),
  126. TestTableDbo::generateSample()
  127. ];
  128. }
  129. }
  130. class SpTest extends LuStoredProcedure {
  131. protected static $_connection = "mydb";
  132. protected static $_storedProcedure = "sp_test";
  133. protected static $_dboClass = LuIntDbo::class;
  134. public static function execute($an_int) {
  135. return self::getSingle([$an_int]);
  136. }
  137. }
  138. class SpTest2 extends LuStoredProcedure {
  139. protected static $_connection = "mydb";
  140. protected static $_storedProcedure = "sp_test2";
  141. protected static $_dboClass = LuIntDbo::class;
  142. protected static $_dboArrayClass = LuIntDboArray::class;
  143. public static function execute($an_int) {
  144. return self::getMultiple([$an_int]);
  145. }
  146. public static function executePaginated($an_int, $page, $perPage) {
  147. return self::getMultiplePaginated([$an_int], $page, $perPage);
  148. }
  149. }
  150. class TestTableDataAccess extends LuDataAccess
  151. {
  152. protected static $_connection = "mydb";
  153. protected static $_table = "test_table";
  154. protected static $_dboClass = TestTableDbo::class;
  155. }
  156. class DatabaseTest extends \PHPUnit_Framework_TestCase
  157. {
  158. public function testSpGetSingle()
  159. {
  160. $this->assertSame(42, LuDbo::serializeValue(SpTest::execute(42)));
  161. }
  162. public function testSpGetSingleNull()
  163. {
  164. $this->assertNull(SpTest::execute(4));
  165. }
  166. public function testSpGetMultiple()
  167. {
  168. $this->assertSame(range(0, 15), SpTest2::execute(42));
  169. }
  170. public function testSpGetMultiplePaginated1()
  171. {
  172. $this->assertSame(["count" => 16, "data" => range(0, 1)], SpTest2::executePaginated(42, 0, 2)->jsonSerialize());
  173. }
  174. public function testSpGetMultiplePaginated2()
  175. {
  176. $this->assertSame(["count" => 16, "data" => range(2, 3)], SpTest2::executePaginated(42, 1, 2)->jsonSerialize());
  177. }
  178. public function testDaGetMultiple()
  179. {
  180. $this->assertSame([["id" => 1, "someText" => "lol2", "someIntegerArray" => [], "createdAt" => "2016-06-15 13:45:23"]],
  181. LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
  182. {
  183. return $q->where("id", "=", 1);
  184. })));
  185. }
  186. public function testDaGetMultipleOffset()
  187. {
  188. $this->assertSame([["id" => 11, "someText" => "lol2", "someIntegerArray" => [], "createdAt" => "2016-06-15 13:45:23"]],
  189. LuDbo::serializeValue(TestTableDataAccess::getMultiple(function(Builder $q)
  190. {
  191. return $q->take(1)->offset(2);
  192. })));
  193. }
  194. public function testDaGetPaginated()
  195. {
  196. $this->assertSame(["count" => 3, "data" =>
  197. [["id" => 11, "someText" => "lol2", "someIntegerArray" => [], "createdAt" => "2016-06-15 13:45:23"]]],
  198. LuDbo::serializeValue(TestTableDataAccess::getMultiplePaginated(function(Builder $q)
  199. {
  200. return $q->orderBy("id", "ASC");
  201. }, 1, 1)));
  202. }
  203. public function testDaGetSingleById()
  204. {
  205. $this->assertSame(["id" => 1, "someText" => "lol2", "someIntegerArray" => [], "createdAt" => "2016-06-15 13:45:23"],
  206. LuDbo::serializeValue(TestTableDataAccess::getSingleById(1)));
  207. }
  208. public function testDatabase()
  209. {
  210. // $dbo = new TestTableDbo();
  211. // $dbo->setId(11);
  212. // $dbo->setSomeIntegerArray('{}');
  213. // $dbo->setSomeText("lol2");
  214. //
  215. // var_dump(TestTableDataAccess::deleteMultiple(function($q)
  216. // {
  217. // return $q->where("some_text", "=", "lol");
  218. // }));
  219. // var_dump(TestTableDataAccess::addSingleId($dbo));
  220. // var_dump(TestTableDataAccess::addSingle($dbo, ['Id']));
  221. // var_dump(TestTableDataAccess::editSingleById($dbo));
  222. // var_dump(TestTableDataAccess::editMultiple($dbo, function($q)
  223. // {
  224. // return $q->where("some_text", "like", "lol%");
  225. // }, ['Id']));
  226. // $pdo = TestTableDataAccess::getPdo();
  227. // $pgsql = new PgSqlDataAccess();
  228. // $pgsql->getStoredProceduresFull($pdo);
  229. // var_dump($pgsql->getStoredProceduresFull($pdo));
  230. }
  231. }