xref: /plugin/sqlite/_test/SQLiteDBTest.php (revision 27eb38dad6ac16ba55cb051804719e1c950a9e0a)
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
51*27eb38daSAndreas Gohr    public function testParameterHandling()
52*27eb38daSAndreas Gohr    {
53*27eb38daSAndreas Gohr        $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db");
54*27eb38daSAndreas Gohr
55*27eb38daSAndreas Gohr        $sql = "SELECT ? AS first, ? AS second, ? AS third";
56*27eb38daSAndreas Gohr
57*27eb38daSAndreas Gohr        $result = $db->queryRecord($sql, ['one', 'two', 'three']);
58*27eb38daSAndreas Gohr        $this->assertEquals(['first' => 'one', 'second' => 'two', 'third' => 'three'], $result);
59*27eb38daSAndreas Gohr
60*27eb38daSAndreas Gohr        $result = $db->queryRecord($sql, 'one', 'two', 'three');
61*27eb38daSAndreas Gohr        $this->assertEquals(['first' => 'one', 'second' => 'two', 'third' => 'three'], $result);
62*27eb38daSAndreas Gohr
63*27eb38daSAndreas Gohr        $sql = "SELECT :first AS first, :second AS second, :third AS third";
64*27eb38daSAndreas Gohr
65*27eb38daSAndreas Gohr        $result = $db->queryRecord($sql, ['first' => 'one', 'second' => 'two', 'third' => 'three']);
66*27eb38daSAndreas Gohr        $this->assertEquals(['first' => 'one', 'second' => 'two', 'third' => 'three'], $result);
67*27eb38daSAndreas Gohr    }
68*27eb38daSAndreas Gohr
69c9d29defSAndreas Gohr    public function testExec()
70c9d29defSAndreas Gohr    {
71c9d29defSAndreas Gohr        $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db");
72c9d29defSAndreas Gohr
73c9d29defSAndreas Gohr        $sql = "INSERT INTO testdata (keyword, value) VALUES (?, ?)";
74c9d29defSAndreas Gohr        $insid = $db->exec($sql, ['test', 'test']);
75c9d29defSAndreas Gohr        $this->assertEquals(11, $insid);
76c9d29defSAndreas Gohr
77c9d29defSAndreas Gohr        $sql = "UPDATE testdata SET value=? WHERE keyword=?";
78c9d29defSAndreas Gohr        $affected = $db->exec($sql, ['test2', 'music']);
79c9d29defSAndreas Gohr        $this->assertEquals(5, $affected);
80c9d29defSAndreas Gohr
81c9d29defSAndreas Gohr    }
82c9d29defSAndreas Gohr
83c9d29defSAndreas Gohr    public function testQueryAll()
84c9d29defSAndreas Gohr    {
85c9d29defSAndreas Gohr        $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db");
86c9d29defSAndreas Gohr        $sql = "SELECT * FROM testdata WHERE keyword=?";
87c9d29defSAndreas Gohr
88c9d29defSAndreas Gohr        $result = $db->queryAll($sql, ['music']);
89c9d29defSAndreas Gohr        $this->assertCount(5, $result);
90c9d29defSAndreas Gohr        $this->assertArrayHasKey('keyword', $result[0]);
91c9d29defSAndreas Gohr        $this->assertArrayHasKey('value', $result[0]);
92c9d29defSAndreas Gohr    }
93c9d29defSAndreas Gohr
94c9d29defSAndreas Gohr    public function testQueryRecord()
95c9d29defSAndreas Gohr    {
96c9d29defSAndreas Gohr        $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db");
97c9d29defSAndreas Gohr        $sql = "SELECT * FROM testdata WHERE tid=?";
98c9d29defSAndreas Gohr
99c9d29defSAndreas Gohr        $result = $db->queryRecord($sql, [4]);
100c9d29defSAndreas Gohr        $this->assertEquals(['tid' => 4, 'keyword' => 'music', 'value' => 'Classic'], $result);
101c9d29defSAndreas Gohr    }
102c9d29defSAndreas Gohr
103c9d29defSAndreas Gohr    public function testSaveRecord()
104c9d29defSAndreas Gohr    {
105c9d29defSAndreas Gohr        $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db");
106c9d29defSAndreas Gohr
107c9d29defSAndreas Gohr        $record = [
108c9d29defSAndreas Gohr            'tid' => 4,
109c9d29defSAndreas Gohr            'keyword' => 'music',
110c9d29defSAndreas Gohr            'value' => 'New Classic',
111c9d29defSAndreas Gohr        ];
112c9d29defSAndreas Gohr
113c9d29defSAndreas Gohr        $newrecord = $db->saveRecord('testdata', $record, false);
114c9d29defSAndreas Gohr        $this->assertNull($newrecord);
115c9d29defSAndreas Gohr
116c9d29defSAndreas Gohr        $newrecord = $db->saveRecord('testdata', $record, true);
117c9d29defSAndreas Gohr        $this->assertEquals($record, $newrecord);
118c9d29defSAndreas Gohr
119c9d29defSAndreas Gohr        $another = [
120c9d29defSAndreas Gohr            'keyword' => 'music',
121c9d29defSAndreas Gohr            'value' => 'Alternative Rock',
122c9d29defSAndreas Gohr        ];
123c9d29defSAndreas Gohr        $newrecord = $db->saveRecord('testdata', $another, false);
124c9d29defSAndreas Gohr        $this->assertEquals(11, $newrecord['tid']);
125c9d29defSAndreas Gohr    }
126c9d29defSAndreas Gohr
127c9d29defSAndreas Gohr    public function testQueryValue()
128c9d29defSAndreas Gohr    {
129c9d29defSAndreas Gohr        $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db");
130c9d29defSAndreas Gohr        $sql = "SELECT value FROM testdata WHERE tid=?";
131c9d29defSAndreas Gohr
132c9d29defSAndreas Gohr        $result = $db->queryValue($sql, [4]);
133c9d29defSAndreas Gohr        $this->assertEquals('Classic', $result);
134c9d29defSAndreas Gohr    }
135c9d29defSAndreas Gohr
13633e488b3SAndreas Gohr    public function testQueryKeyValueList()
13733e488b3SAndreas Gohr    {
13833e488b3SAndreas Gohr        $db = new SQLiteDB('testdb', DOKU_PLUGIN . "sqlite/_test/db");
13933e488b3SAndreas Gohr
14033e488b3SAndreas Gohr        // data has not unique keys, last entry should win
14133e488b3SAndreas Gohr        $sql = "SELECT keyword, value FROM testdata ORDER BY tid";
14233e488b3SAndreas Gohr        $result = $db->queryKeyValueList($sql);
14333e488b3SAndreas Gohr        $this->assertArrayHasKey('music', $result);
14433e488b3SAndreas Gohr        $this->assertEquals('Boring', $result['music']);
14533e488b3SAndreas Gohr
14633e488b3SAndreas Gohr        // reverse is actually unique
14733e488b3SAndreas Gohr        $sql = "SELECT value, keyword FROM testdata";
14833e488b3SAndreas Gohr        $result = $db->queryKeyValueList($sql);
14933e488b3SAndreas Gohr        $this->assertArrayHasKey('Boring', $result);
15033e488b3SAndreas Gohr        $this->assertEquals('music', $result['Classic']);
15133e488b3SAndreas Gohr    }
152c9d29defSAndreas Gohr}
153