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.

QBMapperTest.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. public function __construct() {
  48. $this->addType('intProp', 'int');
  49. $this->addType('boolProp', 'bool');
  50. $this->addType('stringProp', 'string');
  51. $this->addType('integerProp', 'integer');
  52. $this->addType('booleanProp', 'boolean');
  53. }
  54. }
  55. ;
  56. /**
  57. * Class QBTestMapper
  58. *
  59. * @package Test\AppFramework\Db
  60. */
  61. class QBTestMapper extends QBMapper {
  62. public function __construct(IDBConnection $db) {
  63. parent::__construct($db, 'table');
  64. }
  65. public function getParameterTypeForPropertyForTest(Entity $entity, string $property): int {
  66. return parent::getParameterTypeForProperty($entity, $property);
  67. }
  68. }
  69. /**
  70. * Class QBMapperTest
  71. *
  72. * @package Test\AppFramework\Db
  73. */
  74. class QBMapperTest extends \Test\TestCase {
  75. /**
  76. * @var \PHPUnit\Framework\MockObject\MockObject|IDBConnection
  77. */
  78. protected $db;
  79. /**
  80. * @var \PHPUnit\Framework\MockObject\MockObject|IQueryBuilder
  81. */
  82. protected $qb;
  83. /**
  84. * @var \PHPUnit\Framework\MockObject\MockObject|IExpressionBuilder
  85. */
  86. protected $expr;
  87. /**
  88. * @var \Test\AppFramework\Db\QBTestMapper
  89. */
  90. protected $mapper;
  91. /**
  92. * @throws \ReflectionException
  93. */
  94. protected function setUp(): void {
  95. $this->db = $this->getMockBuilder(IDBConnection::class)
  96. ->disableOriginalConstructor()
  97. ->getMock();
  98. $this->qb = $this->getMockBuilder(IQueryBuilder::class)
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $this->expr = $this->getMockBuilder(IExpressionBuilder::class)
  102. ->disableOriginalConstructor()
  103. ->getMock();
  104. $this->qb->method('expr')->willReturn($this->expr);
  105. $this->db->method('getQueryBuilder')->willReturn($this->qb);
  106. $this->mapper = new QBTestMapper($this->db);
  107. }
  108. public function testInsertEntityParameterTypeMapping() {
  109. $entity = new QBTestEntity();
  110. $entity->setIntProp(123);
  111. $entity->setBoolProp(true);
  112. $entity->setStringProp('string');
  113. $entity->setIntegerProp(456);
  114. $entity->setBooleanProp(false);
  115. $intParam = $this->qb->createNamedParameter('int_prop', IQueryBuilder::PARAM_INT);
  116. $boolParam = $this->qb->createNamedParameter('bool_prop', IQueryBuilder::PARAM_BOOL);
  117. $stringParam = $this->qb->createNamedParameter('string_prop', IQueryBuilder::PARAM_STR);
  118. $integerParam = $this->qb->createNamedParameter('integer_prop', IQueryBuilder::PARAM_INT);
  119. $booleanParam = $this->qb->createNamedParameter('boolean_prop', IQueryBuilder::PARAM_BOOL);
  120. $this->qb->expects($this->exactly(5))
  121. ->method('createNamedParameter')
  122. ->withConsecutive(
  123. [$this->equalTo(123), $this->equalTo(IQueryBuilder::PARAM_INT)],
  124. [$this->equalTo(true), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
  125. [$this->equalTo('string'), $this->equalTo(IQueryBuilder::PARAM_STR)],
  126. [$this->equalTo(456), $this->equalTo(IQueryBuilder::PARAM_INT)],
  127. [$this->equalTo(false), $this->equalTo(IQueryBuilder::PARAM_BOOL)]
  128. );
  129. $this->qb->expects($this->exactly(5))
  130. ->method('setValue')
  131. ->withConsecutive(
  132. [$this->equalTo('int_prop'), $this->equalTo($intParam)],
  133. [$this->equalTo('bool_prop'), $this->equalTo($boolParam)],
  134. [$this->equalTo('string_prop'), $this->equalTo($stringParam)],
  135. [$this->equalTo('integer_prop'), $this->equalTo($integerParam)],
  136. [$this->equalTo('boolean_prop'), $this->equalTo($booleanParam)]
  137. );
  138. $this->mapper->insert($entity);
  139. }
  140. public function testUpdateEntityParameterTypeMapping() {
  141. $entity = new QBTestEntity();
  142. $entity->setId(789);
  143. $entity->setIntProp(123);
  144. $entity->setBoolProp('true');
  145. $entity->setStringProp('string');
  146. $entity->setIntegerProp(456);
  147. $entity->setBooleanProp(false);
  148. $idParam = $this->qb->createNamedParameter('id', IQueryBuilder::PARAM_INT);
  149. $intParam = $this->qb->createNamedParameter('int_prop', IQueryBuilder::PARAM_INT);
  150. $boolParam = $this->qb->createNamedParameter('bool_prop', IQueryBuilder::PARAM_BOOL);
  151. $stringParam = $this->qb->createNamedParameter('string_prop', IQueryBuilder::PARAM_STR);
  152. $integerParam = $this->qb->createNamedParameter('integer_prop', IQueryBuilder::PARAM_INT);
  153. $booleanParam = $this->qb->createNamedParameter('boolean_prop', IQueryBuilder::PARAM_BOOL);
  154. $this->qb->expects($this->exactly(6))
  155. ->method('createNamedParameter')
  156. ->withConsecutive(
  157. [$this->equalTo(123), $this->equalTo(IQueryBuilder::PARAM_INT)],
  158. [$this->equalTo(true), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
  159. [$this->equalTo('string'), $this->equalTo(IQueryBuilder::PARAM_STR)],
  160. [$this->equalTo(456), $this->equalTo(IQueryBuilder::PARAM_INT)],
  161. [$this->equalTo(false), $this->equalTo(IQueryBuilder::PARAM_BOOL)],
  162. [$this->equalTo(789), $this->equalTo(IQueryBuilder::PARAM_INT)]
  163. );
  164. $this->qb->expects($this->exactly(5))
  165. ->method('set')
  166. ->withConsecutive(
  167. [$this->equalTo('int_prop'), $this->equalTo($intParam)],
  168. [$this->equalTo('bool_prop'), $this->equalTo($boolParam)],
  169. [$this->equalTo('string_prop'), $this->equalTo($stringParam)],
  170. [$this->equalTo('integer_prop'), $this->equalTo($integerParam)],
  171. [$this->equalTo('boolean_prop'), $this->equalTo($booleanParam)]
  172. );
  173. $this->expr->expects($this->once())
  174. ->method('eq')
  175. ->with($this->equalTo('id'), $this->equalTo($idParam));
  176. $this->mapper->update($entity);
  177. }
  178. public function testGetParameterTypeForProperty() {
  179. $entity = new QBTestEntity();
  180. $intType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'intProp');
  181. $this->assertEquals(IQueryBuilder::PARAM_INT, $intType, 'Int type property mapping incorrect');
  182. $integerType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'integerProp');
  183. $this->assertEquals(IQueryBuilder::PARAM_INT, $integerType, 'Integer type property mapping incorrect');
  184. $boolType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'boolProp');
  185. $this->assertEquals(IQueryBuilder::PARAM_BOOL, $boolType, 'Bool type property mapping incorrect');
  186. $booleanType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'booleanProp');
  187. $this->assertEquals(IQueryBuilder::PARAM_BOOL, $booleanType, 'Boolean type property mapping incorrect');
  188. $stringType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'stringProp');
  189. $this->assertEquals(IQueryBuilder::PARAM_STR, $stringType, 'String type property mapping incorrect');
  190. $unknownType = $this->mapper->getParameterTypeForPropertyForTest($entity, 'someProp');
  191. $this->assertEquals(IQueryBuilder::PARAM_STR, $unknownType, 'Unknown type property mapping incorrect');
  192. }
  193. }