Skip to content

Commit

Permalink
Merge pull request #846 from ruflin/tests-host-refactoring
Browse files Browse the repository at this point in the history
Tests host refactoring
  • Loading branch information
ruflin committed May 16, 2015
2 parents 6cff43b + 6eeca7f commit b703aa6
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 107 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ All notable changes to this project will be documented in this file based on the

### Improvements
- Introduction of Changelog standard based on http://keepachangelog.com/. changes.txt moved to CHANGELOG.md [#844](https://github.com/ruflin/Elastica/issues/844/)
- Make host for all tests dynamic to prepare it for a more dynamic test environment #846






## [2.0.0](https://github.com/ruflin/Elastica/releases/tag/2.0.0) - 2015-05-11
Expand Down
53 changes: 48 additions & 5 deletions test/lib/Elastica/Test/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,58 @@
namespace Elastica\Test;

use Elastica\Client;
use Elastica\Connection;
use Elastica\Index;

class Base extends \PHPUnit_Framework_TestCase
{
protected function _getClient()
/**
* @param array $params Additional configuration params. Host and Port are already set
* @param callback $callback
* @return Client
*/
protected function _getClient(array $params = array(), $callback = null)
{
$config = array(
'host' => $this->_getHost(),
'port' => $this->_getPort(),
);

$config = array_merge($config, $params);

return new Client($config, $callback);
}

/**
* @return string Host to es for elastica tests
*/
protected function _getHost()
{
return getenv('ES_HOST') ?: Connection::DEFAULT_HOST;
}

/**
* @return int Port to es for elastica tests
*/
protected function _getPort()
{
return getenv('ES_PORT') ?: Connection::DEFAULT_PORT;
}

/**
* @return string Proxy url string
*/
protected function _getProxyUrl()
{
return "http://127.0.0.1:12345";
}

/**
* @return string Proxy url string to proxy which returns 403
*/
protected function _getProxyUrl403()
{
return new Client(array(
'host' => getenv('ES_HOST') ?: 'localhost',
'port' => getenv('ES_PORT') ?: 9200,
));
return "http://127.0.0.1:12346";
}

/**
Expand Down Expand Up @@ -49,6 +91,7 @@ protected function _waitForAllocation(Index $index)

protected function tearDown()
{
parent::tearDown();
$this->_getClient()->getIndex('_all')->delete();
$this->_getClient()->getIndex('_all')->clearCache();
}
Expand Down
2 changes: 1 addition & 1 deletion test/lib/Elastica/Test/Bulk/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testAction()
$expected = '{"index":{"_index":"index","_type":"type","_id":1,"_routing":1}}'."\n";
$this->assertEquals($expected, $action->toString());

$client = new Client();
$client = $this->_getClient();
$index = new Index($client, 'index2');
$type = new Type($index, 'type2');

Expand Down
20 changes: 10 additions & 10 deletions test/lib/Elastica/Test/BulkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function testUnicodeBulkSend()

public function testSetIndexType()
{
$client = new Client();
$client = $this->_getClient();
$index = $client->getIndex('index');
$type = $index->getType('type');

Expand Down Expand Up @@ -200,7 +200,7 @@ public function testSetIndexType()

public function testAddActions()
{
$client = new Client();
$client = $this->_getClient();
$bulk = new Bulk($client);

$action1 = new Action(Action::OP_TYPE_DELETE);
Expand Down Expand Up @@ -427,7 +427,7 @@ public function testUdp($clientConfig, $host, $port, $shouldFail = false)
if (!function_exists('socket_create')) {
$this->markTestSkipped('Function socket_create() does not exist.');
}
$client = new Client($clientConfig);
$client = $this->_getClient($clientConfig);
$index = $client->getIndex('elastica_test');
$index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true);
$type = $index->getType('udp_test');
Expand Down Expand Up @@ -668,7 +668,7 @@ public function udpDataProvider()
),
array(
array(),
'localhost',
$this->_getHost(),
null,
),
array(
Expand All @@ -678,13 +678,13 @@ public function udpDataProvider()
),
array(
array(),
'localhost',
$this->_getHost(),
9700,
),
array(
array(
'udp' => array(
'host' => 'localhost',
'host' => $this->_getHost(),
'port' => 9700,
),
),
Expand All @@ -694,17 +694,17 @@ public function udpDataProvider()
array(
array(
'udp' => array(
'host' => 'localhost',
'host' => $this->_getHost(),
'port' => 9800,
),
),
'localhost',
$this->_getHost(),
9700,
),
array(
array(
'udp' => array(
'host' => 'localhost',
'host' => $this->_getHost(),
'port' => 9800,
),
),
Expand All @@ -714,7 +714,7 @@ public function udpDataProvider()
),
array(
array(),
'localhost',
$this->_getHost(),
9800,
true,
),
Expand Down
47 changes: 25 additions & 22 deletions test/lib/Elastica/Test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testConstruct()
public function testConnectionsArray()
{
// Creates a new index 'xodoa' and a type 'user' inside this index
$client = new Client(array('connections' => array(array('host' => 'localhost', 'port' => 9200))));
$client = $this->_getClient(array('connections' => array(array('host' => $this->_getHost(), 'port' => 9200))));
$index = $client->getIndex('elastica_test1');
$index->create(array(), true);

Expand Down Expand Up @@ -56,9 +56,9 @@ public function testConnectionsArray()
public function testTwoServersSame()
{
// Creates a new index 'xodoa' and a type 'user' inside this index
$client = new Client(array('connections' => array(
array('host' => 'localhost', 'port' => 9200),
array('host' => 'localhost', 'port' => 9200),
$client = $this->_getClient(array('connections' => array(
array('host' => $this->_getHost(), 'port' => 9200),
array('host' => $this->_getHost(), 'port' => 9200),
)));
$index = $client->getIndex('elastica_test1');
$index->create(array(), true);
Expand All @@ -67,17 +67,17 @@ public function testTwoServersSame()

// Adds 1 document to the index
$doc1 = new Document(1,
array('username' => 'hans', 'test' => array('2', '3', '5'))
array('username' => 'hans', 'test' => array('2', '3', '5'))
);
$type->addDocument($doc1);

// Adds a list of documents with _bulk upload to the index
$docs = array();
$docs[] = new Document(2,
array('username' => 'john', 'test' => array('1', '3', '6'))
array('username' => 'john', 'test' => array('1', '3', '6'))
);
$docs[] = new Document(3,
array('username' => 'rolf', 'test' => array('2', '3', '7'))
array('username' => 'rolf', 'test' => array('2', '3', '7'))
);
$type->addDocuments($docs);

Expand All @@ -89,26 +89,29 @@ public function testTwoServersSame()

public function testConnectionParamsArePreparedForConnectionsOption()
{
$client = new Client(array('connections' => array(array('url' => 'https://localhost:9200'))));
$url = 'https://' . $this->_getHost() . ':9200';
$client = $this->_getClient(array('connections' => array(array('url' => $url))));
$connection = $client->getConnection();

$this->assertEquals('https://localhost:9200', $connection->getConfig('url'));
$this->assertEquals($url, $connection->getConfig('url'));
}

public function testConnectionParamsArePreparedForServersOption()
{
$client = new Client(array('servers' => array(array('url' => 'https://localhost:9200'))));
$url = 'https://' . $this->_getHost() . ':9200';
$client = $this->_getClient(array('servers' => array(array('url' => $url))));
$connection = $client->getConnection();

$this->assertEquals('https://localhost:9200', $connection->getConfig('url'));
$this->assertEquals($url, $connection->getConfig('url'));
}

public function testConnectionParamsArePreparedForDefaultOptions()
{
$client = new Client(array('url' => 'https://localhost:9200'));
$url = 'https://' . $this->_getHost() . ':9200';
$client = $this->_getClient(array('url' => $url));
$connection = $client->getConnection();

$this->assertEquals('https://localhost:9200', $connection->getConfig('url'));
$this->assertEquals($url, $connection->getConfig('url'));
}

public function testBulk()
Expand Down Expand Up @@ -565,7 +568,7 @@ public function testCallback()
$count++;
};

$client = new Client(array(), $callback);
$client = $this->_getClient(array(), $callback);

// First connection work, second should not work
$connection1 = new Connection(array('port' => '9101', 'timeout' => 2));
Expand All @@ -588,10 +591,10 @@ public function testCallback()

public function testUrlConstructor()
{
$url = 'http://localhost:9200/';
$url = 'http://' . $this->_getHost() . ':9200/';

// Url should overwrite invalid host
$client = new Client(array('url' => $url, 'port' => '9101', 'timeout' => 2));
$client = $this->_getClient(array('url' => $url, 'port' => '9101', 'timeout' => 2));

$response = $client->request('_status');
$this->assertInstanceOf('Elastica\Response', $response);
Expand Down Expand Up @@ -835,7 +838,7 @@ public function testDeleteDocuments()

public function testLastRequestResponse()
{
$client = new Client();
$client = $this->_getClient();
$response = $client->request('_status');

$this->assertInstanceOf('Elastica\Response', $response);
Expand Down Expand Up @@ -943,7 +946,7 @@ public function testConfigValue()
),
'level11' => 'value11',
);
$client = new Client($config);
$client = $this->_getClient($config);

$this->assertNull($client->getConfigValue('level12'));
$this->assertFalse($client->getConfigValue('level12', false));
Expand All @@ -960,7 +963,7 @@ public function testConfigValue()

public function testArrayQuery()
{
$client = new Client();
$client = $this->_getClient();

$index = $client->getIndex('test');
$index->create(array(), true);
Expand All @@ -986,7 +989,7 @@ public function testArrayQuery()

public function testJSONQuery()
{
$client = new Client();
$client = $this->_getClient();

$index = $client->getIndex('test');
$index->create(array(), true);
Expand All @@ -1006,7 +1009,7 @@ public function testJSONQuery()

public function testAddHeader()
{
$client = new Client();
$client = $this->_getClient();

// add one header
$client->addHeader('foo', 'bar');
Expand All @@ -1031,7 +1034,7 @@ public function testAddHeader()

public function testRemoveHeader()
{
$client = new Client();
$client = $this->_getClient();

// set headers
$headers = array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testConnection()
return current($connections);
});

$client = new Client($config);
$client = $this->_getClient($config);
$response = $client->request('/_aliases');

$this->assertEquals(1, $count);
Expand Down
12 changes: 6 additions & 6 deletions test/lib/Elastica/Test/Connection/Strategy/RoundRobinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RoundRobinTest extends Base
public function testConnection()
{
$config = array('connectionStrategy' => 'RoundRobin');
$client = new Client($config);
$client = $this->_getClient($config);
$response = $client->request('/_aliases');
/* @var $response Response */

Expand All @@ -32,7 +32,7 @@ public function testConnection()
public function testOldStrategySetted()
{
$config = array('roundRobin' => true);
$client = new Client($config);
$client = $this->_getClient($config);

$this->_checkStrategy($client);
}
Expand All @@ -43,7 +43,7 @@ public function testOldStrategySetted()
public function testFailConnection()
{
$config = array('connectionStrategy' => 'RoundRobin', 'host' => '255.255.255.0');
$client = new Client($config);
$client = $this->_getClient($config);

$this->_checkStrategy($client);

Expand All @@ -54,15 +54,15 @@ public function testWithOneFailConnection()
{
$connections = array(
new Connection(array('host' => '255.255.255.0')),
new Connection(array('host' => 'localhost')),
new Connection(array('host' => $this->_getHost())),
);

$count = 0;
$callback = function ($connection, $exception, $client) use (&$count) {
++$count;
};

$client = new Client(array('connectionStrategy' => 'RoundRobin'), $callback);
$client = $this->_getClient(array('connectionStrategy' => 'RoundRobin'), $callback);
$client->setConnections($connections);

$response = $client->request('/_aliases');
Expand All @@ -84,7 +84,7 @@ public function testWithNoValidConnection()
);

$count = 0;
$client = new Client(array('roundRobin' => true), function () use (&$count) {
$client = $this->_getClient(array('roundRobin' => true), function () use (&$count) {
++$count;
});

Expand Down
Loading

0 comments on commit b703aa6

Please sign in to comment.