{"id":34977519,"url":"https://github.com/cspray/database-testing","last_synced_at":"2025-12-27T00:29:53.038Z","repository":{"id":98249491,"uuid":"608617515","full_name":"cspray/database-testing","owner":"cspray","description":"Framework-agnostic, low-level library to setup a database for automated testing","archived":false,"fork":false,"pushed_at":"2025-08-21T14:21:12.000Z","size":50,"stargazers_count":4,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-11T00:29:38.450Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"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/cspray.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"zenodo":null}},"created_at":"2023-03-02T11:48:28.000Z","updated_at":"2025-05-09T15:20:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"f1a1ec76-026f-4c97-84d2-1cea1289284d","html_url":"https://github.com/cspray/database-testing","commit_stats":null,"previous_names":["cspray/database-testing","cspray/database-test-case"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/cspray/database-testing","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fdatabase-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fdatabase-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fdatabase-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fdatabase-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cspray","download_url":"https://codeload.github.com/cspray/database-testing/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cspray%2Fdatabase-testing/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28066214,"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","status":"online","status_checked_at":"2025-12-26T02:00:06.189Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2025-12-27T00:29:52.480Z","updated_at":"2025-12-27T00:29:53.029Z","avatar_url":"https://github.com/cspray.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cspray/database-testing\n\nA low-level, framework-agnostic library for setting up a database suitable \nfor automated tests. This library provides the following features:\n\n- The `Cspray\\DatabaseTesting\\ConnectionAdapter` interface that defines how this library,\n  and those that extend it, interact with your database.\n- Comprehensive, customizable strategies for cleaning up your database to ensure each \n  test works with a known state.\n- Declare fixtures, sets of database records, that are loaded for each test.\n- A simple interface to easily introspect the contents of a database\n  table in your test suite.\n- Database and testing-framework agnostic approach\n\nIt cannot be emphasized enough; this library does not provide a turn-key, usable solution \nout-of-the-box. If you use this library directly, instead of one of the extensions that \ntargets a specific database connection and testing framework, you'll need to ensure the \nappropriate concrete implementations and framework integration points are created.\n\n## Complete Libraries\n\nInstead of installing this library directly, we recommend that you install one of the \navailable options from the \"Connection Adapter\" and \"Testing Extension\" list. Chances are, \nyou'll need both. If you don't see your database connection type or testing framework\nlisted, please submit an issue to this repository!\n\n### Connection Adapter\n\n- [cspray/database-testing-pdo](https://github.com/cspray/database-testing-pdo)\n\n### Testing Extension\n\n- [cspray/database-testing-phpunit](https://github.com/cspray/database-testing-phpunit)\n\n## Quick Example\n\nThis example is intended to reflect what should be capable with this library. We're going to \nuse [cspray/database-testing-phpunit](https://github.com/cspray/databse-testing-phpunit) as our testing extension, it is ubiquitous and likely the framework you'll start off using with this library.\n\n```php\n\u003c?php declare(strict_types=1);\n\nnamespace Cspray\\DatabaseTesting\\Demo;\n\nuse Cspray\\DatabaseTesting\\DatabaseCleanup\\TransactionWithRollback;\nuse Cspray\\DatabaseTesting\\Fixture\\LoadFixture;\nuse Cspray\\DatabaseTesting\\Fixture\\SingleRecordFixture;\nuse Cspray\\DatabaseTesting\\TestDatabase;\nuse Cspray\\DatabaseTesting\\PhpUnit\\InjectTestDatabase;\nuse Cspray\\DatabaseTesting\\PhpUnit\\RequiresTestDatabase;\nuse PHPUnit\\Framework\\TestCase;\nuse PDO;\n\n#[RequiresTestDatabase(\n    // this should be implemented by you or provided by an extension to this library\n    new MyPdoConnectionAdapterFactory(),\n    \n    // you could also use Cspray\\DatabaseTesting\\DatabaseCleanup\\TruncateTables\n    // or implement your own Cspray\\DatabaseTesting\\DatabaseCleanup\\CleanupStrategy\n    new TransactionWithRollback()\n)]\nfinal class RepositoryTest extends TestCase {\n\n    #[InjectTestDatabase]\n    private static TestDatabase $testDatabase;\n\n    private PDO $pdo;\n    private MyRepository $myRepository;\n\n    protected function setUp() : void {\n        // be sure to use the connection from TestDatabase! depending on CleanupStrategy,\n        // using a different connection could wind up with a dirty database state\n        $this-\u003epdo = self::$testDatabase-\u003econnection();\n        $this-\u003emyRepository = new MyRepository($this-\u003epdo);\n    }\n    \n    // populate with more appropriate data. recommended to implement your own \n    // Cspray\\DatabaseTesting\\Fixture\\Fixture to reuse datasets across tests\n    #[LoadFixture(\n        new SingleRecordFixture('my_table', [\n            'name' =\u003e 'cspray',\n            'website' =\u003e 'https://cspray.io'\n        ])\n    )]\n    public function testTableHasCorrectlyLoadedFixtures() : void {\n        $table = self::$testDatabase-\u003etable('my_table');\n        \n        self::assertCount(1, $table);\n        \n        self::assertSame('cspray', $table-\u003erow(0)-\u003eget('name'))\n        self::assertSame('website', $table-\u003erow(0)-\u003eget('website'));\n    }\n    \n    public function testTableCanBeReloadedToGetNewlyInsertedRecords() : void {\n        $table = self::$testDatabase-\u003etable('my_table');\n        \n        self::assertCount(0, $table);\n        \n        $this-\u003emyRepository-\u003esave(new MyEntity());\n    \n        $table-\u003ereload();\n        \n        self::assertCount(1, $table);\n    }\n\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcspray%2Fdatabase-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcspray%2Fdatabase-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcspray%2Fdatabase-testing/lists"}