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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. use Illuminate\Database\Query\Builder;
  3. use Luticate\Utils\Controller\LuticateApplication;
  4. use Illuminate\Database\Capsule\Manager as Capsule;
  5. use Luticate\Utils\DataAccess\LuDataAccess;
  6. use Luticate\Utils\DataAccess\PgSqlDataAccess;
  7. use Luticate\Utils\Dbo\LuDbo;
  8. use Luticate\Utils\Dbo\LuDboDeserializeException;
  9. /**
  10. * Created by PhpStorm.
  11. * User: robin
  12. * Date: 6/7/16
  13. * Time: 2:57 PM
  14. */
  15. class TestTableDbo extends LuDbo
  16. {
  17. /**
  18. * @var $_id int
  19. */
  20. private $_id;
  21. /**
  22. * @var $_someText string
  23. */
  24. private $_someText;
  25. /**
  26. * @var $_someIntegerArray int[]
  27. */
  28. private $_someIntegerArray;
  29. /**
  30. * @return int
  31. */
  32. public function getId()
  33. {
  34. return $this->_id;
  35. }
  36. /**
  37. * @param int $id
  38. */
  39. public function setId($id)
  40. {
  41. $this->_id = $id;
  42. }
  43. /**
  44. * @return string
  45. */
  46. public function getSomeText()
  47. {
  48. return $this->_someText;
  49. }
  50. /**
  51. * @param string $someText
  52. */
  53. public function setSomeText($someText)
  54. {
  55. $this->_someText = $someText;
  56. }
  57. /**
  58. * @return int[]
  59. */
  60. public function getSomeIntegerArray()
  61. {
  62. return $this->_someIntegerArray;
  63. }
  64. /**
  65. * @param int[] $someIntegerArray
  66. */
  67. public function setSomeIntegerArray($someIntegerArray)
  68. {
  69. $this->_someIntegerArray = $someIntegerArray;
  70. }
  71. }
  72. class TestTableDboArray extends LuDbo
  73. {
  74. /**
  75. * @var TestTableDbo[]
  76. */
  77. protected $_array;
  78. public function getArray()
  79. {
  80. return $this->_array;
  81. }
  82. public function setArray($value)
  83. {
  84. $this->_array = $value;
  85. }
  86. public function jsonSerialize()
  87. {
  88. return $this->_array;
  89. }
  90. public static function jsonDeserialize($json)
  91. {
  92. if (!is_array($json)) {
  93. throw new LuDboDeserializeException("Invalid array value");
  94. }
  95. $dbo = new static();
  96. $array = [];
  97. foreach ($json as $data) {
  98. $array[] = TestTableDbo::jsonDeserialize($data);
  99. }
  100. $dbo->setArray($array);
  101. return $dbo;
  102. }
  103. public static function generateSample()
  104. {
  105. return [
  106. TestTableDbo::generateSample(),
  107. TestTableDbo::generateSample()
  108. ];
  109. }
  110. }
  111. class TestTableDataAccess extends LuDataAccess
  112. {
  113. protected static $_connection = "mydb";
  114. protected static $_table = "test_table";
  115. protected static $_dboClass = TestTableDbo::class;
  116. protected static $_dboArrayClass = TestTableDboArray::class;
  117. }
  118. class DatabaseTest extends \PHPUnit_Framework_TestCase
  119. {
  120. public function testSetup()
  121. {
  122. // $config = ['databases' => [
  123. // [
  124. // 'name' => 'mydb',
  125. // 'driver' => 'pgsql',
  126. // 'host' => '172.17.0.1',
  127. // 'database' => 'luticate2',
  128. // 'username' => 'dev',
  129. // 'password' => 'dev'
  130. // ]
  131. // ]];
  132. // $app = new LuticateApplication($config);
  133. // $app->setupDatabases();
  134. //
  135. // $dbo = new TestTableDbo();
  136. // $dbo->setId(11);
  137. // $dbo->setSomeIntegerArray('{}');
  138. // $dbo->setSomeText("lol2");
  139. //
  140. // var_dump(TestTableDataAccess::getMultiple(function(Builder $q)
  141. // {
  142. // return $q->where("id", "=", 1);
  143. // }));
  144. // var_dump(TestTableDataAccess::getSingleById(1));
  145. // var_dump(TestTableDataAccess::deleteMultiple(function($q)
  146. // {
  147. // return $q->where("some_text", "=", "lol");
  148. // }));
  149. // var_dump(TestTableDataAccess::addSingleId($dbo));
  150. // var_dump(TestTableDataAccess::addSingle($dbo, ['Id']));
  151. // var_dump(TestTableDataAccess::editSingleById($dbo));
  152. // var_dump(TestTableDataAccess::editMultiple($dbo, function($q)
  153. // {
  154. // return $q->where("some_text", "like", "lol%");
  155. // }, ['Id']));
  156. // $pdo = TestTableDataAccess::getPdo();
  157. // $pgsql = new PgSqlDataAccess();
  158. // $pgsql->getStoredProceduresFull($pdo);
  159. // var_dump($pgsql->getStoredProceduresFull($pdo));
  160. }
  161. }