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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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\PgSqlDataAccess;
  8. use Luticate\Utils\Dbo\LuDbo;
  9. use Luticate\Utils\Dbo\LuDboDeserializeException;
  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 TestTableDataAccess extends LuDataAccess
  131. {
  132. protected static $_connection = "mydb";
  133. protected static $_table = "test_table";
  134. protected static $_dboClass = TestTableDbo::class;
  135. protected static $_dboArrayClass = TestTableDboArray::class;
  136. }
  137. class DatabaseTest extends \PHPUnit_Framework_TestCase
  138. {
  139. public function testSetup()
  140. {
  141. // $config = ['databases' => [
  142. // [
  143. // 'name' => 'mydb',
  144. // 'driver' => 'pgsql',
  145. // 'host' => '172.17.0.1',
  146. // 'database' => 'luticate2',
  147. // 'username' => 'dev',
  148. // 'password' => 'dev'
  149. // ]
  150. // ]];
  151. // $app = new LuticateApplication($config);
  152. // $app->setupDatabases();
  153. //
  154. // $dbo = new TestTableDbo();
  155. // $dbo->setId(11);
  156. // $dbo->setSomeIntegerArray('{}');
  157. // $dbo->setSomeText("lol2");
  158. //
  159. // var_dump(TestTableDataAccess::getMultiple(function(Builder $q)
  160. // {
  161. // return $q->where("id", "=", 1);
  162. // })[0]->jsonSerialize());
  163. // var_dump(TestTableDataAccess::getSingleById(1));
  164. // var_dump(TestTableDataAccess::deleteMultiple(function($q)
  165. // {
  166. // return $q->where("some_text", "=", "lol");
  167. // }));
  168. // var_dump(TestTableDataAccess::addSingleId($dbo));
  169. // var_dump(TestTableDataAccess::addSingle($dbo, ['Id']));
  170. // var_dump(TestTableDataAccess::editSingleById($dbo));
  171. // var_dump(TestTableDataAccess::editMultiple($dbo, function($q)
  172. // {
  173. // return $q->where("some_text", "like", "lol%");
  174. // }, ['Id']));
  175. // $pdo = TestTableDataAccess::getPdo();
  176. // $pgsql = new PgSqlDataAccess();
  177. // $pgsql->getStoredProceduresFull($pdo);
  178. // var_dump($pgsql->getStoredProceduresFull($pdo));
  179. }
  180. }