1
0

QBMapperTest.php 8.7 KB

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