summaryrefslogtreecommitdiff
path: root/libs/picodb/tests/SqliteDriverTest.php
blob: 9965a39ceaf05ccdba2ea7f6ba255a03d2be6fb1 (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
67
68
69
70
<?php

require_once __DIR__.'/../../../vendor/autoload.php';

use PicoDb\Driver\Sqlite;

class SqliteDriverTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var PicoDb\Driver\Sqlite
     */
    private $driver;

    public function setUp()
    {
        $this->driver = new Sqlite(array('filename' => ':memory:'));
    }

    /**
     * @expectedException LogicException
     */
    public function testMissingRequiredParameter()
    {
        new Sqlite(array());
    }

    public function testDuplicateKeyError()
    {
        $this->assertFalse($this->driver->isDuplicateKeyError(1234));
        $this->assertTrue($this->driver->isDuplicateKeyError(23000));
    }

    public function testOperator()
    {
        $this->assertEquals('LIKE', $this->driver->getOperator('LIKE'));
        $this->assertEquals('LIKE', $this->driver->getOperator('ILIKE'));
        $this->assertEquals('', $this->driver->getOperator('FOO'));
    }

    public function testSchemaVersion()
    {
        $this->assertEquals(0, $this->driver->getSchemaVersion());

        $this->driver->setSchemaVersion(1);
        $this->assertEquals(1, $this->driver->getSchemaVersion());

        $this->driver->setSchemaVersion(42);
        $this->assertEquals(42, $this->driver->getSchemaVersion());
    }

    public function testLastInsertId()
    {
        $this->assertEquals(0, $this->driver->getLastId());

        $this->driver->getConnection()->exec('CREATE TABLE foobar (id INTEGER PRIMARY KEY, something TEXT)');
        $this->driver->getConnection()->exec('INSERT INTO foobar (something) VALUES (1)');

        $this->assertEquals(1, $this->driver->getLastId());
    }

    public function testEscape()
    {
        $this->assertEquals('"foobar"', $this->driver->escape('foobar'));
    }

    public function testDatabaseVersion()
    {
        $this->assertStringStartsWith('3.', $this->driver->getDatabaseVersion());
    }
}