1
0

QBMapperDBTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\AppFramework\Db;
  7. use Doctrine\DBAL\Schema\SchemaException;
  8. use OCP\AppFramework\Db\Entity;
  9. use OCP\AppFramework\Db\QBMapper;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\DB\Types;
  12. use OCP\IConfig;
  13. use OCP\IDBConnection;
  14. use OCP\Server;
  15. use Test\TestCase;
  16. /**
  17. * @method void setTime(?\DateTime $time)
  18. * @method ?\DateTime getTime()
  19. * @method void setDatetime(?\DateTimeImmutable $datetime)
  20. * @method ?\DateTimeImmutable getDatetime()
  21. */
  22. class QBDBTestEntity extends Entity {
  23. protected ?\DateTime $time = null;
  24. protected ?\DateTimeImmutable $datetime = null;
  25. public function __construct() {
  26. $this->addType('time', Types::TIME);
  27. $this->addType('datetime', Types::DATETIME_IMMUTABLE);
  28. }
  29. }
  30. /**
  31. * Class QBDBTestMapper
  32. *
  33. * @package Test\AppFramework\Db
  34. */
  35. class QBDBTestMapper extends QBMapper {
  36. public function __construct(IDBConnection $db) {
  37. parent::__construct($db, 'testing', QBDBTestEntity::class);
  38. }
  39. public function getParameterTypeForPropertyForTest(Entity $entity, string $property) {
  40. return parent::getParameterTypeForProperty($entity, $property);
  41. }
  42. public function getById(int $id): QBDBTestEntity {
  43. $qb = $this->db->getQueryBuilder();
  44. $query = $qb
  45. ->select('*')
  46. ->from($this->tableName)
  47. ->where(
  48. $qb->expr()->eq('id', $qb->createPositionalParameter($id, IQueryBuilder::PARAM_INT)),
  49. );
  50. return $this->findEntity($query);
  51. }
  52. }
  53. /**
  54. * Test real database handling (serialization)
  55. * @group DB
  56. */
  57. class QBMapperDBTest extends TestCase {
  58. /** @var \Doctrine\DBAL\Connection|\OCP\IDBConnection */
  59. protected $connection;
  60. protected $schemaSetup = false;
  61. protected function setUp(): void {
  62. parent::setUp();
  63. $this->connection = \OCP\Server::get(IDBConnection::class);
  64. $this->prepareTestingTable();
  65. }
  66. public function testInsertDateTime(): void {
  67. $mapper = new QBDBTestMapper($this->connection);
  68. $entity = new QBDBTestEntity();
  69. $entity->setTime(new \DateTime('2003-01-01 12:34:00'));
  70. $entity->setDatetime(new \DateTimeImmutable('2000-01-01 23:45:00'));
  71. $result = $mapper->insert($entity);
  72. $this->assertNotNull($result->getId());
  73. }
  74. public function testRetrieveDateTime(): void {
  75. $time = new \DateTime('2000-01-01 01:01:00');
  76. $datetime = new \DateTimeImmutable('2000-01-01 02:02:00');
  77. $mapper = new QBDBTestMapper($this->connection);
  78. $entity = new QBDBTestEntity();
  79. $entity->setTime($time);
  80. $entity->setDatetime($datetime);
  81. $result = $mapper->insert($entity);
  82. $this->assertNotNull($result->getId());
  83. $dbEntity = $mapper->getById($result->getId());
  84. $this->assertEquals($time->format('H:i:s'), $dbEntity->getTime()->format('H:i:s'));
  85. $this->assertEquals($datetime->format('Y-m-d H:i:s'), $dbEntity->getDatetime()->format('Y-m-d H:i:s'));
  86. // The date is not saved for "time"
  87. $this->assertNotEquals($time->format('Y'), $dbEntity->getTime()->format('Y'));
  88. }
  89. public function testUpdateDateTime(): void {
  90. $time = new \DateTime('2000-01-01 01:01:00');
  91. $datetime = new \DateTimeImmutable('2000-01-01 02:02:00');
  92. $mapper = new QBDBTestMapper($this->connection);
  93. $entity = new QBDBTestEntity();
  94. $entity->setTime('now');
  95. $entity->setDatetime('now');
  96. /** @var QBDBTestEntity */
  97. $entity = $mapper->insert($entity);
  98. $this->assertNotNull($entity->getId());
  99. // Update the values
  100. $entity->setTime($time);
  101. $entity->setDatetime($datetime);
  102. $mapper->update($entity);
  103. $dbEntity = $mapper->getById($entity->getId());
  104. $this->assertEquals($time->format('H:i:s'), $dbEntity->getTime()->format('H:i:s'));
  105. $this->assertEquals($datetime->format('Y-m-d H:i:s'), $dbEntity->getDatetime()->format('Y-m-d H:i:s'));
  106. }
  107. protected function prepareTestingTable(): void {
  108. if ($this->schemaSetup) {
  109. $this->connection->getQueryBuilder()->delete('testing')->executeStatement();
  110. }
  111. $prefix = Server::get(IConfig::class)->getSystemValueString('dbtableprefix', 'oc_');
  112. $schema = $this->connection->createSchema();
  113. try {
  114. $schema->getTable($prefix . 'testing');
  115. $this->connection->getQueryBuilder()->delete('testing')->executeStatement();
  116. } catch (SchemaException $e) {
  117. $this->schemaSetup = true;
  118. $table = $schema->createTable($prefix . 'testing');
  119. $table->addColumn('id', Types::BIGINT, [
  120. 'autoincrement' => true,
  121. 'notnull' => true,
  122. ]);
  123. $table->addColumn('time', Types::TIME, [
  124. 'notnull' => false,
  125. ]);
  126. $table->addColumn('datetime', Types::DATETIME_IMMUTABLE, [
  127. 'notnull' => false,
  128. ]);
  129. $table->setPrimaryKey(['id']);
  130. $this->connection->migrateToSchema($schema);
  131. }
  132. }
  133. }