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