aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/DB/AdapterTest.php
blob: 99b7cf4e099cdb5b85dcebf239ca35426887ecad (plain)
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
<?php
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test\DB;

use Test\TestCase;

class AdapterTest extends TestCase {
	private string $appId;
	private $connection;

	public function setUp(): void {
		$this->connection = \OC::$server->getDatabaseConnection();
		$this->appId = uniqid('test_db_adapter', true);
	}

	public function tearDown(): void {
		$qb = $this->connection->getQueryBuilder();

		$qb->delete('appconfig')
			->from('appconfig')
			->where($qb->expr()->eq('appid', $qb->createNamedParameter($this->appId)))
			->execute();
	}

	public function testInsertIgnoreOnConflictDuplicate(): void {
		$configKey = uniqid('key', true);
		$expected = [
			[
				'configkey' => $configKey,
				'configvalue' => '1',
			]
		];
		$result = $this->connection->insertIgnoreConflict('appconfig', [
			'appid' => $this->appId,
			'configkey' => $configKey,
			'configvalue' => '1',
		]);
		$this->assertEquals(1, $result);
		$rows = $this->getRows($configKey);
		$this->assertSame($expected, $rows);


		$result = $this->connection->insertIgnoreConflict('appconfig', [
			'appid' => $this->appId,
			'configkey' => $configKey,
			'configvalue' => '2',
		]);
		$this->assertEquals(0, $result);
		$rows = $this->getRows($configKey);
		$this->assertSame($expected, $rows);
	}

	private function getRows(string $configKey): array {
		$qb = $this->connection->getQueryBuilder();
		return $qb->select(['configkey', 'configvalue'])
			->from('appconfig')
			->where($qb->expr()->eq('appid', $qb->createNamedParameter($this->appId)))
			->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($configKey)))
			->execute()
			->fetchAll();
	}
}