定制 vimeo/php-mysql-engine 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vimeo/php-mysql-engine

Composer 安装命令:

composer require vimeo/php-mysql-engine

包简介

A MySQL engine written in PHP for speeding up tests

README 文档

README

PHP MySQL Engine is a library for PHP that allows you to test database-driven applications with an in-memory simulation of MySQL 5.6. This project extends the PDO class and allows you to call common PDO MySQL methods. It supports a wide variety of queries, and some PDO-specific functionality like transactions and different fetch modes.

PHP MySQL Engine is based on Slack's Hack SQL Fake created by Scott Sandler.

You can read an article about this tool here.

Motivation

Currently there are two ways to test code that reads and writes to a database:

  • Mock SQL query execution
    Mocks require an explicit list of queries that are expected to run and results to return. This leads to significant manual work setting up expectations, and tests which are fragile and must be updated even on benign changes to the code or queries. It also means the data access layer is not unit tested.

  • Use an actual database
    It might make sense to test with a separate database instance – this is what we have done in the past at Vimeo. But databases like MySQL are designed to be filled with lots of long-lasting data, whereas unit tests write small amounts of very short-lived data. This means that extra care has to be taken to make sure that test databases are truncated between tests, which creates a performance issue.

PHP MySQL Engine takes a different approach - it parses and executes SELECT, INSERT, UPDATE, and DELETE queries against an in-memory "database" stored in PHP arrays. As long as the amount of data used for testing is small, this solves the problems mentioned above.

SQL Syntax Supported

This library supports a wide variety of query syntax, including:

  • FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT clauses supported as appropriate for each query type
  • JOIN queries with all join types
  • multi-queries such as subqueries, UNION, UNION ALL, INTERSECT, EXCEPT
  • complex expressions such as CASE, BETWEEN, and row comparators (1, 2, 3) < (4, 5, 6)
  • all basic operators implemented with operator precedence
  • column aliases, cross-database queries
  • INSERT ... ON DUPLICATE KEY UPDATE
  • A variety of SQL functions such as COUNT(), NULLIF(), COALESCE(), CONCAT_WS() and many others
  • Temporary variables like @previous_name := user.name
  • Validating parser: the query parser will throw exceptions on most invalid SQL Queries, helping protect your production environment from accidental SQL syntax errors

Unsupported MySQL features

This engine does not support MySQL Stored objects, which precludes the testing of stored procedures, triggers and views.

Caveat Emptor

Unlike Psalm, this package is not designed with a wide audience in mind. For a project to really benefit from this library it should already have a large number of tests that require a database connection to complete, and the project maintainers must understand the tradeoffs associated with using an unofficial MySQL implementation in their test suite.

Known issues

Result types when not emulating prepares

By default the engine returns all data formatted as a string. If $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false) is called, the engine will instead infer column types (for example, SUM(some_int_column) will be given an int type). In some cases php-mysql-engine may do a better job of inferring correct column types than actual MySQL, which defaults to string when it can’t work out a column type. If you do strict type checks on the results you may see small discrepancies.

Installation

composer require --dev vimeo/php-mysql-engine

Usage

PHP MySQL Engine works by providing a subclass of PDO.

You can instantiate the subclass as you would PDO, and use dependency injection or similar to provide that instance to your application code.

// use a class specific to your current PHP version (APIs changed in major versions)
$pdo = new \Vimeo\MysqlEngine\Php8\FakePdo($dsn, $user, $password);
// currently supported attributes
$pdo->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_LOWER);
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);

The rest of your code can operate as normal, using the database in the same way it is used in production.

Why doesn't it support X?

This library aims to support everything its users use in MySQL, rather than every possibly feature MySQL offers. We welcome pull requests to add support for new syntax, sql functions, data types, bug fixes, and other features.

Why doesn’t this project have an issue tracker?

Maintaining open-source projects is hard work, and I don't want to make more work for me or my colleagues. Use this project very much use at your own risk.

If you want to fork the project with an issue tracker, feel free!

Contributing

If you want to create a PR, please make sure it passes unit tests:

vendor/bin/phpunit

and also Psalm's checks

vendor/bin/psalm

Thanks!

vimeo/php-mysql-engine 适用场景与选型建议

vimeo/php-mysql-engine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 280.51k 次下载、GitHub Stars 达 561, 最近一次更新时间为 2020 年 12 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 vimeo/php-mysql-engine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vimeo/php-mysql-engine 我们能提供哪些服务?
定制开发 / 二次开发

基于 vimeo/php-mysql-engine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 280.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 562
  • 点击次数: 18
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 561
  • Watchers: 22
  • Forks: 49
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-29