ElasticSearchTestCase.php 1.35 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\elasticsearch;
4

5
use Yii;
6 7 8
use yii\elasticsearch\Connection;
use yiiunit\TestCase;

Qiang Xue committed
9
Yii::setAlias('@yii/elasticsearch', __DIR__ . '/../../../../extensions/elasticsearch');
10

11
/**
12
 * ElasticSearchTestCase is the base class for all elasticsearch related test cases
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
 */
class ElasticSearchTestCase extends TestCase
{
	protected function setUp()
	{
		$this->mockApplication();

		$databases = $this->getParam('databases');
		$params = isset($databases['elasticsearch']) ? $databases['elasticsearch'] : null;
		if ($params === null || !isset($params['dsn'])) {
			$this->markTestSkipped('No elasticsearch server connection configured.');
		}
		$dsn = explode('/', $params['dsn']);
		$host = $dsn[2];
		if (strpos($host, ':')===false) {
			$host .= ':9200';
		}
		if(!@stream_socket_client($host, $errorNumber, $errorDescription, 0.5)) {
			$this->markTestSkipped('No elasticsearch server running at ' . $params['dsn'] . ' : ' . $errorNumber . ' - ' . $errorDescription);
		}

		parent::setUp();
	}

	/**
	 * @param bool $reset whether to clean up the test database
	 * @return Connection
	 */
	public function getConnection($reset = true)
	{
		$databases = $this->getParam('databases');
		$params = isset($databases['elasticsearch']) ? $databases['elasticsearch'] : array();
45
		$db = new Connection();
46 47 48 49 50
		if ($reset) {
			$db->open();
		}
		return $db;
	}
Qiang Xue committed
51
}