選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DatabaseTest.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. protected static $_dboArrayClass = LuIntDboArray::class;
  138. public static function execute($an_int) {
  139. return self::getSingle([$an_int]);
  140. }
  141. }
  142. class SpTest2 extends LuStoredProcedure {
  143. protected static $_connection = "mydb";
  144. protected static $_storedProcedure = "sp_test2";
  145. protected static $_dboClass = LuIntDbo::class;
  146. protected static $_dboArrayClass = LuIntDboArray::class;
  147. public static function execute($an_int) {
  148. return self::getMultiple([$an_int]);
  149. }
  150. public static function executePaginated($an_int, $page, $perPage) {
  151. return self::getMultiplePaginated([$an_int], $page, $perPage);
  152. }
  153. }
  154. class TestTableDataAccess extends LuDataAccess
  155. {
  156. protected static $_connection = "mydb";
  157. protected static $_table = "test_table";
  158. protected static $_dboClass = TestTableDbo::class;
  159. protected static $_dboArrayClass = TestTableDboArray::class;
  160. }
  161. class DatabaseTest extends \PHPUnit_Framework_TestCase
  162. {
  163. public function testSetup()
  164. {
  165. // $config = ['databases' => [
  166. // [
  167. // 'name' => 'mydb',
  168. // 'driver' => 'pgsql',
  169. // 'host' => '172.17.0.1',
  170. // 'database' => 'luticate2',
  171. // 'username' => 'dev',
  172. // 'password' => 'dev'
  173. // ]
  174. // ]];
  175. // $app = new LuticateApplication($config);
  176. // $app->setupDatabases();
  177. // var_dump(SpTest::execute(42));
  178. // var_dump(SpTest::execute(4));
  179. // var_dump(SpTest2::execute(42));
  180. // var_dump(SpTest2::executePaginated(42, 0, 2)->jsonSerialize());
  181. // var_dump(SpTest2::executePaginated(42, 1, 2)->jsonSerialize());
  182. //
  183. // $dbo = new TestTableDbo();
  184. // $dbo->setId(11);
  185. // $dbo->setSomeIntegerArray('{}');
  186. // $dbo->setSomeText("lol2");
  187. //
  188. // var_dump(TestTableDataAccess::getMultiple(function(Builder $q)
  189. // {
  190. // return $q->where("id", "=", 1);
  191. // })[0]->jsonSerialize());
  192. // var_dump(TestTableDataAccess::getSingleById(1));
  193. // var_dump(TestTableDataAccess::deleteMultiple(function($q)
  194. // {
  195. // return $q->where("some_text", "=", "lol");
  196. // }));
  197. // var_dump(TestTableDataAccess::addSingleId($dbo));
  198. // var_dump(TestTableDataAccess::addSingle($dbo, ['Id']));
  199. // var_dump(TestTableDataAccess::editSingleById($dbo));
  200. // var_dump(TestTableDataAccess::editMultiple($dbo, function($q)
  201. // {
  202. // return $q->where("some_text", "like", "lol%");
  203. // }, ['Id']));
  204. // $pdo = TestTableDataAccess::getPdo();
  205. // $pgsql = new PgSqlDataAccess();
  206. // $pgsql->getStoredProceduresFull($pdo);
  207. // var_dump($pgsql->getStoredProceduresFull($pdo));
  208. }
  209. }