Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SqlDump.php « Fixtures « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9113d5e4e7a9e9b109cf6c3bcd887e0b43e55664 (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
88
89
90
91
92
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
use Piwik\ArchiveProcessor\Rules;
use Piwik\Config;
use Piwik\Db;
use Piwik\Piwik;

/**
 * Reusable fixture. Loads a SQL dump into the DB.
 */
class Piwik_Test_Fixture_SqlDump extends Fixture
{
    public $date = '2012-09-03';
    public $dateTime = '2012-09-03';
    public $period = 'day';
    public $idSite = 'all';
    public $tablesPrefix = 'piwik_';
    public $dumpUrl = "http://piwik-team.s3.amazonaws.com/generated-logs-one-day.sql.gz";

    public function setUp()
    {
        // drop all tables
        Db::dropAllTables();

        // download data dump if url supplied
        if (is_file($this->dumpUrl)) {
            $dumpPath = $this->dumpUrl;
        } else {
            $dumpPath = PIWIK_INCLUDE_PATH . '/tmp/logdump.sql.gz';
            $bufferSize = 1024 * 1024;

            $dump = fopen($this->dumpUrl, 'rb');
            $outfile = fopen($dumpPath, 'wb');
            $bytesRead = 0;
            while (!feof($dump)) {
                fwrite($outfile, fread($dump, $bufferSize), $bufferSize);
                $bytesRead += $bufferSize;
            }
            fclose($dump);
            fclose($outfile);

            if ($bytesRead <= 40 * 1024 * 1024) { // sanity check
                throw new Exception("Could not download sql dump!");
            }
        }

        // unzip the dump
        if (substr($dumpPath, -3) === ".gz") {
            $deflatedDumpPath = PIWIK_INCLUDE_PATH . '/tmp/logdump.sql'; // TODO: should depend on name of URL
            exec("gunzip -c \"" . $dumpPath . "\" > \"$deflatedDumpPath\"", $output, $return);
            if ($return !== 0) {
                throw new Exception("gunzip failed: " . implode("\n", $output));
            }
        } else {
            $deflatedDumpPath = $dumpPath;
        }

        // load the data into the correct database
        $user = Config::getInstance()->database['username'];
        $password = Config::getInstance()->database['password'];
        Config::getInstance()->database['tables_prefix'] = $this->tablesPrefix;

        $cmd = "mysql -u \"$user\" \"--password=$password\" {$this->dbName} < \"" . $deflatedDumpPath . "\" 2>&1";
        exec($cmd, $output, $return);
        if ($return !== 0) {
            throw new Exception("Failed to load sql dump: " . implode("\n", $output));
        }

        // make sure archiving will be called
        Rules::setBrowserTriggerArchiving(true);

        // reload access
        Piwik::setUserHasSuperUserAccess();

        $this->getTestEnvironment()->configOverride = array(
            'database' => array(
                'tables_prefix' => $this->tablesPrefix
            )
        );
        $this->getTestEnvironment()->save();
    }

    public function tearDown()
    {
        // empty
    }
}