您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DatabaseTest.php 3.8KB

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