zumba / elasticsearchunit
PHPUnit extension that supports ElasticSearch Client
Installs: 43 808
Dependents: 2
Suggesters: 0
Security: 0
Stars: 4
Watchers: 35
Forks: 2
Open Issues: 1
Requires
- php: >=5.6.0
- ext-curl: *
Requires (Dev)
- elasticsearch/elasticsearch: ~5.1.0
- phpunit/phpunit: 4.7.7
This package is auto-updated.
Last update: 2024-10-24 04:16:02 UTC
README
ElasticSearchUnit is a PHPUnit extension for test cases that utilize the official ElasticSearch Client as their data source.
Requirements
- PHP 5.4+
- ElasticSearch 1.7+
Testing
- Install dependencies
composer install -dev
- Run
./bin/phpunit
Example use
<?php class MyElasticSearchTestCase extends \PHPUnit_Framework_TestCase { use \Zumba\PHPUnit\Extensions\ElasticSearch\TestTrait; /** * Get the ElasticSearch connection for this test. * * @return Zumba\PHPUnit\Extensions\ElasticSearch\Client\Connector */ public function getElasticSearchConnector() { if (empty($this->connection)) { $clientBuilder = new \Elasticsearch\ClientBuilder(); $this->connection = new \Zumba\PHPUnit\Extensions\ElasticSearch\Client\Connector($clientBuilder->build()); } return $this->connection; } /** * Get the dataset to be used for this test. * * @return Zumba\PHPUnit\Extensions\ElasticSearch\DataSet\DataSet */ public function getElasticSearchDataSet() { $dataset = new \Zumba\PHPUnit\Extensions\ElasticSearch\DataSet\DataSet($this->getElasticSearchConnector()); $dataset->setFixture([ 'some_index' => [ 'some_type' => [ ['name' => 'Document 1'], ['name' => 'Document 2'] ] ] ]); return $dataset; } public function testRead() { $result = $this->getElasticSearchConnector()->getConnection()->search(['index' => 'some_index']); $this->assertEquals(2, $result['hits']['total']); } }
Testing with Docker/VM etc
If Elasticsearch is not running on localhost, you can provide the hostname of your Elasticsearch instance via environment variables:
ES_TEST_HOST=http://docker:9200 ./bin/phpunit
Elasticsearch Version Support
For Elasticsearch 5.x and greater, use version 2.x.
For Elasticsearch 2.x/1.x, use the version 1.x.
Note about PHPUnit Versions
It currently is supporting PHPUnit 4 @before
and @after
but can be used in PHPUnit ~3.7 by either aliasing the elasticSearchSetUp
and elasticSearchTearDown
to setUp
and tearDown
, or by calling elasticSearchSetUp
and elasticSearchTearDown
in your respective methods.