You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DatabaseTest.php 6.7KB

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