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

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