SphinxTestCase.php 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace yiiunit\extensions\sphinx;

use yii\helpers\FileHelper;
use yii\sphinx\Connection;
use Yii;
use yiiunit\TestCase as TestCase;

/**
 * Base class for the Sphinx test cases.
 */
class SphinxTestCase extends TestCase
{
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    /**
     * @var array Sphinx connection configuration.
     */
    protected $sphinxConfig = [
        'dsn' => 'mysql:host=127.0.0.1;port=9306;',
        'username' => '',
        'password' => '',
    ];
    /**
     * @var Connection Sphinx connection instance.
     */
    protected $sphinx;
    /**
     * @var array Database connection configuration.
     */
    protected $dbConfig = [
        'dsn' => 'mysql:host=127.0.0.1;',
        'username' => '',
        'password' => '',
    ];
    /**
     * @var \yii\db\Connection database connection instance.
     */
    protected $db;
39

40 41 42 43
    public static function setUpBeforeClass()
    {
        static::loadClassMap();
    }
44

45 46 47 48 49 50
    protected function setUp()
    {
        parent::setUp();
        if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
            $this->markTestSkipped('pdo and pdo_mysql extension are required.');
        }
51
        $config = self::getParam('sphinx');
52 53 54 55
        if (!empty($config)) {
            $this->sphinxConfig = $config['sphinx'];
            $this->dbConfig = $config['db'];
        }
56 57 58 59 60 61
        // check whether sphinx is running and skip tests if not.
        if (preg_match('/host=([\w\d.]+)/i', $this->sphinxConfig['dsn'], $hm) && preg_match('/port=(\d+)/i', $this->sphinxConfig['dsn'], $pm)) {
            if (!@stream_socket_client($hm[1] . ':' . $pm[1], $errorNumber, $errorDescription, 0.5)) {
                $this->markTestSkipped('No Sphinx searchd running at ' . $hm[1] . ':' . $pm[1] . ' : ' . $errorNumber . ' - ' . $errorDescription);
            }
        }
62 63 64
        $this->mockApplication();
        static::loadClassMap();
    }
65

66 67 68 69 70 71 72
    protected function tearDown()
    {
        if ($this->sphinx) {
            $this->sphinx->close();
        }
        $this->destroyApplication();
    }
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    /**
     * Adds sphinx extension files to [[Yii::$classPath]],
     * avoiding the necessity of usage Composer autoloader.
     */
    protected static function loadClassMap()
    {
        $baseNameSpace = 'yii/sphinx';
        $basePath = realpath(__DIR__. '/../../../../extensions/sphinx');
        $files = FileHelper::findFiles($basePath);
        foreach ($files as $file) {
            $classRelativePath = str_replace($basePath, '', $file);
            $classFullName = str_replace(['/', '.php'], ['\\', ''], $baseNameSpace . $classRelativePath);
            Yii::$classMap[$classFullName] = $file;
        }
    }
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    /**
     * @param  boolean                $reset whether to clean up the test database
     * @param  boolean                $open  whether to open test database
     * @return \yii\sphinx\Connection
     */
    public function getConnection($reset = false, $open = true)
    {
        if (!$reset && $this->sphinx) {
            return $this->sphinx;
        }
        $db = new Connection;
        $db->dsn = $this->sphinxConfig['dsn'];
        if (isset($this->sphinxConfig['username'])) {
            $db->username = $this->sphinxConfig['username'];
            $db->password = $this->sphinxConfig['password'];
        }
        if (isset($this->sphinxConfig['attributes'])) {
            $db->attributes = $this->sphinxConfig['attributes'];
        }
        if ($open) {
            $db->open();
        }
        $this->sphinx = $db;
113

114 115
        return $db;
    }
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    /**
     * Truncates the runtime index.
     * @param string $indexName index name.
     */
    protected function truncateRuntimeIndex($indexName)
    {
        if ($this->sphinx) {
            $this->sphinx->createCommand('TRUNCATE RTINDEX ' . $indexName)->execute();
        }
    }

    /**
     * @param  boolean            $reset whether to clean up the test database
     * @param  boolean            $open  whether to open and populate test database
     * @return \yii\db\Connection
     */
    public function getDbConnection($reset = true, $open = true)
    {
        if (!$reset && $this->db) {
            return $this->db;
        }
        $db = new \yii\db\Connection;
        $db->dsn = $this->dbConfig['dsn'];
        if (isset($this->dbConfig['username'])) {
            $db->username = $this->dbConfig['username'];
            $db->password = $this->dbConfig['password'];
        }
        if (isset($this->dbConfig['attributes'])) {
            $db->attributes = $this->dbConfig['attributes'];
        }
        if ($open) {
            $db->open();
            if (!empty($this->dbConfig['fixture'])) {
                $lines = explode(';', file_get_contents($this->dbConfig['fixture']));
                foreach ($lines as $line) {
                    if (trim($line) !== '') {
                        $db->pdo->exec($line);
                    }
                }
            }
        }
        $this->db = $db;

        return $db;
    }
Qiang Xue committed
162
}