{"id":13616723,"url":"https://github.com/vimeo/php-mysql-engine","last_synced_at":"2025-04-05T20:06:26.714Z","repository":{"id":40414367,"uuid":"325044181","full_name":"vimeo/php-mysql-engine","owner":"vimeo","description":"A MySQL engine written in pure PHP","archived":false,"fork":false,"pushed_at":"2025-03-26T19:40:21.000Z","size":709,"stargazers_count":557,"open_issues_count":8,"forks_count":47,"subscribers_count":25,"default_branch":"master","last_synced_at":"2025-03-29T19:04:14.688Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vimeo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-28T15:13:29.000Z","updated_at":"2025-03-26T19:40:11.000Z","dependencies_parsed_at":"2024-05-08T22:31:42.006Z","dependency_job_id":"ab168712-216f-43ae-8a97-b3ed8585531a","html_url":"https://github.com/vimeo/php-mysql-engine","commit_stats":{"total_commits":338,"total_committers":26,"mean_commits":13.0,"dds":0.3579881656804734,"last_synced_commit":"c2cf5e5cd03f27cb28a7f656869c641374510640"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vimeo%2Fphp-mysql-engine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vimeo%2Fphp-mysql-engine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vimeo%2Fphp-mysql-engine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vimeo%2Fphp-mysql-engine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vimeo","download_url":"https://codeload.github.com/vimeo/php-mysql-engine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247393569,"owners_count":20931812,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T20:01:32.446Z","updated_at":"2025-04-05T20:06:26.693Z","avatar_url":"https://github.com/vimeo.png","language":"PHP","readme":"# PHP MySQL Engine\n\nPHP 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.\n\nPHP MySQL Engine is based on Slack's [Hack SQL Fake](https://github.com/slackhq/hack-sql-fake) created by [Scott Sandler](https://github.com/ssandler).\n\nYou can read an article about this tool [here](https://medium.com/vimeo-engineering-blog/the-great-pretender-faster-application-tests-with-mysql-simulation-26250f13d251).\n\n## Motivation\n\nCurrently there are two ways to test code that reads and writes to a database:\n\n- Mock SQL query execution\u003cbr/\u003e\n  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.\n  \n- Use an actual database\u003cbr /\u003e\n  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.\n\nPHP 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.\n\n## SQL Syntax Supported\n\nThis library supports a wide variety of query syntax, including:\n\n- `FROM`, `WHERE`, `GROUP BY`, `HAVING`, `ORDER BY`, `LIMIT` clauses supported as appropriate for each query type\n- `JOIN` queries with all join types\n- multi-queries such as subqueries, `UNION`, `UNION ALL`, `INTERSECT`, `EXCEPT`\n- complex expressions such as `CASE`, `BETWEEN`, and row comparators `(1, 2, 3) \u003c (4, 5, 6)`\n- all basic operators implemented with operator precedence\n- column aliases, cross-database queries\n- `INSERT ... ON DUPLICATE KEY UPDATE`\n- A variety of SQL functions such as `COUNT(), NULLIF(), COALESCE(), CONCAT_WS()` and many others\n- Temporary variables like `@previous_name := user.name`\n- Validating parser: the query parser will throw exceptions on most invalid SQL Queries, helping protect your production environment from accidental SQL syntax errors\n\n## Unsupported MySQL features\n\nThis engine does _not_ support [MySQL Stored objects](https://dev.mysql.com/doc/refman/5.6/en/stored-objects.html), which precludes the testing of stored procedures, triggers and views.\n\n## Caveat Emptor\n\nUnlike [Psalm](https://github.com/vimeo/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.\n\n## Known issues\n\n### Result types when not emulating prepares\n\nBy default the engine returns all data formatted as a string. If `$pdo-\u003esetAttribute(\\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.\n\n## Installation\n\n```\ncomposer require --dev vimeo/php-mysql-engine\n```\n\n## Usage\n\nPHP MySQL Engine works by providing a subclass of [PDO](https://www.php.net/manual/en/class.pdo.php).\n\nYou can instantiate the subclass as you would `PDO`, and use dependency injection or similar to provide that instance to your application code.\n\n```php\n// use a class specific to your current PHP version (APIs changed in major versions)\n$pdo = new \\Vimeo\\MysqlEngine\\Php8\\FakePdo($dsn, $user, $password);\n// currently supported attributes\n$pdo-\u003esetAttribute(\\PDO::ATTR_CASE, \\PDO::CASE_LOWER);\n$pdo-\u003esetAttribute(\\PDO::ATTR_EMULATE_PREPARES, false);\n```\n\nThe rest of your code can operate as normal, using the database in the same way it is used in production.\n\n## Why doesn't it support X?\n\nThis 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.\n\n## Why doesn’t this project have an issue tracker?\n\nMaintaining 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.\n\nIf you want to fork the project with an issue tracker, feel free!\n\n## Contributing\n\nIf you want to create a PR, please make sure it passes unit tests:\n\n```\nvendor/bin/phpunit\n```\n\nand also Psalm's checks\n\n```\nvendor/bin/psalm\n```\n\nThanks!\n","funding_links":[],"categories":["PHP","Table of Contents","目录","类库"],"sub_categories":["Testing","测试 Testing","数据库"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvimeo%2Fphp-mysql-engine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvimeo%2Fphp-mysql-engine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvimeo%2Fphp-mysql-engine/lists"}