https://github.com/zumba/elasticsearchunit
Elasticsearch extension for PHPUnit.
https://github.com/zumba/elasticsearchunit
Last synced: about 1 year ago
JSON representation
Elasticsearch extension for PHPUnit.
- Host: GitHub
- URL: https://github.com/zumba/elasticsearchunit
- Owner: zumba
- License: other
- Created: 2015-05-12T21:17:18.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2018-12-18T15:07:36.000Z (over 7 years ago)
- Last Synced: 2025-03-28T22:11:20.282Z (about 1 year ago)
- Language: PHP
- Homepage:
- Size: 51.8 KB
- Stars: 4
- Watchers: 34
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: license.txt
Awesome Lists containing this project
README
ElasticSearchUnit is a PHPUnit extension for test cases that utilize the official ElasticSearch Client as their data source.
[](https://travis-ci.org/zumba/elasticsearchunit)
## Requirements
* PHP 5.4+
* ElasticSearch 1.7+
## Testing
1. Install dependencies `composer install -dev`
2. Run `./bin/phpunit`
## Example use
```php
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']);
}
}
```
[See full working example.](https://github.com/zumba/elasticsearchunit/blob/master/examples/PizzaTraitTest.php)
## Testing with Docker/VM etc
If Elasticsearch is not running on localhost, you can provide the hostname of your Elasticsearch instance via environment variables:
```bash
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.