AdapterTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\DB;
  7. use Test\TestCase;
  8. class AdapterTest extends TestCase {
  9. private string $appId;
  10. private $connection;
  11. public function setUp(): void {
  12. $this->connection = \OC::$server->getDatabaseConnection();
  13. $this->appId = uniqid('test_db_adapter', true);
  14. }
  15. public function tearDown(): void {
  16. $qb = $this->connection->getQueryBuilder();
  17. $qb->delete('appconfig')
  18. ->from('appconfig')
  19. ->where($qb->expr()->eq('appid', $qb->createNamedParameter($this->appId)))
  20. ->execute();
  21. }
  22. public function testInsertIgnoreOnConflictDuplicate(): void {
  23. $configKey = uniqid('key', true);
  24. $expected = [
  25. [
  26. 'configkey' => $configKey,
  27. 'configvalue' => '1',
  28. ]
  29. ];
  30. $result = $this->connection->insertIgnoreConflict('appconfig', [
  31. 'appid' => $this->appId,
  32. 'configkey' => $configKey,
  33. 'configvalue' => '1',
  34. ]);
  35. $this->assertEquals(1, $result);
  36. $rows = $this->getRows($configKey);
  37. $this->assertSame($expected, $rows);
  38. $result = $this->connection->insertIgnoreConflict('appconfig', [
  39. 'appid' => $this->appId,
  40. 'configkey' => $configKey,
  41. 'configvalue' => '2',
  42. ]);
  43. $this->assertEquals(0, $result);
  44. $rows = $this->getRows($configKey);
  45. $this->assertSame($expected, $rows);
  46. }
  47. private function getRows(string $configKey): array {
  48. $qb = $this->connection->getQueryBuilder();
  49. return $qb->select(['configkey', 'configvalue'])
  50. ->from('appconfig')
  51. ->where($qb->expr()->eq('appid', $qb->createNamedParameter($this->appId)))
  52. ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($configKey)))
  53. ->execute()
  54. ->fetchAll();
  55. }
  56. }