PHP code example of zumba / elasticsearchunit

1. Go to this page and download the library: Download zumba/elasticsearchunit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zumba / elasticsearchunit example snippets




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']);
	}

}