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