1
0

QBMapperTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * @copyright 2019, Marius David Wieschollek <git.public@mdns.eu>
  4. *
  5. * @author Marius David Wieschollek <git.public@mdns.eu>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\AppFramework\Db;
  24. use OCP\AppFramework\Db\Entity;
  25. use OCP\AppFramework\Db\QBMapper;
  26. use OCP\DB\QueryBuilder\IExpressionBuilder;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\IDBConnection;
  29. /**
  30. * @method bool getBoolProp()
  31. * @method void setBoolProp(bool $boolProp)
  32. * @method integer getIntProp()
  33. * @method void setIntProp(integer $intProp)
  34. * @method string getStringProp()
  35. * @method void setStringProp(string $stringProp)
  36. * @method bool getBooleanProp()
  37. * @method void setBooleanProp(bool $booleanProp)
  38. * @method integer getIntegerProp()
  39. * @method void setIntegerProp(integer $integerProp)
  40. */
  41. class QBTestEntity extends Entity {
  42. protected $intProp;
  43. protected $boolProp;
  44. protected $stringProp;
  45. protected $integerProp;
  46. protected $booleanProp;
  47. protected $jsonProp;
  48. public function __construct() {
  49. $this->addType('intProp', 'int');
  50. $this->addType('boolProp', 'bool');
  51. $this->addType('stringProp', 'string');
  52. $this->addType('integerProp', 'integer');
  53. $this->addType('booleanProp', 'boolean');
  54. $this->addType('jsonProp', 'json');
  55. }
  56. }
  57. /**
  58. * Class QBTestMapper
  59. *
  60. * @package Test\AppFramework\Db
  61. */
  62. class QBTestMapper extends QBMapper {
  63. public function __construct(IDBConnection $db) {
  64. parent::__construct($db, 'table');
  65. }
  66. public function getParameterTypeForPropertyForTest(Entity $entity, string $property) {
  67. return parent::getParameterTypeForProperty($entity, $property);
  68. }
  69. }
  70. /**
  71. * Class QBMapperTest
  72. *
  73. * @package Test\AppFramework\Db
  74. */
  75. class QBMapperTest extends \Test\TestCase {
  76. /**
  77. * @var \PHPUnit\Framework\MockObject\MockObject|IDBConnection
  78. */
  79. protected $db;
  80. /**
  81. * @var \PHPUnit\Framework\MockObject\MockObject|IQueryBuilder
  82. */
  83. protected $qb;
  84. /**
  85. * @var \PHPUnit\Framework\MockObject\MockObject|IExpressionBuilder
  86. */
  87. protected $expr;
  88. /**
  89. * @var \Test\AppFramework\Db\QBTestMapper
  90. */
  91. protected $mapper;
  92. /**
  93. * @throws \ReflectionException
  94. */
  95. protected function setUp(): void {
  96. $this->db = $this->getMockBuilder(IDBConnection::class)
  97. ->disableOriginalConstructor()
  98. ->getMock();
  99. $this->qb = $this->getMockBuilder(IQueryBuilder::class)
  100. ->disableOriginalConstructor()
  101. ->getMock();
  102. $this->expr = $this->getMockBuilder(IExpressionBuilder::class)
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $this->qb->method('expr')->willReturn($this->expr);
  106. $this->db->method('getQueryBuilder')->willReturn($this->qb);
  107. $this->mapper = new QBTestMapper($this->db);
  108. }
  109. public function testInsertEntityParameterTypeMapping() {
  110. $entity = new QBTestEntity();
  111. $entity->setIntProp(123);
  112. $entity->setBoolProp(true);
  113. $entity->setStringProp('string');
  114. $entity->setIntegerProp(456);
  115. $entity->setBooleanProp(false);
  116. $intParam = $this->qb->createNamedParameter('int_prop', IQueryBuilder::PARAM_INT);
  117. $boolParam = $this->qb->createNamedParameter('bool_prop', IQueryBuilder::PARAM_BOOL);
  118. $stringParam = $this->qb->createNamedParameter('string_prop', IQueryBuilder::PARAM_STR);
  119. $integerParam = $this->qb->createNamedParameter('integer_prop', IQueryBuilder::PARAM_INT);
  120. $booleanParam = $this->qb->createNamedParameter('boolean_prop', IQueryBuilder::PARAM_BOOL);
  121. $this->qb->expects($this->exactly(5))
  122. ->method('createNamedParameter')
  123. ->withConsecutive(
  124. [$this->equalTo(123), $this->equalTo(IQueryBuilder::PARAM_INT)],
  125. [$this->equalTo(true), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
  126. [$this->equalTo('string'), $this->equalTo(IQueryBuilder::PARAM_STR)],
  127. [$this->equalTo(456), $this->equalTo(IQueryBuilder::PARAM_INT)],
  128. [$this->equalTo(false), $this->equalTo(IQueryBuilder::PARAM_BOOL)]
  129. );
  130. $this->qb->expects($this->exactly(5))
  131. ->method('setValue')
  132. ->withConsecutive(
  133. [$this->equalTo('int_prop'), $this->equalTo($intParam)],
  134. [$this->equalTo('bool_prop'), $this->equalTo($boolParam)],
  135. [$this->equalTo('string_prop'), $this->equalTo($stringParam)],
  136. [$this->equalTo('integer_prop'), $this->equalTo($integerParam)],
  137. [$this->equalTo('boolean_prop'), $this->equalTo($booleanParam)]
  138. );
  139. $this->mapper->insert($entity);
  140. }
  141. public function testUpdateEntityParameterTypeMapping() {
  142. $entity = new QBTestEntity();
  143. $entity->setId(789);
  144. $entity->setIntProp(123);
  145. $entity->setBoolProp('true');
  146. $entity->setStringProp('string');
  147. $entity->setIntegerProp(456);
  148. $entity->setBooleanProp(false);
  149. $entity->setJsonProp(["hello" => "world"]);
  150. $idParam = $this->qb->createNamedParameter('id', IQueryBuilder::PARAM_INT);
  151. $intParam = $this->qb->createNamedParameter('int_prop', IQueryBuilder::PARAM_INT);
  152. $boolParam = $this->qb->createNamedParameter('bool_prop', IQueryBuilder::PARAM_BOOL);
  153. $stringParam = $this->qb->createNamedParameter('string_prop', IQueryBuilder::PARAM_STR);
  154. $integerParam = $this->qb->createNamedParameter('integer_prop', IQueryBuilder::PARAM_INT);
  155. $booleanParam = $this->qb->createNamedParameter('boolean_prop', IQueryBuilder::PARAM_BOOL);
  156. $jsonParam = $this->qb->createNamedParameter('json_prop', IQueryBuilder::PARAM_JSON);
  157. $this->qb->expects($this->exactly(7))
  158. ->method('createNamedParameter')
  159. ->withConsecutive(
  160. [$this->equalTo(123), $this->equalTo(IQueryBuilder::PARAM_INT)],
  161. [$this->equalTo(true), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
  162. [$this->equalTo('string'), $this->equalTo(IQueryBuilder::PARAM_STR)],
  163. [$this->equalTo(456), $this->equalTo(IQueryBuilder::PARAM_INT)],
  164. [$this->equalTo(false), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
  165. [$this->equalTo(["hello" => "world"]), $this->equalTo(IQueryBuilder::PARAM_JSON)],
  166. [$this->equalTo(789), $this->equalTo(IQueryBuilder::PARAM_INT)],
  167. );
  168. $this->qb->expects($this->exactly(6))
  169. ->method('set')
  170. ->withConsecutive(
  171. [$this->equalTo('int_prop'), $this->equalTo($intParam)],
  172. [$this->equalTo('bool_prop'), $this->equalTo($boolParam)],
  173. [$this->equalTo('string_prop'), $this->equalTo($stringParam)],
  174. [$this->equalTo('integer_prop'), $this->equalTo($integerParam)],
  175. [$this->equalTo('boolean_prop'), $this->equalTo($booleanParam)],
  176. [$this->equalTo('json_prop'), $this->equalTo($jsonParam)]
  177. );
  178. $this->expr->expects($this->once())
  179. ->method('eq')
  180. ->with($this->equalTo('id'), $this->equalTo($idParam));
  181. $this->mapper->update($entity);
  182. }
  183. public function testGetParameterTypeForProperty() {
  184. $entity = new QBTestEntity();
  185. $intType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'intProp');
  186. $this->assertEquals(IQueryBuilder::PARAM_INT, $intType, 'Int type property mapping incorrect');
  187. $integerType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'integerProp');
  188. $this->assertEquals(IQueryBuilder::PARAM_INT, $integerType, 'Integer type property mapping incorrect');
  189. $boolType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'boolProp');
  190. $this->assertEquals(IQueryBuilder::PARAM_BOOL, $boolType, 'Bool type property mapping incorrect');
  191. $booleanType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'booleanProp');
  192. $this->assertEquals(IQueryBuilder::PARAM_BOOL, $booleanType, 'Boolean type property mapping incorrect');
  193. $stringType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'stringProp');
  194. $this->assertEquals(IQueryBuilder::PARAM_STR, $stringType, 'String type property mapping incorrect');
  195. $jsonType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'jsonProp');
  196. $this->assertEquals(IQueryBuilder::PARAM_JSON, $jsonType, 'JSON type property mapping incorrect');
  197. $unknownType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'someProp');
  198. $this->assertEquals(IQueryBuilder::PARAM_STR, $unknownType, 'Unknown type property mapping incorrect');
  199. }
  200. }