{"id":34977516,"url":"https://github.com/cspray/database-testing-phpunit","last_synced_at":"2026-03-15T08:51:16.976Z","repository":{"id":292374727,"uuid":"980261407","full_name":"cspray/database-testing-phpunit","owner":"cspray","description":"A testing extension for cspray/database-testing to setup a database for testing with PHPUnit","archived":false,"fork":false,"pushed_at":"2025-11-25T17:49:33.000Z","size":21,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-28T12:57:51.608Z","etag":null,"topics":["php8","phpunit-extension"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cspray.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null}},"created_at":"2025-05-08T20:52:10.000Z","updated_at":"2025-11-25T17:49:34.000Z","dependencies_parsed_at":"2025-05-09T16:32:07.200Z","dependency_job_id":"f8290d74-c454-43a4-b8c9-4cb53cbd66c2","html_url":"https://github.com/cspray/database-testing-phpunit","commit_stats":null,"previous_names":["cspray/database-testing-phpunit"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cspray/database-testing-phpunit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fdatabase-testing-phpunit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fdatabase-testing-phpunit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fdatabase-testing-phpunit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fdatabase-testing-phpunit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cspray","download_url":"https://codeload.github.com/cspray/database-testing-phpunit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fdatabase-testing-phpunit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30539324,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-15T07:17:37.589Z","status":"ssl_error","status_checked_at":"2026-03-15T07:17:31.738Z","response_time":61,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["php8","phpunit-extension"],"created_at":"2025-12-27T00:29:51.938Z","updated_at":"2026-03-15T08:51:16.953Z","avatar_url":"https://github.com/cspray.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cspray/database-testing-phpunit\n\nA PHPUnit Extension for [cspray/database-testing](https://github.com/cspray/database-testing), designed to facilitate setting up a test database suitable for automated testing.\n\n## Installation\n\nComposer is the only supported method for installing this library.\n\n```shell\ncomposer require --dev cspray/database-testing-phpunit\n```\n\nPlease note, it is highly likely you'll need a library that provides an implementation of `Cspray\\DatabaseTesting\\ConnectionAdapter\\ConnectionAdapter`! This library does not come with the adapter appropriate for your database connection!\n\n\n## Review the tests!\n\nIf you're here to learn more about how to use `cspray/databse-testing` in your PHPUnit tests, check out the tests in this library! The `phpunit.xml` config and the implemented tests provide a working example on how to use it!\n\n## Quick Start\n\nThe examples below uses a `ConnectionAdapter` and `ConnectionAdapterFactory` available from `cspray/database-testing-pdo`. If PDO is not an appropriate connection for your use case, please replace with the appropriate `ConnectionAdapter` implementation.\n\n### Register your Extension\n\nThe first step is to register the extension in your PHPUnit configuration. Ensure the following XML is present in `phpunit.xml` (or the file you use for you PHPUnit config).\n\n```xml\n\u003cextensions\u003e\n    \u003cbootstrap class=\"Cspray\\DatabaseTesting\\PhpUnit\\DatabaseTestingExtension\" /\u003e\n\u003c/extensions\u003e\n```\n\n### Attribute your TestCase\n\nThe next step is to add the `#[RequiresTestDatabase]` attribute to your PHPUnit TestCase. There's no inheritance or traits required, you don't need to change what `TestCase` you extend!\n\n```php\n\u003c?php declare(strict_types=1);\n\nnamespace Cspray\\DatabaseTesting\\PhpUnit\\Demo;\n\nuse Cspray\\DatabaseTesting\\DatabaseCleanup\\TransactionWithRollback;\nuse Cspray\\DatabaseTesting\\Pdo\\Sqlite\\SqliteConnectionAdapterFactory;\nuse Cspray\\DatabaseTesting\\PhpUnit\\RequiresTestDatabase;\nuse PHPUnit\\Framework\\TestCase;\n\n#[RequiresTestDatabase(\n    new SqliteConnectionAdapterFactory(\n        ':memory:',\n        '/path/to/my/schema'\n    ),\n    new TransactionWithRollback()\n)]\nfinal class MyDemoTest extends TestCase {\n\n}\n```\n\n### Inject the TestDatabase\n\nTo work with your test database there is a helper class with the type, `Cspray\\DatabaseTesting\\TestDatabase`. This object allows accessing the underlying test database connection, load fixtures, and access the data in a given table.\n\nThis object is managed by the extension. It is created when your TestCase has started, before any tests or setup have run. This property MUST BE static, the extension does not have access to the TestCase instance that is running.\n\n```php\n\u003c?php declare(strict_types=1);\n\nnamespace Cspray\\DatabaseTesting\\PhpUnit\\Demo;\n\nuse Cspray\\DatabaseTesting\\DatabaseCleanup\\TransactionWithRollback;\nuse Cspray\\DatabaseTesting\\Pdo\\Sqlite\\SqliteConnectionAdapterFactory;\nuse Cspray\\DatabaseTesting\\PhpUnit\\InjectTestDatabase;\nuse Cspray\\DatabaseTesting\\PhpUnit\\RequiresTestDatabase;\nuse Cspray\\DatabaseTesting\\TestDatabase;\nuse PHPUnit\\Framework\\TestCase;\n\n#[RequiresTestDatabase(\n    new SqliteConnectionAdapterFactory(\n        ':memory:',\n        '/path/to/my/schema'\n    ),\n    new TransactionWithRollback()\n)]\nfinal class MyDemoTest extends TestCase {\n\n    #[InjectTestDatabase]\n    private static TestDatabase $testDatabase;\n\n}\n```\n\n### Load Fixtures and Run Test\n\nIn our example, we're going to load tests both in `setUp` and as an attribute on a given test. This example is meant to show in what order fixtures get ran, and to show that there are alternatives to loading fixtures if you do not like attributes.\n\n```php\n\u003c?php declare(strict_types=1);\n\nnamespace Cspray\\DatabaseTesting\\PhpUnit\\Demo;\n\nuse Cspray\\DatabaseTesting\\DatabaseCleanup\\TransactionWithRollback;\nuse Cspray\\DatabaseTesting\\Fixture\\LoadFixture;\nuse Cspray\\DatabaseTesting\\Fixture\\SingleRecordFixture;\nuse Cspray\\DatabaseTesting\\Pdo\\Sqlite\\SqliteConnectionAdapterFactory;\nuse Cspray\\DatabaseTesting\\PhpUnit\\InjectTestDatabase;\nuse Cspray\\DatabaseTesting\\PhpUnit\\RequiresTestDatabase;\nuse Cspray\\DatabaseTesting\\TestDatabase;\nuse PHPUnit\\Framework\\TestCase;\n\n#[RequiresTestDatabase(\n    new SqliteConnectionAdapterFactory(\n        ':memory:',\n        '/path/to/my/schema'\n    ),\n    new TransactionWithRollback()\n)]\nfinal class MyDemoTest extends TestCase {\n\n    #[InjectTestDatabase]\n    private static TestDatabase $testDatabase;\n\n    protected function setUp() : void{\n        parent::setUp(); // TODO: Change the autogenerated stub\n        self::$testDatabase-\u003eloadFixtures([\n            new SingleRecordFixture('my_table', ['name' =\u003e 'from setup'])\n        ]);\n    }\n    \n    #[LoadFixture(\n        new SingleRecordFixture('my_table', ['name' =\u003e 'from attribute'])\n    )]\n    public function testTableHasCorrectRecords() : void {\n        $table = self::$testDatabase-\u003etable('my_table');\n        \n        self::assertCount(2, $table);\n        self::assertSame('from attribute', $table-\u003erow(0)-\u003eget('name'));\n        self::assertSame('from setup', $table-\u003erow(1)-\u003eget('name'));\n    }\n\n}\n```\n\nGenerally speaking, I would not recommend mixing approaches to loading fixtures. Either use the `#[LoadFixture]` Attribute, or use `TestDatabase` helper in your setup or test methods.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcspray%2Fdatabase-testing-phpunit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcspray%2Fdatabase-testing-phpunit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcspray%2Fdatabase-testing-phpunit/lists"}