Skip to content

Commit

Permalink
Merge pull request #171 from martiis/issue-168
Browse files Browse the repository at this point in the history
Expose elasticsearch hosts from config_test.yml to parameters_test.yml
  • Loading branch information
saimaz committed Feb 2, 2015
2 parents d0ded7c + 82cd81d commit 03a333a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/Tests/app/data/
/phpunit.xml
/composer.lock
/Tests/app/config/parameters_test.yml
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ install:
- wget -q -O conf.py https://raw.githubusercontent.com/ongr-io/docs-aggregator/master/source/conf-travis.py

before_script:
- composer install
- composer install --no-interaction

script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
Expand Down
4 changes: 2 additions & 2 deletions DataCollector/ElasticsearchDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private function handleRecords($route, $records)
$this->time += $record['context']['duration'];
$this->addQuery($route, $record, $queryBody);
} else {
$position = strpos($record['message'], '-d');
$position = strpos($record['message'], ' -d');
$queryBody = $position !== false ? substr($record['message'], $position + 3) : '';
}
}
Expand All @@ -174,7 +174,7 @@ private function handleRecords($route, $records)
private function addQuery($route, $record, $queryBody)
{
parse_str(parse_url($record['context']['uri'], PHP_URL_QUERY), $httpParameters);
$body = json_decode(trim($queryBody, "'"), true);
$body = json_decode(trim($queryBody, " '\r\t\n"), true);
$this->queries[$route][] = array_merge(
[
'body' => $body !== null ? json_encode($body, JSON_PRETTY_PRINT) : '',
Expand Down
32 changes: 22 additions & 10 deletions Tests/Functional/DataCollector/ElasticsearchDataCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,12 @@ public function testGetQueries()
$queries = $this->getCollector()->getQueries();

$lastQuery = end($queries[ElasticsearchDataCollector::UNDEFINED_ROUTE]);
$time = $lastQuery['time'];
unset($lastQuery['time']);
$this->checkQueryParameters($lastQuery);

$this->assertGreaterThan(0.0, $time, 'Time should be greater than 0');
$this->assertEquals(
[
'body' => '',
'method' => 'GET',
'path' => '/ongr-elasticsearch-bundle-test/product/2',
'host' => '127.0.0.1',
'httpParameters' => [],
'scheme' => 'http',
'port' => 9200,
Expand All @@ -122,16 +118,12 @@ public function testGetTermQuery()

$queries = $this->getCollector()->getQueries();
$lastQuery = end($queries[ElasticsearchDataCollector::UNDEFINED_ROUTE]);
$time = $lastQuery['time'];
unset($lastQuery['time']);
$this->checkQueryParameters($lastQuery);

$this->assertGreaterThan(0.0, $time, 'Time should be greater than 0');
$this->assertEquals(
[
'body' => $this->getFileContents('collector_body_0.json'),
'method' => 'POST',
'path' => '/ongr-elasticsearch-bundle-test/product/_search',
'host' => '127.0.0.1',
'httpParameters' => [],
'scheme' => 'http',
'port' => 9200,
Expand All @@ -141,6 +133,26 @@ public function testGetTermQuery()
);
}

/**
* Checks query parameters that are not static.
*
* @param array $query
*/
public function checkQueryParameters(&$query)
{
$this->assertArrayHasKey('time', $query, 'Query should have time set.');
$this->assertGreaterThan(0.0, $query['time'], 'Time should be greater than 0');
unset($query['time']);

$this->assertArrayHasKey('host', $query, 'Query should have host set.');
$this->assertNotEmpty($query['host'], 'Host should not be empty');
unset($query['host']);

$this->assertArrayHasKey('path', $query, 'Query should have host path set.');
$this->assertNotEmpty($query['path'], 'Path should not be empty.');
unset($query['path']);
}

/**
* @return ElasticsearchDataCollector
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

namespace ONGR\ElasticsearchBundle\Tests\Functional\DependencyInjection;

use ONGR\ElasticsearchBundle\Test\TestHelperTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ElasticsearchExtensionTest extends WebTestCase
{
use TestHelperTrait;

/**
* @return array
*/
Expand Down Expand Up @@ -81,16 +84,12 @@ public function testContainerDefaultParams()

$expectedConnections = [
'default' => [
'hosts' => ['127.0.0.1:9200'],
'index_name' => 'ongr-elasticsearch-bundle-test',
'settings' => [
'refresh_interval' => -1,
'number_of_replicas' => 0,
],
],
'bar' => [
'hosts' => ['127.0.0.1:9200'],
'index_name' => 'ongr-elasticsearch-bundle-bar-test',
'settings' => [
'refresh_interval' => -1,
'number_of_replicas' => 1,
Expand All @@ -113,7 +112,7 @@ public function testContainerDefaultParams()
];
$actualManagers = $container->getParameter('es.managers');

$this->assertEquals($expectedConnections, $actualConnections);
$this->assertArrayContainsArray($expectedConnections, $actualConnections);
$this->assertEquals($expectedManagers, $actualManagers);
}
}
11 changes: 7 additions & 4 deletions Tests/app/config/config_test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
imports:
- { resource: parameters_test.yml }

# Framework Configuration
framework:
secret: "TOP-SECRET"
Expand All @@ -7,15 +10,15 @@ ongr_elasticsearch:
connections:
default:
hosts:
- { host: 127.0.0.1:9200 }
index_name: ongr-elasticsearch-bundle-test
- { host: "%elasticsearch_test_connection.host%" }
index_name: "%elasticsearch_test_connection.default.index_name%"
settings:
refresh_interval: -1
number_of_replicas: 0
bar:
hosts:
- { host: 127.0.0.1:9200 }
index_name: ongr-elasticsearch-bundle-bar-test
- { host: "%elasticsearch_test_connection.host%" }
index_name: "%elasticsearch_test_connection.bar.index_name%"
settings:
refresh_interval: -1
number_of_replicas: 1
Expand Down
4 changes: 4 additions & 0 deletions Tests/app/config/parameters_test.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
elasticsearch_test_connection.host: "127.0.0.1:9200"
elasticsearch_test_connection.default.index_name: "ongr-elasticsearch-bundle-default-test"
elasticsearch_test_connection.bar.index_name: "ongr-elasticsearch-bundle-bar-test"
16 changes: 15 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,23 @@
"mikey179/vfsStream": "~1.4",
"phpunit/phpunit": "~4.1",
"squizlabs/php_codesniffer": "~1.5",
"ongr/ongr-strict-standard": "~1.0"
"ongr/ongr-strict-standard": "~1.0",
"incenteev/composer-parameter-handler": "~2.0"
},
"autoload": {
"psr-4": { "ONGR\\ElasticsearchBundle\\": "" }
},
"scripts": {
"post-install-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters"
],
"post-update-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters"
]
},
"extra": {
"incenteev-parameters": {
"file": "Tests/app/config/parameters_test.yml"
}
}
}

0 comments on commit 03a333a

Please sign in to comment.