summaryrefslogtreecommitdiff
path: root/libs/picodb/tests/PostgresLobTest.php
blob: 39cf8fb00d8502ea8cb4625eff797d3b258a7614 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php

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

use PicoDb\Database;

class PostgresLobTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var PicoDb\Database
     */
    private $db;

    public function setUp()
    {
        $this->db = new Database(array('driver' => 'postgres', 'hostname' => 'localhost', 'username' => 'postgres', 'password' => 'postgres', 'database' => 'picodb'));
        $this->db->getConnection()->exec('DROP TABLE IF EXISTS large_objects');
        $this->db->getConnection()->exec('CREATE TABLE large_objects (id VARCHAR(20), file_content bytea)');
    }

    public function testInsert()
    {
        $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test'));
        $this->assertTrue($result);
    }

    public function testInsertFromString()
    {
        $data = 'test';
        $result = $this->db->largeObject('large_objects')->insertFromString('file_content', $data, array('id' => 'test'));
        $this->assertTrue($result);
    }

    public function testInsertWithOptionalParams()
    {
        $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__);
        $this->assertTrue($result);
    }

    public function testFindOneColumnAsStream()
    {
        $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test'));
        $this->assertTrue($result);

        $fd = $this->db->largeObject('large_objects')->eq('id', 'test')->findOneColumnAsStream('file_content');
        $contents = fread($fd, filesize(__FILE__));
        fclose($fd);

        $this->assertSame(md5(file_get_contents(__FILE__)), md5($contents));
    }

    public function testFindOneColumnAsString()
    {
        $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test'));
        $this->assertTrue($result);

        $contents = $this->db->largeObject('large_objects')->eq('id', 'test')->findOneColumnAsString('file_content');
        $this->assertSame(md5(file_get_contents(__FILE__)), md5($contents));
    }

    public function testUpdate()
    {
        $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test1'));
        $this->assertTrue($result);

        $result = $this->db->largeObject('large_objects')->insertFromFile('file_content', __FILE__, array('id' => 'test2'));
        $this->assertTrue($result);

        $result = $this->db->largeObject('large_objects')->eq('id', 'test1')->updateFromFile('file_content', __DIR__.'/../LICENSE');
        $this->assertTrue($result);

        $contents = $this->db->largeObject('large_objects')->eq('id', 'test1')->findOneColumnAsString('file_content');
        $this->assertSame(md5(file_get_contents(__DIR__.'/../LICENSE')), md5($contents));

        $contents = $this->db->largeObject('large_objects')->eq('id', 'test2')->findOneColumnAsString('file_content');
        $this->assertSame(md5(file_get_contents(__FILE__)), md5($contents));

        $result = $this->db->largeObject('large_objects')->updateFromFile('file_content', __DIR__.'/../composer.json');
        $this->assertTrue($result);

        $contents = $this->db->largeObject('large_objects')->eq('id', 'test1')->findOneColumnAsString('file_content');
        $this->assertSame(md5(file_get_contents(__DIR__.'/../composer.json')), md5($contents));

        $contents = $this->db->largeObject('large_objects')->eq('id', 'test2')->findOneColumnAsString('file_content');
        $this->assertSame(md5(file_get_contents(__DIR__.'/../composer.json')), md5($contents));
    }
}