subsan/codeception-module-elasticsearch

Codeception 的 Elasticsearch 模块

v1.0.0 2020-10-13 16:03 UTC

This package is auto-updated.

Last update: 2024-09-26 01:45:42 UTC


README

使用 elasticsearch/elasticsearch 官方 PHP 客户端连接到 Elasticsearch

可以在每次测试或测试套件运行后通过删除所有或配置中列出的索引来进行清理。可以在每次测试或测试套件运行前从文件系统恢复快照。

安装

Composer

composer require --dev "subsan/codeception-module-elasticsearch"

Elasticsearch 配置

如果您需要使用快照,您需要将 elasticsearch 配置中的路径添加到快照

elasticsearch.yml

path.repo: ["/PATH_TO_YOUR_PROJECT/tests/_data/elasticsearch/"]

用法

配置

  • hosts 必需 - Elasticsearch 主机
  • snapshotPath - 快照路径
  • snapshotName - 快照名称
  • compressedSnapshot: true - 快照是否压缩
  • populateBeforeTest: false - 是否在测试套件开始前加载快照
  • populateBeforeSuite: false - 是否在每个测试前重新加载快照
  • cleanup: false - 在每次测试或测试套件完成后从列表 [indexes] 删除索引或所有(如果 indexes 为空)
  • indexes: null - 在每次测试或测试套件完成后删除的索引列表。

示例(《acceptance.suite.yml》)

   modules:
        - Elasticsearch:
            hosts:
              - host: 'localhost'
                port: 9200
                user: 'elastic'
                pass: ''
            snapshotPath: 'tests/_data/elasticsearch'
            snapshotName: 'snapshot_name'
            compressedSnapshot: true
            populateBeforeTest: true
            populateBeforeSuite: true
            cleanup: true
            indexes:
              - index1
              - index2

创建快照示例

$hosts = \Jam\Core\Core::getInstance()->config()->elasticsearch()::[YOUR_HOSTS];
$raw   = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();

// create repo
$repoParams = [
    'repository' => 'codeception',
    'body'       => [
        'type'     => 'fs',
        'settings' => [
            'location' => '/PATH_TO_YOUR_PROJECT/tests/_data/elasticsearch',
            'compress' => true
        ]
    ]
];
$raw->snapshot()->createRepository($repoParams);

$restoreParams = [
    'repository'          => 'codeception',
    'snapshot'            => 'snapshotName',
    'wait_for_completion' => true,
    'body'                => [
        "indices"              => "INDEX_1,INDEX_2",
        "include_global_state" => false
    ]
];
$raw->snapshot()->create($restoreParams);

公共属性

  • elasticsearchClient - Elasticsearch\Client 实例

操作

seeDocumentInElasticsearch

断言具有给定 id 的文档存在于索引中。提供索引名称和文档 id。

$I->seeDocumentInElasticsearch('testIndex', 111);
  • param string $index
  • param string|int $id

dontSeeDocumentInElasticsearch

效果与 ->seeDocumentInElasticsearch 相反。断言在索引中不存在具有给定 id 的文档。提供索引名称和文档 id。

$I->dontSeeDocumentInElasticsearch('testIndex', 222);
  • param string $index
  • param string|int $id

grabDocumentFromElasticsearch

返回获取文档 功能 的响应。

$response = $I->grabDocumentFromElasticsearch('testIndex', 111);
print_r($response);

响应包含一些元数据(索引、版本等),以及一个 _source 字段,这是您发送到 Elasticsearch 的原始文档。

Array
(
    [_index] => testIndex
    [_type] => _doc
    [_id] => 111
    [_version] => 1
    [_seq_no] => 4205
    [_primary_term] => 1
    [found] => 1
    [_source] => Array
        (
            [testField] => abc
        )
  • param string $index

  • param string|int $id

  • return array $response

haveInElasticsearch

将文档插入索引

$item = [
    'testField' => 'abc'
];
$I->haveInElasticsearch('testIndex', 222, $item);
  • param string $index

  • param string|int $id

  • param array $body

  • return array $response