SphinxTestCase.php 3.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?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
{
	/**
	 * @var array Sphinx connection configuration.
	 */
	protected $sphinxConfig = [
		'dsn' => 'mysql:host=127.0.0.1;port=9306;',
		'username' => '',
		'password' => '',
	];
	/**
24
	 * @var Connection Sphinx connection instance.
25 26
	 */
	protected $sphinx;
27 28 29 30 31 32 33 34 35 36 37 38
	/**
	 * @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 44 45 46 47 48 49 50

	public static function setUpBeforeClass()
	{
		static::loadClassMap();
	}

	protected function setUp()
	{
		parent::setUp();
		if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) {
			$this->markTestSkipped('pdo and pdo_mysql extension are required.');
		}
51 52 53 54 55
		$config = $this->getParam('sphinx');
		if (!empty($config)) {
			$this->sphinxConfig = $config['sphinx'];
			$this->dbConfig = $config['db'];
		}
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
		$this->mockApplication();
		static::loadClassMap();
	}

	protected function tearDown()
	{
		if ($this->sphinx) {
			$this->sphinx->close();
		}
		$this->destroyApplication();
	}

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

	/**
	 * @param bool $reset whether to clean up the test database
86
	 * @param bool $open whether to open test database
87
	 * @return \yii\sphinx\Connection
88
	 */
89
	public function getConnection($reset = false, $open = true)
90 91 92 93
	{
		if (!$reset && $this->sphinx) {
			return $this->sphinx;
		}
94
		$db = new Connection;
95 96 97 98 99 100 101 102 103 104 105 106 107 108
		$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;
		return $db;
	}
109 110 111 112 113 114 115 116 117 118 119

	/**
	 * Truncates the runtime index.
	 * @param string $indexName index name.
	 */
	protected function truncateRuntimeIndex($indexName)
	{
		if ($this->sphinx) {
			$this->sphinx->createCommand('TRUNCATE RTINDEX ' . $indexName)->execute();
		}
	}
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

	/**
	 * @param bool $reset whether to clean up the test database
	 * @param bool $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
154
}