ajant/db-mock-library

数据库模拟与虚拟数据管理库

2.1.0 2016-10-29 07:34 UTC

This package is auto-updated.

Last update: 2024-09-25 18:33:03 UTC


README

Build Status Coverage Status

数据库模拟与虚拟数据管理库

Wiki

这是一个数据库存根/模拟/原型库。其主要用途包括

  1. 在不使用实际数据库的情况下测试应用程序(通过模拟数据持久层,使用DbMockLibrary)

  2. 快速原型设计,同时推迟任何特定数据库代码的编写(再次通过模拟数据持久层,使用DbMockLibrary)

  3. 开发阶段虚拟数据管理

描述

  1. 如果数据持久代码与业务逻辑代码分开,位于应用程序的不同层,那么在测试期间可以使用DbMockLibrary模拟数据持久层。这样就可以测试调用数据持久层的对象,而不需要实际使用真实数据库。因此,测试更快,代码与测试的分离得到实现。DbMockLibrary可用于在测试环境中模拟数据持久层功能

  2. 当项目处于原型设计阶段时,通常在那个时刻选择数据库并不是必需的。有时,在该阶段推迟决策一段时间甚至有益,直到某些功能/架构解决方案成形。所需的是拥有一些“虚拟数据”,以便使用它来测试功能和概念。DbMockLibrary提供了一个功能丰富的“虚拟数据”平台。

  3. 在开发过程中,通常需要有一种简单的方法来从数据库中加载/删除“虚拟数据”,以便能够测试功能,而无需从生产数据库创建转储。DbMockLibrary为此提供了针对一些最受欢迎的数据库的简单管理方式

需求

您需要:PHP版本5.4+

安装

使用composer安装最新版本

require "ajant/db-mock-library": ~1

自动加载库

use DbMockLibrary/DbMockLibrary

目前,MySQL、MongoDb和Elasticsearch数据库已实现。

快速开始

以下是如何使用库测试应用程序DB功能的示例。

MySQL

引导

...
// 2 tables, 2 rows each
$data = [
    'table_1' => [
        -1 => ['foo' => 20, 'id' => -1],
        -2 => ['foo' => 50, 'id' => -2]
    ],
    'table_2' => [
        -1 => ['bar' => 30, 'id' => -1, 'table_1_id' => -1],
        -2 => ['bar' => 10, 'id' => -2, 'table_1_id' => -2]
    ]
];
// table_1_id is foreign key, referencing id column
$dependencies = [
    [
        DependencyHandler::DEPENDENT => ['table_2' => 'table_1_id'],
        DependencyHandler::ON        => ['table_1' => 'id']
    ]
];
// initialize MySQL
MySQL::initMySQL($data, 'localhost', 'DbMockLibraryTest', 'root', '', $dependencies);
...

测试设置

...
// inserts both rows of table_2 and both rows of table_1, because
MySQL::getInstance()->setUp(['table_2' => [-1, -2]]);
...

测试清理

...
// removes all rows inserted during set up phase
MySQL::getInstance()->cleanUp();
...

Elasticsearch

注意

假设所有用于测试的索引和映射记录已经在Elasticsearch数据库中。

引导

...
// 4 indexes, 2 rows each
// 4th index is percolator index
$data = [
    'index_1' => [
        0 => ['foo' => 20, 'id' => -1],
        1 => ['foo' => 50, 'id' => -2],
    ],
    'index_2' => [
        0 => ['bar' => 30, 'id' => -1, 'index_1_id' => -1],
        1 => ['bar' => 10, 'id' => -2, 'index_1_id' => -2],
    ],
    'index_3' => [
        'record1' => ['field1' => 'value11', 'field2' => 'value12'],
        'record2' => ['field1' => 'value21', 'field2' => 'value22'],
    ],
    'index_4' => [
        'percolatorRecord1' => [
            'routing' => 0,
            'body' => [
                'query' => [
                    'bool' => [
                        'minimum_number_should_match' => 1,
                        'should' => [
                            [
                                'match' => [
                                    'someField' => [
                                        'query' => 'foo',
                                        'operator' => 'and'
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
        'percolatorRecord2' => [
            'routing' => 1,
            'body' => [
                'query' => [
                    'terms' => [
                        'someField' => [
                            'someValue'
                        ],
                    ],
                ],
            ],
        ],
    ],
];
// index_1_id is foreign key, referencing id column
$dependencies = [
    [
        DependencyHandler::DEPENDENT => ['index_2' => 'index_1_id'],
        DependencyHandler::ON        => ['index_1' => 'id'],
    ],
];
$indexTypes = [
    'index_1' => 'someType',
    'index_2' => 'someType',
    'index_3' => 'someType',
    'index_4' => '.percolator',
];
// initialize Elasticsearch
$client = \Elasticsearch\ClientBuilder::create()->setHosts(['https://:9200'])->build();
Elasticsearch::initElasticsearch($client, $data, $dependencies, $indexTypes);
...

测试设置功能

...
// inserts both rows of index_2 and both rows of index_1, because
Elasticsearch::getInstance()->setUp(['index_2' => [0, 1]]);
// inserts all indexes
Elasticsearch::getInstance()->setUp();
// inserts only index_1
Elasticsearch::getInstance()->setUp(['index_1']);
...

测试清理功能

...
// removes all rows inserted during set up phase, including dependencies
Elasticsearch::getInstance()->cleanUp();
// removes all indexes
Elasticsearch::getInstance()->tearDown();
// removes only index_1
Elasticsearch::getInstance()->tearDown(['index_1']);
...