PHPackages                             czukowski/phpunit-sql - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. czukowski/phpunit-sql

ActiveLibrary[Testing &amp; Quality](/categories/testing)

czukowski/phpunit-sql
=====================

SQL strings testing helpers for PHPUnit

9.0(6y ago)031.2k↓100%1[1 issues](https://github.com/czukowski/phpunit-sql/issues)1MITPHP

Since Mar 2Pushed 5y agoCompare

[ Source](https://github.com/czukowski/phpunit-sql)[ Packagist](https://packagist.org/packages/czukowski/phpunit-sql)[ Docs](https://github.com/czukowski/phpunit-sql)[ RSS](/packages/czukowski-phpunit-sql/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (23)Used By (1)

SQL strings testing for PHPUnit
===============================

[](#sql-strings-testing-for-phpunit)

[![PHPUnit](https://github.com/czukowski/phpunit-sql/workflows/PHPUnit/badge.svg)](https://github.com/czukowski/phpunit-sql/workflows/PHPUnit/badge.svg)

A constraint and assert method for testing SQL strings equality while ignoring whitespace. Can be useful for testing results of query builders (especially those for long and complex queries) against well-formatted 'expected' queries stored in files.

This does not replace the need to verify that the queries actually do the intended job.

Installation
------------

[](#installation)

Pick your version! Version numbering follows major PHPUnit version numbers, so for a given PHPUnit N.x, the installation command would look like this:

```
composer require czukowski/phpunit-sql "~N.0"
```

Usage
-----

[](#usage)

Use `Cz\PHPUnit\SQL\AssertTrait` trait in a test case class, this will enable methods for comparing SQL queries equality equal except for space and a terminal semicolon. An SQL query may be denoted as strings or objects castable to strings. Arrays of SQL queries are also acceptable and can be used to compare series of queries. For the purposes of the comparison, array with a single SQL query element is equal to the SQL query element itself, so there's no need to remember to eg. convert arguments to arrays all the time.

1. `assertEqualsSQLQueries` method will verify equality of two queries or series of queries.

    ```
    $this->assertEqualsSQLQueries($expected, $actual);
    ```
2. `assertExecutedSQLQueries` method will verify that a query or a series of queries has been executed by a database abstraction layer. In order to be able to do it, a `getDatabaseDriver`method must be implemented by the test case class, that returns an object implementing the `Cz\PHPUnit\SQL\DatabaseDriverInterface` interface. That can be a database abstraction layer connection class with a fake database driver or something, which is injected into the tested application code.

    ```
    $this->assertExecutedSQLQueries($expected);
    ```

    The interface implementation is available in `Cz\PHPUnit\SQL\DatabaseDriverTrait` for easy inclusion into custom implementations.
3. `loadSQLQueries` method will load SQL query or a series thereof from a file and return an array of queries. Splitting of queries by a delimiter `;` works only if the next query after a delimiter starts from the following line. Other than that, there may be newlines and blank lines inside the queries and in between of them, they do not get removed on load. By default, the method looks for the file in a subfolder named after the file name of the current class (presumably test case). That behavior can be changed by overriding `getLoadFilePath` method.

    ```
    $this->loadSQLQueries($expected);
    ```

    The assertion methods will flatten arrays of queries, so multiple files may be loaded without a need to process them further.

    ```
    $this->assertExecutedSQLQueries([
        $this->loadSQLQueries('SelectItems.sql'),
        $this->loadSQLQueries('InsertNewItems.sql'),
        $this->loadSQLQueries('DeleteOldItems.sql'),
    ]);
    ```

**Does not match your specific needs?** No problem, the `AssertTrait` is extremely simple, you can clone and adjust it for your project or come up with a completely different implementation.

Known issues
------------

[](#known-issues)

In order to compare SQL queries, a rather naive tokenizer function is used to convert query strings to arrays. It may not cover some edge cases when uncommon operators or SQL syntax is used in queries (specifically DDL was not tested), but it should be fairly easy to fix.

License
-------

[](#license)

This work is released under the MIT License. See LICENSE.md for details.

###  Health Score

37

—

LowBetter than 84% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~48 days

Recently: every ~126 days

Total

16

Last Release

2223d ago

Major Versions

4.0.2 → 5.0.22018-10-04

5.0.2 → 6.0.22018-10-04

6.0.2 → 7.0.22018-10-04

7.0.2 → 8.02019-02-11

6.0.3 → 9.02020-02-20

PHP version history (4 changes)6.0PHP &gt;=7.0

5.0PHP &gt;=5.6

4.0PHP &gt;=5.4

7.0PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/3a53f9e84b8af6f4e617504c176bae99b56220f35bffba0a499c9db100a3b43f?d=identicon)[czukowski](/maintainers/czukowski)

---

Top Contributors

[![czukowski](https://avatars.githubusercontent.com/u/186792?v=4)](https://github.com/czukowski "czukowski (43 commits)")

---

Tags

phpunitsqlphpunitsql

### Embed Badge

![Health badge](/badges/czukowski-phpunit-sql/health.svg)

```
[![Health](https://phpackages.com/badges/czukowski-phpunit-sql/health.svg)](https://phpackages.com/packages/czukowski-phpunit-sql)
```

###  Alternatives

[brianium/paratest

Parallel testing for PHP

2.5k113.9M637](/packages/brianium-paratest)[johnkary/phpunit-speedtrap

Find and report on slow tests in your PHPUnit test suite

78836.7M114](/packages/johnkary-phpunit-speedtrap)[spatie/phpunit-snapshot-assertions

Snapshot testing with PHPUnit

69417.3M441](/packages/spatie-phpunit-snapshot-assertions)[phpunit/phpunit-selenium

Selenium Server integration for PHPUnit

59610.9M135](/packages/phpunit-phpunit-selenium)[phpspec/prophecy-phpunit

Integrating the Prophecy mocking library in PHPUnit test cases

19253.3M1.3k](/packages/phpspec-prophecy-phpunit)[yoast/phpunit-polyfills

Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests

18337.0M685](/packages/yoast-phpunit-polyfills)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
