QBMapperTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\AppFramework\Db;
  7. use OCP\AppFramework\Db\Entity;
  8. use OCP\AppFramework\Db\QBMapper;
  9. use OCP\DB\QueryBuilder\IExpressionBuilder;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\IDBConnection;
  12. /**
  13. * @method bool getBoolProp()
  14. * @method void setBoolProp(bool $boolProp)
  15. * @method integer getIntProp()
  16. * @method void setIntProp(integer $intProp)
  17. * @method string getStringProp()
  18. * @method void setStringProp(string $stringProp)
  19. * @method bool getBooleanProp()
  20. * @method void setBooleanProp(bool $booleanProp)
  21. * @method integer getIntegerProp()
  22. * @method void setIntegerProp(integer $integerProp)
  23. */
  24. class QBTestEntity extends Entity {
  25. protected $intProp;
  26. protected $boolProp;
  27. protected $stringProp;
  28. protected $integerProp;
  29. protected $booleanProp;
  30. protected $jsonProp;
  31. public function __construct() {
  32. $this->addType('intProp', 'int');
  33. $this->addType('boolProp', 'bool');
  34. $this->addType('stringProp', 'string');
  35. $this->addType('integerProp', 'integer');
  36. $this->addType('booleanProp', 'boolean');
  37. $this->addType('jsonProp', 'json');
  38. }
  39. }
  40. /**
  41. * Class QBTestMapper
  42. *
  43. * @package Test\AppFramework\Db
  44. */
  45. class QBTestMapper extends QBMapper {
  46. public function __construct(IDBConnection $db) {
  47. parent::__construct($db, 'table');
  48. }
  49. public function getParameterTypeForPropertyForTest(Entity $entity, string $property) {
  50. return parent::getParameterTypeForProperty($entity, $property);
  51. }
  52. }
  53. /**
  54. * Class QBMapperTest
  55. *
  56. * @package Test\AppFramework\Db
  57. */
  58. class QBMapperTest extends \Test\TestCase {
  59. /**
  60. * @var \PHPUnit\Framework\MockObject\MockObject|IDBConnection
  61. */
  62. protected $db;
  63. /**
  64. * @var \PHPUnit\Framework\MockObject\MockObject|IQueryBuilder
  65. */
  66. protected $qb;
  67. /**
  68. * @var \PHPUnit\Framework\MockObject\MockObject|IExpressionBuilder
  69. */
  70. protected $expr;
  71. /**
  72. * @var \Test\AppFramework\Db\QBTestMapper
  73. */
  74. protected $mapper;
  75. /**
  76. * @throws \ReflectionException
  77. */
  78. protected function setUp(): void {
  79. $this->db = $this->getMockBuilder(IDBConnection::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $this->qb = $this->getMockBuilder(IQueryBuilder::class)
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $this->expr = $this->getMockBuilder(IExpressionBuilder::class)
  86. ->disableOriginalConstructor()
  87. ->getMock();
  88. $this->qb->method('expr')->willReturn($this->expr);
  89. $this->db->method('getQueryBuilder')->willReturn($this->qb);
  90. $this->mapper = new QBTestMapper($this->db);
  91. }
  92. public function testInsertEntityParameterTypeMapping(): void {
  93. $entity = new QBTestEntity();
  94. $entity->setIntProp(123);
  95. $entity->setBoolProp(true);
  96. $entity->setStringProp('string');
  97. $entity->setIntegerProp(456);
  98. $entity->setBooleanProp(false);
  99. $intParam = $this->qb->createNamedParameter('int_prop', IQueryBuilder::PARAM_INT);
  100. $boolParam = $this->qb->createNamedParameter('bool_prop', IQueryBuilder::PARAM_BOOL);
  101. $stringParam = $this->qb->createNamedParameter('string_prop', IQueryBuilder::PARAM_STR);
  102. $integerParam = $this->qb->createNamedParameter('integer_prop', IQueryBuilder::PARAM_INT);
  103. $booleanParam = $this->qb->createNamedParameter('boolean_prop', IQueryBuilder::PARAM_BOOL);
  104. $this->qb->expects($this->exactly(5))
  105. ->method('createNamedParameter')
  106. ->withConsecutive(
  107. [$this->equalTo(123), $this->equalTo(IQueryBuilder::PARAM_INT)],
  108. [$this->equalTo(true), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
  109. [$this->equalTo('string'), $this->equalTo(IQueryBuilder::PARAM_STR)],
  110. [$this->equalTo(456), $this->equalTo(IQueryBuilder::PARAM_INT)],
  111. [$this->equalTo(false), $this->equalTo(IQueryBuilder::PARAM_BOOL)]
  112. );
  113. $this->qb->expects($this->exactly(5))
  114. ->method('setValue')
  115. ->withConsecutive(
  116. [$this->equalTo('int_prop'), $this->equalTo($intParam)],
  117. [$this->equalTo('bool_prop'), $this->equalTo($boolParam)],
  118. [$this->equalTo('string_prop'), $this->equalTo($stringParam)],
  119. [$this->equalTo('integer_prop'), $this->equalTo($integerParam)],
  120. [$this->equalTo('boolean_prop'), $this->equalTo($booleanParam)]
  121. );
  122. $this->mapper->insert($entity);
  123. }
  124. public function testUpdateEntityParameterTypeMapping(): void {
  125. $entity = new QBTestEntity();
  126. $entity->setId(789);
  127. $entity->setIntProp(123);
  128. $entity->setBoolProp('true');
  129. $entity->setStringProp('string');
  130. $entity->setIntegerProp(456);
  131. $entity->setBooleanProp(false);
  132. $entity->setJsonProp(['hello' => 'world']);
  133. $idParam = $this->qb->createNamedParameter('id', IQueryBuilder::PARAM_INT);
  134. $intParam = $this->qb->createNamedParameter('int_prop', IQueryBuilder::PARAM_INT);
  135. $boolParam = $this->qb->createNamedParameter('bool_prop', IQueryBuilder::PARAM_BOOL);
  136. $stringParam = $this->qb->createNamedParameter('string_prop', IQueryBuilder::PARAM_STR);
  137. $integerParam = $this->qb->createNamedParameter('integer_prop', IQueryBuilder::PARAM_INT);
  138. $booleanParam = $this->qb->createNamedParameter('boolean_prop', IQueryBuilder::PARAM_BOOL);
  139. $jsonParam = $this->qb->createNamedParameter('json_prop', IQueryBuilder::PARAM_JSON);
  140. $this->qb->expects($this->exactly(7))
  141. ->method('createNamedParameter')
  142. ->withConsecutive(
  143. [$this->equalTo(123), $this->equalTo(IQueryBuilder::PARAM_INT)],
  144. [$this->equalTo(true), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
  145. [$this->equalTo('string'), $this->equalTo(IQueryBuilder::PARAM_STR)],
  146. [$this->equalTo(456), $this->equalTo(IQueryBuilder::PARAM_INT)],
  147. [$this->equalTo(false), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
  148. [$this->equalTo(['hello' => 'world']), $this->equalTo(IQueryBuilder::PARAM_JSON)],
  149. [$this->equalTo(789), $this->equalTo(IQueryBuilder::PARAM_INT)],
  150. );
  151. $this->qb->expects($this->exactly(6))
  152. ->method('set')
  153. ->withConsecutive(
  154. [$this->equalTo('int_prop'), $this->equalTo($intParam)],
  155. [$this->equalTo('bool_prop'), $this->equalTo($boolParam)],
  156. [$this->equalTo('string_prop'), $this->equalTo($stringParam)],
  157. [$this->equalTo('integer_prop'), $this->equalTo($integerParam)],
  158. [$this->equalTo('boolean_prop'), $this->equalTo($booleanParam)],
  159. [$this->equalTo('json_prop'), $this->equalTo($jsonParam)]
  160. );
  161. $this->expr->expects($this->once())
  162. ->method('eq')
  163. ->with($this->equalTo('id'), $this->equalTo($idParam));
  164. $this->mapper->update($entity);
  165. }
  166. public function testGetParameterTypeForProperty(): void {
  167. $entity = new QBTestEntity();
  168. $intType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'intProp');
  169. $this->assertEquals(IQueryBuilder::PARAM_INT, $intType, 'Int type property mapping incorrect');
  170. $integerType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'integerProp');
  171. $this->assertEquals(IQueryBuilder::PARAM_INT, $integerType, 'Integer type property mapping incorrect');
  172. $boolType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'boolProp');
  173. $this->assertEquals(IQueryBuilder::PARAM_BOOL, $boolType, 'Bool type property mapping incorrect');
  174. $booleanType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'booleanProp');
  175. $this->assertEquals(IQueryBuilder::PARAM_BOOL, $booleanType, 'Boolean type property mapping incorrect');
  176. $stringType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'stringProp');
  177. $this->assertEquals(IQueryBuilder::PARAM_STR, $stringType, 'String type property mapping incorrect');
  178. $jsonType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'jsonProp');
  179. $this->assertEquals(IQueryBuilder::PARAM_JSON, $jsonType, 'JSON type property mapping incorrect');
  180. $unknownType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'someProp');
  181. $this->assertEquals(IQueryBuilder::PARAM_STR, $unknownType, 'Unknown type property mapping incorrect');
  182. }
  183. }