Download the PHP package slack/hack-sql-fake without Composer

On this page you can find all versions of the php package slack/hack-sql-fake. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package hack-sql-fake

Hack SQL Fake

Build Status

Hack SQL Fake is unit testing library for Hack. It enables testing database-driven applications with an in-memory simulation of MySQL. It supports a wide variety of queries, rapid snapshot/restore of the database between test cases, and more. This is done with a Fake Object, which contains an implementation of the database, avoiding the need for explicit stubbing or mocking.

Motivation

In most unit testing libraries, SQL queries are traditionally replaced with Mock or Stub implementations. Mocks require an explicit list of queries that are expected to run and results to return, while stubs may not even check the queries being run and simply return a hard coded result. 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.

Another common strategy is to test using an actual database, such as SQLite. This creates a situation in which the database in tests may not match the behavior of the production database, and any code using specialized features of the production database may be untestable. It also means that different test cases are not isolated from each other, which can make tests difficult to debug. This can be resolved by truncating tables between each test case, but that can create a performance problem.

Hack SQL Fake takes a different approach - it parses and executes SELECT, INSERT, UPDATE, and DELETE queries against an in-memory "database" stored in hack 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:

For an overview of everything that's supported, see the tests/ for this library, which document nearly all of the SQL features it supports.

Usage

Requirements

SQL Fake requires:

Installing

This package can be installed via composer:

Versioning and Releases

Hack/HHVM is a rapidly evolving ecosystem with frequent breaking changes. As such, release numbers in this project track HHVM version numbers. We aim to support the latest LTS (long term support) release of HHVM on the main branch. Long lived branches for other supported HHVM versions may also be maintained. At present, releases for HHVM 4.80, 4.56, and 4.40 are supported. See the https://github.com/slackhq/hack-sql-fake/releases page for compatibility.

How it works

SQL Fake works by providing a subclass of AsyncMysqlConnectionPool, the recommended method of querying MySQL built-in to Hack. A subclass for AsyncMysqlClient is provided as well.

This library assumes you currently have some form of establishing a database connection using AsyncMysqlConnectionPool. The best way to use SQLFake will depend on your code, but you can use dependency injection or fb_intercept() to instantiate a Slack\SQLFake\AsyncMysqlConnectionPool when testing. This will behave like a database for the rest of your test run.

Once per test run, you should also call Slack\SQLFake\init() to register database schema. See Exporting Database Schema for instructions.

For example, assume you have a class in your source code that manages database connections called Db with a method named getConnectionPool(): AsyncMysqlConnectionPool. In your tests, you can intercept that function to return an instance of SQLFake's connection pool.

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

Setup and Teardown

You can use the Slack\SQLFake\snapshot($name); and Slack\SQLFake\restore($name); functions to make snapshots and restore those snapshots. This can help sharing setup between test cases while isolating database modifications the tests make from other tests. If using HackTest, you may want to call snapshot() in beforeFirstTestAsync and restore() in beforeEachTestAsync().

Exporting Database Schema

By default, SQL Fake will allow you to summon arbitrary tables into existence with insert statements, without having schema information. However, the library includes a schema exporter which will generate Hack code from .sql files. This allows SQL Fake to be much more rigorous, improving the value of tests:

Dumping schema to *.sql files

The first step to utilizing schema is to create one or sql files containing your schema. You should create one per database (possibly multiple per server) using commands like this:

Generating hack schemas from *.sql files

Pass all of the previously-generated SQL files as arguments to bin/hack-sql-schema and output the results to any Hack file.

This will generate a file containing a single const representing your database schema. You can put this file anywhere you want in your codebase, as the constant it contains can be autoloaded. The file names of the sql files will be used as database names, with a shape representing the schema for each table in that database.

Passing schema to SQLFake

The intended use case for schema is to generate a constant containing the schema and assign it to SQL Fake at startup - but you can choose to do assign it at any time with any value. Assuming you generate a constant using the above scripts named DB_SCHEMA, you would assign it to SQLFake like this:

This makes SQLFake aware of which tables and columns exist in which databases. You'll also want to tell it which servers contain this information.

Server configuration

MySQL connections operate on a host or ip. Since SQLFake operates at the same level as AsyncMysqlClient, it also organizes data by hostname. We recommend using fake hostnames in tests that bear some relationship to the production database infrastructure.

At any time, you can tell SQL Fake about which servers exist and provide settings for those by hostname. Any attempt to connect to servers which haven't been explicitly defined will result in an exception.

Metrics

SQL Fake is able to gather metrics about the queries that run during tests, and optionally include stack traces on where those queries ran. This is useful if you'd like to track how many and what kinds of queries are run in key sections of code. This can use a lot of memory, so it is disabled by default.

To enable metrics gathering, set Slack\SQLFake\Metrics::$enable = true;. You can then call:

Callstacks

When recording information about queries that were invoked in a test, you can capture the callstack at the time the query was run. This can help clarify where those queries are coming from and can be useful to report on which code paths trigger the most queries. To enable callstacks, provide SQLFake\Metrics::$enableCallstacks = true;. SQLFake will automatically filter its own functions out of the end of these callstacks. You can also filter out your own low level library code, ensuring the callstacks are most readable, by passing a keyset of patterns to ignore:

Any function names matching those patterns will be removed from the end of the stack trace.

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. See our #issues page for a wishlist.

Contributing

See Contributing

Development

A VSCode devcontainer setup is included in the repository. With a local docker daemon running, invoke code . in the root of your checkout of this repository and VSCode should automatically build a container and install dependencies. A debug action is included to run unit tests in the HHVM debugger as well.

Latest Releases

See Change log for more details.


All versions of hack-sql-fake with dependencies

PHP Build Version
Package Version
Requires hhvm/hsl-experimental Version ^4.108.0
facebook/hack-codegen Version ^4.3.12
facebook/hh-clilib Version ^2.0.0
hhvm/hhvm-autoload Version ^2.0.13|^3.1.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package slack/hack-sql-fake contains the following files

Loading the files please wait ....