1c9d29defSAndreas Gohr<?php /** @noinspection SqlDialectInspection */ 2c9d29defSAndreas Gohr 3c9d29defSAndreas Gohrnamespace dokuwiki\plugin\sqlite\test; 4c9d29defSAndreas Gohr 5c9d29defSAndreas Gohruse dokuwiki\plugin\sqlite\SQLiteDB; 6c9d29defSAndreas Gohruse DokuWikiTest; 7c9d29defSAndreas Gohr 8c9d29defSAndreas Gohr/** 9c9d29defSAndreas Gohr * Test the new SQLiteDB class 10c9d29defSAndreas Gohr * 11c9d29defSAndreas Gohr * @group plugin_sqlite 12c9d29defSAndreas Gohr * @group plugins 13c9d29defSAndreas Gohr */ 14c9d29defSAndreas Gohrclass SQLiteDBTest extends DokuWikiTest 15c9d29defSAndreas Gohr{ 16c9d29defSAndreas Gohr 17c9d29defSAndreas Gohr protected $res; 18c9d29defSAndreas Gohr 19c9d29defSAndreas Gohr /** @inheritdoc */ 20c9d29defSAndreas Gohr public function setUp(): void 21c9d29defSAndreas Gohr { 22c9d29defSAndreas Gohr global $conf; 23c9d29defSAndreas Gohr $this->pluginsEnabled[] = 'sqlite'; 24c9d29defSAndreas Gohr 25c9d29defSAndreas Gohr // reset database before each test 26c9d29defSAndreas Gohr if (file_exists($conf['metadir'] . '/testdb.sqlite3')) { 27c9d29defSAndreas Gohr unlink($conf['metadir'] . '/testdb.sqlite3'); 28c9d29defSAndreas Gohr } 29c9d29defSAndreas Gohr 30c9d29defSAndreas Gohr parent::setUp(); 31c9d29defSAndreas Gohr } 32c9d29defSAndreas Gohr 33c9d29defSAndreas Gohr public function testDB() 34c9d29defSAndreas Gohr { 35c9d29defSAndreas Gohr $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db"); 3610cb854aSAndreas Gohr $this->assertInstanceOf(\PDO::class, $db->getPdo()); 37c9d29defSAndreas Gohr } 38c9d29defSAndreas Gohr 39c9d29defSAndreas Gohr public function testQuery() 40c9d29defSAndreas Gohr { 41c9d29defSAndreas Gohr $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db"); 42c9d29defSAndreas Gohr $sql = "SELECT * FROM testdata WHERE keyword=?"; 43c9d29defSAndreas Gohr 44c9d29defSAndreas Gohr $stmt = $db->query($sql, ['music']); 45c9d29defSAndreas Gohr $this->assertInstanceOf(\PDOStatement::class, $stmt); 46c9d29defSAndreas Gohr $result = $stmt->fetchAll(\PDO::FETCH_ASSOC); 47c9d29defSAndreas Gohr $this->assertCount(5, $result); 48c9d29defSAndreas Gohr $stmt->closeCursor(); 49c9d29defSAndreas Gohr } 50c9d29defSAndreas Gohr 51c9d29defSAndreas Gohr public function testExec() 52c9d29defSAndreas Gohr { 53c9d29defSAndreas Gohr $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db"); 54c9d29defSAndreas Gohr 55c9d29defSAndreas Gohr $sql = "INSERT INTO testdata (keyword, value) VALUES (?, ?)"; 56c9d29defSAndreas Gohr $insid = $db->exec($sql, ['test', 'test']); 57c9d29defSAndreas Gohr $this->assertEquals(11, $insid); 58c9d29defSAndreas Gohr 59c9d29defSAndreas Gohr $sql = "UPDATE testdata SET value=? WHERE keyword=?"; 60c9d29defSAndreas Gohr $affected = $db->exec($sql, ['test2', 'music']); 61c9d29defSAndreas Gohr $this->assertEquals(5, $affected); 62c9d29defSAndreas Gohr 63c9d29defSAndreas Gohr } 64c9d29defSAndreas Gohr 65c9d29defSAndreas Gohr public function testQueryAll() 66c9d29defSAndreas Gohr { 67c9d29defSAndreas Gohr $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db"); 68c9d29defSAndreas Gohr $sql = "SELECT * FROM testdata WHERE keyword=?"; 69c9d29defSAndreas Gohr 70c9d29defSAndreas Gohr $result = $db->queryAll($sql, ['music']); 71c9d29defSAndreas Gohr $this->assertCount(5, $result); 72c9d29defSAndreas Gohr $this->assertArrayHasKey('keyword', $result[0]); 73c9d29defSAndreas Gohr $this->assertArrayHasKey('value', $result[0]); 74c9d29defSAndreas Gohr } 75c9d29defSAndreas Gohr 76c9d29defSAndreas Gohr public function testQueryRecord() 77c9d29defSAndreas Gohr { 78c9d29defSAndreas Gohr $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db"); 79c9d29defSAndreas Gohr $sql = "SELECT * FROM testdata WHERE tid=?"; 80c9d29defSAndreas Gohr 81c9d29defSAndreas Gohr $result = $db->queryRecord($sql, [4]); 82c9d29defSAndreas Gohr $this->assertEquals(['tid' => 4, 'keyword' => 'music', 'value' => 'Classic'], $result); 83c9d29defSAndreas Gohr } 84c9d29defSAndreas Gohr 85c9d29defSAndreas Gohr public function testSaveRecord() 86c9d29defSAndreas Gohr { 87c9d29defSAndreas Gohr $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db"); 88c9d29defSAndreas Gohr 89c9d29defSAndreas Gohr $record = [ 90c9d29defSAndreas Gohr 'tid' => 4, 91c9d29defSAndreas Gohr 'keyword' => 'music', 92c9d29defSAndreas Gohr 'value' => 'New Classic', 93c9d29defSAndreas Gohr ]; 94c9d29defSAndreas Gohr 95c9d29defSAndreas Gohr $newrecord = $db->saveRecord('testdata', $record, false); 96c9d29defSAndreas Gohr $this->assertNull($newrecord); 97c9d29defSAndreas Gohr 98c9d29defSAndreas Gohr $newrecord = $db->saveRecord('testdata', $record, true); 99c9d29defSAndreas Gohr $this->assertEquals($record, $newrecord); 100c9d29defSAndreas Gohr 101c9d29defSAndreas Gohr $another = [ 102c9d29defSAndreas Gohr 'keyword' => 'music', 103c9d29defSAndreas Gohr 'value' => 'Alternative Rock', 104c9d29defSAndreas Gohr ]; 105c9d29defSAndreas Gohr $newrecord = $db->saveRecord('testdata', $another, false); 106c9d29defSAndreas Gohr $this->assertEquals(11, $newrecord['tid']); 107c9d29defSAndreas Gohr } 108c9d29defSAndreas Gohr 109c9d29defSAndreas Gohr public function testQueryValue() 110c9d29defSAndreas Gohr { 111c9d29defSAndreas Gohr $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db"); 112c9d29defSAndreas Gohr $sql = "SELECT value FROM testdata WHERE tid=?"; 113c9d29defSAndreas Gohr 114c9d29defSAndreas Gohr $result = $db->queryValue($sql, [4]); 115c9d29defSAndreas Gohr $this->assertEquals('Classic', $result); 116c9d29defSAndreas Gohr } 117c9d29defSAndreas Gohr 118*33e488b3SAndreas Gohr public function testQueryKeyValueList() 119*33e488b3SAndreas Gohr { 120*33e488b3SAndreas Gohr $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db"); 121*33e488b3SAndreas Gohr 122*33e488b3SAndreas Gohr // data has not unique keys, last entry should win 123*33e488b3SAndreas Gohr $sql = "SELECT keyword, value FROM testdata ORDER BY tid"; 124*33e488b3SAndreas Gohr $result = $db->queryKeyValueList($sql); 125*33e488b3SAndreas Gohr $this->assertArrayHasKey('music', $result); 126*33e488b3SAndreas Gohr $this->assertEquals('Boring', $result['music']); 127*33e488b3SAndreas Gohr 128*33e488b3SAndreas Gohr // reverse is actually unique 129*33e488b3SAndreas Gohr $sql = "SELECT value, keyword FROM testdata"; 130*33e488b3SAndreas Gohr $result = $db->queryKeyValueList($sql); 131*33e488b3SAndreas Gohr $this->assertArrayHasKey('Boring', $result); 132*33e488b3SAndreas Gohr $this->assertEquals('music', $result['Classic']); 133*33e488b3SAndreas Gohr } 134c9d29defSAndreas Gohr} 135