ajant/db-mock-library
Composer 安装命令:
composer require ajant/db-mock-library
包简介
Db mocking & dummy data management library
README 文档
README
Db mocking & dummy data management library
This is a database stubbing/mocking/prototyping library. Its principal uses are meant to be:
-
testing the application without using actual database (by mocking data persistence layer, using DbMockLibrary)
-
quick prototyping, while delaying the writing of any database specific code (again by mocking data persistence layer, using DbMockLibrary)
-
dummy data management during development phase
Description:
-
If data persistence code is kept separate from business logic code, in a different layer of the application, then data persistence layer can be mocked using DbMockLibrary during testing. That way objects that call on data persistence layer can be tested, without actually using a real database. As a result tests are faster and better code & test separation is achieved. DbMockLibrary could be used to mock data persistence layer functionality in the testing environment
-
When project is in prototyping stage, often making choice on database is not necessarily needed at that time. Sometimes it's even beneficial to postpone the decision for a while during that phase, until some features/architectural solutions take shape. What is needed is to have some "dummy data" available, to test out features and concepts with it. DbMockLibrary provides feature rich "dummy data" platform.
-
During development, it's often convenient to have some easy way to load/remove "dummy data" from the database, in order to be able to test out features, without having to create dumps from the production database. DbMockLibrary provides a simple way to manage this process for some of the most popular databases
Requirements
You'll need: PHP version 5.4+
Installation
Install the latest version with composer:
require "ajant/db-mock-library": ~1
Auto-load the library:
use DbMockLibrary/DbMockLibrary
As of now MySQL, MongoDb and Elasticsearch databases have been implemented.
Quick start
Here's the example, how to use the library for testing DB features of the application.
MySQL
Bootstrapping:
... // 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); ...
Test set up:
... // inserts both rows of table_2 and both rows of table_1, because MySQL::getInstance()->setUp(['table_2' => [-1, -2]]); ...
Test tear down:
... // removes all rows inserted during set up phase MySQL::getInstance()->cleanUp(); ...
Elasticsearch
Note:
It is presumed that all indexes and mappings for records that are to be used in testing are already in Elasticsearch database.
Bootstrapping:
... // 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(['http://localhost:9200'])->build(); Elasticsearch::initElasticsearch($client, $data, $dependencies, $indexTypes); ...
Test set up features:
... // 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']); ...
Test tear down features:
... // 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']); ...
ajant/db-mock-library 适用场景与选型建议
ajant/db-mock-library 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.44k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 12 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ajant/db-mock-library 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ajant/db-mock-library 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 18.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-12-14